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 | |
drh | 6033e15 | 2012-11-13 11:08:49 +0000 | [diff] [blame] | 49 | /* Use posix_fallocate() if it is available |
| 50 | */ |
| 51 | #if !defined(HAVE_POSIX_FALLOCATE) \ |
| 52 | && (_XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L) |
| 53 | # define HAVE_POSIX_FALLOCATE 1 |
| 54 | #endif |
| 55 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 56 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 57 | ** There are various methods for file locking used for concurrency |
| 58 | ** control: |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 59 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 60 | ** 1. POSIX locking (the default), |
| 61 | ** 2. No locking, |
| 62 | ** 3. Dot-file locking, |
| 63 | ** 4. flock() locking, |
| 64 | ** 5. AFP locking (OSX only), |
| 65 | ** 6. Named POSIX semaphores (VXWorks only), |
| 66 | ** 7. proxy locking. (OSX only) |
| 67 | ** |
| 68 | ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE |
| 69 | ** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic |
| 70 | ** selection of the appropriate locking style based on the filesystem |
| 71 | ** where the database is located. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 72 | */ |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 73 | #if !defined(SQLITE_ENABLE_LOCKING_STYLE) |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 74 | # if defined(__APPLE__) |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 75 | # define SQLITE_ENABLE_LOCKING_STYLE 1 |
| 76 | # else |
| 77 | # define SQLITE_ENABLE_LOCKING_STYLE 0 |
| 78 | # endif |
| 79 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 80 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 81 | /* |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 82 | ** Define the OS_VXWORKS pre-processor macro to 1 if building on |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 83 | ** vxworks, or 0 otherwise. |
| 84 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 85 | #ifndef OS_VXWORKS |
| 86 | # if defined(__RTP__) || defined(_WRS_KERNEL) |
| 87 | # define OS_VXWORKS 1 |
| 88 | # else |
| 89 | # define OS_VXWORKS 0 |
| 90 | # endif |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 91 | #endif |
| 92 | |
| 93 | /* |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 94 | ** These #defines should enable >2GB file support on Posix if the |
| 95 | ** underlying operating system supports it. If the OS lacks |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 96 | ** large file support, these should be no-ops. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 97 | ** |
| 98 | ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch |
| 99 | ** on the compiler command line. This is necessary if you are compiling |
| 100 | ** on a recent machine (ex: RedHat 7.2) but you want your code to work |
| 101 | ** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2 |
| 102 | ** without this option, LFS is enable. But LFS does not exist in the kernel |
| 103 | ** in RedHat 6.0, so the code won't work. Hence, for maximum binary |
| 104 | ** portability you should omit LFS. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 105 | ** |
| 106 | ** The previous paragraph was written in 2005. (This paragraph is written |
| 107 | ** on 2008-11-28.) These days, all Linux kernels support large files, so |
| 108 | ** you should probably leave LFS enabled. But some embedded platforms might |
| 109 | ** 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] | 110 | */ |
| 111 | #ifndef SQLITE_DISABLE_LFS |
| 112 | # define _LARGE_FILE 1 |
| 113 | # ifndef _FILE_OFFSET_BITS |
| 114 | # define _FILE_OFFSET_BITS 64 |
| 115 | # endif |
| 116 | # define _LARGEFILE_SOURCE 1 |
| 117 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 118 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 119 | /* |
| 120 | ** standard include files. |
| 121 | */ |
| 122 | #include <sys/types.h> |
| 123 | #include <sys/stat.h> |
| 124 | #include <fcntl.h> |
| 125 | #include <unistd.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 126 | #include <time.h> |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 127 | #include <sys/time.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 128 | #include <errno.h> |
drh | b469f46 | 2010-12-22 21:48:50 +0000 | [diff] [blame] | 129 | #ifndef SQLITE_OMIT_WAL |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 130 | #include <sys/mman.h> |
drh | b469f46 | 2010-12-22 21:48:50 +0000 | [diff] [blame] | 131 | #endif |
drh | 1da88f0 | 2011-12-17 16:09:16 +0000 | [diff] [blame] | 132 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 133 | |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 134 | #if SQLITE_ENABLE_LOCKING_STYLE |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 135 | # include <sys/ioctl.h> |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 136 | # if OS_VXWORKS |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 137 | # include <semaphore.h> |
| 138 | # include <limits.h> |
| 139 | # else |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 140 | # include <sys/file.h> |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 141 | # include <sys/param.h> |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 142 | # endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 143 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 144 | |
drh | f8b4d8c | 2010-03-05 13:53:22 +0000 | [diff] [blame] | 145 | #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS) |
drh | 84a2bf6 | 2010-03-05 13:41:06 +0000 | [diff] [blame] | 146 | # include <sys/mount.h> |
| 147 | #endif |
| 148 | |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 149 | #ifdef HAVE_UTIME |
| 150 | # include <utime.h> |
| 151 | #endif |
| 152 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 153 | /* |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 154 | ** Allowed values of unixFile.fsFlags |
| 155 | */ |
| 156 | #define SQLITE_FSFLAGS_IS_MSDOS 0x1 |
| 157 | |
| 158 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 159 | ** If we are to be thread-safe, include the pthreads header and define |
| 160 | ** the SQLITE_UNIX_THREADS macro. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 161 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 162 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 163 | # include <pthread.h> |
| 164 | # define SQLITE_UNIX_THREADS 1 |
| 165 | #endif |
| 166 | |
| 167 | /* |
| 168 | ** Default permissions when creating a new file |
| 169 | */ |
| 170 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 171 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 172 | #endif |
| 173 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 174 | /* |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 175 | ** Default permissions when creating auto proxy dir |
| 176 | */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 177 | #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 178 | # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 |
| 179 | #endif |
| 180 | |
| 181 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 182 | ** Maximum supported path-length. |
| 183 | */ |
| 184 | #define MAX_PATHNAME 512 |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 185 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 186 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 187 | ** Only set the lastErrno if the error code is a real error and not |
| 188 | ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 189 | */ |
| 190 | #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |
| 191 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 192 | /* Forward references */ |
| 193 | typedef struct unixShm unixShm; /* Connection shared memory */ |
| 194 | typedef struct unixShmNode unixShmNode; /* Shared memory instance */ |
| 195 | typedef struct unixInodeInfo unixInodeInfo; /* An i-node */ |
| 196 | typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 197 | |
| 198 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 199 | ** Sometimes, after a file handle is closed by SQLite, the file descriptor |
| 200 | ** cannot be closed immediately. In these cases, instances of the following |
| 201 | ** structure are used to store the file descriptor while waiting for an |
| 202 | ** opportunity to either close or reuse it. |
| 203 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 204 | struct UnixUnusedFd { |
| 205 | int fd; /* File descriptor to close */ |
| 206 | int flags; /* Flags this file descriptor was opened with */ |
| 207 | UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ |
| 208 | }; |
| 209 | |
| 210 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 211 | ** The unixFile structure is subclass of sqlite3_file specific to the unix |
| 212 | ** VFS implementations. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 213 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 214 | typedef struct unixFile unixFile; |
| 215 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 216 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 217 | sqlite3_vfs *pVfs; /* The VFS that created this unixFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 218 | unixInodeInfo *pInode; /* Info about locks on this inode */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 219 | int h; /* The file descriptor */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 220 | unsigned char eFileLock; /* The type of lock held on this fd */ |
drh | 3ee3484 | 2012-02-11 21:21:17 +0000 | [diff] [blame] | 221 | unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 222 | int lastErrno; /* The unix errno from last I/O error */ |
| 223 | void *lockingContext; /* Locking style specific state */ |
| 224 | UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 225 | const char *zPath; /* Name of the file */ |
| 226 | unixShm *pShm; /* Shared memory segment information */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 227 | int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ |
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 | sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */ |
| 255 | sqlite3_int64 mmapOrigsize; /* Actual size of mapping at pMapRegion */ |
| 256 | sqlite3_int64 mmapLimit; /* Configured FCNTL_MMAP_SIZE value */ |
| 257 | void *pMapRegion; /* Memory mapped region */ |
| 258 | int nFetchOut; /* Number of outstanding xFetch refs */ |
| 259 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 260 | #ifdef SQLITE_TEST |
| 261 | /* In test mode, increase the size of this structure a bit so that |
| 262 | ** it is larger than the struct CrashFile defined in test6.c. |
| 263 | */ |
| 264 | char aPadding[32]; |
| 265 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 266 | }; |
| 267 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 268 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 269 | ** Allowed values for the unixFile.ctrlFlags bitmask: |
| 270 | */ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 271 | #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ |
| 272 | #define UNIXFILE_RDONLY 0x02 /* Connection is read only */ |
| 273 | #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ |
dan | ee140c4 | 2011-08-25 13:46:32 +0000 | [diff] [blame] | 274 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 275 | # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ |
| 276 | #else |
| 277 | # define UNIXFILE_DIRSYNC 0x00 |
| 278 | #endif |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 279 | #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 280 | #define UNIXFILE_DELETE 0x20 /* Delete on close */ |
| 281 | #define UNIXFILE_URI 0x40 /* Filename might have query parameters */ |
| 282 | #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 283 | |
| 284 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 285 | ** Include code that is common to all os_*.c files |
| 286 | */ |
| 287 | #include "os_common.h" |
| 288 | |
| 289 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 290 | ** Define various macros that are missing from some systems. |
| 291 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 292 | #ifndef O_LARGEFILE |
| 293 | # define O_LARGEFILE 0 |
| 294 | #endif |
| 295 | #ifdef SQLITE_DISABLE_LFS |
| 296 | # undef O_LARGEFILE |
| 297 | # define O_LARGEFILE 0 |
| 298 | #endif |
| 299 | #ifndef O_NOFOLLOW |
| 300 | # define O_NOFOLLOW 0 |
| 301 | #endif |
| 302 | #ifndef O_BINARY |
| 303 | # define O_BINARY 0 |
| 304 | #endif |
| 305 | |
| 306 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 307 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 308 | ** testing and debugging only. |
| 309 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 310 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 311 | #define threadid pthread_self() |
| 312 | #else |
| 313 | #define threadid 0 |
| 314 | #endif |
| 315 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 316 | /* |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 317 | ** Different Unix systems declare open() in different ways. Same use |
| 318 | ** open(const char*,int,mode_t). Others use open(const char*,int,...). |
| 319 | ** The difference is important when using a pointer to the function. |
| 320 | ** |
| 321 | ** The safest way to deal with the problem is to always use this wrapper |
| 322 | ** which always has the same well-defined interface. |
| 323 | */ |
| 324 | static int posixOpen(const char *zFile, int flags, int mode){ |
| 325 | return open(zFile, flags, mode); |
| 326 | } |
| 327 | |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 328 | /* |
| 329 | ** On some systems, calls to fchown() will trigger a message in a security |
| 330 | ** log if they come from non-root processes. So avoid calling fchown() if |
| 331 | ** we are not running as root. |
| 332 | */ |
| 333 | static int posixFchown(int fd, uid_t uid, gid_t gid){ |
| 334 | return geteuid() ? 0 : fchown(fd,uid,gid); |
| 335 | } |
| 336 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 337 | /* Forward reference */ |
| 338 | static int openDirectory(const char*, int*); |
| 339 | |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 340 | /* |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 341 | ** Many system calls are accessed through pointer-to-functions so that |
| 342 | ** they may be overridden at runtime to facilitate fault injection during |
| 343 | ** testing and sandboxing. The following array holds the names and pointers |
| 344 | ** to all overrideable system calls. |
| 345 | */ |
| 346 | static struct unix_syscall { |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 347 | const char *zName; /* Name of the sytem call */ |
| 348 | sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ |
| 349 | sqlite3_syscall_ptr pDefault; /* Default value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 350 | } aSyscall[] = { |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 351 | { "open", (sqlite3_syscall_ptr)posixOpen, 0 }, |
| 352 | #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent) |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 353 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 354 | { "close", (sqlite3_syscall_ptr)close, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 355 | #define osClose ((int(*)(int))aSyscall[1].pCurrent) |
| 356 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 357 | { "access", (sqlite3_syscall_ptr)access, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 358 | #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent) |
| 359 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 360 | { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 361 | #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent) |
| 362 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 363 | { "stat", (sqlite3_syscall_ptr)stat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 364 | #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) |
| 365 | |
| 366 | /* |
| 367 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 368 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 369 | ** that always succeeds. This means that locking does not occur under |
| 370 | ** DJGPP. But it is DOS - what did you expect? |
| 371 | */ |
| 372 | #ifdef __DJGPP__ |
| 373 | { "fstat", 0, 0 }, |
| 374 | #define osFstat(a,b,c) 0 |
| 375 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 376 | { "fstat", (sqlite3_syscall_ptr)fstat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 377 | #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent) |
| 378 | #endif |
| 379 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 380 | { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 381 | #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent) |
| 382 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 383 | { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 384 | #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 385 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 386 | { "read", (sqlite3_syscall_ptr)read, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 387 | #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) |
| 388 | |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 389 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 390 | { "pread", (sqlite3_syscall_ptr)pread, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 391 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 392 | { "pread", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 393 | #endif |
| 394 | #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) |
| 395 | |
| 396 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 397 | { "pread64", (sqlite3_syscall_ptr)pread64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 398 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 399 | { "pread64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 400 | #endif |
| 401 | #define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent) |
| 402 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 403 | { "write", (sqlite3_syscall_ptr)write, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 404 | #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) |
| 405 | |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 406 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 407 | { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 408 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 409 | { "pwrite", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 410 | #endif |
| 411 | #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 412 | aSyscall[12].pCurrent) |
| 413 | |
| 414 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 415 | { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 416 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 417 | { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 418 | #endif |
| 419 | #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 420 | aSyscall[13].pCurrent) |
| 421 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 422 | { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 423 | #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 424 | |
| 425 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 426 | { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 427 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 428 | { "fallocate", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 429 | #endif |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 430 | #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 431 | |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 432 | { "unlink", (sqlite3_syscall_ptr)unlink, 0 }, |
| 433 | #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent) |
| 434 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 435 | { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 }, |
| 436 | #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent) |
| 437 | |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 438 | { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 }, |
| 439 | #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent) |
| 440 | |
| 441 | { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 }, |
| 442 | #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent) |
| 443 | |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 444 | { "fchown", (sqlite3_syscall_ptr)posixFchown, 0 }, |
dan | d3eaebd | 2012-02-13 08:50:23 +0000 | [diff] [blame] | 445 | #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent) |
drh | 23c4b97 | 2012-02-11 23:55:15 +0000 | [diff] [blame] | 446 | |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 447 | }; /* End of the overrideable system calls */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 448 | |
| 449 | /* |
| 450 | ** This is the xSetSystemCall() method of sqlite3_vfs for all of the |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 451 | ** "unix" VFSes. Return SQLITE_OK opon successfully updating the |
| 452 | ** system call pointer, or SQLITE_NOTFOUND if there is no configurable |
| 453 | ** system call named zName. |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 454 | */ |
| 455 | static int unixSetSystemCall( |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 456 | sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ |
| 457 | const char *zName, /* Name of system call to override */ |
| 458 | sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 459 | ){ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 460 | unsigned int i; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 461 | int rc = SQLITE_NOTFOUND; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 462 | |
| 463 | UNUSED_PARAMETER(pNotUsed); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 464 | if( zName==0 ){ |
| 465 | /* If no zName is given, restore all system calls to their default |
| 466 | ** settings and return NULL |
| 467 | */ |
dan | 51438a7 | 2011-04-02 17:00:47 +0000 | [diff] [blame] | 468 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 469 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 470 | if( aSyscall[i].pDefault ){ |
| 471 | aSyscall[i].pCurrent = aSyscall[i].pDefault; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | }else{ |
| 475 | /* If zName is specified, operate on only the one system call |
| 476 | ** specified. |
| 477 | */ |
| 478 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 479 | if( strcmp(zName, aSyscall[i].zName)==0 ){ |
| 480 | if( aSyscall[i].pDefault==0 ){ |
| 481 | aSyscall[i].pDefault = aSyscall[i].pCurrent; |
| 482 | } |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 483 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 484 | if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault; |
| 485 | aSyscall[i].pCurrent = pNewFunc; |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | return rc; |
| 491 | } |
| 492 | |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 493 | /* |
| 494 | ** Return the value of a system call. Return NULL if zName is not a |
| 495 | ** recognized system call name. NULL is also returned if the system call |
| 496 | ** is currently undefined. |
| 497 | */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 498 | static sqlite3_syscall_ptr unixGetSystemCall( |
| 499 | sqlite3_vfs *pNotUsed, |
| 500 | const char *zName |
| 501 | ){ |
| 502 | unsigned int i; |
| 503 | |
| 504 | UNUSED_PARAMETER(pNotUsed); |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 505 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 506 | if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent; |
| 507 | } |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | ** Return the name of the first system call after zName. If zName==NULL |
| 513 | ** then return the name of the first system call. Return NULL if zName |
| 514 | ** is the last system call or if zName is not the name of a valid |
| 515 | ** system call. |
| 516 | */ |
| 517 | static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){ |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 518 | int i = -1; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 519 | |
| 520 | UNUSED_PARAMETER(p); |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 521 | if( zName ){ |
| 522 | for(i=0; i<ArraySize(aSyscall)-1; i++){ |
| 523 | if( strcmp(zName, aSyscall[i].zName)==0 ) break; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 526 | for(i++; i<ArraySize(aSyscall); i++){ |
| 527 | if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 528 | } |
| 529 | return 0; |
| 530 | } |
| 531 | |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 532 | /* |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 533 | ** Invoke open(). Do so multiple times, until it either succeeds or |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 534 | ** fails for some reason other than EINTR. |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 535 | ** |
| 536 | ** If the file creation mode "m" is 0 then set it to the default for |
| 537 | ** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally |
| 538 | ** 0644) as modified by the system umask. If m is not 0, then |
| 539 | ** make the file creation mode be exactly m ignoring the umask. |
| 540 | ** |
| 541 | ** The m parameter will be non-zero only when creating -wal, -journal, |
| 542 | ** and -shm files. We want those files to have *exactly* the same |
| 543 | ** permissions as their original database, unadulterated by the umask. |
| 544 | ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a |
| 545 | ** transaction crashes and leaves behind hot journals, then any |
| 546 | ** process that is able to write to the database will also be able to |
| 547 | ** recover the hot journals. |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 548 | */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 549 | static int robust_open(const char *z, int f, mode_t m){ |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 550 | int fd; |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 551 | mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS; |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 552 | do{ |
| 553 | #if defined(O_CLOEXEC) |
| 554 | fd = osOpen(z,f|O_CLOEXEC,m2); |
| 555 | #else |
| 556 | fd = osOpen(z,f,m2); |
| 557 | #endif |
| 558 | }while( fd<0 && errno==EINTR ); |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 559 | if( fd>=0 ){ |
| 560 | if( m!=0 ){ |
| 561 | struct stat statbuf; |
dan | b83c21e | 2013-03-05 15:27:34 +0000 | [diff] [blame] | 562 | if( osFstat(fd, &statbuf)==0 |
| 563 | && statbuf.st_size==0 |
drh | cfc1769 | 2013-03-06 01:41:53 +0000 | [diff] [blame] | 564 | && (statbuf.st_mode&0777)!=m |
dan | b83c21e | 2013-03-05 15:27:34 +0000 | [diff] [blame] | 565 | ){ |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 566 | osFchmod(fd, m); |
| 567 | } |
| 568 | } |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 569 | #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0) |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 570 | osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 571 | #endif |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 572 | } |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 573 | return fd; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 574 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 575 | |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 576 | /* |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 577 | ** Helper functions to obtain and relinquish the global mutex. The |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 578 | ** global mutex is used to protect the unixInodeInfo and |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 579 | ** vxworksFileId objects used by this file, all of which may be |
| 580 | ** shared by multiple threads. |
| 581 | ** |
| 582 | ** Function unixMutexHeld() is used to assert() that the global mutex |
| 583 | ** is held when required. This function is only used as part of assert() |
| 584 | ** statements. e.g. |
| 585 | ** |
| 586 | ** unixEnterMutex() |
| 587 | ** assert( unixMutexHeld() ); |
| 588 | ** unixEnterLeave() |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 589 | */ |
| 590 | static void unixEnterMutex(void){ |
| 591 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 592 | } |
| 593 | static void unixLeaveMutex(void){ |
| 594 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 595 | } |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 596 | #ifdef SQLITE_DEBUG |
| 597 | static int unixMutexHeld(void) { |
| 598 | return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 599 | } |
| 600 | #endif |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 601 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 602 | |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 603 | #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 604 | /* |
| 605 | ** Helper function for printing out trace information from debugging |
| 606 | ** binaries. This returns the string represetation of the supplied |
| 607 | ** integer lock-type. |
| 608 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 609 | static const char *azFileLock(int eFileLock){ |
| 610 | switch( eFileLock ){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 611 | case NO_LOCK: return "NONE"; |
| 612 | case SHARED_LOCK: return "SHARED"; |
| 613 | case RESERVED_LOCK: return "RESERVED"; |
| 614 | case PENDING_LOCK: return "PENDING"; |
| 615 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 616 | } |
| 617 | return "ERROR"; |
| 618 | } |
| 619 | #endif |
| 620 | |
| 621 | #ifdef SQLITE_LOCK_TRACE |
| 622 | /* |
| 623 | ** Print out information about all locking operations. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 624 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 625 | ** This routine is used for troubleshooting locks on multithreaded |
| 626 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 627 | ** command-line option on the compiler. This code is normally |
| 628 | ** turned off. |
| 629 | */ |
| 630 | static int lockTrace(int fd, int op, struct flock *p){ |
| 631 | char *zOpName, *zType; |
| 632 | int s; |
| 633 | int savedErrno; |
| 634 | if( op==F_GETLK ){ |
| 635 | zOpName = "GETLK"; |
| 636 | }else if( op==F_SETLK ){ |
| 637 | zOpName = "SETLK"; |
| 638 | }else{ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 639 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 640 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 641 | return s; |
| 642 | } |
| 643 | if( p->l_type==F_RDLCK ){ |
| 644 | zType = "RDLCK"; |
| 645 | }else if( p->l_type==F_WRLCK ){ |
| 646 | zType = "WRLCK"; |
| 647 | }else if( p->l_type==F_UNLCK ){ |
| 648 | zType = "UNLCK"; |
| 649 | }else{ |
| 650 | assert( 0 ); |
| 651 | } |
| 652 | assert( p->l_whence==SEEK_SET ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 653 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 654 | savedErrno = errno; |
| 655 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 656 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 657 | (int)p->l_pid, s); |
| 658 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 659 | struct flock l2; |
| 660 | l2 = *p; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 661 | osFcntl(fd, F_GETLK, &l2); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 662 | if( l2.l_type==F_RDLCK ){ |
| 663 | zType = "RDLCK"; |
| 664 | }else if( l2.l_type==F_WRLCK ){ |
| 665 | zType = "WRLCK"; |
| 666 | }else if( l2.l_type==F_UNLCK ){ |
| 667 | zType = "UNLCK"; |
| 668 | }else{ |
| 669 | assert( 0 ); |
| 670 | } |
| 671 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 672 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 673 | } |
| 674 | errno = savedErrno; |
| 675 | return s; |
| 676 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 677 | #undef osFcntl |
| 678 | #define osFcntl lockTrace |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 679 | #endif /* SQLITE_LOCK_TRACE */ |
| 680 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 681 | /* |
| 682 | ** Retry ftruncate() calls that fail due to EINTR |
| 683 | */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 684 | static int robust_ftruncate(int h, sqlite3_int64 sz){ |
| 685 | int rc; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 686 | do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR ); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 687 | return rc; |
| 688 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 689 | |
| 690 | /* |
| 691 | ** This routine translates a standard POSIX errno code into something |
| 692 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 693 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 694 | ** and a variety of "please close the file descriptor NOW" errors into |
| 695 | ** SQLITE_IOERR |
| 696 | ** |
| 697 | ** Errors during initialization of locks, or file system support for locks, |
| 698 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 699 | */ |
| 700 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
| 701 | switch (posixError) { |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 702 | #if 0 |
| 703 | /* At one point this code was not commented out. In theory, this branch |
| 704 | ** should never be hit, as this function should only be called after |
| 705 | ** a locking-related function (i.e. fcntl()) has returned non-zero with |
| 706 | ** the value of errno as the first argument. Since a system call has failed, |
| 707 | ** errno should be non-zero. |
| 708 | ** |
| 709 | ** Despite this, if errno really is zero, we still don't want to return |
| 710 | ** SQLITE_OK. The system call failed, and *some* SQLite error should be |
| 711 | ** propagated back to the caller. Commenting this branch out means errno==0 |
| 712 | ** will be handled by the "default:" case below. |
| 713 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 714 | case 0: |
| 715 | return SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 716 | #endif |
| 717 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 718 | case EAGAIN: |
| 719 | case ETIMEDOUT: |
| 720 | case EBUSY: |
| 721 | case EINTR: |
| 722 | case ENOLCK: |
| 723 | /* random NFS retry error, unless during file system support |
| 724 | * introspection, in which it actually means what it says */ |
| 725 | return SQLITE_BUSY; |
| 726 | |
| 727 | case EACCES: |
| 728 | /* EACCES is like EAGAIN during locking operations, but not any other time*/ |
| 729 | if( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 730 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 731 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 732 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 733 | return SQLITE_BUSY; |
| 734 | } |
| 735 | /* else fall through */ |
| 736 | case EPERM: |
| 737 | return SQLITE_PERM; |
| 738 | |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 739 | /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And |
| 740 | ** this module never makes such a call. And the code in SQLite itself |
| 741 | ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons |
| 742 | ** this case is also commented out. If the system does set errno to EDEADLK, |
| 743 | ** the default SQLITE_IOERR_XXX code will be returned. */ |
| 744 | #if 0 |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 745 | case EDEADLK: |
| 746 | return SQLITE_IOERR_BLOCKED; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 747 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 748 | |
| 749 | #if EOPNOTSUPP!=ENOTSUP |
| 750 | case EOPNOTSUPP: |
| 751 | /* something went terribly awry, unless during file system support |
| 752 | * introspection, in which it actually means what it says */ |
| 753 | #endif |
| 754 | #ifdef ENOTSUP |
| 755 | case ENOTSUP: |
| 756 | /* invalid fd, unless during file system support introspection, in which |
| 757 | * it actually means what it says */ |
| 758 | #endif |
| 759 | case EIO: |
| 760 | case EBADF: |
| 761 | case EINVAL: |
| 762 | case ENOTCONN: |
| 763 | case ENODEV: |
| 764 | case ENXIO: |
| 765 | case ENOENT: |
dan | 33067e7 | 2011-07-15 13:43:34 +0000 | [diff] [blame] | 766 | #ifdef ESTALE /* ESTALE is not defined on Interix systems */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 767 | case ESTALE: |
dan | 33067e7 | 2011-07-15 13:43:34 +0000 | [diff] [blame] | 768 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 769 | case ENOSYS: |
| 770 | /* these should force the client to close the file and reconnect */ |
| 771 | |
| 772 | default: |
| 773 | return sqliteIOErr; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | |
| 778 | |
| 779 | /****************************************************************************** |
| 780 | ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 781 | ** |
| 782 | ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 783 | ** the device number and the inode number. But this does not work on VxWorks. |
| 784 | ** On VxWorks, a unique file id must be based on the canonical filename. |
| 785 | ** |
| 786 | ** A pointer to an instance of the following structure can be used as a |
| 787 | ** unique file ID in VxWorks. Each instance of this structure contains |
| 788 | ** a copy of the canonical filename. There is also a reference count. |
| 789 | ** The structure is reclaimed when the number of pointers to it drops to |
| 790 | ** zero. |
| 791 | ** |
| 792 | ** There are never very many files open at one time and lookups are not |
| 793 | ** a performance-critical path, so it is sufficient to put these |
| 794 | ** structures on a linked list. |
| 795 | */ |
| 796 | struct vxworksFileId { |
| 797 | struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 798 | int nRef; /* Number of references to this one */ |
| 799 | int nName; /* Length of the zCanonicalName[] string */ |
| 800 | char *zCanonicalName; /* Canonical filename */ |
| 801 | }; |
| 802 | |
| 803 | #if OS_VXWORKS |
| 804 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 805 | ** All unique filenames are held on a linked list headed by this |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 806 | ** variable: |
| 807 | */ |
| 808 | static struct vxworksFileId *vxworksFileList = 0; |
| 809 | |
| 810 | /* |
| 811 | ** Simplify a filename into its canonical form |
| 812 | ** by making the following changes: |
| 813 | ** |
| 814 | ** * removing any trailing and duplicate / |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 815 | ** * convert /./ into just / |
| 816 | ** * convert /A/../ where A is any simple name into just / |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 817 | ** |
| 818 | ** Changes are made in-place. Return the new name length. |
| 819 | ** |
| 820 | ** The original filename is in z[0..n-1]. Return the number of |
| 821 | ** characters in the simplified name. |
| 822 | */ |
| 823 | static int vxworksSimplifyName(char *z, int n){ |
| 824 | int i, j; |
| 825 | while( n>1 && z[n-1]=='/' ){ n--; } |
| 826 | for(i=j=0; i<n; i++){ |
| 827 | if( z[i]=='/' ){ |
| 828 | if( z[i+1]=='/' ) continue; |
| 829 | if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 830 | i += 1; |
| 831 | continue; |
| 832 | } |
| 833 | if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 834 | while( j>0 && z[j-1]!='/' ){ j--; } |
| 835 | if( j>0 ){ j--; } |
| 836 | i += 2; |
| 837 | continue; |
| 838 | } |
| 839 | } |
| 840 | z[j++] = z[i]; |
| 841 | } |
| 842 | z[j] = 0; |
| 843 | return j; |
| 844 | } |
| 845 | |
| 846 | /* |
| 847 | ** Find a unique file ID for the given absolute pathname. Return |
| 848 | ** a pointer to the vxworksFileId object. This pointer is the unique |
| 849 | ** file ID. |
| 850 | ** |
| 851 | ** The nRef field of the vxworksFileId object is incremented before |
| 852 | ** the object is returned. A new vxworksFileId object is created |
| 853 | ** and added to the global list if necessary. |
| 854 | ** |
| 855 | ** If a memory allocation error occurs, return NULL. |
| 856 | */ |
| 857 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 858 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 859 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 860 | int n; /* Length of zAbsoluteName string */ |
| 861 | |
| 862 | assert( zAbsoluteName[0]=='/' ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 863 | n = (int)strlen(zAbsoluteName); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 864 | pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) ); |
| 865 | if( pNew==0 ) return 0; |
| 866 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 867 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 868 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 869 | |
| 870 | /* Search for an existing entry that matching the canonical name. |
| 871 | ** If found, increment the reference count and return a pointer to |
| 872 | ** the existing file ID. |
| 873 | */ |
| 874 | unixEnterMutex(); |
| 875 | for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 876 | if( pCandidate->nName==n |
| 877 | && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 878 | ){ |
| 879 | sqlite3_free(pNew); |
| 880 | pCandidate->nRef++; |
| 881 | unixLeaveMutex(); |
| 882 | return pCandidate; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | /* No match was found. We will make a new file ID */ |
| 887 | pNew->nRef = 1; |
| 888 | pNew->nName = n; |
| 889 | pNew->pNext = vxworksFileList; |
| 890 | vxworksFileList = pNew; |
| 891 | unixLeaveMutex(); |
| 892 | return pNew; |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | ** Decrement the reference count on a vxworksFileId object. Free |
| 897 | ** the object when the reference count reaches zero. |
| 898 | */ |
| 899 | static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 900 | unixEnterMutex(); |
| 901 | assert( pId->nRef>0 ); |
| 902 | pId->nRef--; |
| 903 | if( pId->nRef==0 ){ |
| 904 | struct vxworksFileId **pp; |
| 905 | for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 906 | assert( *pp==pId ); |
| 907 | *pp = pId->pNext; |
| 908 | sqlite3_free(pId); |
| 909 | } |
| 910 | unixLeaveMutex(); |
| 911 | } |
| 912 | #endif /* OS_VXWORKS */ |
| 913 | /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 914 | ******************************************************************************/ |
| 915 | |
| 916 | |
| 917 | /****************************************************************************** |
| 918 | *************************** Posix Advisory Locking **************************** |
| 919 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 920 | ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 921 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 922 | ** sets or clears a lock, that operation overrides any prior locks set |
| 923 | ** by the same process. It does not explicitly say so, but this implies |
| 924 | ** that it overrides locks set by the same process using a different |
| 925 | ** file descriptor. Consider this test case: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 926 | ** |
| 927 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 928 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 929 | ** |
| 930 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 931 | ** one is a hard or symbolic link to the other) then if you set |
| 932 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 933 | ** on fd2, it works. I would have expected the second lock to |
| 934 | ** fail since there was already a lock on the file due to fd1. |
| 935 | ** But not so. Since both locks came from the same process, the |
| 936 | ** second overrides the first, even though they were on different |
| 937 | ** file descriptors opened on different file names. |
| 938 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 939 | ** This means that we cannot use POSIX locks to synchronize file access |
| 940 | ** among competing threads of the same process. POSIX locks will work fine |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 941 | ** to synchronize access for threads in separate processes, but not |
| 942 | ** threads within the same process. |
| 943 | ** |
| 944 | ** To work around the problem, SQLite has to manage file locks internally |
| 945 | ** on its own. Whenever a new database is opened, we have to find the |
| 946 | ** specific inode of the database file (the inode is determined by the |
| 947 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 948 | ** and check for locks already existing on that inode. When locks are |
| 949 | ** created or removed, we have to look at our own internal record of the |
| 950 | ** locks to see if another thread has previously set a lock on that same |
| 951 | ** inode. |
| 952 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 953 | ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 954 | ** For VxWorks, we have to use the alternative unique ID system based on |
| 955 | ** canonical filename and implemented in the previous division.) |
| 956 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 957 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 958 | ** descriptor. It is now a structure that holds the integer file |
| 959 | ** descriptor and a pointer to a structure that describes the internal |
| 960 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 961 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 962 | ** point to the same locking structure. The locking structure keeps |
| 963 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 964 | ** field that tells us its internal lock status. cnt==0 means the |
| 965 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 966 | ** cnt>0 means there are cnt shared locks on the file. |
| 967 | ** |
| 968 | ** Any attempt to lock or unlock a file first checks the locking |
| 969 | ** structure. The fcntl() system call is only invoked to set a |
| 970 | ** POSIX lock if the internal lock structure transitions between |
| 971 | ** a locked and an unlocked state. |
| 972 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 973 | ** But wait: there are yet more problems with POSIX advisory locks. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 974 | ** |
| 975 | ** If you close a file descriptor that points to a file that has locks, |
| 976 | ** all locks on that file that are owned by the current process are |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 977 | ** released. To work around this problem, each unixInodeInfo object |
| 978 | ** maintains a count of the number of pending locks on tha inode. |
| 979 | ** When an attempt is made to close an unixFile, if there are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 980 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 981 | ** to close() the file descriptor is deferred until all of the locks clear. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 982 | ** The unixInodeInfo structure keeps a list of file descriptors that need to |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 983 | ** be closed and that list is walked (and cleared) when the last lock |
| 984 | ** clears. |
| 985 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 986 | ** Yet another problem: LinuxThreads do not play well with posix locks. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 987 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 988 | ** Many older versions of linux use the LinuxThreads library which is |
| 989 | ** not posix compliant. Under LinuxThreads, a lock created by thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 990 | ** A cannot be modified or overridden by a different thread B. |
| 991 | ** Only thread A can modify the lock. Locking behavior is correct |
| 992 | ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 993 | ** on linux - with NPTL a lock created by thread A can override locks |
| 994 | ** in thread B. But there is no way to know at compile-time which |
| 995 | ** threading library is being used. So there is no way to know at |
| 996 | ** compile-time whether or not thread A can override locks on thread B. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 997 | ** 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] | 998 | ** current process. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 999 | ** |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1000 | ** SQLite used to support LinuxThreads. But support for LinuxThreads |
| 1001 | ** was dropped beginning with version 3.7.0. SQLite will still work with |
| 1002 | ** LinuxThreads provided that (1) there is no more than one connection |
| 1003 | ** per database file in the same process and (2) database connections |
| 1004 | ** do not move across threads. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1005 | */ |
| 1006 | |
| 1007 | /* |
| 1008 | ** An instance of the following structure serves as the key used |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1009 | ** to locate a particular unixInodeInfo object. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1010 | */ |
| 1011 | struct unixFileId { |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1012 | dev_t dev; /* Device number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1013 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1014 | struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1015 | #else |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1016 | ino_t ino; /* Inode number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1017 | #endif |
| 1018 | }; |
| 1019 | |
| 1020 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1021 | ** An instance of the following structure is allocated for each open |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1022 | ** inode. Or, on LinuxThreads, there is one of these structures for |
| 1023 | ** each inode opened by each thread. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1024 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1025 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1026 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1027 | ** object keeps a count of the number of unixFile pointing to it. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1028 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1029 | struct unixInodeInfo { |
| 1030 | struct unixFileId fileId; /* The lookup key */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1031 | int nShared; /* Number of SHARED locks held */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1032 | unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
| 1033 | unsigned char bProcessLock; /* An exclusive process lock is held */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1034 | int nRef; /* Number of pointers to this structure */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1035 | unixShmNode *pShmNode; /* Shared memory associated with this inode */ |
| 1036 | int nLock; /* Number of outstanding file locks */ |
| 1037 | UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
| 1038 | unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ |
| 1039 | unixInodeInfo *pPrev; /* .... doubly linked */ |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 1040 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1041 | unsigned long long sharedByte; /* for AFP simulated shared lock */ |
| 1042 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1043 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1044 | sem_t *pSem; /* Named POSIX semaphore */ |
| 1045 | char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1046 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1047 | }; |
| 1048 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1049 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1050 | ** A lists of all unixInodeInfo objects. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1051 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1052 | static unixInodeInfo *inodeList = 0; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1053 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1054 | /* |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1055 | ** |
| 1056 | ** This function - unixLogError_x(), is only ever called via the macro |
| 1057 | ** unixLogError(). |
| 1058 | ** |
| 1059 | ** It is invoked after an error occurs in an OS function and errno has been |
| 1060 | ** set. It logs a message using sqlite3_log() containing the current value of |
| 1061 | ** errno and, if possible, the human-readable equivalent from strerror() or |
| 1062 | ** strerror_r(). |
| 1063 | ** |
| 1064 | ** The first argument passed to the macro should be the error code that |
| 1065 | ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). |
| 1066 | ** The two subsequent arguments should be the name of the OS function that |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 1067 | ** failed (e.g. "unlink", "open") and the associated file-system path, |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1068 | ** if any. |
| 1069 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1070 | #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__) |
| 1071 | static int unixLogErrorAtLine( |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1072 | int errcode, /* SQLite error code */ |
| 1073 | const char *zFunc, /* Name of OS function that failed */ |
| 1074 | const char *zPath, /* File path associated with error */ |
| 1075 | int iLine /* Source line number where error occurred */ |
| 1076 | ){ |
| 1077 | char *zErr; /* Message from strerror() or equivalent */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1078 | int iErrno = errno; /* Saved syscall error number */ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1079 | |
| 1080 | /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use |
| 1081 | ** the strerror() function to obtain the human-readable error message |
| 1082 | ** equivalent to errno. Otherwise, use strerror_r(). |
| 1083 | */ |
| 1084 | #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) |
| 1085 | char aErr[80]; |
| 1086 | memset(aErr, 0, sizeof(aErr)); |
| 1087 | zErr = aErr; |
| 1088 | |
| 1089 | /* 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] | 1090 | ** assume that the system provides the GNU version of strerror_r() that |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1091 | ** returns a pointer to a buffer containing the error message. That pointer |
| 1092 | ** may point to aErr[], or it may point to some static storage somewhere. |
| 1093 | ** Otherwise, assume that the system provides the POSIX version of |
| 1094 | ** strerror_r(), which always writes an error message into aErr[]. |
| 1095 | ** |
| 1096 | ** If the code incorrectly assumes that it is the POSIX version that is |
| 1097 | ** available, the error message will often be an empty string. Not a |
| 1098 | ** huge problem. Incorrectly concluding that the GNU version is available |
| 1099 | ** could lead to a segfault though. |
| 1100 | */ |
| 1101 | #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) |
| 1102 | zErr = |
| 1103 | # endif |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1104 | strerror_r(iErrno, aErr, sizeof(aErr)-1); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1105 | |
| 1106 | #elif SQLITE_THREADSAFE |
| 1107 | /* This is a threadsafe build, but strerror_r() is not available. */ |
| 1108 | zErr = ""; |
| 1109 | #else |
| 1110 | /* Non-threadsafe build, use strerror(). */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1111 | zErr = strerror(iErrno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1112 | #endif |
| 1113 | |
| 1114 | assert( errcode!=SQLITE_OK ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1115 | if( zPath==0 ) zPath = ""; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1116 | sqlite3_log(errcode, |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1117 | "os_unix.c:%d: (%d) %s(%s) - %s", |
| 1118 | iLine, iErrno, zFunc, zPath, zErr |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1119 | ); |
| 1120 | |
| 1121 | return errcode; |
| 1122 | } |
| 1123 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1124 | /* |
| 1125 | ** Close a file descriptor. |
| 1126 | ** |
| 1127 | ** We assume that close() almost always works, since it is only in a |
| 1128 | ** very sick application or on a very sick platform that it might fail. |
| 1129 | ** If it does fail, simply leak the file descriptor, but do log the |
| 1130 | ** error. |
| 1131 | ** |
| 1132 | ** Note that it is not safe to retry close() after EINTR since the |
| 1133 | ** file descriptor might have already been reused by another thread. |
| 1134 | ** So we don't even try to recover from an EINTR. Just log the error |
| 1135 | ** and move on. |
| 1136 | */ |
| 1137 | static void robust_close(unixFile *pFile, int h, int lineno){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1138 | if( osClose(h) ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1139 | unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", |
| 1140 | pFile ? pFile->zPath : 0, lineno); |
| 1141 | } |
| 1142 | } |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1143 | |
| 1144 | /* |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1145 | ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1146 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1147 | static void closePendingFds(unixFile *pFile){ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1148 | unixInodeInfo *pInode = pFile->pInode; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1149 | UnixUnusedFd *p; |
| 1150 | UnixUnusedFd *pNext; |
| 1151 | for(p=pInode->pUnused; p; p=pNext){ |
| 1152 | pNext = p->pNext; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1153 | robust_close(pFile, p->fd, __LINE__); |
| 1154 | sqlite3_free(p); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1155 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1156 | pInode->pUnused = 0; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1160 | ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1161 | ** |
| 1162 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1163 | ** when this function is called. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1164 | */ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1165 | static void releaseInodeInfo(unixFile *pFile){ |
| 1166 | unixInodeInfo *pInode = pFile->pInode; |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1167 | assert( unixMutexHeld() ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1168 | if( ALWAYS(pInode) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1169 | pInode->nRef--; |
| 1170 | if( pInode->nRef==0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1171 | assert( pInode->pShmNode==0 ); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1172 | closePendingFds(pFile); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1173 | if( pInode->pPrev ){ |
| 1174 | assert( pInode->pPrev->pNext==pInode ); |
| 1175 | pInode->pPrev->pNext = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1176 | }else{ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1177 | assert( inodeList==pInode ); |
| 1178 | inodeList = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1179 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1180 | if( pInode->pNext ){ |
| 1181 | assert( pInode->pNext->pPrev==pInode ); |
| 1182 | pInode->pNext->pPrev = pInode->pPrev; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1183 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1184 | sqlite3_free(pInode); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1185 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1190 | ** Given a file descriptor, locate the unixInodeInfo object that |
| 1191 | ** describes that file descriptor. Create a new one if necessary. The |
| 1192 | ** return value might be uninitialized if an error occurs. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1193 | ** |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1194 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1195 | ** when this function is called. |
| 1196 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1197 | ** Return an appropriate error code. |
| 1198 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1199 | static int findInodeInfo( |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1200 | unixFile *pFile, /* Unix file with file desc used in the key */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1201 | unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1202 | ){ |
| 1203 | int rc; /* System call return code */ |
| 1204 | int fd; /* The file descriptor for pFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1205 | struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ |
| 1206 | struct stat statbuf; /* Low-level file information */ |
| 1207 | unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1208 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1209 | assert( unixMutexHeld() ); |
| 1210 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1211 | /* Get low-level information about the file that we can used to |
| 1212 | ** create a unique name for the file. |
| 1213 | */ |
| 1214 | fd = pFile->h; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1215 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1216 | if( rc!=0 ){ |
| 1217 | pFile->lastErrno = errno; |
| 1218 | #ifdef EOVERFLOW |
| 1219 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 1220 | #endif |
| 1221 | return SQLITE_IOERR; |
| 1222 | } |
| 1223 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1224 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1225 | /* On OS X on an msdos filesystem, the inode number is reported |
| 1226 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 1227 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 1228 | ** we always increase the file size to 1 by writing a single byte |
| 1229 | ** prior to accessing the inode number. The one byte written is |
| 1230 | ** an ASCII 'S' character which also happens to be the first byte |
| 1231 | ** in the header of every SQLite database. In this way, if there |
| 1232 | ** is a race condition such that another thread has already populated |
| 1233 | ** the first page of the database, no damage is done. |
| 1234 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1235 | if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 1236 | do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR ); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1237 | if( rc!=1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1238 | pFile->lastErrno = errno; |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1239 | return SQLITE_IOERR; |
| 1240 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1241 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1242 | if( rc!=0 ){ |
| 1243 | pFile->lastErrno = errno; |
| 1244 | return SQLITE_IOERR; |
| 1245 | } |
| 1246 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1247 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1248 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1249 | memset(&fileId, 0, sizeof(fileId)); |
| 1250 | fileId.dev = statbuf.st_dev; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1251 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1252 | fileId.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1253 | #else |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1254 | fileId.ino = statbuf.st_ino; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1255 | #endif |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1256 | pInode = inodeList; |
| 1257 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 1258 | pInode = pInode->pNext; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1259 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1260 | if( pInode==0 ){ |
| 1261 | pInode = sqlite3_malloc( sizeof(*pInode) ); |
| 1262 | if( pInode==0 ){ |
| 1263 | return SQLITE_NOMEM; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1264 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1265 | memset(pInode, 0, sizeof(*pInode)); |
| 1266 | memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
| 1267 | pInode->nRef = 1; |
| 1268 | pInode->pNext = inodeList; |
| 1269 | pInode->pPrev = 0; |
| 1270 | if( inodeList ) inodeList->pPrev = pInode; |
| 1271 | inodeList = pInode; |
| 1272 | }else{ |
| 1273 | pInode->nRef++; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1274 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1275 | *ppInode = pInode; |
| 1276 | return SQLITE_OK; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1277 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1278 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1279 | |
| 1280 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1281 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1282 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1283 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1284 | ** 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] | 1285 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1286 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1287 | int rc = SQLITE_OK; |
| 1288 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1289 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1290 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1291 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1292 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1293 | assert( pFile ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1294 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1295 | |
| 1296 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1297 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1298 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1301 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1302 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1303 | #ifndef __DJGPP__ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1304 | if( !reserved && !pFile->pInode->bProcessLock ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1305 | struct flock lock; |
| 1306 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1307 | lock.l_start = RESERVED_BYTE; |
| 1308 | lock.l_len = 1; |
| 1309 | lock.l_type = F_WRLCK; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1310 | if( osFcntl(pFile->h, F_GETLK, &lock) ){ |
| 1311 | rc = SQLITE_IOERR_CHECKRESERVEDLOCK; |
| 1312 | pFile->lastErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1313 | } else if( lock.l_type!=F_UNLCK ){ |
| 1314 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1315 | } |
| 1316 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1317 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1318 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1319 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1320 | OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1321 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1322 | *pResOut = reserved; |
| 1323 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1327 | ** Attempt to set a system-lock on the file pFile. The lock is |
| 1328 | ** described by pLock. |
| 1329 | ** |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1330 | ** If the pFile was opened read/write from unix-excl, then the only lock |
| 1331 | ** ever obtained is an exclusive lock, and it is obtained exactly once |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1332 | ** the first time any lock is attempted. All subsequent system locking |
| 1333 | ** operations become no-ops. Locking operations still happen internally, |
| 1334 | ** in order to coordinate access between separate database connections |
| 1335 | ** within this process, but all of that is handled in memory and the |
| 1336 | ** operating system does not participate. |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1337 | ** |
| 1338 | ** This function is a pass-through to fcntl(F_SETLK) if pFile is using |
| 1339 | ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl" |
| 1340 | ** and is read-only. |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1341 | ** |
| 1342 | ** Zero is returned if the call completes successfully, or -1 if a call |
| 1343 | ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()). |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1344 | */ |
| 1345 | static int unixFileLock(unixFile *pFile, struct flock *pLock){ |
| 1346 | int rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1347 | unixInodeInfo *pInode = pFile->pInode; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1348 | assert( unixMutexHeld() ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1349 | assert( pInode!=0 ); |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1350 | if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock) |
| 1351 | && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0) |
| 1352 | ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1353 | if( pInode->bProcessLock==0 ){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1354 | struct flock lock; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1355 | assert( pInode->nLock==0 ); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1356 | lock.l_whence = SEEK_SET; |
| 1357 | lock.l_start = SHARED_FIRST; |
| 1358 | lock.l_len = SHARED_SIZE; |
| 1359 | lock.l_type = F_WRLCK; |
| 1360 | rc = osFcntl(pFile->h, F_SETLK, &lock); |
| 1361 | if( rc<0 ) return rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1362 | pInode->bProcessLock = 1; |
| 1363 | pInode->nLock++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1364 | }else{ |
| 1365 | rc = 0; |
| 1366 | } |
| 1367 | }else{ |
| 1368 | rc = osFcntl(pFile->h, F_SETLK, pLock); |
| 1369 | } |
| 1370 | return rc; |
| 1371 | } |
| 1372 | |
| 1373 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1374 | ** Lock the file with the lock specified by parameter eFileLock - one |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1375 | ** of the following: |
| 1376 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1377 | ** (1) SHARED_LOCK |
| 1378 | ** (2) RESERVED_LOCK |
| 1379 | ** (3) PENDING_LOCK |
| 1380 | ** (4) EXCLUSIVE_LOCK |
| 1381 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1382 | ** Sometimes when requesting one lock state, additional lock states |
| 1383 | ** are inserted in between. The locking might fail on one of the later |
| 1384 | ** transitions leaving the lock state different from what it started but |
| 1385 | ** still short of its goal. The following chart shows the allowed |
| 1386 | ** transitions and the inserted intermediate states: |
| 1387 | ** |
| 1388 | ** UNLOCKED -> SHARED |
| 1389 | ** SHARED -> RESERVED |
| 1390 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1391 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1392 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1393 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1394 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1395 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1396 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1397 | static int unixLock(sqlite3_file *id, int eFileLock){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1398 | /* The following describes the implementation of the various locks and |
| 1399 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1400 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1401 | ** confusion with SQLite lock names). The algorithms are complicated |
| 1402 | ** slightly in order to be compatible with windows systems simultaneously |
| 1403 | ** accessing the same database file, in case that is ever required. |
| 1404 | ** |
| 1405 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1406 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1407 | ** range', a range of 510 bytes at a well known offset. |
| 1408 | ** |
| 1409 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 1410 | ** byte'. If this is successful, a random byte from the 'shared byte |
| 1411 | ** range' is read-locked and the lock on the 'pending byte' released. |
| 1412 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1413 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1414 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1415 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1416 | ** |
| 1417 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1418 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1419 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1420 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1421 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1422 | ** This property is used by the algorithm for rolling back a journal file |
| 1423 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1424 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1425 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1426 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1427 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1428 | ** within this range, this ensures that no other locks are held on the |
| 1429 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1430 | ** |
| 1431 | ** The reason a single byte cannot be used instead of the 'shared byte |
| 1432 | ** range' is that some versions of windows do not support read-locks. By |
| 1433 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 1434 | ** even if the locking primitive used is always a write-lock. |
| 1435 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1436 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1437 | unixFile *pFile = (unixFile*)id; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 1438 | unixInodeInfo *pInode; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1439 | struct flock lock; |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 1440 | int tErrno = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1441 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1442 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1443 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 1444 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 1445 | azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid())); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1446 | |
| 1447 | /* 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] | 1448 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1449 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1450 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1451 | if( pFile->eFileLock>=eFileLock ){ |
| 1452 | OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
| 1453 | azFileLock(eFileLock))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1454 | return SQLITE_OK; |
| 1455 | } |
| 1456 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1457 | /* Make sure the locking sequence is correct. |
| 1458 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 1459 | ** (2) SQLite never explicitly requests a pendig lock. |
| 1460 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1461 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1462 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 1463 | assert( eFileLock!=PENDING_LOCK ); |
| 1464 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1465 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1466 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1467 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1468 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1469 | pInode = pFile->pInode; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1470 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1471 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1472 | ** handle that precludes the requested lock, return BUSY. |
| 1473 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1474 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 1475 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1476 | ){ |
| 1477 | rc = SQLITE_BUSY; |
| 1478 | goto end_lock; |
| 1479 | } |
| 1480 | |
| 1481 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1482 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1483 | ** return SQLITE_OK. |
| 1484 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1485 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1486 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1487 | assert( eFileLock==SHARED_LOCK ); |
| 1488 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1489 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1490 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1491 | pInode->nShared++; |
| 1492 | pInode->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1493 | goto end_lock; |
| 1494 | } |
| 1495 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1496 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1497 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1498 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1499 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1500 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1501 | lock.l_len = 1L; |
| 1502 | lock.l_whence = SEEK_SET; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1503 | if( eFileLock==SHARED_LOCK |
| 1504 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1505 | ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1506 | lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1507 | lock.l_start = PENDING_BYTE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1508 | if( unixFileLock(pFile, &lock) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1509 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1510 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1511 | if( rc!=SQLITE_BUSY ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1512 | pFile->lastErrno = tErrno; |
| 1513 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1514 | goto end_lock; |
| 1515 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | |
| 1519 | /* If control gets to this point, then actually go ahead and make |
| 1520 | ** operating system calls for the specified lock. |
| 1521 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1522 | if( eFileLock==SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1523 | assert( pInode->nShared==0 ); |
| 1524 | assert( pInode->eFileLock==0 ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1525 | assert( rc==SQLITE_OK ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1526 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1527 | /* Now get the read-lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1528 | lock.l_start = SHARED_FIRST; |
| 1529 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1530 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1531 | tErrno = errno; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1532 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1533 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1534 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1535 | /* Drop the temporary PENDING lock */ |
| 1536 | lock.l_start = PENDING_BYTE; |
| 1537 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1538 | lock.l_type = F_UNLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1539 | if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){ |
| 1540 | /* This could happen with a network mount */ |
| 1541 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1542 | rc = SQLITE_IOERR_UNLOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1543 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1544 | |
| 1545 | if( rc ){ |
| 1546 | if( rc!=SQLITE_BUSY ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1547 | pFile->lastErrno = tErrno; |
| 1548 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1549 | goto end_lock; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1550 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1551 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1552 | pInode->nLock++; |
| 1553 | pInode->nShared = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1554 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1555 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1556 | /* We are trying for an exclusive lock but another thread in this |
| 1557 | ** same process is still holding a shared lock. */ |
| 1558 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1559 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1560 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1561 | ** assumed that there is a SHARED or greater lock on the file |
| 1562 | ** already. |
| 1563 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1564 | assert( 0!=pFile->eFileLock ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1565 | lock.l_type = F_WRLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1566 | |
| 1567 | assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK ); |
| 1568 | if( eFileLock==RESERVED_LOCK ){ |
| 1569 | lock.l_start = RESERVED_BYTE; |
| 1570 | lock.l_len = 1L; |
| 1571 | }else{ |
| 1572 | lock.l_start = SHARED_FIRST; |
| 1573 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1574 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1575 | |
| 1576 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1577 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1578 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1579 | if( rc!=SQLITE_BUSY ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1580 | pFile->lastErrno = tErrno; |
| 1581 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1582 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1583 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1584 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1585 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 1586 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1587 | /* Set up the transaction-counter change checking flags when |
| 1588 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1589 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1590 | ** write operation (not a hot journal rollback). |
| 1591 | */ |
| 1592 | if( rc==SQLITE_OK |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1593 | && pFile->eFileLock<=SHARED_LOCK |
| 1594 | && eFileLock==RESERVED_LOCK |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1595 | ){ |
| 1596 | pFile->transCntrChng = 0; |
| 1597 | pFile->dbUpdate = 0; |
| 1598 | pFile->inNormalWrite = 1; |
| 1599 | } |
| 1600 | #endif |
| 1601 | |
| 1602 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1603 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1604 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1605 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1606 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 1607 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1608 | pInode->eFileLock = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1609 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1610 | |
| 1611 | end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1612 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1613 | OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), |
| 1614 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1615 | return rc; |
| 1616 | } |
| 1617 | |
| 1618 | /* |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1619 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1620 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1621 | */ |
| 1622 | static void setPendingFd(unixFile *pFile){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1623 | unixInodeInfo *pInode = pFile->pInode; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1624 | UnixUnusedFd *p = pFile->pUnused; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1625 | p->pNext = pInode->pUnused; |
| 1626 | pInode->pUnused = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1627 | pFile->h = -1; |
| 1628 | pFile->pUnused = 0; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1632 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1633 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1634 | ** |
| 1635 | ** If the locking level of the file descriptor is already at or below |
| 1636 | ** the requested locking level, this routine is a no-op. |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1637 | ** |
| 1638 | ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 1639 | ** the byte range is divided into 2 parts and the first part is unlocked then |
| 1640 | ** set to a read lock, then the other part is simply unlocked. This works |
| 1641 | ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 1642 | ** remove the write lock on a region when a read lock is set. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1643 | */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1644 | static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1645 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1646 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1647 | struct flock lock; |
| 1648 | int rc = SQLITE_OK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1649 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1650 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1651 | 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] | 1652 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1653 | getpid())); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1654 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1655 | assert( eFileLock<=SHARED_LOCK ); |
| 1656 | if( pFile->eFileLock<=eFileLock ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1657 | return SQLITE_OK; |
| 1658 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1659 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1660 | pInode = pFile->pInode; |
| 1661 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1662 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1663 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1664 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 1665 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1666 | /* When reducing a lock such that other processes can start |
| 1667 | ** reading the database file again, make sure that the |
| 1668 | ** transaction counter was updated if any part of the database |
| 1669 | ** file changed. If the transaction counter is not updated, |
| 1670 | ** other connections to the same file might not realize that |
| 1671 | ** the file has changed and hence might not know to flush their |
| 1672 | ** cache. The use of a stale cache can lead to database corruption. |
| 1673 | */ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1674 | pFile->inNormalWrite = 0; |
| 1675 | #endif |
| 1676 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1677 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 1678 | ** before establishing the readlock - to avoid a race condition we downgrade |
| 1679 | ** the lock in 2 blocks, so that part of the range will be covered by a |
| 1680 | ** write lock until the rest is covered by a read lock: |
| 1681 | ** 1: [WWWWW] |
| 1682 | ** 2: [....W] |
| 1683 | ** 3: [RRRRW] |
| 1684 | ** 4: [RRRR.] |
| 1685 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1686 | if( eFileLock==SHARED_LOCK ){ |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1687 | |
| 1688 | #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE |
drh | 87e79ae | 2011-03-08 13:06:41 +0000 | [diff] [blame] | 1689 | (void)handleNFSUnlock; |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1690 | assert( handleNFSUnlock==0 ); |
| 1691 | #endif |
| 1692 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1693 | if( handleNFSUnlock ){ |
drh | 026663d | 2011-04-01 13:29:29 +0000 | [diff] [blame] | 1694 | int tErrno; /* Error code from system call errors */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1695 | off_t divSize = SHARED_SIZE - 1; |
| 1696 | |
| 1697 | lock.l_type = F_UNLCK; |
| 1698 | lock.l_whence = SEEK_SET; |
| 1699 | lock.l_start = SHARED_FIRST; |
| 1700 | lock.l_len = divSize; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 1701 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1702 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1703 | rc = SQLITE_IOERR_UNLOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1704 | if( IS_LOCK_ERROR(rc) ){ |
| 1705 | pFile->lastErrno = tErrno; |
| 1706 | } |
| 1707 | goto end_unlock; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1708 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1709 | lock.l_type = F_RDLCK; |
| 1710 | lock.l_whence = SEEK_SET; |
| 1711 | lock.l_start = SHARED_FIRST; |
| 1712 | lock.l_len = divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1713 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1714 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1715 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1716 | if( IS_LOCK_ERROR(rc) ){ |
| 1717 | pFile->lastErrno = tErrno; |
| 1718 | } |
| 1719 | goto end_unlock; |
| 1720 | } |
| 1721 | lock.l_type = F_UNLCK; |
| 1722 | lock.l_whence = SEEK_SET; |
| 1723 | lock.l_start = SHARED_FIRST+divSize; |
| 1724 | lock.l_len = SHARED_SIZE-divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1725 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1726 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1727 | rc = SQLITE_IOERR_UNLOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1728 | if( IS_LOCK_ERROR(rc) ){ |
| 1729 | pFile->lastErrno = tErrno; |
| 1730 | } |
| 1731 | goto end_unlock; |
| 1732 | } |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1733 | }else |
| 1734 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 1735 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1736 | lock.l_type = F_RDLCK; |
| 1737 | lock.l_whence = SEEK_SET; |
| 1738 | lock.l_start = SHARED_FIRST; |
| 1739 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1740 | if( unixFileLock(pFile, &lock) ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1741 | /* In theory, the call to unixFileLock() cannot fail because another |
| 1742 | ** process is holding an incompatible lock. If it does, this |
| 1743 | ** indicates that the other process is not following the locking |
| 1744 | ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning |
| 1745 | ** SQLITE_BUSY would confuse the upper layer (in practice it causes |
| 1746 | ** an assert to fail). */ |
| 1747 | rc = SQLITE_IOERR_RDLOCK; |
| 1748 | pFile->lastErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1749 | goto end_unlock; |
| 1750 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1751 | } |
| 1752 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1753 | lock.l_type = F_UNLCK; |
| 1754 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1755 | lock.l_start = PENDING_BYTE; |
| 1756 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1757 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1758 | pInode->eFileLock = SHARED_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1759 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1760 | rc = SQLITE_IOERR_UNLOCK; |
| 1761 | pFile->lastErrno = errno; |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 1762 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1763 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1764 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1765 | if( eFileLock==NO_LOCK ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1766 | /* Decrement the shared lock counter. Release the lock using an |
| 1767 | ** OS call only when all threads in this same process have released |
| 1768 | ** the lock. |
| 1769 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1770 | pInode->nShared--; |
| 1771 | if( pInode->nShared==0 ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1772 | lock.l_type = F_UNLCK; |
| 1773 | lock.l_whence = SEEK_SET; |
| 1774 | lock.l_start = lock.l_len = 0L; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1775 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1776 | pInode->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1777 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1778 | rc = SQLITE_IOERR_UNLOCK; |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1779 | pFile->lastErrno = errno; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1780 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1781 | pFile->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1782 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1785 | /* Decrement the count of locks against this same file. When the |
| 1786 | ** count reaches zero, close any other file descriptors whose close |
| 1787 | ** was deferred because of outstanding locks. |
| 1788 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1789 | pInode->nLock--; |
| 1790 | assert( pInode->nLock>=0 ); |
| 1791 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1792 | closePendingFds(pFile); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1793 | } |
| 1794 | } |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1795 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1796 | end_unlock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1797 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1798 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1799 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1803 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1804 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1805 | ** |
| 1806 | ** If the locking level of the file descriptor is already at or below |
| 1807 | ** the requested locking level, this routine is a no-op. |
| 1808 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1809 | static int unixUnlock(sqlite3_file *id, int eFileLock){ |
dan | a1afc74 | 2013-03-25 13:50:49 +0000 | [diff] [blame] | 1810 | assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 ); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1811 | return posixUnlock(id, eFileLock, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 1814 | static int unixMapfile(unixFile *pFd, i64 nByte); |
| 1815 | static void unixUnmapfile(unixFile *pFd); |
| 1816 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1817 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1818 | ** This function performs the parts of the "close file" operation |
| 1819 | ** common to all locking schemes. It closes the directory and file |
| 1820 | ** handles, if they are valid, and sets all fields of the unixFile |
| 1821 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1822 | ** |
| 1823 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 1824 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 1825 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1826 | */ |
| 1827 | static int closeUnixFile(sqlite3_file *id){ |
| 1828 | unixFile *pFile = (unixFile*)id; |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 1829 | unixUnmapfile(pFile); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1830 | if( pFile->h>=0 ){ |
| 1831 | robust_close(pFile, pFile->h, __LINE__); |
| 1832 | pFile->h = -1; |
| 1833 | } |
| 1834 | #if OS_VXWORKS |
| 1835 | if( pFile->pId ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 1836 | if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 1837 | osUnlink(pFile->pId->zCanonicalName); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1838 | } |
| 1839 | vxworksReleaseFileId(pFile->pId); |
| 1840 | pFile->pId = 0; |
| 1841 | } |
| 1842 | #endif |
| 1843 | OSTRACE(("CLOSE %-3d\n", pFile->h)); |
| 1844 | OpenCounter(-1); |
| 1845 | sqlite3_free(pFile->pUnused); |
| 1846 | memset(pFile, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1847 | return SQLITE_OK; |
| 1848 | } |
| 1849 | |
| 1850 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1851 | ** Close a file. |
| 1852 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1853 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1854 | int rc = SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1855 | unixFile *pFile = (unixFile *)id; |
| 1856 | unixUnlock(id, NO_LOCK); |
| 1857 | unixEnterMutex(); |
| 1858 | |
| 1859 | /* unixFile.pInode is always valid here. Otherwise, a different close |
| 1860 | ** routine (e.g. nolockClose()) would be called instead. |
| 1861 | */ |
| 1862 | assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); |
| 1863 | if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){ |
| 1864 | /* If there are outstanding locks, do not actually close the file just |
| 1865 | ** yet because that would clear those locks. Instead, add the file |
| 1866 | ** descriptor to pInode->pUnused list. It will be automatically closed |
| 1867 | ** when the last lock is cleared. |
| 1868 | */ |
| 1869 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1870 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1871 | releaseInodeInfo(pFile); |
| 1872 | rc = closeUnixFile(id); |
| 1873 | unixLeaveMutex(); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1874 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1877 | /************** End of the posix advisory lock implementation ***************** |
| 1878 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1879 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1880 | /****************************************************************************** |
| 1881 | ****************************** No-op Locking ********************************** |
| 1882 | ** |
| 1883 | ** Of the various locking implementations available, this is by far the |
| 1884 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 1885 | ** file for reading or writing. |
| 1886 | ** |
| 1887 | ** This locking mode is appropriate for use on read-only databases |
| 1888 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 1889 | ** also be used if the application employs some external mechanism to |
| 1890 | ** prevent simultaneous access of the same database by two or more |
| 1891 | ** database connections. But there is a serious risk of database |
| 1892 | ** corruption if this locking mode is used in situations where multiple |
| 1893 | ** database connections are accessing the same database file at the same |
| 1894 | ** time and one or more of those connections are writing. |
| 1895 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1896 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1897 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 1898 | UNUSED_PARAMETER(NotUsed); |
| 1899 | *pResOut = 0; |
| 1900 | return SQLITE_OK; |
| 1901 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1902 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1903 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1904 | return SQLITE_OK; |
| 1905 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1906 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1907 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1908 | return SQLITE_OK; |
| 1909 | } |
| 1910 | |
| 1911 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1912 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1913 | */ |
| 1914 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1915 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | /******************* End of the no-op lock implementation ********************* |
| 1919 | ******************************************************************************/ |
| 1920 | |
| 1921 | /****************************************************************************** |
| 1922 | ************************* Begin dot-file Locking ****************************** |
| 1923 | ** |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1924 | ** The dotfile locking implementation uses the existance of separate lock |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 1925 | ** files (really a directory) to control access to the database. This works |
| 1926 | ** on just about every filesystem imaginable. But there are serious downsides: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1927 | ** |
| 1928 | ** (1) There is zero concurrency. A single reader blocks all other |
| 1929 | ** connections from reading or writing the database. |
| 1930 | ** |
| 1931 | ** (2) An application crash or power loss can leave stale lock files |
| 1932 | ** sitting around that need to be cleared manually. |
| 1933 | ** |
| 1934 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 1935 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1936 | ** |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 1937 | ** Dotfile locking works by creating a subdirectory in the same directory as |
| 1938 | ** the database and with the same name but with a ".lock" extension added. |
| 1939 | ** The existance of a lock directory implies an EXCLUSIVE lock. All other |
| 1940 | ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1941 | */ |
| 1942 | |
| 1943 | /* |
| 1944 | ** 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] | 1945 | ** lock directory. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1946 | */ |
| 1947 | #define DOTLOCK_SUFFIX ".lock" |
| 1948 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1949 | /* |
| 1950 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1951 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1952 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1953 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1954 | ** |
| 1955 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 1956 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 1957 | ** is held on the file and false if the file is unlocked. |
| 1958 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1959 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 1960 | int rc = SQLITE_OK; |
| 1961 | int reserved = 0; |
| 1962 | unixFile *pFile = (unixFile*)id; |
| 1963 | |
| 1964 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1965 | |
| 1966 | assert( pFile ); |
| 1967 | |
| 1968 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1969 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1970 | /* Either this connection or some other connection in the same process |
| 1971 | ** holds a lock on the file. No need to check further. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1972 | reserved = 1; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1973 | }else{ |
| 1974 | /* The lock is held if and only if the lockfile exists */ |
| 1975 | const char *zLockFile = (const char*)pFile->lockingContext; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1976 | reserved = osAccess(zLockFile, 0)==0; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1977 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1978 | OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1979 | *pResOut = reserved; |
| 1980 | return rc; |
| 1981 | } |
| 1982 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1983 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1984 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1985 | ** of the following: |
| 1986 | ** |
| 1987 | ** (1) SHARED_LOCK |
| 1988 | ** (2) RESERVED_LOCK |
| 1989 | ** (3) PENDING_LOCK |
| 1990 | ** (4) EXCLUSIVE_LOCK |
| 1991 | ** |
| 1992 | ** Sometimes when requesting one lock state, additional lock states |
| 1993 | ** are inserted in between. The locking might fail on one of the later |
| 1994 | ** transitions leaving the lock state different from what it started but |
| 1995 | ** still short of its goal. The following chart shows the allowed |
| 1996 | ** transitions and the inserted intermediate states: |
| 1997 | ** |
| 1998 | ** UNLOCKED -> SHARED |
| 1999 | ** SHARED -> RESERVED |
| 2000 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2001 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2002 | ** PENDING -> EXCLUSIVE |
| 2003 | ** |
| 2004 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2005 | ** routine to lower a locking level. |
| 2006 | ** |
| 2007 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 2008 | ** But we track the other locking levels internally. |
| 2009 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2010 | static int dotlockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2011 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2012 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2013 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2014 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2015 | |
| 2016 | /* If we have any lock, then the lock file already exists. All we have |
| 2017 | ** to do is adjust our internal record of the lock level. |
| 2018 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2019 | if( pFile->eFileLock > NO_LOCK ){ |
| 2020 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2021 | /* Always update the timestamp on the old file */ |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 2022 | #ifdef HAVE_UTIME |
| 2023 | utime(zLockFile, NULL); |
| 2024 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2025 | utimes(zLockFile, NULL); |
| 2026 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2027 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | /* grab an exclusive lock */ |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2031 | rc = osMkdir(zLockFile, 0777); |
| 2032 | if( rc<0 ){ |
| 2033 | /* failed to open/create the lock directory */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2034 | int tErrno = errno; |
| 2035 | if( EEXIST == tErrno ){ |
| 2036 | rc = SQLITE_BUSY; |
| 2037 | } else { |
| 2038 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2039 | if( IS_LOCK_ERROR(rc) ){ |
| 2040 | pFile->lastErrno = tErrno; |
| 2041 | } |
| 2042 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2043 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2044 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2045 | |
| 2046 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2047 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2048 | return rc; |
| 2049 | } |
| 2050 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2051 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2052 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2053 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2054 | ** |
| 2055 | ** If the locking level of the file descriptor is already at or below |
| 2056 | ** the requested locking level, this routine is a no-op. |
| 2057 | ** |
| 2058 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 2059 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2060 | static int dotlockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2061 | unixFile *pFile = (unixFile*)id; |
| 2062 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2063 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2064 | |
| 2065 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2066 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2067 | pFile->eFileLock, getpid())); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2068 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2069 | |
| 2070 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2071 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2072 | return SQLITE_OK; |
| 2073 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2074 | |
| 2075 | /* To downgrade to shared, simply update our internal notion of the |
| 2076 | ** lock state. No need to mess with the file on disk. |
| 2077 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2078 | if( eFileLock==SHARED_LOCK ){ |
| 2079 | pFile->eFileLock = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2080 | return SQLITE_OK; |
| 2081 | } |
| 2082 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2083 | /* To fully unlock the database, delete the lock file */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2084 | assert( eFileLock==NO_LOCK ); |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2085 | rc = osRmdir(zLockFile); |
| 2086 | if( rc<0 && errno==ENOTDIR ) rc = osUnlink(zLockFile); |
| 2087 | if( rc<0 ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 2088 | int tErrno = errno; |
drh | 13e0ea9 | 2011-12-11 02:29:25 +0000 | [diff] [blame] | 2089 | rc = 0; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2090 | if( ENOENT != tErrno ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2091 | rc = SQLITE_IOERR_UNLOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2092 | } |
| 2093 | if( IS_LOCK_ERROR(rc) ){ |
| 2094 | pFile->lastErrno = tErrno; |
| 2095 | } |
| 2096 | return rc; |
| 2097 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2098 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2099 | return SQLITE_OK; |
| 2100 | } |
| 2101 | |
| 2102 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2103 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2104 | */ |
| 2105 | static int dotlockClose(sqlite3_file *id) { |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 2106 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2107 | if( id ){ |
| 2108 | unixFile *pFile = (unixFile*)id; |
| 2109 | dotlockUnlock(id, NO_LOCK); |
| 2110 | sqlite3_free(pFile->lockingContext); |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 2111 | rc = closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2112 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2113 | return rc; |
| 2114 | } |
| 2115 | /****************** End of the dot-file lock implementation ******************* |
| 2116 | ******************************************************************************/ |
| 2117 | |
| 2118 | /****************************************************************************** |
| 2119 | ************************** Begin flock Locking ******************************** |
| 2120 | ** |
| 2121 | ** Use the flock() system call to do file locking. |
| 2122 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2123 | ** flock() locking is like dot-file locking in that the various |
| 2124 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 2125 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 2126 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 2127 | ** still works when you do this, but concurrency is reduced since |
| 2128 | ** only a single process can be reading the database at a time. |
| 2129 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2130 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if |
| 2131 | ** compiling for VXWORKS. |
| 2132 | */ |
| 2133 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2134 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2135 | /* |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2136 | ** Retry flock() calls that fail with EINTR |
| 2137 | */ |
| 2138 | #ifdef EINTR |
| 2139 | static int robust_flock(int fd, int op){ |
| 2140 | int rc; |
| 2141 | do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR ); |
| 2142 | return rc; |
| 2143 | } |
| 2144 | #else |
drh | 5c81927 | 2011-02-23 14:00:12 +0000 | [diff] [blame] | 2145 | # define robust_flock(a,b) flock(a,b) |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2146 | #endif |
| 2147 | |
| 2148 | |
| 2149 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2150 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2151 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2152 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2153 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2154 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2155 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 2156 | int rc = SQLITE_OK; |
| 2157 | int reserved = 0; |
| 2158 | unixFile *pFile = (unixFile*)id; |
| 2159 | |
| 2160 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2161 | |
| 2162 | assert( pFile ); |
| 2163 | |
| 2164 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2165 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2166 | reserved = 1; |
| 2167 | } |
| 2168 | |
| 2169 | /* Otherwise see if some other process holds it. */ |
| 2170 | if( !reserved ){ |
| 2171 | /* attempt to get the lock */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2172 | int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2173 | if( !lrc ){ |
| 2174 | /* got the lock, unlock it */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2175 | lrc = robust_flock(pFile->h, LOCK_UN); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2176 | if ( lrc ) { |
| 2177 | int tErrno = errno; |
| 2178 | /* unlock failed with an error */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2179 | lrc = SQLITE_IOERR_UNLOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2180 | if( IS_LOCK_ERROR(lrc) ){ |
| 2181 | pFile->lastErrno = tErrno; |
| 2182 | rc = lrc; |
| 2183 | } |
| 2184 | } |
| 2185 | } else { |
| 2186 | int tErrno = errno; |
| 2187 | reserved = 1; |
| 2188 | /* someone else might have it reserved */ |
| 2189 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2190 | if( IS_LOCK_ERROR(lrc) ){ |
| 2191 | pFile->lastErrno = tErrno; |
| 2192 | rc = lrc; |
| 2193 | } |
| 2194 | } |
| 2195 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2196 | OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2197 | |
| 2198 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2199 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2200 | rc = SQLITE_OK; |
| 2201 | reserved=1; |
| 2202 | } |
| 2203 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2204 | *pResOut = reserved; |
| 2205 | return rc; |
| 2206 | } |
| 2207 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2208 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2209 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2210 | ** of the following: |
| 2211 | ** |
| 2212 | ** (1) SHARED_LOCK |
| 2213 | ** (2) RESERVED_LOCK |
| 2214 | ** (3) PENDING_LOCK |
| 2215 | ** (4) EXCLUSIVE_LOCK |
| 2216 | ** |
| 2217 | ** Sometimes when requesting one lock state, additional lock states |
| 2218 | ** are inserted in between. The locking might fail on one of the later |
| 2219 | ** transitions leaving the lock state different from what it started but |
| 2220 | ** still short of its goal. The following chart shows the allowed |
| 2221 | ** transitions and the inserted intermediate states: |
| 2222 | ** |
| 2223 | ** UNLOCKED -> SHARED |
| 2224 | ** SHARED -> RESERVED |
| 2225 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2226 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2227 | ** PENDING -> EXCLUSIVE |
| 2228 | ** |
| 2229 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 2230 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2231 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2232 | ** access the file. |
| 2233 | ** |
| 2234 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2235 | ** routine to lower a locking level. |
| 2236 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2237 | static int flockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2238 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2239 | unixFile *pFile = (unixFile*)id; |
| 2240 | |
| 2241 | assert( pFile ); |
| 2242 | |
| 2243 | /* if we already have a lock, it is exclusive. |
| 2244 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2245 | if (pFile->eFileLock > NO_LOCK) { |
| 2246 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2247 | return SQLITE_OK; |
| 2248 | } |
| 2249 | |
| 2250 | /* grab an exclusive lock */ |
| 2251 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2252 | if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2253 | int tErrno = errno; |
| 2254 | /* didn't get, must be busy */ |
| 2255 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2256 | if( IS_LOCK_ERROR(rc) ){ |
| 2257 | pFile->lastErrno = tErrno; |
| 2258 | } |
| 2259 | } else { |
| 2260 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2261 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2262 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2263 | OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), |
| 2264 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2265 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2266 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2267 | rc = SQLITE_BUSY; |
| 2268 | } |
| 2269 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2270 | return rc; |
| 2271 | } |
| 2272 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2273 | |
| 2274 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2275 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2276 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2277 | ** |
| 2278 | ** If the locking level of the file descriptor is already at or below |
| 2279 | ** the requested locking level, this routine is a no-op. |
| 2280 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2281 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2282 | unixFile *pFile = (unixFile*)id; |
| 2283 | |
| 2284 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2285 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
| 2286 | pFile->eFileLock, getpid())); |
| 2287 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2288 | |
| 2289 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2290 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2291 | return SQLITE_OK; |
| 2292 | } |
| 2293 | |
| 2294 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2295 | if (eFileLock==SHARED_LOCK) { |
| 2296 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2297 | return SQLITE_OK; |
| 2298 | } |
| 2299 | |
| 2300 | /* no, really, unlock. */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2301 | if( robust_flock(pFile->h, LOCK_UN) ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2302 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2303 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2304 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2305 | return SQLITE_IOERR_UNLOCK; |
| 2306 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2307 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2308 | return SQLITE_OK; |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | /* |
| 2313 | ** Close a file. |
| 2314 | */ |
| 2315 | static int flockClose(sqlite3_file *id) { |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 2316 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2317 | if( id ){ |
| 2318 | flockUnlock(id, NO_LOCK); |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 2319 | rc = closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2320 | } |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 2321 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2322 | } |
| 2323 | |
| 2324 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 2325 | |
| 2326 | /******************* End of the flock lock implementation ********************* |
| 2327 | ******************************************************************************/ |
| 2328 | |
| 2329 | /****************************************************************************** |
| 2330 | ************************ Begin Named Semaphore Locking ************************ |
| 2331 | ** |
| 2332 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2333 | ** |
| 2334 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 2335 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 2336 | ** the database file at a time. This reduces potential concurrency, but |
| 2337 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2338 | */ |
| 2339 | #if OS_VXWORKS |
| 2340 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2341 | /* |
| 2342 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2343 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2344 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2345 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2346 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2347 | static int semCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 2348 | int rc = SQLITE_OK; |
| 2349 | int reserved = 0; |
| 2350 | unixFile *pFile = (unixFile*)id; |
| 2351 | |
| 2352 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2353 | |
| 2354 | assert( pFile ); |
| 2355 | |
| 2356 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2357 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2358 | reserved = 1; |
| 2359 | } |
| 2360 | |
| 2361 | /* Otherwise see if some other process holds it. */ |
| 2362 | if( !reserved ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2363 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2364 | struct stat statBuf; |
| 2365 | |
| 2366 | if( sem_trywait(pSem)==-1 ){ |
| 2367 | int tErrno = errno; |
| 2368 | if( EAGAIN != tErrno ){ |
| 2369 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 2370 | pFile->lastErrno = tErrno; |
| 2371 | } else { |
| 2372 | /* someone else has the lock when we are in NO_LOCK */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2373 | reserved = (pFile->eFileLock < SHARED_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2374 | } |
| 2375 | }else{ |
| 2376 | /* we could have it if we want it */ |
| 2377 | sem_post(pSem); |
| 2378 | } |
| 2379 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2380 | OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2381 | |
| 2382 | *pResOut = reserved; |
| 2383 | return rc; |
| 2384 | } |
| 2385 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2386 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2387 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2388 | ** of the following: |
| 2389 | ** |
| 2390 | ** (1) SHARED_LOCK |
| 2391 | ** (2) RESERVED_LOCK |
| 2392 | ** (3) PENDING_LOCK |
| 2393 | ** (4) EXCLUSIVE_LOCK |
| 2394 | ** |
| 2395 | ** Sometimes when requesting one lock state, additional lock states |
| 2396 | ** are inserted in between. The locking might fail on one of the later |
| 2397 | ** transitions leaving the lock state different from what it started but |
| 2398 | ** still short of its goal. The following chart shows the allowed |
| 2399 | ** transitions and the inserted intermediate states: |
| 2400 | ** |
| 2401 | ** UNLOCKED -> SHARED |
| 2402 | ** SHARED -> RESERVED |
| 2403 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2404 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2405 | ** PENDING -> EXCLUSIVE |
| 2406 | ** |
| 2407 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 2408 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2409 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2410 | ** access the file. |
| 2411 | ** |
| 2412 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2413 | ** routine to lower a locking level. |
| 2414 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2415 | static int semLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2416 | unixFile *pFile = (unixFile*)id; |
| 2417 | int fd; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2418 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2419 | int rc = SQLITE_OK; |
| 2420 | |
| 2421 | /* if we already have a lock, it is exclusive. |
| 2422 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2423 | if (pFile->eFileLock > NO_LOCK) { |
| 2424 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2425 | rc = SQLITE_OK; |
| 2426 | goto sem_end_lock; |
| 2427 | } |
| 2428 | |
| 2429 | /* lock semaphore now but bail out when already locked. */ |
| 2430 | if( sem_trywait(pSem)==-1 ){ |
| 2431 | rc = SQLITE_BUSY; |
| 2432 | goto sem_end_lock; |
| 2433 | } |
| 2434 | |
| 2435 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2436 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2437 | |
| 2438 | sem_end_lock: |
| 2439 | return rc; |
| 2440 | } |
| 2441 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2442 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2443 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2444 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2445 | ** |
| 2446 | ** If the locking level of the file descriptor is already at or below |
| 2447 | ** the requested locking level, this routine is a no-op. |
| 2448 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2449 | static int semUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2450 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2451 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2452 | |
| 2453 | assert( pFile ); |
| 2454 | assert( pSem ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2455 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2456 | pFile->eFileLock, getpid())); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2457 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2458 | |
| 2459 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2460 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2461 | return SQLITE_OK; |
| 2462 | } |
| 2463 | |
| 2464 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2465 | if (eFileLock==SHARED_LOCK) { |
| 2466 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2467 | return SQLITE_OK; |
| 2468 | } |
| 2469 | |
| 2470 | /* no, really unlock. */ |
| 2471 | if ( sem_post(pSem)==-1 ) { |
| 2472 | int rc, tErrno = errno; |
| 2473 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2474 | if( IS_LOCK_ERROR(rc) ){ |
| 2475 | pFile->lastErrno = tErrno; |
| 2476 | } |
| 2477 | return rc; |
| 2478 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2479 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2480 | return SQLITE_OK; |
| 2481 | } |
| 2482 | |
| 2483 | /* |
| 2484 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2485 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2486 | static int semClose(sqlite3_file *id) { |
| 2487 | if( id ){ |
| 2488 | unixFile *pFile = (unixFile*)id; |
| 2489 | semUnlock(id, NO_LOCK); |
| 2490 | assert( pFile ); |
| 2491 | unixEnterMutex(); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2492 | releaseInodeInfo(pFile); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2493 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2494 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2495 | } |
| 2496 | return SQLITE_OK; |
| 2497 | } |
| 2498 | |
| 2499 | #endif /* OS_VXWORKS */ |
| 2500 | /* |
| 2501 | ** Named semaphore locking is only available on VxWorks. |
| 2502 | ** |
| 2503 | *************** End of the named semaphore lock implementation **************** |
| 2504 | ******************************************************************************/ |
| 2505 | |
| 2506 | |
| 2507 | /****************************************************************************** |
| 2508 | *************************** Begin AFP Locking ********************************* |
| 2509 | ** |
| 2510 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2511 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2512 | ** |
| 2513 | ** Third-party implementations of AFP are available. But this code here |
| 2514 | ** only works on OSX. |
| 2515 | */ |
| 2516 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2517 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2518 | /* |
| 2519 | ** The afpLockingContext structure contains all afp lock specific state |
| 2520 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2521 | typedef struct afpLockingContext afpLockingContext; |
| 2522 | struct afpLockingContext { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2523 | int reserved; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2524 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2525 | }; |
| 2526 | |
| 2527 | struct ByteRangeLockPB2 |
| 2528 | { |
| 2529 | unsigned long long offset; /* offset to first byte to lock */ |
| 2530 | unsigned long long length; /* nbr of bytes to lock */ |
| 2531 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2532 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2533 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2534 | int fd; /* file desc to assoc this lock with */ |
| 2535 | }; |
| 2536 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2537 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2538 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2539 | /* |
| 2540 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2541 | ** AFP filesystem. |
| 2542 | ** |
| 2543 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2544 | */ |
| 2545 | static int afpSetLock( |
| 2546 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2547 | unixFile *pFile, /* Open file descriptor on path */ |
| 2548 | unsigned long long offset, /* First byte to be locked */ |
| 2549 | unsigned long long length, /* Number of bytes to lock */ |
| 2550 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2551 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2552 | struct ByteRangeLockPB2 pb; |
| 2553 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2554 | |
| 2555 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2556 | pb.startEndFlag = 0; |
| 2557 | pb.offset = offset; |
| 2558 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2559 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2560 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2561 | OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2562 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2563 | offset, length)); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2564 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2565 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2566 | int rc; |
| 2567 | int tErrno = errno; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2568 | OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2569 | path, tErrno, strerror(tErrno))); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2570 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2571 | rc = SQLITE_BUSY; |
| 2572 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2573 | rc = sqliteErrorFromPosixError(tErrno, |
| 2574 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2575 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2576 | if( IS_LOCK_ERROR(rc) ){ |
| 2577 | pFile->lastErrno = tErrno; |
| 2578 | } |
| 2579 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2580 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2581 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2582 | } |
| 2583 | } |
| 2584 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2585 | /* |
| 2586 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2587 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2588 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2589 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2590 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2591 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2592 | int rc = SQLITE_OK; |
| 2593 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2594 | unixFile *pFile = (unixFile*)id; |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2595 | afpLockingContext *context; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2596 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2597 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2598 | |
| 2599 | assert( pFile ); |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2600 | context = (afpLockingContext *) pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2601 | if( context->reserved ){ |
| 2602 | *pResOut = 1; |
| 2603 | return SQLITE_OK; |
| 2604 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2605 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2606 | |
| 2607 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2608 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2609 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
| 2612 | /* Otherwise see if some other process holds it. |
| 2613 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2614 | if( !reserved ){ |
| 2615 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2616 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2617 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2618 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2619 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2620 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2621 | } else { |
| 2622 | /* if we failed to get the lock then someone else must have it */ |
| 2623 | reserved = 1; |
| 2624 | } |
| 2625 | if( IS_LOCK_ERROR(lrc) ){ |
| 2626 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2627 | } |
| 2628 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2629 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2630 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2631 | OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2632 | |
| 2633 | *pResOut = reserved; |
| 2634 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2635 | } |
| 2636 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2637 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2638 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2639 | ** of the following: |
| 2640 | ** |
| 2641 | ** (1) SHARED_LOCK |
| 2642 | ** (2) RESERVED_LOCK |
| 2643 | ** (3) PENDING_LOCK |
| 2644 | ** (4) EXCLUSIVE_LOCK |
| 2645 | ** |
| 2646 | ** Sometimes when requesting one lock state, additional lock states |
| 2647 | ** are inserted in between. The locking might fail on one of the later |
| 2648 | ** transitions leaving the lock state different from what it started but |
| 2649 | ** still short of its goal. The following chart shows the allowed |
| 2650 | ** transitions and the inserted intermediate states: |
| 2651 | ** |
| 2652 | ** UNLOCKED -> SHARED |
| 2653 | ** SHARED -> RESERVED |
| 2654 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2655 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2656 | ** PENDING -> EXCLUSIVE |
| 2657 | ** |
| 2658 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2659 | ** routine to lower a locking level. |
| 2660 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2661 | static int afpLock(sqlite3_file *id, int eFileLock){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2662 | int rc = SQLITE_OK; |
| 2663 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2664 | unixInodeInfo *pInode = pFile->pInode; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2665 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2666 | |
| 2667 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2668 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 2669 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2670 | azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2671 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2672 | /* 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] | 2673 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2674 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2675 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2676 | if( pFile->eFileLock>=eFileLock ){ |
| 2677 | OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
| 2678 | azFileLock(eFileLock))); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2679 | return SQLITE_OK; |
| 2680 | } |
| 2681 | |
| 2682 | /* Make sure the locking sequence is correct |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2683 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 2684 | ** (2) SQLite never explicitly requests a pendig lock. |
| 2685 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2686 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2687 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 2688 | assert( eFileLock!=PENDING_LOCK ); |
| 2689 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2690 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2691 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2692 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2693 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2694 | pInode = pFile->pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2695 | |
| 2696 | /* If some thread using this PID has a lock via a different unixFile* |
| 2697 | ** handle that precludes the requested lock, return BUSY. |
| 2698 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2699 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 2700 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2701 | ){ |
| 2702 | rc = SQLITE_BUSY; |
| 2703 | goto afp_end_lock; |
| 2704 | } |
| 2705 | |
| 2706 | /* If a SHARED lock is requested, and some thread using this PID already |
| 2707 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 2708 | ** return SQLITE_OK. |
| 2709 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2710 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2711 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2712 | assert( eFileLock==SHARED_LOCK ); |
| 2713 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2714 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2715 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2716 | pInode->nShared++; |
| 2717 | pInode->nLock++; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2718 | goto afp_end_lock; |
| 2719 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2720 | |
| 2721 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2722 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2723 | ** be released. |
| 2724 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2725 | if( eFileLock==SHARED_LOCK |
| 2726 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2727 | ){ |
| 2728 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2729 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2730 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2731 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2732 | goto afp_end_lock; |
| 2733 | } |
| 2734 | } |
| 2735 | |
| 2736 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2737 | ** operating system calls for the specified lock. |
| 2738 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2739 | if( eFileLock==SHARED_LOCK ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2740 | int lrc1, lrc2, lrc1Errno = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2741 | long lk, mask; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2742 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2743 | assert( pInode->nShared==0 ); |
| 2744 | assert( pInode->eFileLock==0 ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2745 | |
| 2746 | mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2747 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2748 | /* note that the quality of the randomness doesn't matter that much */ |
| 2749 | lk = random(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2750 | pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2751 | lrc1 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2752 | SHARED_FIRST+pInode->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2753 | if( IS_LOCK_ERROR(lrc1) ){ |
| 2754 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2755 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2756 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2757 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2758 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2759 | if( IS_LOCK_ERROR(lrc1) ) { |
| 2760 | pFile->lastErrno = lrc1Errno; |
| 2761 | rc = lrc1; |
| 2762 | goto afp_end_lock; |
| 2763 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 2764 | rc = lrc2; |
| 2765 | goto afp_end_lock; |
| 2766 | } else if( lrc1 != SQLITE_OK ) { |
| 2767 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2768 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2769 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2770 | pInode->nLock++; |
| 2771 | pInode->nShared = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2772 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2773 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2774 | /* We are trying for an exclusive lock but another thread in this |
| 2775 | ** same process is still holding a shared lock. */ |
| 2776 | rc = SQLITE_BUSY; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2777 | }else{ |
| 2778 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 2779 | ** assumed that there is a SHARED or greater lock on the file |
| 2780 | ** already. |
| 2781 | */ |
| 2782 | int failed = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2783 | assert( 0!=pFile->eFileLock ); |
| 2784 | if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2785 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2786 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2787 | if( !failed ){ |
| 2788 | context->reserved = 1; |
| 2789 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2790 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2791 | if (!failed && eFileLock == EXCLUSIVE_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2792 | /* Acquire an EXCLUSIVE lock */ |
| 2793 | |
| 2794 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2795 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2796 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2797 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2798 | pInode->sharedByte, 1, 0)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2799 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2800 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2801 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2802 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2803 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2804 | SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2805 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 2806 | ** a critical I/O error |
| 2807 | */ |
| 2808 | rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : |
| 2809 | SQLITE_IOERR_LOCK; |
| 2810 | goto afp_end_lock; |
| 2811 | } |
| 2812 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2813 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2814 | } |
| 2815 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2816 | if( failed ){ |
| 2817 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2818 | } |
| 2819 | } |
| 2820 | |
| 2821 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2822 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2823 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2824 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 2825 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2826 | pInode->eFileLock = PENDING_LOCK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
| 2829 | afp_end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2830 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2831 | OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), |
| 2832 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2833 | return rc; |
| 2834 | } |
| 2835 | |
| 2836 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2837 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2838 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2839 | ** |
| 2840 | ** If the locking level of the file descriptor is already at or below |
| 2841 | ** the requested locking level, this routine is a no-op. |
| 2842 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2843 | static int afpUnlock(sqlite3_file *id, int eFileLock) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2844 | int rc = SQLITE_OK; |
| 2845 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2846 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2847 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 2848 | int skipShared = 0; |
| 2849 | #ifdef SQLITE_TEST |
| 2850 | int h = pFile->h; |
| 2851 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2852 | |
| 2853 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2854 | 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] | 2855 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2856 | getpid())); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2857 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2858 | assert( eFileLock<=SHARED_LOCK ); |
| 2859 | if( pFile->eFileLock<=eFileLock ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2860 | return SQLITE_OK; |
| 2861 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2862 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2863 | pInode = pFile->pInode; |
| 2864 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2865 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2866 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2867 | SimulateIOErrorBenign(1); |
| 2868 | SimulateIOError( h=(-1) ) |
| 2869 | SimulateIOErrorBenign(0); |
| 2870 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 2871 | #ifdef SQLITE_DEBUG |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2872 | /* When reducing a lock such that other processes can start |
| 2873 | ** reading the database file again, make sure that the |
| 2874 | ** transaction counter was updated if any part of the database |
| 2875 | ** file changed. If the transaction counter is not updated, |
| 2876 | ** other connections to the same file might not realize that |
| 2877 | ** the file has changed and hence might not know to flush their |
| 2878 | ** cache. The use of a stale cache can lead to database corruption. |
| 2879 | */ |
| 2880 | assert( pFile->inNormalWrite==0 |
| 2881 | || pFile->dbUpdate==0 |
| 2882 | || pFile->transCntrChng==1 ); |
| 2883 | pFile->inNormalWrite = 0; |
| 2884 | #endif |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2885 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2886 | if( pFile->eFileLock==EXCLUSIVE_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2887 | rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2888 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2889 | /* only re-establish the shared lock if necessary */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2890 | int sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2891 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 2892 | } else { |
| 2893 | skipShared = 1; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2894 | } |
| 2895 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2896 | if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2897 | rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2898 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2899 | if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2900 | rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 2901 | if( !rc ){ |
| 2902 | context->reserved = 0; |
| 2903 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2904 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2905 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ |
| 2906 | pInode->eFileLock = SHARED_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2907 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2908 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2909 | if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2910 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2911 | /* Decrement the shared lock counter. Release the lock using an |
| 2912 | ** OS call only when all threads in this same process have released |
| 2913 | ** the lock. |
| 2914 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2915 | unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 2916 | pInode->nShared--; |
| 2917 | if( pInode->nShared==0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2918 | SimulateIOErrorBenign(1); |
| 2919 | SimulateIOError( h=(-1) ) |
| 2920 | SimulateIOErrorBenign(0); |
| 2921 | if( !skipShared ){ |
| 2922 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 2923 | } |
| 2924 | if( !rc ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2925 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2926 | pFile->eFileLock = NO_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2927 | } |
| 2928 | } |
| 2929 | if( rc==SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2930 | pInode->nLock--; |
| 2931 | assert( pInode->nLock>=0 ); |
| 2932 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 2933 | closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2934 | } |
| 2935 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2936 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2937 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2938 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2939 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2940 | return rc; |
| 2941 | } |
| 2942 | |
| 2943 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2944 | ** Close a file & cleanup AFP specific locking context |
| 2945 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2946 | static int afpClose(sqlite3_file *id) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2947 | int rc = SQLITE_OK; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2948 | if( id ){ |
| 2949 | unixFile *pFile = (unixFile*)id; |
| 2950 | afpUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2951 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2952 | if( pFile->pInode && pFile->pInode->nLock ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2953 | /* If there are outstanding locks, do not actually close the file just |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2954 | ** yet because that would clear those locks. Instead, add the file |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2955 | ** descriptor to pInode->aPending. It will be automatically closed when |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2956 | ** the last lock is cleared. |
| 2957 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2958 | setPendingFd(pFile); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2959 | } |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2960 | releaseInodeInfo(pFile); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2961 | sqlite3_free(pFile->lockingContext); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2962 | rc = closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2963 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2964 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2965 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2968 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2969 | /* |
| 2970 | ** The code above is the AFP lock implementation. The code is specific |
| 2971 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2972 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 2973 | ** VFS is not available. |
| 2974 | ** |
| 2975 | ********************* End of the AFP lock implementation ********************** |
| 2976 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2977 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2978 | /****************************************************************************** |
| 2979 | *************************** Begin NFS Locking ********************************/ |
| 2980 | |
| 2981 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 2982 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2983 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2984 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2985 | ** |
| 2986 | ** If the locking level of the file descriptor is already at or below |
| 2987 | ** the requested locking level, this routine is a no-op. |
| 2988 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2989 | static int nfsUnlock(sqlite3_file *id, int eFileLock){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 2990 | return posixUnlock(id, eFileLock, 1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
| 2993 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 2994 | /* |
| 2995 | ** The code above is the NFS lock implementation. The code is specific |
| 2996 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2997 | ** is available. |
| 2998 | ** |
| 2999 | ********************* End of the NFS lock implementation ********************** |
| 3000 | ******************************************************************************/ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3001 | |
| 3002 | /****************************************************************************** |
| 3003 | **************** Non-locking sqlite3_file methods ***************************** |
| 3004 | ** |
| 3005 | ** The next division contains implementations for all methods of the |
| 3006 | ** sqlite3_file object other than the locking methods. The locking |
| 3007 | ** methods were defined in divisions above (one locking method per |
| 3008 | ** division). Those methods that are common to all locking modes |
| 3009 | ** are gather together into this division. |
| 3010 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3011 | |
| 3012 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3013 | ** Seek to the offset passed as the second argument, then read cnt |
| 3014 | ** bytes into pBuf. Return the number of bytes actually read. |
| 3015 | ** |
| 3016 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 3017 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 3018 | ** one system to another. Since SQLite does not define USE_PREAD |
| 3019 | ** any any form by default, we will not attempt to define _XOPEN_SOURCE. |
| 3020 | ** See tickets #2741 and #2681. |
| 3021 | ** |
| 3022 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 3023 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3024 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3025 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 3026 | int got; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3027 | int prior = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3028 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3029 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3030 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3031 | TIMER_START; |
drh | c1fd2cf | 2012-10-01 12:16:26 +0000 | [diff] [blame] | 3032 | assert( cnt==(cnt&0x1ffff) ); |
| 3033 | cnt &= 0x1ffff; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3034 | do{ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3035 | #if defined(USE_PREAD) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3036 | got = osPread(id->h, pBuf, cnt, offset); |
| 3037 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3038 | #elif defined(USE_PREAD64) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3039 | got = osPread64(id->h, pBuf, cnt, offset); |
| 3040 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3041 | #else |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3042 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 3043 | SimulateIOError( newOffset-- ); |
| 3044 | if( newOffset!=offset ){ |
| 3045 | if( newOffset == -1 ){ |
| 3046 | ((unixFile*)id)->lastErrno = errno; |
| 3047 | }else{ |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 3048 | ((unixFile*)id)->lastErrno = 0; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3049 | } |
| 3050 | return -1; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3051 | } |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3052 | got = osRead(id->h, pBuf, cnt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3053 | #endif |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3054 | if( got==cnt ) break; |
| 3055 | if( got<0 ){ |
| 3056 | if( errno==EINTR ){ got = 1; continue; } |
| 3057 | prior = 0; |
| 3058 | ((unixFile*)id)->lastErrno = errno; |
| 3059 | break; |
| 3060 | }else if( got>0 ){ |
| 3061 | cnt -= got; |
| 3062 | offset += got; |
| 3063 | prior += got; |
| 3064 | pBuf = (void*)(got + (char*)pBuf); |
| 3065 | } |
| 3066 | }while( got>0 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3067 | TIMER_END; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3068 | OSTRACE(("READ %-3d %5d %7lld %llu\n", |
| 3069 | id->h, got+prior, offset-prior, TIMER_ELAPSED)); |
| 3070 | return got+prior; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3071 | } |
| 3072 | |
| 3073 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3074 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 3075 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 3076 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3077 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3078 | static int unixRead( |
| 3079 | sqlite3_file *id, |
| 3080 | void *pBuf, |
| 3081 | int amt, |
| 3082 | sqlite3_int64 offset |
| 3083 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3084 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3085 | int got; |
| 3086 | assert( id ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3087 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3088 | /* If this is a database file (not a journal, master-journal or temp |
| 3089 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3090 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3091 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3092 | || offset>=PENDING_BYTE+512 |
| 3093 | || offset+amt<=PENDING_BYTE |
| 3094 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3095 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3096 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3097 | /* Deal with as much of this write request as possible by transfering |
| 3098 | ** data to the memory mapping using memcpy(). */ |
| 3099 | if( offset<pFile->mmapSize ){ |
| 3100 | if( offset+amt <= pFile->mmapSize ){ |
| 3101 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt); |
| 3102 | return SQLITE_OK; |
| 3103 | }else{ |
| 3104 | int nCopy = pFile->mmapSize - offset; |
| 3105 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy); |
| 3106 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3107 | amt -= nCopy; |
| 3108 | offset += nCopy; |
| 3109 | } |
| 3110 | } |
| 3111 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3112 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3113 | if( got==amt ){ |
| 3114 | return SQLITE_OK; |
| 3115 | }else if( got<0 ){ |
| 3116 | /* lastErrno set by seekAndRead */ |
| 3117 | return SQLITE_IOERR_READ; |
| 3118 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3119 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3120 | /* Unread parts of the buffer must be zero-filled */ |
| 3121 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 3122 | return SQLITE_IOERR_SHORT_READ; |
| 3123 | } |
| 3124 | } |
| 3125 | |
| 3126 | /* |
| 3127 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 3128 | ** Return the number of bytes actually read. Update the offset. |
| 3129 | ** |
| 3130 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 3131 | ** is set before returning. |
| 3132 | */ |
| 3133 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
| 3134 | int got; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3135 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3136 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3137 | #endif |
drh | c1fd2cf | 2012-10-01 12:16:26 +0000 | [diff] [blame] | 3138 | assert( cnt==(cnt&0x1ffff) ); |
| 3139 | cnt &= 0x1ffff; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3140 | TIMER_START; |
| 3141 | #if defined(USE_PREAD) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 3142 | do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3143 | #elif defined(USE_PREAD64) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 3144 | do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3145 | #else |
drh | bd1e50c | 2011-08-19 14:54:12 +0000 | [diff] [blame] | 3146 | do{ |
| 3147 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 3148 | SimulateIOError( newOffset-- ); |
| 3149 | if( newOffset!=offset ){ |
| 3150 | if( newOffset == -1 ){ |
| 3151 | ((unixFile*)id)->lastErrno = errno; |
| 3152 | }else{ |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 3153 | ((unixFile*)id)->lastErrno = 0; |
drh | bd1e50c | 2011-08-19 14:54:12 +0000 | [diff] [blame] | 3154 | } |
| 3155 | return -1; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3156 | } |
drh | bd1e50c | 2011-08-19 14:54:12 +0000 | [diff] [blame] | 3157 | got = osWrite(id->h, pBuf, cnt); |
| 3158 | }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3159 | #endif |
| 3160 | TIMER_END; |
| 3161 | if( got<0 ){ |
| 3162 | ((unixFile*)id)->lastErrno = errno; |
| 3163 | } |
| 3164 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3165 | OSTRACE(("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3166 | return got; |
| 3167 | } |
| 3168 | |
| 3169 | |
| 3170 | /* |
| 3171 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 3172 | ** or some other error code on failure. |
| 3173 | */ |
| 3174 | static int unixWrite( |
| 3175 | sqlite3_file *id, |
| 3176 | const void *pBuf, |
| 3177 | int amt, |
| 3178 | sqlite3_int64 offset |
| 3179 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3180 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3181 | int wrote = 0; |
| 3182 | assert( id ); |
| 3183 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3184 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3185 | /* If this is a database file (not a journal, master-journal or temp |
| 3186 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3187 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3188 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3189 | || offset>=PENDING_BYTE+512 |
| 3190 | || offset+amt<=PENDING_BYTE |
| 3191 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3192 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3193 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3194 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3195 | /* If we are doing a normal write to a database file (as opposed to |
| 3196 | ** doing a hot-journal rollback or a write to some file other than a |
| 3197 | ** normal database file) then record the fact that the database |
| 3198 | ** has changed. If the transaction counter is modified, record that |
| 3199 | ** fact too. |
| 3200 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3201 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3202 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 3203 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3204 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3205 | char oldCntr[4]; |
| 3206 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3207 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3208 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3209 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3210 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 3211 | } |
| 3212 | } |
| 3213 | } |
| 3214 | #endif |
| 3215 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3216 | /* Deal with as much of this write request as possible by transfering |
| 3217 | ** data from the memory mapping using memcpy(). */ |
| 3218 | if( offset<pFile->mmapSize ){ |
| 3219 | if( offset+amt <= pFile->mmapSize ){ |
| 3220 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt); |
| 3221 | return SQLITE_OK; |
| 3222 | }else{ |
| 3223 | int nCopy = pFile->mmapSize - offset; |
| 3224 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy); |
| 3225 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3226 | amt -= nCopy; |
| 3227 | offset += nCopy; |
| 3228 | } |
| 3229 | } |
| 3230 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3231 | while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3232 | amt -= wrote; |
| 3233 | offset += wrote; |
| 3234 | pBuf = &((char*)pBuf)[wrote]; |
| 3235 | } |
| 3236 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 3237 | SimulateDiskfullError(( wrote=0, amt=1 )); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3238 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3239 | if( amt>0 ){ |
drh | a21b83b | 2011-04-15 12:36:10 +0000 | [diff] [blame] | 3240 | if( wrote<0 && pFile->lastErrno!=ENOSPC ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3241 | /* lastErrno set by seekAndWrite */ |
| 3242 | return SQLITE_IOERR_WRITE; |
| 3243 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3244 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3245 | return SQLITE_FULL; |
| 3246 | } |
| 3247 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3248 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3249 | return SQLITE_OK; |
| 3250 | } |
| 3251 | |
| 3252 | #ifdef SQLITE_TEST |
| 3253 | /* |
| 3254 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3255 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3256 | */ |
| 3257 | int sqlite3_sync_count = 0; |
| 3258 | int sqlite3_fullsync_count = 0; |
| 3259 | #endif |
| 3260 | |
| 3261 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3262 | ** We do not trust systems to provide a working fdatasync(). Some do. |
drh | 20f8e13 | 2011-08-31 21:01:55 +0000 | [diff] [blame] | 3263 | ** Others do no. To be safe, we will stick with the (slightly slower) |
| 3264 | ** fsync(). If you know that your system does support fdatasync() correctly, |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3265 | ** then simply compile with -Dfdatasync=fdatasync |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3266 | */ |
drh | 20f8e13 | 2011-08-31 21:01:55 +0000 | [diff] [blame] | 3267 | #if !defined(fdatasync) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3268 | # define fdatasync fsync |
| 3269 | #endif |
| 3270 | |
| 3271 | /* |
| 3272 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 3273 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 3274 | ** only available on Mac OS X. But that could change. |
| 3275 | */ |
| 3276 | #ifdef F_FULLFSYNC |
| 3277 | # define HAVE_FULLFSYNC 1 |
| 3278 | #else |
| 3279 | # define HAVE_FULLFSYNC 0 |
| 3280 | #endif |
| 3281 | |
| 3282 | |
| 3283 | /* |
| 3284 | ** The fsync() system call does not work as advertised on many |
| 3285 | ** unix systems. The following procedure is an attempt to make |
| 3286 | ** it work better. |
| 3287 | ** |
| 3288 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 3289 | ** for testing when we want to run through the test suite quickly. |
| 3290 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 3291 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 3292 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3293 | ** |
| 3294 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 3295 | ** The idea behind dataOnly is that it should only write the file content |
| 3296 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 3297 | ** unchanged since the file size is part of the inode. However, |
| 3298 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 3299 | ** file size has changed. The only real difference between fdatasync() |
| 3300 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 3301 | ** inode if the mtime or owner or other inode attributes have changed. |
| 3302 | ** We only care about the file size, not the other file attributes, so |
| 3303 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 3304 | ** So, we always use fdatasync() if it is available, regardless of |
| 3305 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3306 | */ |
| 3307 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3308 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3309 | |
| 3310 | /* The following "ifdef/elif/else/" block has the same structure as |
| 3311 | ** the one below. It is replicated here solely to avoid cluttering |
| 3312 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 3313 | */ |
| 3314 | #ifdef SQLITE_NO_SYNC |
| 3315 | UNUSED_PARAMETER(fd); |
| 3316 | UNUSED_PARAMETER(fullSync); |
| 3317 | UNUSED_PARAMETER(dataOnly); |
| 3318 | #elif HAVE_FULLFSYNC |
| 3319 | UNUSED_PARAMETER(dataOnly); |
| 3320 | #else |
| 3321 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3322 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3323 | #endif |
| 3324 | |
| 3325 | /* Record the number of times that we do a normal fsync() and |
| 3326 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 3327 | ** gets called with the correct arguments. |
| 3328 | */ |
| 3329 | #ifdef SQLITE_TEST |
| 3330 | if( fullSync ) sqlite3_fullsync_count++; |
| 3331 | sqlite3_sync_count++; |
| 3332 | #endif |
| 3333 | |
| 3334 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 3335 | ** no-op |
| 3336 | */ |
| 3337 | #ifdef SQLITE_NO_SYNC |
| 3338 | rc = SQLITE_OK; |
| 3339 | #elif HAVE_FULLFSYNC |
| 3340 | if( fullSync ){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3341 | rc = osFcntl(fd, F_FULLFSYNC, 0); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3342 | }else{ |
| 3343 | rc = 1; |
| 3344 | } |
| 3345 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3346 | ** It shouldn't be possible for fullfsync to fail on the local |
| 3347 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 3348 | ** isn't supported for this file system. So, attempt an fsync |
| 3349 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 3350 | ** It'd be better to detect fullfsync support once and avoid |
| 3351 | ** the fcntl call every time sync is called. |
| 3352 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3353 | if( rc ) rc = fsync(fd); |
| 3354 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3355 | #elif defined(__APPLE__) |
| 3356 | /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 3357 | ** so currently we default to the macro that redefines fdatasync to fsync |
| 3358 | */ |
| 3359 | rc = fsync(fd); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3360 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3361 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 3362 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3363 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3364 | rc = fsync(fd); |
| 3365 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3366 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3367 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 3368 | |
| 3369 | if( OS_VXWORKS && rc!= -1 ){ |
| 3370 | rc = 0; |
| 3371 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3372 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3373 | } |
| 3374 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3375 | /* |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3376 | ** Open a file descriptor to the directory containing file zFilename. |
| 3377 | ** If successful, *pFd is set to the opened file descriptor and |
| 3378 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 3379 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 3380 | ** value. |
| 3381 | ** |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3382 | ** The directory file descriptor is used for only one thing - to |
| 3383 | ** fsync() a directory to make sure file creation and deletion events |
| 3384 | ** are flushed to disk. Such fsyncs are not needed on newer |
| 3385 | ** journaling filesystems, but are required on older filesystems. |
| 3386 | ** |
| 3387 | ** This routine can be overridden using the xSetSysCall interface. |
| 3388 | ** The ability to override this routine was added in support of the |
| 3389 | ** chromium sandbox. Opening a directory is a security risk (we are |
| 3390 | ** told) so making it overrideable allows the chromium sandbox to |
| 3391 | ** replace this routine with a harmless no-op. To make this routine |
| 3392 | ** a no-op, replace it with a stub that returns SQLITE_OK but leaves |
| 3393 | ** *pFd set to a negative number. |
| 3394 | ** |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3395 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 3396 | ** the file descriptor *pFd using close(). |
| 3397 | */ |
| 3398 | static int openDirectory(const char *zFilename, int *pFd){ |
| 3399 | int ii; |
| 3400 | int fd = -1; |
| 3401 | char zDirname[MAX_PATHNAME+1]; |
| 3402 | |
| 3403 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
| 3404 | for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--); |
| 3405 | if( ii>0 ){ |
| 3406 | zDirname[ii] = '\0'; |
| 3407 | fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0); |
| 3408 | if( fd>=0 ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3409 | OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); |
| 3410 | } |
| 3411 | } |
| 3412 | *pFd = fd; |
| 3413 | return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname)); |
| 3414 | } |
| 3415 | |
| 3416 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3417 | ** Make sure all writes to a particular file are committed to disk. |
| 3418 | ** |
| 3419 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 3420 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 3421 | ** file data is synced. |
| 3422 | ** |
| 3423 | ** Under Unix, also make sure that the directory entry for the file |
| 3424 | ** has been created by fsync-ing the directory that contains the file. |
| 3425 | ** If we do not do this and we encounter a power failure, the directory |
| 3426 | ** entry for the journal might not exist after we reboot. The next |
| 3427 | ** SQLite to access the file will not know that the journal exists (because |
| 3428 | ** the directory entry for the journal was never created) and the transaction |
| 3429 | ** will not roll back - possibly leading to database corruption. |
| 3430 | */ |
| 3431 | static int unixSync(sqlite3_file *id, int flags){ |
| 3432 | int rc; |
| 3433 | unixFile *pFile = (unixFile*)id; |
| 3434 | |
| 3435 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 3436 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 3437 | |
| 3438 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 3439 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 3440 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 3441 | ); |
| 3442 | |
| 3443 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 3444 | ** line is to test that doing so does not cause any problems. |
| 3445 | */ |
| 3446 | SimulateDiskfullError( return SQLITE_FULL ); |
| 3447 | |
| 3448 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3449 | OSTRACE(("SYNC %-3d\n", pFile->h)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3450 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 3451 | SimulateIOError( rc=1 ); |
| 3452 | if( rc ){ |
| 3453 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3454 | return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3455 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3456 | |
| 3457 | /* Also fsync the directory containing the file if the DIRSYNC flag |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3458 | ** is set. This is a one-time occurrance. Many systems (examples: AIX) |
| 3459 | ** are unable to fsync a directory, so ignore errors on the fsync. |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3460 | */ |
| 3461 | if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){ |
| 3462 | int dirfd; |
| 3463 | OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3464 | HAVE_FULLFSYNC, isFullsync)); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3465 | rc = osOpenDirectory(pFile->zPath, &dirfd); |
| 3466 | if( rc==SQLITE_OK && dirfd>=0 ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3467 | full_fsync(dirfd, 0, 0); |
| 3468 | robust_close(pFile, dirfd, __LINE__); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 3469 | }else if( rc==SQLITE_CANTOPEN ){ |
| 3470 | rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3471 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3472 | pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3473 | } |
| 3474 | return rc; |
| 3475 | } |
| 3476 | |
| 3477 | /* |
| 3478 | ** Truncate an open file to a specified size |
| 3479 | */ |
| 3480 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3481 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3482 | int rc; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3483 | assert( pFile ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3484 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3485 | |
| 3486 | /* If the user has configured a chunk-size for this file, truncate the |
| 3487 | ** file so that it consists of an integer number of chunks (i.e. the |
| 3488 | ** actual file size after the operation may be larger than the requested |
| 3489 | ** size). |
| 3490 | */ |
drh | b8af4b7 | 2012-04-05 20:04:39 +0000 | [diff] [blame] | 3491 | if( pFile->szChunk>0 ){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3492 | nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 3493 | } |
| 3494 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3495 | rc = robust_ftruncate(pFile->h, (off_t)nByte); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3496 | if( rc ){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3497 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3498 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3499 | }else{ |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3500 | #ifdef SQLITE_DEBUG |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3501 | /* If we are doing a normal write to a database file (as opposed to |
| 3502 | ** doing a hot-journal rollback or a write to some file other than a |
| 3503 | ** normal database file) and we truncate the file to zero length, |
| 3504 | ** that effectively updates the change counter. This might happen |
| 3505 | ** when restoring a database using the backup API from a zero-length |
| 3506 | ** source. |
| 3507 | */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3508 | if( pFile->inNormalWrite && nByte==0 ){ |
| 3509 | pFile->transCntrChng = 1; |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3510 | } |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3511 | #endif |
dan | c000331 | 2013-03-22 17:46:11 +0000 | [diff] [blame] | 3512 | |
| 3513 | /* If the file was just truncated to a size smaller than the currently |
| 3514 | ** mapped region, reduce the effective mapping size as well. SQLite will |
| 3515 | ** use read() and write() to access data beyond this point from now on. |
| 3516 | */ |
| 3517 | if( nByte<pFile->mmapSize ){ |
| 3518 | pFile->mmapSize = nByte; |
| 3519 | } |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3520 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3521 | return SQLITE_OK; |
| 3522 | } |
| 3523 | } |
| 3524 | |
| 3525 | /* |
| 3526 | ** Determine the current size of a file in bytes |
| 3527 | */ |
| 3528 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 3529 | int rc; |
| 3530 | struct stat buf; |
| 3531 | assert( id ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3532 | rc = osFstat(((unixFile*)id)->h, &buf); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3533 | SimulateIOError( rc=1 ); |
| 3534 | if( rc!=0 ){ |
| 3535 | ((unixFile*)id)->lastErrno = errno; |
| 3536 | return SQLITE_IOERR_FSTAT; |
| 3537 | } |
| 3538 | *pSize = buf.st_size; |
| 3539 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3540 | /* When opening a zero-size database, the findInodeInfo() procedure |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3541 | ** writes a single byte into that file in order to work around a bug |
| 3542 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 3543 | ** layers, we need to report this file size as zero even though it is |
| 3544 | ** really 1. Ticket #3260. |
| 3545 | */ |
| 3546 | if( *pSize==1 ) *pSize = 0; |
| 3547 | |
| 3548 | |
| 3549 | return SQLITE_OK; |
| 3550 | } |
| 3551 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3552 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3553 | /* |
| 3554 | ** Handler for proxy-locking file-control verbs. Defined below in the |
| 3555 | ** proxying locking division. |
| 3556 | */ |
| 3557 | static int proxyFileControl(sqlite3_file*,int,void*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3558 | #endif |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3559 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3560 | /* |
| 3561 | ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 3562 | ** file-control operation. Enlarge the database to nBytes in size |
| 3563 | ** (rounded up to the next chunk-size). If the database is already |
| 3564 | ** nBytes or larger, this routine is a no-op. |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3565 | */ |
| 3566 | static int fcntlSizeHint(unixFile *pFile, i64 nByte){ |
mistachkin | d589a54 | 2011-08-30 01:23:34 +0000 | [diff] [blame] | 3567 | if( pFile->szChunk>0 ){ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3568 | i64 nSize; /* Required file size */ |
| 3569 | struct stat buf; /* Used to hold return values of fstat() */ |
| 3570 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3571 | if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3572 | |
| 3573 | nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; |
| 3574 | if( nSize>(i64)buf.st_size ){ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3575 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3576 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3577 | /* The code below is handling the return value of osFallocate() |
| 3578 | ** correctly. posix_fallocate() is defined to "returns zero on success, |
| 3579 | ** or an error number on failure". See the manpage for details. */ |
| 3580 | int err; |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3581 | do{ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3582 | err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size); |
| 3583 | }while( err==EINTR ); |
| 3584 | if( err ) return SQLITE_IOERR_WRITE; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3585 | #else |
| 3586 | /* If the OS does not have posix_fallocate(), fake it. First use |
| 3587 | ** ftruncate() to set the file size, then write a single byte to |
| 3588 | ** the last byte in each block within the extended region. This |
| 3589 | ** is the same technique used by glibc to implement posix_fallocate() |
| 3590 | ** on systems that do not have a real fallocate() system call. |
| 3591 | */ |
| 3592 | int nBlk = buf.st_blksize; /* File-system block size */ |
| 3593 | i64 iWrite; /* Next offset to write to */ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3594 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3595 | if( robust_ftruncate(pFile->h, nSize) ){ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3596 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3597 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3598 | } |
| 3599 | iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1; |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3600 | while( iWrite<nSize ){ |
| 3601 | int nWrite = seekAndWrite(pFile, iWrite, "", 1); |
| 3602 | if( nWrite!=1 ) return SQLITE_IOERR_WRITE; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3603 | iWrite += nBlk; |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3604 | } |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3605 | #endif |
| 3606 | } |
| 3607 | } |
| 3608 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3609 | if( pFile->mmapLimit>0 ){ |
| 3610 | int rc; |
| 3611 | if( pFile->szChunk<=0 ){ |
| 3612 | if( robust_ftruncate(pFile->h, nByte) ){ |
| 3613 | pFile->lastErrno = errno; |
| 3614 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
| 3615 | } |
| 3616 | } |
| 3617 | |
| 3618 | rc = unixMapfile(pFile, nByte); |
| 3619 | return rc; |
| 3620 | } |
| 3621 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3622 | return SQLITE_OK; |
| 3623 | } |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3624 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3625 | /* |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3626 | ** If *pArg is inititially negative then this is a query. Set *pArg to |
| 3627 | ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set. |
| 3628 | ** |
| 3629 | ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags. |
| 3630 | */ |
| 3631 | static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){ |
| 3632 | if( *pArg<0 ){ |
| 3633 | *pArg = (pFile->ctrlFlags & mask)!=0; |
| 3634 | }else if( (*pArg)==0 ){ |
| 3635 | pFile->ctrlFlags &= ~mask; |
| 3636 | }else{ |
| 3637 | pFile->ctrlFlags |= mask; |
| 3638 | } |
| 3639 | } |
| 3640 | |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3641 | /* Forward declaration */ |
| 3642 | static int unixGetTempname(int nBuf, char *zBuf); |
| 3643 | |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3644 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3645 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3646 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3647 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3648 | unixFile *pFile = (unixFile*)id; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3649 | switch( op ){ |
| 3650 | case SQLITE_FCNTL_LOCKSTATE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3651 | *(int*)pArg = pFile->eFileLock; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3652 | return SQLITE_OK; |
| 3653 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3654 | case SQLITE_LAST_ERRNO: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3655 | *(int*)pArg = pFile->lastErrno; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3656 | return SQLITE_OK; |
| 3657 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3658 | case SQLITE_FCNTL_CHUNK_SIZE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3659 | pFile->szChunk = *(int *)pArg; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3660 | return SQLITE_OK; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3661 | } |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3662 | case SQLITE_FCNTL_SIZE_HINT: { |
dan | da04ea4 | 2011-08-23 05:10:39 +0000 | [diff] [blame] | 3663 | int rc; |
| 3664 | SimulateIOErrorBenign(1); |
| 3665 | rc = fcntlSizeHint(pFile, *(i64 *)pArg); |
| 3666 | SimulateIOErrorBenign(0); |
| 3667 | return rc; |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3668 | } |
| 3669 | case SQLITE_FCNTL_PERSIST_WAL: { |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3670 | unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg); |
| 3671 | return SQLITE_OK; |
| 3672 | } |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3673 | case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { |
| 3674 | unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg); |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3675 | return SQLITE_OK; |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3676 | } |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 3677 | case SQLITE_FCNTL_VFSNAME: { |
| 3678 | *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName); |
| 3679 | return SQLITE_OK; |
| 3680 | } |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3681 | case SQLITE_FCNTL_TEMPFILENAME: { |
| 3682 | char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname ); |
| 3683 | if( zTFile ){ |
| 3684 | unixGetTempname(pFile->pVfs->mxPathname, zTFile); |
| 3685 | *(char**)pArg = zTFile; |
| 3686 | } |
| 3687 | return SQLITE_OK; |
| 3688 | } |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3689 | case SQLITE_FCNTL_MMAP_SIZE: { |
| 3690 | pFile->mmapLimit = *(i64*)pArg; |
dan | b2d3de3 | 2013-03-14 18:34:37 +0000 | [diff] [blame] | 3691 | return SQLITE_OK; |
| 3692 | } |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3693 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3694 | /* The pager calls this method to signal that it has done |
| 3695 | ** a rollback and that the database is therefore unchanged and |
| 3696 | ** it hence it is OK for the transaction change counter to be |
| 3697 | ** unchanged. |
| 3698 | */ |
| 3699 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 3700 | ((unixFile*)id)->dbUpdate = 0; |
| 3701 | return SQLITE_OK; |
| 3702 | } |
| 3703 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3704 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3705 | case SQLITE_SET_LOCKPROXYFILE: |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3706 | case SQLITE_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3707 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3708 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3709 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3710 | } |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 3711 | return SQLITE_NOTFOUND; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 3712 | } |
| 3713 | |
| 3714 | /* |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3715 | ** Return the sector size in bytes of the underlying block device for |
| 3716 | ** the specified file. This is almost always 512 bytes, but may be |
| 3717 | ** larger for some devices. |
| 3718 | ** |
| 3719 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 3720 | ** if two files are created in the same file-system directory (i.e. |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3721 | ** a database and its journal file) that the sector size will be the |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3722 | ** same for both. |
| 3723 | */ |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3724 | #ifndef __QNXNTO__ |
| 3725 | static int unixSectorSize(sqlite3_file *NotUsed){ |
| 3726 | UNUSED_PARAMETER(NotUsed); |
drh | 8942d41 | 2012-01-02 18:20:14 +0000 | [diff] [blame] | 3727 | return SQLITE_DEFAULT_SECTOR_SIZE; |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3728 | } |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3729 | #endif |
| 3730 | |
| 3731 | /* |
| 3732 | ** The following version of unixSectorSize() is optimized for QNX. |
| 3733 | */ |
| 3734 | #ifdef __QNXNTO__ |
| 3735 | #include <sys/dcmd_blk.h> |
| 3736 | #include <sys/statvfs.h> |
| 3737 | static int unixSectorSize(sqlite3_file *id){ |
| 3738 | unixFile *pFile = (unixFile*)id; |
| 3739 | if( pFile->sectorSize == 0 ){ |
| 3740 | struct statvfs fsInfo; |
| 3741 | |
| 3742 | /* Set defaults for non-supported filesystems */ |
| 3743 | pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 3744 | pFile->deviceCharacteristics = 0; |
| 3745 | if( fstatvfs(pFile->h, &fsInfo) == -1 ) { |
| 3746 | return pFile->sectorSize; |
| 3747 | } |
| 3748 | |
| 3749 | if( !strcmp(fsInfo.f_basetype, "tmp") ) { |
| 3750 | pFile->sectorSize = fsInfo.f_bsize; |
| 3751 | pFile->deviceCharacteristics = |
| 3752 | SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */ |
| 3753 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3754 | ** the write succeeds */ |
| 3755 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3756 | ** so it is ordered */ |
| 3757 | 0; |
| 3758 | }else if( strstr(fsInfo.f_basetype, "etfs") ){ |
| 3759 | pFile->sectorSize = fsInfo.f_bsize; |
| 3760 | pFile->deviceCharacteristics = |
| 3761 | /* etfs cluster size writes are atomic */ |
| 3762 | (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) | |
| 3763 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3764 | ** the write succeeds */ |
| 3765 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3766 | ** so it is ordered */ |
| 3767 | 0; |
| 3768 | }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){ |
| 3769 | pFile->sectorSize = fsInfo.f_bsize; |
| 3770 | pFile->deviceCharacteristics = |
| 3771 | SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */ |
| 3772 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3773 | ** the write succeeds */ |
| 3774 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3775 | ** so it is ordered */ |
| 3776 | 0; |
| 3777 | }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){ |
| 3778 | pFile->sectorSize = fsInfo.f_bsize; |
| 3779 | pFile->deviceCharacteristics = |
| 3780 | /* full bitset of atomics from max sector size and smaller */ |
| 3781 | ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 3782 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3783 | ** so it is ordered */ |
| 3784 | 0; |
| 3785 | }else if( strstr(fsInfo.f_basetype, "dos") ){ |
| 3786 | pFile->sectorSize = fsInfo.f_bsize; |
| 3787 | pFile->deviceCharacteristics = |
| 3788 | /* full bitset of atomics from max sector size and smaller */ |
| 3789 | ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 3790 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3791 | ** so it is ordered */ |
| 3792 | 0; |
| 3793 | }else{ |
| 3794 | pFile->deviceCharacteristics = |
| 3795 | SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */ |
| 3796 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3797 | ** the write succeeds */ |
| 3798 | 0; |
| 3799 | } |
| 3800 | } |
| 3801 | /* Last chance verification. If the sector size isn't a multiple of 512 |
| 3802 | ** then it isn't valid.*/ |
| 3803 | if( pFile->sectorSize % 512 != 0 ){ |
| 3804 | pFile->deviceCharacteristics = 0; |
| 3805 | pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 3806 | } |
| 3807 | return pFile->sectorSize; |
| 3808 | } |
| 3809 | #endif /* __QNXNTO__ */ |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3810 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3811 | /* |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3812 | ** Return the device characteristics for the file. |
| 3813 | ** |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3814 | ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default. |
| 3815 | ** However, that choice is contraversial since technically the underlying |
| 3816 | ** file system does not always provide powersafe overwrites. (In other |
| 3817 | ** words, after a power-loss event, parts of the file that were never |
| 3818 | ** written might end up being altered.) However, non-PSOW behavior is very, |
| 3819 | ** very rare. And asserting PSOW makes a large reduction in the amount |
| 3820 | ** of required I/O for journaling, since a lot of padding is eliminated. |
| 3821 | ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control |
| 3822 | ** 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] | 3823 | */ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3824 | static int unixDeviceCharacteristics(sqlite3_file *id){ |
| 3825 | unixFile *p = (unixFile*)id; |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3826 | int rc = 0; |
| 3827 | #ifdef __QNXNTO__ |
| 3828 | if( p->sectorSize==0 ) unixSectorSize(id); |
| 3829 | rc = p->deviceCharacteristics; |
| 3830 | #endif |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3831 | if( p->ctrlFlags & UNIXFILE_PSOW ){ |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3832 | rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE; |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3833 | } |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3834 | return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3835 | } |
| 3836 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3837 | #ifndef SQLITE_OMIT_WAL |
| 3838 | |
| 3839 | |
| 3840 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3841 | ** Object used to represent an shared memory buffer. |
| 3842 | ** |
| 3843 | ** When multiple threads all reference the same wal-index, each thread |
| 3844 | ** has its own unixShm object, but they all point to a single instance |
| 3845 | ** of this unixShmNode object. In other words, each wal-index is opened |
| 3846 | ** only once per process. |
| 3847 | ** |
| 3848 | ** Each unixShmNode object is connected to a single unixInodeInfo object. |
| 3849 | ** We could coalesce this object into unixInodeInfo, but that would mean |
| 3850 | ** every open file that does not use shared memory (in other words, most |
| 3851 | ** open files) would have to carry around this extra information. So |
| 3852 | ** the unixInodeInfo object contains a pointer to this unixShmNode object |
| 3853 | ** and the unixShmNode object is created only when needed. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3854 | ** |
| 3855 | ** unixMutexHeld() must be true when creating or destroying |
| 3856 | ** this object or while reading or writing the following fields: |
| 3857 | ** |
| 3858 | ** nRef |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3859 | ** |
| 3860 | ** The following fields are read-only after the object is created: |
| 3861 | ** |
| 3862 | ** fid |
| 3863 | ** zFilename |
| 3864 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3865 | ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3866 | ** unixMutexHeld() is true when reading or writing any other field |
| 3867 | ** in this structure. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3868 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3869 | struct unixShmNode { |
| 3870 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3871 | sqlite3_mutex *mutex; /* Mutex to access this object */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3872 | char *zFilename; /* Name of the mmapped file */ |
| 3873 | int h; /* Open file descriptor */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3874 | int szRegion; /* Size of shared-memory regions */ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 3875 | u16 nRegion; /* Size of array apRegion */ |
| 3876 | u8 isReadonly; /* True if read-only */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3877 | char **apRegion; /* Array of mapped shared-memory regions */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3878 | int nRef; /* Number of unixShm objects pointing to this */ |
| 3879 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3880 | #ifdef SQLITE_DEBUG |
| 3881 | u8 exclMask; /* Mask of exclusive locks held */ |
| 3882 | u8 sharedMask; /* Mask of shared locks held */ |
| 3883 | u8 nextShmId; /* Next available unixShm.id value */ |
| 3884 | #endif |
| 3885 | }; |
| 3886 | |
| 3887 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3888 | ** Structure used internally by this VFS to record the state of an |
| 3889 | ** open shared memory connection. |
| 3890 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3891 | ** The following fields are initialized when this object is created and |
| 3892 | ** are read-only thereafter: |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3893 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3894 | ** unixShm.pFile |
| 3895 | ** unixShm.id |
| 3896 | ** |
| 3897 | ** All other fields are read/write. The unixShm.pFile->mutex must be held |
| 3898 | ** while accessing any read/write fields. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3899 | */ |
| 3900 | struct unixShm { |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3901 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 3902 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3903 | u8 hasMutex; /* True if holding the unixShmNode mutex */ |
drh | fd53231 | 2011-08-31 18:35:34 +0000 | [diff] [blame] | 3904 | u8 id; /* Id of this connection within its unixShmNode */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3905 | u16 sharedMask; /* Mask of shared locks held */ |
| 3906 | u16 exclMask; /* Mask of exclusive locks held */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3907 | }; |
| 3908 | |
| 3909 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3910 | ** Constants used for locking |
| 3911 | */ |
drh | bd9676c | 2010-06-23 17:58:38 +0000 | [diff] [blame] | 3912 | #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
drh | 4222441 | 2010-05-31 14:28:25 +0000 | [diff] [blame] | 3913 | #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3914 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3915 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3916 | ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3917 | ** |
| 3918 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 3919 | ** otherwise. |
| 3920 | */ |
| 3921 | static int unixShmSystemLock( |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3922 | unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */ |
| 3923 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3924 | int ofst, /* First byte of the locking range */ |
| 3925 | int n /* Number of bytes to lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3926 | ){ |
| 3927 | struct flock f; /* The posix advisory locking structure */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3928 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3929 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3930 | /* Access to the unixShmNode object is serialized by the caller */ |
| 3931 | assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3932 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3933 | /* Shared locks never span more than one byte */ |
| 3934 | assert( n==1 || lockType!=F_RDLCK ); |
| 3935 | |
| 3936 | /* Locks are within range */ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3937 | assert( n>=1 && n<SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3938 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3939 | if( pShmNode->h>=0 ){ |
| 3940 | /* Initialize the locking parameters */ |
| 3941 | memset(&f, 0, sizeof(f)); |
| 3942 | f.l_type = lockType; |
| 3943 | f.l_whence = SEEK_SET; |
| 3944 | f.l_start = ofst; |
| 3945 | f.l_len = n; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3946 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3947 | rc = osFcntl(pShmNode->h, F_SETLK, &f); |
| 3948 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 3949 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3950 | |
| 3951 | /* Update the global lock state and do debug tracing */ |
| 3952 | #ifdef SQLITE_DEBUG |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3953 | { u16 mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3954 | OSTRACE(("SHM-LOCK ")); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3955 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3956 | if( rc==SQLITE_OK ){ |
| 3957 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3958 | OSTRACE(("unlock %d ok", ofst)); |
| 3959 | pShmNode->exclMask &= ~mask; |
| 3960 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3961 | }else if( lockType==F_RDLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3962 | OSTRACE(("read-lock %d ok", ofst)); |
| 3963 | pShmNode->exclMask &= ~mask; |
| 3964 | pShmNode->sharedMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3965 | }else{ |
| 3966 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3967 | OSTRACE(("write-lock %d ok", ofst)); |
| 3968 | pShmNode->exclMask |= mask; |
| 3969 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3970 | } |
| 3971 | }else{ |
| 3972 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3973 | OSTRACE(("unlock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3974 | }else if( lockType==F_RDLCK ){ |
| 3975 | OSTRACE(("read-lock failed")); |
| 3976 | }else{ |
| 3977 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3978 | OSTRACE(("write-lock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3979 | } |
| 3980 | } |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 3981 | OSTRACE((" - afterwards %03x,%03x\n", |
| 3982 | pShmNode->sharedMask, pShmNode->exclMask)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3983 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3984 | #endif |
| 3985 | |
| 3986 | return rc; |
| 3987 | } |
| 3988 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3989 | |
| 3990 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3991 | ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3992 | ** |
| 3993 | ** This is not a VFS shared-memory method; it is a utility function called |
| 3994 | ** by VFS shared-memory methods. |
| 3995 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3996 | static void unixShmPurge(unixFile *pFd){ |
| 3997 | unixShmNode *p = pFd->pInode->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3998 | assert( unixMutexHeld() ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3999 | if( p && p->nRef==0 ){ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4000 | int i; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4001 | assert( p->pInode==pFd->pInode ); |
drh | df3aa16 | 2011-06-24 11:29:51 +0000 | [diff] [blame] | 4002 | sqlite3_mutex_free(p->mutex); |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4003 | for(i=0; i<p->nRegion; i++){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4004 | if( p->h>=0 ){ |
| 4005 | munmap(p->apRegion[i], p->szRegion); |
| 4006 | }else{ |
| 4007 | sqlite3_free(p->apRegion[i]); |
| 4008 | } |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4009 | } |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4010 | sqlite3_free(p->apRegion); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4011 | if( p->h>=0 ){ |
| 4012 | robust_close(pFd, p->h, __LINE__); |
| 4013 | p->h = -1; |
| 4014 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4015 | p->pInode->pShmNode = 0; |
| 4016 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4017 | } |
| 4018 | } |
| 4019 | |
| 4020 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4021 | ** Open a shared-memory area associated with open database file pDbFd. |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4022 | ** This particular implementation uses mmapped files. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4023 | ** |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4024 | ** The file used to implement shared-memory is in the same directory |
| 4025 | ** as the open database file and has the same name as the open database |
| 4026 | ** file with the "-shm" suffix added. For example, if the database file |
| 4027 | ** is "/home/user1/config.db" then the file that is created and mmapped |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4028 | ** for shared memory will be called "/home/user1/config.db-shm". |
| 4029 | ** |
| 4030 | ** Another approach to is to use files in /dev/shm or /dev/tmp or an |
| 4031 | ** some other tmpfs mount. But if a file in a different directory |
| 4032 | ** from the database file is used, then differing access permissions |
| 4033 | ** or a chroot() might cause two different processes on the same |
| 4034 | ** database to end up using different files for shared memory - |
| 4035 | ** meaning that their memory would not really be shared - resulting |
| 4036 | ** in database corruption. Nevertheless, this tmpfs file usage |
| 4037 | ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" |
| 4038 | ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time |
| 4039 | ** option results in an incompatible build of SQLite; builds of SQLite |
| 4040 | ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the |
| 4041 | ** same database file at the same time, database corruption will likely |
| 4042 | ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered |
| 4043 | ** "unsupported" and may go away in a future SQLite release. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4044 | ** |
| 4045 | ** When opening a new shared-memory file, if no other instances of that |
| 4046 | ** file are currently open, in this process or in other processes, then |
| 4047 | ** the file must be truncated to zero length or have its header cleared. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4048 | ** |
| 4049 | ** If the original database file (pDbFd) is using the "unix-excl" VFS |
| 4050 | ** that means that an exclusive lock is held on the database file and |
| 4051 | ** that no other processes are able to read or write the database. In |
| 4052 | ** that case, we do not really need shared memory. No shared memory |
| 4053 | ** file is created. The shared memory will be simulated with heap memory. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4054 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4055 | static int unixOpenSharedMemory(unixFile *pDbFd){ |
| 4056 | struct unixShm *p = 0; /* The connection to be opened */ |
| 4057 | struct unixShmNode *pShmNode; /* The underlying mmapped file */ |
| 4058 | int rc; /* Result code */ |
| 4059 | unixInodeInfo *pInode; /* The inode of fd */ |
| 4060 | char *zShmFilename; /* Name of the file used for SHM */ |
| 4061 | int nShmFilename; /* Size of the SHM filename in bytes */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4062 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4063 | /* Allocate space for the new unixShm object. */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4064 | p = sqlite3_malloc( sizeof(*p) ); |
| 4065 | if( p==0 ) return SQLITE_NOMEM; |
| 4066 | memset(p, 0, sizeof(*p)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4067 | assert( pDbFd->pShm==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4068 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4069 | /* Check to see if a unixShmNode object already exists. Reuse an existing |
| 4070 | ** one if present. Create a new one if necessary. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4071 | */ |
| 4072 | unixEnterMutex(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4073 | pInode = pDbFd->pInode; |
| 4074 | pShmNode = pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4075 | if( pShmNode==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4076 | struct stat sStat; /* fstat() info for database file */ |
| 4077 | |
| 4078 | /* Call fstat() to figure out the permissions on the database file. If |
| 4079 | ** 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] | 4080 | ** with the same permissions. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4081 | */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4082 | if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4083 | rc = SQLITE_IOERR_FSTAT; |
| 4084 | goto shm_open_err; |
| 4085 | } |
| 4086 | |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4087 | #ifdef SQLITE_SHM_DIRECTORY |
drh | 52bcde0 | 2012-01-03 14:50:45 +0000 | [diff] [blame] | 4088 | nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4089 | #else |
drh | 52bcde0 | 2012-01-03 14:50:45 +0000 | [diff] [blame] | 4090 | nShmFilename = 6 + (int)strlen(pDbFd->zPath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4091 | #endif |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4092 | pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4093 | if( pShmNode==0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4094 | rc = SQLITE_NOMEM; |
| 4095 | goto shm_open_err; |
| 4096 | } |
drh | 9cb5a0d | 2012-01-05 21:19:54 +0000 | [diff] [blame] | 4097 | memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename); |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4098 | zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1]; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4099 | #ifdef SQLITE_SHM_DIRECTORY |
| 4100 | sqlite3_snprintf(nShmFilename, zShmFilename, |
| 4101 | SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", |
| 4102 | (u32)sStat.st_ino, (u32)sStat.st_dev); |
| 4103 | #else |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4104 | sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath); |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 4105 | sqlite3FileSuffix3(pDbFd->zPath, zShmFilename); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4106 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4107 | pShmNode->h = -1; |
| 4108 | pDbFd->pInode->pShmNode = pShmNode; |
| 4109 | pShmNode->pInode = pDbFd->pInode; |
| 4110 | pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 4111 | if( pShmNode->mutex==0 ){ |
| 4112 | rc = SQLITE_NOMEM; |
| 4113 | goto shm_open_err; |
| 4114 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4115 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4116 | if( pInode->bProcessLock==0 ){ |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 4117 | int openFlags = O_RDWR | O_CREAT; |
drh | 9291372 | 2011-12-23 00:07:33 +0000 | [diff] [blame] | 4118 | if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 4119 | openFlags = O_RDONLY; |
| 4120 | pShmNode->isReadonly = 1; |
| 4121 | } |
| 4122 | pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777)); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4123 | if( pShmNode->h<0 ){ |
drh | c96d1e7 | 2012-02-11 18:51:34 +0000 | [diff] [blame] | 4124 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename); |
| 4125 | goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4126 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4127 | |
| 4128 | /* If this process is running as root, make sure that the SHM file |
| 4129 | ** is owned by the same user that owns the original database. Otherwise, |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 4130 | ** the original owner will not be able to connect. |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4131 | */ |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 4132 | osFchown(pShmNode->h, sStat.st_uid, sStat.st_gid); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4133 | |
| 4134 | /* Check to see if another process is holding the dead-man switch. |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4135 | ** If not, truncate the file to zero length. |
| 4136 | */ |
| 4137 | rc = SQLITE_OK; |
| 4138 | if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ |
| 4139 | if( robust_ftruncate(pShmNode->h, 0) ){ |
| 4140 | rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4141 | } |
| 4142 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4143 | if( rc==SQLITE_OK ){ |
| 4144 | rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1); |
| 4145 | } |
| 4146 | if( rc ) goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4147 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4148 | } |
| 4149 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4150 | /* Make the new connection a child of the unixShmNode */ |
| 4151 | p->pShmNode = pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4152 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4153 | p->id = pShmNode->nextShmId++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4154 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4155 | pShmNode->nRef++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4156 | pDbFd->pShm = p; |
| 4157 | unixLeaveMutex(); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 4158 | |
| 4159 | /* The reference count on pShmNode has already been incremented under |
| 4160 | ** the cover of the unixEnterMutex() mutex and the pointer from the |
| 4161 | ** new (struct unixShm) object to the pShmNode has been set. All that is |
| 4162 | ** left to do is to link the new object into the linked list starting |
| 4163 | ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex |
| 4164 | ** mutex. |
| 4165 | */ |
| 4166 | sqlite3_mutex_enter(pShmNode->mutex); |
| 4167 | p->pNext = pShmNode->pFirst; |
| 4168 | pShmNode->pFirst = p; |
| 4169 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4170 | return SQLITE_OK; |
| 4171 | |
| 4172 | /* Jump here on any error */ |
| 4173 | shm_open_err: |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4174 | unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4175 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4176 | unixLeaveMutex(); |
| 4177 | return rc; |
| 4178 | } |
| 4179 | |
| 4180 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4181 | ** This function is called to obtain a pointer to region iRegion of the |
| 4182 | ** shared-memory associated with the database file fd. Shared-memory regions |
| 4183 | ** are numbered starting from zero. Each shared-memory region is szRegion |
| 4184 | ** bytes in size. |
| 4185 | ** |
| 4186 | ** If an error occurs, an error code is returned and *pp is set to NULL. |
| 4187 | ** |
| 4188 | ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory |
| 4189 | ** region has not been allocated (by any client, including one running in a |
| 4190 | ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 4191 | ** bExtend is non-zero and the requested shared-memory region has not yet |
| 4192 | ** been allocated, it is allocated by this function. |
| 4193 | ** |
| 4194 | ** If the shared-memory region has already been allocated or is allocated by |
| 4195 | ** this call as described above, then it is mapped into this processes |
| 4196 | ** address space (if it is not already), *pp is set to point to the mapped |
| 4197 | ** memory and SQLITE_OK returned. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4198 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4199 | static int unixShmMap( |
| 4200 | sqlite3_file *fd, /* Handle open on database file */ |
| 4201 | int iRegion, /* Region to retrieve */ |
| 4202 | int szRegion, /* Size of regions */ |
| 4203 | int bExtend, /* True to extend file if necessary */ |
| 4204 | void volatile **pp /* OUT: Mapped memory */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4205 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4206 | unixFile *pDbFd = (unixFile*)fd; |
| 4207 | unixShm *p; |
| 4208 | unixShmNode *pShmNode; |
| 4209 | int rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4210 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4211 | /* If the shared-memory file has not yet been opened, open it now. */ |
| 4212 | if( pDbFd->pShm==0 ){ |
| 4213 | rc = unixOpenSharedMemory(pDbFd); |
| 4214 | if( rc!=SQLITE_OK ) return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4215 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4216 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4217 | p = pDbFd->pShm; |
| 4218 | pShmNode = p->pShmNode; |
| 4219 | sqlite3_mutex_enter(pShmNode->mutex); |
| 4220 | assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4221 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4222 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4223 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4224 | |
| 4225 | if( pShmNode->nRegion<=iRegion ){ |
| 4226 | char **apNew; /* New apRegion[] array */ |
| 4227 | int nByte = (iRegion+1)*szRegion; /* Minimum required file size */ |
| 4228 | struct stat sStat; /* Used by fstat() */ |
| 4229 | |
| 4230 | pShmNode->szRegion = szRegion; |
| 4231 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4232 | if( pShmNode->h>=0 ){ |
| 4233 | /* The requested region is not mapped into this processes address space. |
| 4234 | ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 4235 | ** large enough to contain the requested region). |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4236 | */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4237 | if( osFstat(pShmNode->h, &sStat) ){ |
| 4238 | rc = SQLITE_IOERR_SHMSIZE; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4239 | goto shmpage_out; |
| 4240 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4241 | |
| 4242 | if( sStat.st_size<nByte ){ |
| 4243 | /* The requested memory region does not exist. If bExtend is set to |
| 4244 | ** false, exit early. *pp will be set to NULL and SQLITE_OK returned. |
| 4245 | ** |
| 4246 | ** Alternatively, if bExtend is true, use ftruncate() to allocate |
| 4247 | ** the requested memory region. |
| 4248 | */ |
| 4249 | if( !bExtend ) goto shmpage_out; |
drh | 0fbb50e | 2012-11-13 10:54:12 +0000 | [diff] [blame] | 4250 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
| 4251 | if( osFallocate(pShmNode->h, sStat.st_size, nByte)!=0 ){ |
| 4252 | rc = unixLogError(SQLITE_IOERR_SHMSIZE, "fallocate", |
| 4253 | pShmNode->zFilename); |
| 4254 | goto shmpage_out; |
| 4255 | } |
| 4256 | #else |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4257 | if( robust_ftruncate(pShmNode->h, nByte) ){ |
| 4258 | rc = unixLogError(SQLITE_IOERR_SHMSIZE, "ftruncate", |
| 4259 | pShmNode->zFilename); |
| 4260 | goto shmpage_out; |
| 4261 | } |
drh | 0fbb50e | 2012-11-13 10:54:12 +0000 | [diff] [blame] | 4262 | #endif |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4263 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4264 | } |
| 4265 | |
| 4266 | /* Map the requested memory region into this processes address space. */ |
| 4267 | apNew = (char **)sqlite3_realloc( |
| 4268 | pShmNode->apRegion, (iRegion+1)*sizeof(char *) |
| 4269 | ); |
| 4270 | if( !apNew ){ |
| 4271 | rc = SQLITE_IOERR_NOMEM; |
| 4272 | goto shmpage_out; |
| 4273 | } |
| 4274 | pShmNode->apRegion = apNew; |
| 4275 | while(pShmNode->nRegion<=iRegion){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4276 | void *pMem; |
| 4277 | if( pShmNode->h>=0 ){ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4278 | pMem = mmap(0, szRegion, |
| 4279 | pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 4280 | MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4281 | ); |
| 4282 | if( pMem==MAP_FAILED ){ |
drh | 50990db | 2011-04-13 20:26:13 +0000 | [diff] [blame] | 4283 | rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4284 | goto shmpage_out; |
| 4285 | } |
| 4286 | }else{ |
| 4287 | pMem = sqlite3_malloc(szRegion); |
| 4288 | if( pMem==0 ){ |
| 4289 | rc = SQLITE_NOMEM; |
| 4290 | goto shmpage_out; |
| 4291 | } |
| 4292 | memset(pMem, 0, szRegion); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4293 | } |
| 4294 | pShmNode->apRegion[pShmNode->nRegion] = pMem; |
| 4295 | pShmNode->nRegion++; |
| 4296 | } |
| 4297 | } |
| 4298 | |
| 4299 | shmpage_out: |
| 4300 | if( pShmNode->nRegion>iRegion ){ |
| 4301 | *pp = pShmNode->apRegion[iRegion]; |
| 4302 | }else{ |
| 4303 | *pp = 0; |
| 4304 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4305 | if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4306 | sqlite3_mutex_leave(pShmNode->mutex); |
| 4307 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
| 4310 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4311 | ** Change the lock state for a shared-memory segment. |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4312 | ** |
| 4313 | ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little |
| 4314 | ** different here than in posix. In xShmLock(), one can go from unlocked |
| 4315 | ** to shared and back or from unlocked to exclusive and back. But one may |
| 4316 | ** not go from shared to exclusive or from exclusive to shared. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4317 | */ |
| 4318 | static int unixShmLock( |
| 4319 | sqlite3_file *fd, /* Database file holding the shared memory */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4320 | int ofst, /* First lock to acquire or release */ |
| 4321 | int n, /* Number of locks to acquire or release */ |
| 4322 | int flags /* What to do with the lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4323 | ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4324 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 4325 | unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 4326 | unixShm *pX; /* For looping over all siblings */ |
| 4327 | unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 4328 | int rc = SQLITE_OK; /* Result code */ |
| 4329 | u16 mask; /* Mask of locks to take or release */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4330 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4331 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4332 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4333 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4334 | assert( n>=1 ); |
| 4335 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 4336 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 4337 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 4338 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 4339 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4340 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4341 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4342 | |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4343 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4344 | assert( n>1 || mask==(1<<ofst) ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4345 | sqlite3_mutex_enter(pShmNode->mutex); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4346 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 4347 | u16 allMask = 0; /* Mask of locks held by siblings */ |
| 4348 | |
| 4349 | /* See if any siblings hold this same lock */ |
| 4350 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 4351 | if( pX==p ) continue; |
| 4352 | assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
| 4353 | allMask |= pX->sharedMask; |
| 4354 | } |
| 4355 | |
| 4356 | /* Unlock the system-level locks */ |
| 4357 | if( (mask & allMask)==0 ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4358 | rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4359 | }else{ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4360 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4361 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4362 | |
| 4363 | /* Undo the local locks */ |
| 4364 | if( rc==SQLITE_OK ){ |
| 4365 | p->exclMask &= ~mask; |
| 4366 | p->sharedMask &= ~mask; |
| 4367 | } |
| 4368 | }else if( flags & SQLITE_SHM_SHARED ){ |
| 4369 | u16 allShared = 0; /* Union of locks held by connections other than "p" */ |
| 4370 | |
| 4371 | /* Find out which shared locks are already held by sibling connections. |
| 4372 | ** If any sibling already holds an exclusive lock, go ahead and return |
| 4373 | ** SQLITE_BUSY. |
| 4374 | */ |
| 4375 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4376 | if( (pX->exclMask & mask)!=0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4377 | rc = SQLITE_BUSY; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4378 | break; |
| 4379 | } |
| 4380 | allShared |= pX->sharedMask; |
| 4381 | } |
| 4382 | |
| 4383 | /* Get shared locks at the system level, if necessary */ |
| 4384 | if( rc==SQLITE_OK ){ |
| 4385 | if( (allShared & mask)==0 ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4386 | rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4387 | }else{ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4388 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4389 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4390 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4391 | |
| 4392 | /* Get the local shared locks */ |
| 4393 | if( rc==SQLITE_OK ){ |
| 4394 | p->sharedMask |= mask; |
| 4395 | } |
| 4396 | }else{ |
| 4397 | /* Make sure no sibling connections hold locks that will block this |
| 4398 | ** lock. If any do, return SQLITE_BUSY right away. |
| 4399 | */ |
| 4400 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4401 | if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ |
| 4402 | rc = SQLITE_BUSY; |
| 4403 | break; |
| 4404 | } |
| 4405 | } |
| 4406 | |
| 4407 | /* Get the exclusive locks at the system level. Then if successful |
| 4408 | ** also mark the local connection as being locked. |
| 4409 | */ |
| 4410 | if( rc==SQLITE_OK ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4411 | rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4412 | if( rc==SQLITE_OK ){ |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4413 | assert( (p->sharedMask & mask)==0 ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4414 | p->exclMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4415 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4416 | } |
| 4417 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4418 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 4419 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
| 4420 | p->id, getpid(), p->sharedMask, p->exclMask)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4421 | return rc; |
| 4422 | } |
| 4423 | |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4424 | /* |
| 4425 | ** Implement a memory barrier or memory fence on shared memory. |
| 4426 | ** |
| 4427 | ** All loads and stores begun before the barrier must complete before |
| 4428 | ** any load or store begun after the barrier. |
| 4429 | */ |
| 4430 | static void unixShmBarrier( |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4431 | sqlite3_file *fd /* Database file holding the shared memory */ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4432 | ){ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 4433 | UNUSED_PARAMETER(fd); |
drh | b29ad85 | 2010-06-01 00:03:57 +0000 | [diff] [blame] | 4434 | unixEnterMutex(); |
| 4435 | unixLeaveMutex(); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4436 | } |
| 4437 | |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4438 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4439 | ** Close a connection to shared-memory. Delete the underlying |
| 4440 | ** storage if deleteFlag is true. |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 4441 | ** |
| 4442 | ** If there is no shared memory associated with the connection then this |
| 4443 | ** routine is a harmless no-op. |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4444 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4445 | static int unixShmUnmap( |
| 4446 | sqlite3_file *fd, /* The underlying database file */ |
| 4447 | int deleteFlag /* Delete shared-memory if true */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4448 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4449 | unixShm *p; /* The connection to be closed */ |
| 4450 | unixShmNode *pShmNode; /* The underlying shared-memory file */ |
| 4451 | unixShm **pp; /* For looping over sibling connections */ |
| 4452 | unixFile *pDbFd; /* The underlying database file */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4453 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4454 | pDbFd = (unixFile*)fd; |
| 4455 | p = pDbFd->pShm; |
| 4456 | if( p==0 ) return SQLITE_OK; |
| 4457 | pShmNode = p->pShmNode; |
| 4458 | |
| 4459 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4460 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4461 | |
| 4462 | /* Remove connection p from the set of connections associated |
| 4463 | ** with pShmNode */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4464 | sqlite3_mutex_enter(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4465 | for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 4466 | *pp = p->pNext; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4467 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4468 | /* Free the connection p */ |
| 4469 | sqlite3_free(p); |
| 4470 | pDbFd->pShm = 0; |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4471 | sqlite3_mutex_leave(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4472 | |
| 4473 | /* If pShmNode->nRef has reached 0, then close the underlying |
| 4474 | ** shared-memory file, too */ |
| 4475 | unixEnterMutex(); |
| 4476 | assert( pShmNode->nRef>0 ); |
| 4477 | pShmNode->nRef--; |
| 4478 | if( pShmNode->nRef==0 ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 4479 | if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4480 | unixShmPurge(pDbFd); |
| 4481 | } |
| 4482 | unixLeaveMutex(); |
| 4483 | |
| 4484 | return SQLITE_OK; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4485 | } |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4486 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4487 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4488 | #else |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 4489 | # define unixShmMap 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4490 | # define unixShmLock 0 |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4491 | # define unixShmBarrier 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4492 | # define unixShmUnmap 0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4493 | #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 4494 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4495 | /* |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame^] | 4496 | ** If it is currently memory mapped, unmap file pFd. |
dan | d306e1a | 2013-03-20 18:25:49 +0000 | [diff] [blame] | 4497 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4498 | static void unixUnmapfile(unixFile *pFd){ |
| 4499 | assert( pFd->nFetchOut==0 ); |
| 4500 | if( pFd->pMapRegion ){ |
| 4501 | munmap(pFd->pMapRegion, pFd->mmapOrigsize); |
| 4502 | pFd->pMapRegion = 0; |
| 4503 | pFd->mmapSize = 0; |
| 4504 | pFd->mmapOrigsize = 0; |
| 4505 | } |
| 4506 | } |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4507 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame^] | 4508 | /* |
| 4509 | ** Memory map or remap the file opened by file-descriptor pFd (if the file |
| 4510 | ** is already mapped, the existing mapping is replaced by the new). Or, if |
| 4511 | ** there already exists a mapping for this file, and there are still |
| 4512 | ** outstanding xFetch() references to it, this function is a no-op. |
| 4513 | ** |
| 4514 | ** If parameter nByte is non-negative, then it is the requested size of |
| 4515 | ** the mapping to create. Otherwise, if nByte is less than zero, then the |
| 4516 | ** requested size is the size of the file on disk. The actual size of the |
| 4517 | ** created mapping is either the requested size or the value configured |
| 4518 | ** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller. |
| 4519 | ** |
| 4520 | ** SQLITE_OK is returned if no error occurs (even if the mapping is not |
| 4521 | ** recreated as a result of outstanding references) or an SQLite error |
| 4522 | ** code otherwise. |
| 4523 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4524 | static int unixMapfile(unixFile *pFd, i64 nByte){ |
| 4525 | i64 nMap = nByte; |
| 4526 | int rc; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 4527 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4528 | assert( nMap>=0 || pFd->nFetchOut==0 ); |
| 4529 | if( pFd->nFetchOut>0 ) return SQLITE_OK; |
| 4530 | |
| 4531 | if( nMap<0 ){ |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 4532 | struct stat statbuf; /* Low-level file information */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4533 | rc = osFstat(pFd->h, &statbuf); |
| 4534 | if( rc!=SQLITE_OK ){ |
| 4535 | return SQLITE_IOERR_FSTAT; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 4536 | } |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4537 | nMap = statbuf.st_size; |
| 4538 | } |
| 4539 | if( nMap>pFd->mmapLimit ){ |
| 4540 | nMap = pFd->mmapLimit; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 4541 | } |
| 4542 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4543 | if( nMap!=pFd->mmapSize ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4544 | unixUnmapfile(pFd); |
dan | d306e1a | 2013-03-20 18:25:49 +0000 | [diff] [blame] | 4545 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4546 | if( nMap>0 ){ |
| 4547 | void *pNew; |
| 4548 | int flags = PROT_READ; |
| 4549 | if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE; |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame^] | 4550 | pNew = mmap(0, nMap, flags, MAP_SHARED, pFd->h, 0); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4551 | if( pNew==MAP_FAILED ){ |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame^] | 4552 | return SQLITE_IOERR_MMAP; |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4553 | } |
dan | d306e1a | 2013-03-20 18:25:49 +0000 | [diff] [blame] | 4554 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4555 | pFd->pMapRegion = pNew; |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame^] | 4556 | pFd->mmapSize = nMap; |
| 4557 | pFd->mmapOrigsize = nMap; |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4558 | } |
| 4559 | } |
| 4560 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4561 | return SQLITE_OK; |
| 4562 | } |
| 4563 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame^] | 4564 | /* |
| 4565 | ** If possible, return a pointer to a mapping of file fd starting at offset |
| 4566 | ** iOff. The mapping must be valid for at least nAmt bytes. |
| 4567 | ** |
| 4568 | ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK. |
| 4569 | ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK. |
| 4570 | ** Finally, if an error does occur, return an SQLite error code. The final |
| 4571 | ** value of *pp is undefined in this case. |
| 4572 | ** |
| 4573 | ** If this function does return a pointer, the caller must eventually |
| 4574 | ** release the reference by calling unixUnfetch(). |
| 4575 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4576 | static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ |
| 4577 | unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
| 4578 | *pp = 0; |
| 4579 | |
| 4580 | if( pFd->mmapLimit>0 ){ |
| 4581 | if( pFd->pMapRegion==0 ){ |
| 4582 | int rc = unixMapfile(pFd, -1); |
| 4583 | if( rc!=SQLITE_OK ) return rc; |
| 4584 | } |
| 4585 | if( pFd->mmapSize >= iOff+nAmt ){ |
| 4586 | *pp = &((u8 *)pFd->pMapRegion)[iOff]; |
| 4587 | pFd->nFetchOut++; |
| 4588 | } |
| 4589 | } |
| 4590 | return SQLITE_OK; |
| 4591 | } |
| 4592 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame^] | 4593 | /* |
| 4594 | ** If the second argument is non-NULL, then this function releases a |
| 4595 | ** reference obtained by an earlier call to unixFetch(). Or, if the second |
| 4596 | ** argument is NULL, then this function is being called to inform the VFS |
| 4597 | ** layer that, according to POSIX, any existing mapping may now be invalid |
| 4598 | ** and should be unmapped. |
| 4599 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4600 | static int unixUnfetch(sqlite3_file *fd, void *p){ |
| 4601 | unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
| 4602 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame^] | 4603 | /* If p==0 (unmap the entire file) then there must be no outstanding |
| 4604 | ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference), |
| 4605 | ** then there must be at least one outstanding. */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4606 | assert( (p==0)==(pFd->nFetchOut==0) ); |
| 4607 | |
| 4608 | if( p ){ |
| 4609 | pFd->nFetchOut--; |
| 4610 | }else{ |
| 4611 | unixUnmapfile(pFd); |
| 4612 | } |
| 4613 | |
| 4614 | assert( pFd->nFetchOut>=0 ); |
| 4615 | return SQLITE_OK; |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4616 | } |
| 4617 | |
| 4618 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4619 | ** Here ends the implementation of all sqlite3_file methods. |
| 4620 | ** |
| 4621 | ********************** End sqlite3_file Methods ******************************* |
| 4622 | ******************************************************************************/ |
| 4623 | |
| 4624 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4625 | ** This division contains definitions of sqlite3_io_methods objects that |
| 4626 | ** implement various file locking strategies. It also contains definitions |
| 4627 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 4628 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 4629 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 4630 | ** the correct finder-function for that VFS. |
| 4631 | ** |
| 4632 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 4633 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 4634 | ** looks at the filesystem type and tries to guess the best locking |
| 4635 | ** strategy from that. |
| 4636 | ** |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4637 | ** For finder-funtion F, two objects are created: |
| 4638 | ** |
| 4639 | ** (1) The real finder-function named "FImpt()". |
| 4640 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4641 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4642 | ** |
| 4643 | ** |
| 4644 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 4645 | ** objects. We have to do this instead of letting pAppData point |
| 4646 | ** directly at the finder-function since C90 rules prevent a void* |
| 4647 | ** from be cast into a function pointer. |
| 4648 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4649 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4650 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4651 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4652 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 4653 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 4654 | ** |
| 4655 | ** * An I/O method finder function called FINDER that returns a pointer |
| 4656 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4657 | */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4658 | #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4659 | static const sqlite3_io_methods METHOD = { \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4660 | VERSION, /* iVersion */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4661 | CLOSE, /* xClose */ \ |
| 4662 | unixRead, /* xRead */ \ |
| 4663 | unixWrite, /* xWrite */ \ |
| 4664 | unixTruncate, /* xTruncate */ \ |
| 4665 | unixSync, /* xSync */ \ |
| 4666 | unixFileSize, /* xFileSize */ \ |
| 4667 | LOCK, /* xLock */ \ |
| 4668 | UNLOCK, /* xUnlock */ \ |
| 4669 | CKLOCK, /* xCheckReservedLock */ \ |
| 4670 | unixFileControl, /* xFileControl */ \ |
| 4671 | unixSectorSize, /* xSectorSize */ \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4672 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 4673 | unixShmMap, /* xShmMap */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4674 | unixShmLock, /* xShmLock */ \ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4675 | unixShmBarrier, /* xShmBarrier */ \ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4676 | unixShmUnmap, /* xShmUnmap */ \ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4677 | unixFetch, /* xFetch */ \ |
| 4678 | unixUnfetch, /* xUnfetch */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4679 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4680 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 4681 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4682 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4683 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4684 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4685 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4686 | |
| 4687 | /* |
| 4688 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 4689 | ** locking strategies. Functions that return pointers to these methods |
| 4690 | ** are also created. |
| 4691 | */ |
| 4692 | IOMETHODS( |
| 4693 | posixIoFinder, /* Finder function name */ |
| 4694 | posixIoMethods, /* sqlite3_io_methods object name */ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4695 | 3, /* shared memory and mmap are enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4696 | unixClose, /* xClose method */ |
| 4697 | unixLock, /* xLock method */ |
| 4698 | unixUnlock, /* xUnlock method */ |
| 4699 | unixCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4700 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4701 | IOMETHODS( |
| 4702 | nolockIoFinder, /* Finder function name */ |
| 4703 | nolockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4704 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4705 | nolockClose, /* xClose method */ |
| 4706 | nolockLock, /* xLock method */ |
| 4707 | nolockUnlock, /* xUnlock method */ |
| 4708 | nolockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4709 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4710 | IOMETHODS( |
| 4711 | dotlockIoFinder, /* Finder function name */ |
| 4712 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4713 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4714 | dotlockClose, /* xClose method */ |
| 4715 | dotlockLock, /* xLock method */ |
| 4716 | dotlockUnlock, /* xUnlock method */ |
| 4717 | dotlockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4718 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4719 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4720 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4721 | IOMETHODS( |
| 4722 | flockIoFinder, /* Finder function name */ |
| 4723 | flockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4724 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4725 | flockClose, /* xClose method */ |
| 4726 | flockLock, /* xLock method */ |
| 4727 | flockUnlock, /* xUnlock method */ |
| 4728 | flockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4729 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4730 | #endif |
| 4731 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4732 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4733 | IOMETHODS( |
| 4734 | semIoFinder, /* Finder function name */ |
| 4735 | semIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4736 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4737 | semClose, /* xClose method */ |
| 4738 | semLock, /* xLock method */ |
| 4739 | semUnlock, /* xUnlock method */ |
| 4740 | semCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4741 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4742 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4743 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4744 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4745 | IOMETHODS( |
| 4746 | afpIoFinder, /* Finder function name */ |
| 4747 | afpIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4748 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4749 | afpClose, /* xClose method */ |
| 4750 | afpLock, /* xLock method */ |
| 4751 | afpUnlock, /* xUnlock method */ |
| 4752 | afpCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4753 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4754 | #endif |
| 4755 | |
| 4756 | /* |
| 4757 | ** The proxy locking method is a "super-method" in the sense that it |
| 4758 | ** opens secondary file descriptors for the conch and lock files and |
| 4759 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 4760 | ** secondary files. For this reason, the division that implements |
| 4761 | ** proxy locking is located much further down in the file. But we need |
| 4762 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 4763 | ** for proxy locking here. So we forward declare the I/O methods. |
| 4764 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4765 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4766 | static int proxyClose(sqlite3_file*); |
| 4767 | static int proxyLock(sqlite3_file*, int); |
| 4768 | static int proxyUnlock(sqlite3_file*, int); |
| 4769 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4770 | IOMETHODS( |
| 4771 | proxyIoFinder, /* Finder function name */ |
| 4772 | proxyIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4773 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4774 | proxyClose, /* xClose method */ |
| 4775 | proxyLock, /* xLock method */ |
| 4776 | proxyUnlock, /* xUnlock method */ |
| 4777 | proxyCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4778 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4779 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4780 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4781 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 4782 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 4783 | IOMETHODS( |
| 4784 | nfsIoFinder, /* Finder function name */ |
| 4785 | nfsIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4786 | 1, /* shared memory is disabled */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4787 | unixClose, /* xClose method */ |
| 4788 | unixLock, /* xLock method */ |
| 4789 | nfsUnlock, /* xUnlock method */ |
| 4790 | unixCheckReservedLock /* xCheckReservedLock method */ |
| 4791 | ) |
| 4792 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4793 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4794 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4795 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4796 | ** This "finder" function attempts to determine the best locking strategy |
| 4797 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4798 | ** object that implements that strategy. |
| 4799 | ** |
| 4800 | ** This is for MacOSX only. |
| 4801 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4802 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4803 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4804 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4805 | ){ |
| 4806 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4807 | const char *zFilesystem; /* Filesystem type name */ |
| 4808 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4809 | } aMap[] = { |
| 4810 | { "hfs", &posixIoMethods }, |
| 4811 | { "ufs", &posixIoMethods }, |
| 4812 | { "afpfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4813 | { "smbfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4814 | { "webdav", &nolockIoMethods }, |
| 4815 | { 0, 0 } |
| 4816 | }; |
| 4817 | int i; |
| 4818 | struct statfs fsInfo; |
| 4819 | struct flock lockInfo; |
| 4820 | |
| 4821 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4822 | /* If filePath==NULL that means we are dealing with a transient file |
| 4823 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4824 | return &nolockIoMethods; |
| 4825 | } |
| 4826 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 4827 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 4828 | return &nolockIoMethods; |
| 4829 | } |
| 4830 | for(i=0; aMap[i].zFilesystem; i++){ |
| 4831 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 4832 | return aMap[i].pMethods; |
| 4833 | } |
| 4834 | } |
| 4835 | } |
| 4836 | |
| 4837 | /* Default case. Handles, amongst others, "nfs". |
| 4838 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 4839 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4840 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4841 | lockInfo.l_len = 1; |
| 4842 | lockInfo.l_start = 0; |
| 4843 | lockInfo.l_whence = SEEK_SET; |
| 4844 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4845 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4846 | if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 4847 | return &nfsIoMethods; |
| 4848 | } else { |
| 4849 | return &posixIoMethods; |
| 4850 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4851 | }else{ |
| 4852 | return &dotlockIoMethods; |
| 4853 | } |
| 4854 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4855 | static const sqlite3_io_methods |
| 4856 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4857 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4858 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4859 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4860 | #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE |
| 4861 | /* |
| 4862 | ** This "finder" function attempts to determine the best locking strategy |
| 4863 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
| 4864 | ** object that implements that strategy. |
| 4865 | ** |
| 4866 | ** This is for VXWorks only. |
| 4867 | */ |
| 4868 | static const sqlite3_io_methods *autolockIoFinderImpl( |
| 4869 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4870 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4871 | ){ |
| 4872 | struct flock lockInfo; |
| 4873 | |
| 4874 | if( !filePath ){ |
| 4875 | /* If filePath==NULL that means we are dealing with a transient file |
| 4876 | ** that does not need to be locked. */ |
| 4877 | return &nolockIoMethods; |
| 4878 | } |
| 4879 | |
| 4880 | /* Test if fcntl() is supported and use POSIX style locks. |
| 4881 | ** Otherwise fall back to the named semaphore method. |
| 4882 | */ |
| 4883 | lockInfo.l_len = 1; |
| 4884 | lockInfo.l_start = 0; |
| 4885 | lockInfo.l_whence = SEEK_SET; |
| 4886 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4887 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4888 | return &posixIoMethods; |
| 4889 | }else{ |
| 4890 | return &semIoMethods; |
| 4891 | } |
| 4892 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4893 | static const sqlite3_io_methods |
| 4894 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4895 | |
| 4896 | #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */ |
| 4897 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4898 | /* |
| 4899 | ** An abstract type for a pointer to a IO method finder function: |
| 4900 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4901 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4902 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4903 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4904 | /**************************************************************************** |
| 4905 | **************************** sqlite3_vfs methods **************************** |
| 4906 | ** |
| 4907 | ** This division contains the implementation of methods on the |
| 4908 | ** sqlite3_vfs object. |
| 4909 | */ |
| 4910 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4911 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4912 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4913 | */ |
| 4914 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4915 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4916 | int h, /* Open file descriptor of file being opened */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4917 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4918 | const char *zFilename, /* Name of the file being opened */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 4919 | int ctrlFlags /* Zero or more UNIXFILE_* values */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4920 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4921 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4922 | unixFile *pNew = (unixFile *)pId; |
| 4923 | int rc = SQLITE_OK; |
| 4924 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4925 | assert( pNew->pInode==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4926 | |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4927 | /* Usually the path zFilename should not be a relative pathname. The |
| 4928 | ** exception is when opening the proxy "conch" file in builds that |
| 4929 | ** include the special Apple locking styles. |
| 4930 | */ |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4931 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | f7f55ed | 2010-10-05 18:22:47 +0000 | [diff] [blame] | 4932 | assert( zFilename==0 || zFilename[0]=='/' |
| 4933 | || pVfs->pAppData==(void*)&autolockIoFinder ); |
| 4934 | #else |
| 4935 | assert( zFilename==0 || zFilename[0]=='/' ); |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4936 | #endif |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4937 | |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 4938 | /* No locking occurs in temporary files */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 4939 | assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 4940 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4941 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4942 | pNew->h = h; |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 4943 | pNew->pVfs = pVfs; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4944 | pNew->zPath = zFilename; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 4945 | pNew->ctrlFlags = (u8)ctrlFlags; |
| 4946 | if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0), |
| 4947 | "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 4948 | pNew->ctrlFlags |= UNIXFILE_PSOW; |
drh | bec7c97 | 2011-12-23 00:25:02 +0000 | [diff] [blame] | 4949 | } |
drh | 503a686 | 2013-03-01 01:07:17 +0000 | [diff] [blame] | 4950 | if( strcmp(pVfs->zName,"unix-excl")==0 ){ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 4951 | pNew->ctrlFlags |= UNIXFILE_EXCL; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 4952 | } |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 4953 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4954 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 4955 | pNew->pId = vxworksFindFileId(zFilename); |
| 4956 | if( pNew->pId==0 ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 4957 | ctrlFlags |= UNIXFILE_NOLOCK; |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 4958 | rc = SQLITE_NOMEM; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4959 | } |
| 4960 | #endif |
| 4961 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 4962 | if( ctrlFlags & UNIXFILE_NOLOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4963 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4964 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4965 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4966 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4967 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 4968 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 4969 | ** zFilename remains valid until file is closed, to support */ |
| 4970 | pNew->lockingContext = (void*)zFilename; |
| 4971 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4972 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4973 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4974 | if( pLockingStyle == &posixIoMethods |
| 4975 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 4976 | || pLockingStyle == &nfsIoMethods |
| 4977 | #endif |
| 4978 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4979 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4980 | rc = findInodeInfo(pNew, &pNew->pInode); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4981 | if( rc!=SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4982 | /* If an error occured in findInodeInfo(), close the file descriptor |
| 4983 | ** immediately, before releasing the mutex. findInodeInfo() may fail |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4984 | ** in two scenarios: |
| 4985 | ** |
| 4986 | ** (a) A call to fstat() failed. |
| 4987 | ** (b) A malloc failed. |
| 4988 | ** |
| 4989 | ** Scenario (b) may only occur if the process is holding no other |
| 4990 | ** file descriptors open on the same file. If there were other file |
| 4991 | ** descriptors on this file, then no malloc would be required by |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4992 | ** findInodeInfo(). If this is the case, it is quite safe to close |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4993 | ** handle h - as it is guaranteed that no posix locks will be released |
| 4994 | ** by doing so. |
| 4995 | ** |
| 4996 | ** If scenario (a) caused the error then things are not so safe. The |
| 4997 | ** implicit assumption here is that if fstat() fails, things are in |
| 4998 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 4999 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5000 | robust_close(pNew, h, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5001 | h = -1; |
| 5002 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5003 | unixLeaveMutex(); |
| 5004 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5005 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5006 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 5007 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5008 | /* AFP locking uses the file path so it needs to be included in |
| 5009 | ** the afpLockingContext. |
| 5010 | */ |
| 5011 | afpLockingContext *pCtx; |
| 5012 | pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 5013 | if( pCtx==0 ){ |
| 5014 | rc = SQLITE_NOMEM; |
| 5015 | }else{ |
| 5016 | /* NB: zFilename exists and remains valid until the file is closed |
| 5017 | ** according to requirement F11141. So we do not need to make a |
| 5018 | ** copy of the filename. */ |
| 5019 | pCtx->dbPath = zFilename; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5020 | pCtx->reserved = 0; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5021 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5022 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5023 | rc = findInodeInfo(pNew, &pNew->pInode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5024 | if( rc!=SQLITE_OK ){ |
| 5025 | sqlite3_free(pNew->lockingContext); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5026 | robust_close(pNew, h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5027 | h = -1; |
| 5028 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5029 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5030 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5031 | } |
| 5032 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5033 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5034 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 5035 | /* Dotfile locking uses the file path so it needs to be included in |
| 5036 | ** the dotlockLockingContext |
| 5037 | */ |
| 5038 | char *zLockFile; |
| 5039 | int nFilename; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5040 | assert( zFilename!=0 ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5041 | nFilename = (int)strlen(zFilename) + 6; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5042 | zLockFile = (char *)sqlite3_malloc(nFilename); |
| 5043 | if( zLockFile==0 ){ |
| 5044 | rc = SQLITE_NOMEM; |
| 5045 | }else{ |
| 5046 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5047 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5048 | pNew->lockingContext = zLockFile; |
| 5049 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5050 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5051 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5052 | else if( pLockingStyle == &semIoMethods ){ |
| 5053 | /* Named semaphore locking uses the file path so it needs to be |
| 5054 | ** included in the semLockingContext |
| 5055 | */ |
| 5056 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5057 | rc = findInodeInfo(pNew, &pNew->pInode); |
| 5058 | if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 5059 | char *zSemName = pNew->pInode->aSemName; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5060 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 5061 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5062 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 5063 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5064 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5065 | pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 5066 | if( pNew->pInode->pSem == SEM_FAILED ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5067 | rc = SQLITE_NOMEM; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5068 | pNew->pInode->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5069 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5070 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5071 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5072 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5073 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 5074 | |
| 5075 | pNew->lastErrno = 0; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5076 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5077 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5078 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
drh | 309e655 | 2010-02-05 18:00:26 +0000 | [diff] [blame] | 5079 | h = -1; |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5080 | osUnlink(zFilename); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5081 | isDelete = 0; |
| 5082 | } |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5083 | if( isDelete ) pNew->ctrlFlags |= UNIXFILE_DELETE; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5084 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5085 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5086 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5087 | }else{ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5088 | pNew->pMethod = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5089 | OpenCounter(+1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5090 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5091 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 5092 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 5093 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5094 | /* |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5095 | ** Return the name of a directory in which to put temporary files. |
| 5096 | ** If no suitable temporary file directory can be found, return NULL. |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5097 | */ |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 5098 | static const char *unixTempFileDir(void){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5099 | static const char *azDirs[] = { |
| 5100 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5101 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5102 | "/var/tmp", |
| 5103 | "/usr/tmp", |
| 5104 | "/tmp", |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5105 | 0 /* List terminator */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5106 | }; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5107 | unsigned int i; |
| 5108 | struct stat buf; |
| 5109 | const char *zDir = 0; |
| 5110 | |
| 5111 | azDirs[0] = sqlite3_temp_directory; |
| 5112 | if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); |
drh | 19515c8 | 2010-06-19 23:53:11 +0000 | [diff] [blame] | 5113 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){ |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5114 | if( zDir==0 ) continue; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5115 | if( osStat(zDir, &buf) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5116 | if( !S_ISDIR(buf.st_mode) ) continue; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5117 | if( osAccess(zDir, 07) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5118 | break; |
| 5119 | } |
| 5120 | return zDir; |
| 5121 | } |
| 5122 | |
| 5123 | /* |
| 5124 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 5125 | ** by the calling process and must be big enough to hold at least |
| 5126 | ** pVfs->mxPathname bytes. |
| 5127 | */ |
| 5128 | static int unixGetTempname(int nBuf, char *zBuf){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5129 | static const unsigned char zChars[] = |
| 5130 | "abcdefghijklmnopqrstuvwxyz" |
| 5131 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 5132 | "0123456789"; |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 5133 | unsigned int i, j; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5134 | const char *zDir; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5135 | |
| 5136 | /* It's odd to simulate an io-error here, but really this is just |
| 5137 | ** using the io-error infrastructure to test that SQLite handles this |
| 5138 | ** function failing. |
| 5139 | */ |
| 5140 | SimulateIOError( return SQLITE_IOERR ); |
| 5141 | |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 5142 | zDir = unixTempFileDir(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5143 | if( zDir==0 ) zDir = "."; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5144 | |
| 5145 | /* Check that the output buffer is large enough for the temporary file |
| 5146 | ** name. If it is not, return SQLITE_ERROR. |
| 5147 | */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5148 | if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5149 | return SQLITE_ERROR; |
| 5150 | } |
| 5151 | |
| 5152 | do{ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5153 | sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5154 | j = (int)strlen(zBuf); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5155 | sqlite3_randomness(15, &zBuf[j]); |
| 5156 | for(i=0; i<15; i++, j++){ |
| 5157 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 5158 | } |
| 5159 | zBuf[j] = 0; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5160 | zBuf[j+1] = 0; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5161 | }while( osAccess(zBuf,0)==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5162 | return SQLITE_OK; |
| 5163 | } |
| 5164 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5165 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 5166 | /* |
| 5167 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 5168 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 5169 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 5170 | */ |
| 5171 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 5172 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 5173 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5174 | /* |
| 5175 | ** Search for an unused file descriptor that was opened on the database |
| 5176 | ** file (not a journal or master-journal file) identified by pathname |
| 5177 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 5178 | ** argument to this function. |
| 5179 | ** |
| 5180 | ** Such a file descriptor may exist if a database connection was closed |
| 5181 | ** but the associated file descriptor could not be closed because some |
| 5182 | ** other file descriptor open on the same file is holding a file-lock. |
| 5183 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 5184 | ** describing "Posix Advisory Locking" at the start of this file for |
| 5185 | ** further details. Also, ticket #4018. |
| 5186 | ** |
| 5187 | ** If a suitable file descriptor is found, then it is returned. If no |
| 5188 | ** such file descriptor is located, -1 is returned. |
| 5189 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5190 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 5191 | UnixUnusedFd *pUnused = 0; |
| 5192 | |
| 5193 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 5194 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 5195 | ** but because no way to test it is currently available. It is better |
| 5196 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 5197 | ** feature. */ |
| 5198 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5199 | struct stat sStat; /* Results of stat() call */ |
| 5200 | |
| 5201 | /* A stat() call may fail for various reasons. If this happens, it is |
| 5202 | ** almost certain that an open() call on the same path will also fail. |
| 5203 | ** For this reason, if an error occurs in the stat() call here, it is |
| 5204 | ** ignored and -1 is returned. The caller will try to open a new file |
| 5205 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 5206 | ** |
| 5207 | ** Even if a subsequent open() call does succeed, the consequences of |
| 5208 | ** not searching for a resusable file descriptor are not dire. */ |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 5209 | if( 0==osStat(zPath, &sStat) ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 5210 | unixInodeInfo *pInode; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5211 | |
| 5212 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5213 | pInode = inodeList; |
| 5214 | while( pInode && (pInode->fileId.dev!=sStat.st_dev |
| 5215 | || pInode->fileId.ino!=sStat.st_ino) ){ |
| 5216 | pInode = pInode->pNext; |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 5217 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5218 | if( pInode ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5219 | UnixUnusedFd **pp; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5220 | for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5221 | pUnused = *pp; |
| 5222 | if( pUnused ){ |
| 5223 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5224 | } |
| 5225 | } |
| 5226 | unixLeaveMutex(); |
| 5227 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5228 | #endif /* if !OS_VXWORKS */ |
| 5229 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5230 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5231 | |
| 5232 | /* |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5233 | ** This function is called by unixOpen() to determine the unix permissions |
drh | f65bc91 | 2010-07-14 20:51:34 +0000 | [diff] [blame] | 5234 | ** 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] | 5235 | ** and a value suitable for passing as the third argument to open(2) is |
| 5236 | ** written to *pMode. If an IO error occurs, an SQLite error code is |
| 5237 | ** returned and the value of *pMode is not modified. |
| 5238 | ** |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 5239 | ** In most cases cases, this routine sets *pMode to 0, which will become |
| 5240 | ** an indication to robust_open() to create the file using |
| 5241 | ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask. |
| 5242 | ** 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] | 5243 | ** this function queries the file-system for the permissions on the |
| 5244 | ** corresponding database file and sets *pMode to this value. Whenever |
| 5245 | ** possible, WAL and journal files are created using the same permissions |
| 5246 | ** as the associated database file. |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 5247 | ** |
| 5248 | ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the |
| 5249 | ** original filename is unavailable. But 8_3_NAMES is only used for |
| 5250 | ** FAT filesystems and permissions do not matter there, so just use |
| 5251 | ** the default permissions. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5252 | */ |
| 5253 | static int findCreateFileMode( |
| 5254 | const char *zPath, /* Path of file (possibly) being created */ |
| 5255 | int flags, /* Flags passed as 4th argument to xOpen() */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5256 | mode_t *pMode, /* OUT: Permissions to open file with */ |
| 5257 | uid_t *pUid, /* OUT: uid to set on the file */ |
| 5258 | gid_t *pGid /* OUT: gid to set on the file */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5259 | ){ |
| 5260 | int rc = SQLITE_OK; /* Return Code */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 5261 | *pMode = 0; |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5262 | *pUid = 0; |
| 5263 | *pGid = 0; |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5264 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5265 | char zDb[MAX_PATHNAME+1]; /* Database file path */ |
| 5266 | int nDb; /* Number of valid bytes in zDb */ |
| 5267 | struct stat sStat; /* Output of stat() on database file */ |
| 5268 | |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5269 | /* zPath is a path to a WAL or journal file. The following block derives |
| 5270 | ** the path to the associated database file from zPath. This block handles |
| 5271 | ** the following naming conventions: |
| 5272 | ** |
| 5273 | ** "<path to db>-journal" |
| 5274 | ** "<path to db>-wal" |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 5275 | ** "<path to db>-journalNN" |
| 5276 | ** "<path to db>-walNN" |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5277 | ** |
drh | d337c5b | 2011-10-20 18:23:35 +0000 | [diff] [blame] | 5278 | ** where NN is a decimal number. The NN naming schemes are |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5279 | ** used by the test_multiplex.c module. |
| 5280 | */ |
| 5281 | nDb = sqlite3Strlen30(zPath) - 1; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5282 | #ifdef SQLITE_ENABLE_8_3_NAMES |
dan | 28a67fd | 2011-12-12 19:48:43 +0000 | [diff] [blame] | 5283 | while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--; |
drh | d337c5b | 2011-10-20 18:23:35 +0000 | [diff] [blame] | 5284 | if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5285 | #else |
| 5286 | while( zPath[nDb]!='-' ){ |
| 5287 | assert( nDb>0 ); |
| 5288 | assert( zPath[nDb]!='\n' ); |
| 5289 | nDb--; |
| 5290 | } |
| 5291 | #endif |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5292 | memcpy(zDb, zPath, nDb); |
| 5293 | zDb[nDb] = '\0'; |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5294 | |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 5295 | if( 0==osStat(zDb, &sStat) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5296 | *pMode = sStat.st_mode & 0777; |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5297 | *pUid = sStat.st_uid; |
| 5298 | *pGid = sStat.st_gid; |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5299 | }else{ |
| 5300 | rc = SQLITE_IOERR_FSTAT; |
| 5301 | } |
| 5302 | }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 5303 | *pMode = 0600; |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5304 | } |
| 5305 | return rc; |
| 5306 | } |
| 5307 | |
| 5308 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5309 | ** Open the file zPath. |
| 5310 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5311 | ** Previously, the SQLite OS layer used three functions in place of this |
| 5312 | ** one: |
| 5313 | ** |
| 5314 | ** sqlite3OsOpenReadWrite(); |
| 5315 | ** sqlite3OsOpenReadOnly(); |
| 5316 | ** sqlite3OsOpenExclusive(); |
| 5317 | ** |
| 5318 | ** These calls correspond to the following combinations of flags: |
| 5319 | ** |
| 5320 | ** ReadWrite() -> (READWRITE | CREATE) |
| 5321 | ** ReadOnly() -> (READONLY) |
| 5322 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 5323 | ** |
| 5324 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 5325 | ** true, the file was configured to be automatically deleted when the |
| 5326 | ** file handle closed. To achieve the same effect using this new |
| 5327 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 5328 | ** OpenExclusive(). |
| 5329 | */ |
| 5330 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5331 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 5332 | const char *zPath, /* Pathname of file to be opened */ |
| 5333 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 5334 | int flags, /* Input flags to control the opening */ |
| 5335 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5336 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5337 | unixFile *p = (unixFile *)pFile; |
| 5338 | int fd = -1; /* File descriptor returned by open() */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5339 | int openFlags = 0; /* Flags to pass to open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5340 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5341 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5342 | int rc = SQLITE_OK; /* Function Return Code */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5343 | int ctrlFlags = 0; /* UNIXFILE_* flags */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5344 | |
| 5345 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 5346 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 5347 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 5348 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 5349 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5350 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5351 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 5352 | #endif |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 5353 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 5354 | struct statfs fsInfo; |
| 5355 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5356 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5357 | /* If creating a master or main-file journal, this function will open |
| 5358 | ** a file-descriptor on the directory too. The first time unixSync() |
| 5359 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 5360 | */ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5361 | int syncDir = (isCreate && ( |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5362 | eType==SQLITE_OPEN_MASTER_JOURNAL |
| 5363 | || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 5364 | || eType==SQLITE_OPEN_WAL |
| 5365 | )); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5366 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5367 | /* If argument zPath is a NULL pointer, this function is required to open |
| 5368 | ** a temporary file. Use this buffer to store the file name in. |
| 5369 | */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5370 | char zTmpname[MAX_PATHNAME+2]; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5371 | const char *zName = zPath; |
| 5372 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5373 | /* Check the following statements are true: |
| 5374 | ** |
| 5375 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 5376 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 5377 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5378 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5379 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5380 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5381 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5382 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5383 | assert(isDelete==0 || isCreate); |
| 5384 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5385 | /* The main DB, main journal, WAL file and master journal are never |
| 5386 | ** automatically deleted. Nor are they ever temporary files. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5387 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 5388 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 5389 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5390 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5391 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5392 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 5393 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 5394 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 5395 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5396 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5397 | ); |
| 5398 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5399 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5400 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5401 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5402 | UnixUnusedFd *pUnused; |
| 5403 | pUnused = findReusableFd(zName, flags); |
| 5404 | if( pUnused ){ |
| 5405 | fd = pUnused->fd; |
| 5406 | }else{ |
dan | 6aa657f | 2009-08-24 18:57:58 +0000 | [diff] [blame] | 5407 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5408 | if( !pUnused ){ |
| 5409 | return SQLITE_NOMEM; |
| 5410 | } |
| 5411 | } |
| 5412 | p->pUnused = pUnused; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5413 | |
| 5414 | /* Database filenames are double-zero terminated if they are not |
| 5415 | ** URIs with parameters. Hence, they can always be passed into |
| 5416 | ** sqlite3_uri_parameter(). */ |
| 5417 | assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 ); |
| 5418 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5419 | }else if( !zName ){ |
| 5420 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5421 | assert(isDelete && !syncDir); |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5422 | rc = unixGetTempname(MAX_PATHNAME+2, zTmpname); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5423 | if( rc!=SQLITE_OK ){ |
| 5424 | return rc; |
| 5425 | } |
| 5426 | zName = zTmpname; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5427 | |
| 5428 | /* Generated temporary filenames are always double-zero terminated |
| 5429 | ** for use by sqlite3_uri_parameter(). */ |
| 5430 | assert( zName[strlen(zName)+1]==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5431 | } |
| 5432 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5433 | /* Determine the value of the flags parameter passed to POSIX function |
| 5434 | ** open(). These must be calculated even if open() is not called, as |
| 5435 | ** they may be stored as part of the file handle and used by the |
| 5436 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5437 | if( isReadonly ) openFlags |= O_RDONLY; |
| 5438 | if( isReadWrite ) openFlags |= O_RDWR; |
| 5439 | if( isCreate ) openFlags |= O_CREAT; |
| 5440 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 5441 | openFlags |= (O_LARGEFILE|O_BINARY); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5442 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5443 | if( fd<0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5444 | mode_t openMode; /* Permissions to create file with */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5445 | uid_t uid; /* Userid for the file */ |
| 5446 | gid_t gid; /* Groupid for the file */ |
| 5447 | rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5448 | if( rc!=SQLITE_OK ){ |
| 5449 | assert( !p->pUnused ); |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5450 | assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5451 | return rc; |
| 5452 | } |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5453 | fd = robust_open(zName, openFlags, openMode); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5454 | OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5455 | if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ |
| 5456 | /* Failed to open the file for read/write access. Try read-only. */ |
| 5457 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5458 | openFlags &= ~(O_RDWR|O_CREAT); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5459 | flags |= SQLITE_OPEN_READONLY; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5460 | openFlags |= O_RDONLY; |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 5461 | isReadonly = 1; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5462 | fd = robust_open(zName, openFlags, openMode); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5463 | } |
| 5464 | if( fd<0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5465 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5466 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5467 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5468 | |
| 5469 | /* If this process is running as root and if creating a new rollback |
| 5470 | ** 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] | 5471 | ** the same as the original database. |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5472 | */ |
| 5473 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 5474 | osFchown(fd, uid, gid); |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5475 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5476 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5477 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5478 | if( pOutFlags ){ |
| 5479 | *pOutFlags = flags; |
| 5480 | } |
| 5481 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5482 | if( p->pUnused ){ |
| 5483 | p->pUnused->fd = fd; |
| 5484 | p->pUnused->flags = flags; |
| 5485 | } |
| 5486 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5487 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5488 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5489 | zPath = zName; |
| 5490 | #else |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5491 | osUnlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5492 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5493 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 5494 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5495 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5496 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 5497 | } |
| 5498 | #endif |
| 5499 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5500 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5501 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5502 | |
| 5503 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5504 | if( fstatfs(fd, &fsInfo) == -1 ){ |
| 5505 | ((unixFile*)pFile)->lastErrno = errno; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5506 | robust_close(p, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5507 | return SQLITE_IOERR_ACCESS; |
| 5508 | } |
| 5509 | if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 5510 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 5511 | } |
| 5512 | #endif |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5513 | |
| 5514 | /* Set up appropriate ctrlFlags */ |
| 5515 | if( isDelete ) ctrlFlags |= UNIXFILE_DELETE; |
| 5516 | if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY; |
| 5517 | if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK; |
| 5518 | if( syncDir ) ctrlFlags |= UNIXFILE_DIRSYNC; |
| 5519 | if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI; |
| 5520 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5521 | #if SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5522 | #if SQLITE_PREFER_PROXY_LOCKING |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5523 | isAutoProxy = 1; |
| 5524 | #endif |
| 5525 | if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5526 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 5527 | int useProxy = 0; |
| 5528 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5529 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 5530 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5531 | if( envforce!=NULL ){ |
| 5532 | useProxy = atoi(envforce)>0; |
| 5533 | }else{ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5534 | if( statfs(zPath, &fsInfo) == -1 ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5535 | /* In theory, the close(fd) call is sub-optimal. If the file opened |
| 5536 | ** with fd is a database file, and there are other connections open |
| 5537 | ** on that file that are currently holding advisory locks on it, |
| 5538 | ** then the call to close() will cancel those locks. In practice, |
| 5539 | ** we're assuming that statfs() doesn't fail very often. At least |
| 5540 | ** not while other file descriptors opened by the same process on |
| 5541 | ** the same file are working. */ |
| 5542 | p->lastErrno = errno; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5543 | robust_close(p, fd, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5544 | rc = SQLITE_IOERR_ACCESS; |
| 5545 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5546 | } |
| 5547 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 5548 | } |
| 5549 | if( useProxy ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5550 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5551 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5552 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5553 | if( rc!=SQLITE_OK ){ |
| 5554 | /* Use unixClose to clean up the resources added in fillInUnixFile |
| 5555 | ** and clear all the structure's references. Specifically, |
| 5556 | ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 5557 | */ |
| 5558 | unixClose(pFile); |
| 5559 | return rc; |
| 5560 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5561 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5562 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5563 | } |
| 5564 | } |
| 5565 | #endif |
| 5566 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5567 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
| 5568 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5569 | open_finished: |
| 5570 | if( rc!=SQLITE_OK ){ |
| 5571 | sqlite3_free(p->pUnused); |
| 5572 | } |
| 5573 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5574 | } |
| 5575 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5576 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5577 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5578 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 5579 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5580 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5581 | static int unixDelete( |
| 5582 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 5583 | const char *zPath, /* Name of file to be deleted */ |
| 5584 | int dirSync /* If true, fsync() directory after deleting file */ |
| 5585 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5586 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5587 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5588 | SimulateIOError(return SQLITE_IOERR_DELETE); |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 5589 | if( osUnlink(zPath)==(-1) ){ |
| 5590 | if( errno==ENOENT ){ |
| 5591 | rc = SQLITE_IOERR_DELETE_NOENT; |
| 5592 | }else{ |
drh | b430816 | 2012-11-09 21:40:02 +0000 | [diff] [blame] | 5593 | rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath); |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 5594 | } |
drh | b430816 | 2012-11-09 21:40:02 +0000 | [diff] [blame] | 5595 | return rc; |
drh | 5d4feff | 2010-07-14 01:45:22 +0000 | [diff] [blame] | 5596 | } |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 5597 | #ifndef SQLITE_DISABLE_DIRSYNC |
drh | e349519 | 2012-01-05 16:07:30 +0000 | [diff] [blame] | 5598 | if( (dirSync & 1)!=0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5599 | int fd; |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 5600 | rc = osOpenDirectory(zPath, &fd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5601 | if( rc==SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5602 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5603 | if( fsync(fd)==-1 ) |
| 5604 | #else |
| 5605 | if( fsync(fd) ) |
| 5606 | #endif |
| 5607 | { |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5608 | rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5609 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5610 | robust_close(0, fd, __LINE__); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 5611 | }else if( rc==SQLITE_CANTOPEN ){ |
| 5612 | rc = SQLITE_OK; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5613 | } |
| 5614 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 5615 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5616 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5617 | } |
| 5618 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5619 | /* |
| 5620 | ** Test the existance of or access permissions of file zPath. The |
| 5621 | ** test performed depends on the value of flags: |
| 5622 | ** |
| 5623 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 5624 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 5625 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 5626 | ** |
| 5627 | ** Otherwise return 0. |
| 5628 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5629 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5630 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 5631 | const char *zPath, /* Path of the file to examine */ |
| 5632 | int flags, /* What do we want to learn about the zPath file? */ |
| 5633 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5634 | ){ |
rse | 25c0d1a | 2007-09-20 08:38:14 +0000 | [diff] [blame] | 5635 | int amode = 0; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5636 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5637 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5638 | switch( flags ){ |
| 5639 | case SQLITE_ACCESS_EXISTS: |
| 5640 | amode = F_OK; |
| 5641 | break; |
| 5642 | case SQLITE_ACCESS_READWRITE: |
| 5643 | amode = W_OK|R_OK; |
| 5644 | break; |
drh | 50d3f90 | 2007-08-27 21:10:36 +0000 | [diff] [blame] | 5645 | case SQLITE_ACCESS_READ: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5646 | amode = R_OK; |
| 5647 | break; |
| 5648 | |
| 5649 | default: |
| 5650 | assert(!"Invalid flags argument"); |
| 5651 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5652 | *pResOut = (osAccess(zPath, amode)==0); |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 5653 | if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){ |
| 5654 | struct stat buf; |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 5655 | if( 0==osStat(zPath, &buf) && buf.st_size==0 ){ |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 5656 | *pResOut = 0; |
| 5657 | } |
| 5658 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5659 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5660 | } |
| 5661 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5662 | |
| 5663 | /* |
| 5664 | ** Turn a relative pathname into a full pathname. The relative path |
| 5665 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 5666 | ** zPath. |
| 5667 | ** |
| 5668 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 5669 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 5670 | ** this buffer before returning. |
| 5671 | */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 5672 | static int unixFullPathname( |
| 5673 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 5674 | const char *zPath, /* Possibly relative input path */ |
| 5675 | int nOut, /* Size of output buffer in bytes */ |
| 5676 | char *zOut /* Output buffer */ |
| 5677 | ){ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 5678 | |
| 5679 | /* It's odd to simulate an io-error here, but really this is just |
| 5680 | ** using the io-error infrastructure to test that SQLite handles this |
| 5681 | ** function failing. This function could fail if, for example, the |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5682 | ** current working directory has been unlinked. |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 5683 | */ |
| 5684 | SimulateIOError( return SQLITE_ERROR ); |
| 5685 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 5686 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 5687 | UNUSED_PARAMETER(pVfs); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5688 | |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 5689 | zOut[nOut-1] = '\0'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5690 | if( zPath[0]=='/' ){ |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 5691 | sqlite3_snprintf(nOut, zOut, "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5692 | }else{ |
| 5693 | int nCwd; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5694 | if( osGetcwd(zOut, nOut-1)==0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5695 | return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5696 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5697 | nCwd = (int)strlen(zOut); |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 5698 | sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5699 | } |
| 5700 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5701 | } |
| 5702 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 5703 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5704 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 5705 | /* |
| 5706 | ** Interfaces for opening a shared library, finding entry points |
| 5707 | ** within the shared library, and closing the shared library. |
| 5708 | */ |
| 5709 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5710 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 5711 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5712 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 5713 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 5714 | |
| 5715 | /* |
| 5716 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 5717 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 5718 | ** message is available, it is written to zBufOut. If no error message |
| 5719 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 5720 | ** error message. |
| 5721 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5722 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
dan | 3239053 | 2010-11-29 18:36:22 +0000 | [diff] [blame] | 5723 | const char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5724 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5725 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5726 | zErr = dlerror(); |
| 5727 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 5728 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5729 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5730 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5731 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5732 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 5733 | /* |
| 5734 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 5735 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 5736 | ** returns a void* which is really a pointer to a function. So how do we |
| 5737 | ** use dlsym() with -pedantic-errors? |
| 5738 | ** |
| 5739 | ** Variable x below is defined to be a pointer to a function taking |
| 5740 | ** parameters void* and const char* and returning a pointer to a function. |
| 5741 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 5742 | ** (That assignment requires a cast.) Then we call the function that |
| 5743 | ** x points to. |
| 5744 | ** |
| 5745 | ** This work-around is unlikely to work correctly on any system where |
| 5746 | ** you really cannot cast a function pointer into void*. But then, on the |
| 5747 | ** other hand, dlsym() will not work on such a system either, so we have |
| 5748 | ** not really lost anything. |
| 5749 | */ |
| 5750 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5751 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5752 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 5753 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5754 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5755 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 5756 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5757 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5758 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5759 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 5760 | #define unixDlOpen 0 |
| 5761 | #define unixDlError 0 |
| 5762 | #define unixDlSym 0 |
| 5763 | #define unixDlClose 0 |
| 5764 | #endif |
| 5765 | |
| 5766 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5767 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5768 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5769 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 5770 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 5771 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5772 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5773 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 5774 | ** errors. The reports issued by valgrind are incorrect - we would |
| 5775 | ** prefer that the randomness be increased by making use of the |
| 5776 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 5777 | ** some users. Rather than argue, it seems easier just to initialize |
| 5778 | ** the whole array and silence valgrind, even if that means less randomness |
| 5779 | ** in the random seed. |
| 5780 | ** |
| 5781 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 5782 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5783 | ** tests repeatable. |
| 5784 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5785 | memset(zBuf, 0, nBuf); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5786 | #if !defined(SQLITE_TEST) |
| 5787 | { |
drh | c18b404 | 2012-02-10 03:10:27 +0000 | [diff] [blame] | 5788 | int pid, fd, got; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5789 | fd = robust_open("/dev/urandom", O_RDONLY, 0); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5790 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 5791 | time_t t; |
| 5792 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5793 | memcpy(zBuf, &t, sizeof(t)); |
| 5794 | pid = getpid(); |
| 5795 | memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid)); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 5796 | assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf ); |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 5797 | nBuf = sizeof(t) + sizeof(pid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5798 | }else{ |
drh | c18b404 | 2012-02-10 03:10:27 +0000 | [diff] [blame] | 5799 | do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5800 | robust_close(0, fd, __LINE__); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5801 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5802 | } |
| 5803 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 5804 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5805 | } |
| 5806 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5807 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5808 | /* |
| 5809 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5810 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 5811 | ** The return value is the number of microseconds of sleep actually |
| 5812 | ** requested from the underlying operating system, a number which |
| 5813 | ** might be greater than or equal to the argument, but not less |
| 5814 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5815 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5816 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5817 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5818 | struct timespec sp; |
| 5819 | |
| 5820 | sp.tv_sec = microseconds / 1000000; |
| 5821 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 5822 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 5823 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5824 | return microseconds; |
| 5825 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5826 | usleep(microseconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 5827 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5828 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5829 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5830 | int seconds = (microseconds+999999)/1000000; |
| 5831 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 5832 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 5833 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 5834 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 5835 | } |
| 5836 | |
| 5837 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5838 | ** The following variable, if set to a non-zero value, is interpreted as |
| 5839 | ** the number of seconds since 1970 and is used to set the result of |
| 5840 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5841 | */ |
| 5842 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5843 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5844 | #endif |
| 5845 | |
| 5846 | /* |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5847 | ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 5848 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 5849 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 5850 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 5851 | ** proleptic Gregorian calendar. |
| 5852 | ** |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5853 | ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
| 5854 | ** cannot be found. |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5855 | */ |
| 5856 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 5857 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5858 | int rc = SQLITE_OK; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5859 | #if defined(NO_GETTOD) |
| 5860 | time_t t; |
| 5861 | time(&t); |
dan | 15eac4e | 2010-11-22 17:26:07 +0000 | [diff] [blame] | 5862 | *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5863 | #elif OS_VXWORKS |
| 5864 | struct timespec sNow; |
| 5865 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 5866 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 5867 | #else |
| 5868 | struct timeval sNow; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5869 | if( gettimeofday(&sNow, 0)==0 ){ |
| 5870 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
| 5871 | }else{ |
| 5872 | rc = SQLITE_ERROR; |
| 5873 | } |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5874 | #endif |
| 5875 | |
| 5876 | #ifdef SQLITE_TEST |
| 5877 | if( sqlite3_current_time ){ |
| 5878 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 5879 | } |
| 5880 | #endif |
| 5881 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5882 | return rc; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5883 | } |
| 5884 | |
| 5885 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5886 | ** Find the current time (in Universal Coordinated Time). Write the |
| 5887 | ** current time and date as a Julian Day number into *prNow and |
| 5888 | ** return 0. Return 1 if the time and date cannot be found. |
| 5889 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5890 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | b87a666 | 2011-10-13 01:01:14 +0000 | [diff] [blame] | 5891 | sqlite3_int64 i = 0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5892 | int rc; |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 5893 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5894 | rc = unixCurrentTimeInt64(0, &i); |
drh | 0dcb0a7 | 2010-05-03 18:22:52 +0000 | [diff] [blame] | 5895 | *prNow = i/86400000.0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5896 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5897 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5898 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5899 | /* |
| 5900 | ** We added the xGetLastError() method with the intention of providing |
| 5901 | ** better low-level error messages when operating-system problems come up |
| 5902 | ** during SQLite operation. But so far, none of that has been implemented |
| 5903 | ** in the core. So this routine is never called. For now, it is merely |
| 5904 | ** a place-holder. |
| 5905 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5906 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 5907 | UNUSED_PARAMETER(NotUsed); |
| 5908 | UNUSED_PARAMETER(NotUsed2); |
| 5909 | UNUSED_PARAMETER(NotUsed3); |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 5910 | return 0; |
| 5911 | } |
| 5912 | |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5913 | |
| 5914 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5915 | ************************ End of sqlite3_vfs methods *************************** |
| 5916 | ******************************************************************************/ |
| 5917 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5918 | /****************************************************************************** |
| 5919 | ************************** Begin Proxy Locking ******************************** |
| 5920 | ** |
| 5921 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 5922 | ** other locking methods on secondary lock files. Proxy locking is a |
| 5923 | ** meta-layer over top of the primitive locking implemented above. For |
| 5924 | ** this reason, the division that implements of proxy locking is deferred |
| 5925 | ** until late in the file (here) after all of the other I/O methods have |
| 5926 | ** been defined - so that the primitive locking methods are available |
| 5927 | ** as services to help with the implementation of proxy locking. |
| 5928 | ** |
| 5929 | **** |
| 5930 | ** |
| 5931 | ** The default locking schemes in SQLite use byte-range locks on the |
| 5932 | ** database file to coordinate safe, concurrent access by multiple readers |
| 5933 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 5934 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 5935 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 5936 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 5937 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 5938 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 5939 | ** address in the shared range is taken for a SHARED lock, the entire |
| 5940 | ** shared range is taken for an EXCLUSIVE lock): |
| 5941 | ** |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 5942 | ** PENDING_BYTE 0x40000000 |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5943 | ** RESERVED_BYTE 0x40000001 |
| 5944 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 5945 | ** |
| 5946 | ** This works well on the local file system, but shows a nearly 100x |
| 5947 | ** slowdown in read performance on AFP because the AFP client disables |
| 5948 | ** the read cache when byte-range locks are present. Enabling the read |
| 5949 | ** cache exposes a cache coherency problem that is present on all OS X |
| 5950 | ** supported network file systems. NFS and AFP both observe the |
| 5951 | ** close-to-open semantics for ensuring cache coherency |
| 5952 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 5953 | ** address the requirements for concurrent database access by multiple |
| 5954 | ** readers and writers |
| 5955 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 5956 | ** |
| 5957 | ** To address the performance and cache coherency issues, proxy file locking |
| 5958 | ** changes the way database access is controlled by limiting access to a |
| 5959 | ** single host at a time and moving file locks off of the database file |
| 5960 | ** and onto a proxy file on the local file system. |
| 5961 | ** |
| 5962 | ** |
| 5963 | ** Using proxy locks |
| 5964 | ** ----------------- |
| 5965 | ** |
| 5966 | ** C APIs |
| 5967 | ** |
| 5968 | ** sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE, |
| 5969 | ** <proxy_path> | ":auto:"); |
| 5970 | ** sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>); |
| 5971 | ** |
| 5972 | ** |
| 5973 | ** SQL pragmas |
| 5974 | ** |
| 5975 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 5976 | ** PRAGMA [database.]lock_proxy_file |
| 5977 | ** |
| 5978 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 5979 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 5980 | ** a proxy path based on the user's temp dir |
| 5981 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 5982 | ** actual proxy file name is generated from the name and path of the |
| 5983 | ** database file. For example: |
| 5984 | ** |
| 5985 | ** For database path "/Users/me/foo.db" |
| 5986 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 5987 | ** |
| 5988 | ** Once a lock proxy is configured for a database connection, it can not |
| 5989 | ** be removed, however it may be switched to a different proxy path via |
| 5990 | ** the above APIs (assuming the conch file is not being held by another |
| 5991 | ** connection or process). |
| 5992 | ** |
| 5993 | ** |
| 5994 | ** How proxy locking works |
| 5995 | ** ----------------------- |
| 5996 | ** |
| 5997 | ** Proxy file locking relies primarily on two new supporting files: |
| 5998 | ** |
| 5999 | ** * conch file to limit access to the database file to a single host |
| 6000 | ** at a time |
| 6001 | ** |
| 6002 | ** * proxy file to act as a proxy for the advisory locks normally |
| 6003 | ** taken on the database |
| 6004 | ** |
| 6005 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 6006 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 6007 | ** contents and comparing the host's unique host ID (see below) and lock |
| 6008 | ** proxy path against the values stored in the conch. The conch file is |
| 6009 | ** stored in the same directory as the database file and the file name |
| 6010 | ** is patterned after the database file name as ".<databasename>-conch". |
| 6011 | ** If the conch file does not exist, or it's contents do not match the |
| 6012 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 6013 | ** lock and the conch file contents is updated with the host ID and proxy |
| 6014 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 6015 | ** is held by another process (with a shared lock), the exclusive lock |
| 6016 | ** will fail and SQLITE_BUSY is returned. |
| 6017 | ** |
| 6018 | ** The proxy file - a single-byte file used for all advisory file locks |
| 6019 | ** normally taken on the database file. This allows for safe sharing |
| 6020 | ** of the database file for multiple readers and writers on the same |
| 6021 | ** host (the conch ensures that they all use the same local lock file). |
| 6022 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6023 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 6024 | ** only taken when the first request to lock database file is made. |
| 6025 | ** This matches the semantics of the traditional locking behavior, where |
| 6026 | ** opening a connection to a database file does not take a lock on it. |
| 6027 | ** The shared lock and an open file descriptor are maintained until |
| 6028 | ** the connection to the database is closed. |
| 6029 | ** |
| 6030 | ** The proxy file and the lock file are never deleted so they only need |
| 6031 | ** to be created the first time they are used. |
| 6032 | ** |
| 6033 | ** Configuration options |
| 6034 | ** --------------------- |
| 6035 | ** |
| 6036 | ** SQLITE_PREFER_PROXY_LOCKING |
| 6037 | ** |
| 6038 | ** Database files accessed on non-local file systems are |
| 6039 | ** automatically configured for proxy locking, lock files are |
| 6040 | ** named automatically using the same logic as |
| 6041 | ** PRAGMA lock_proxy_file=":auto:" |
| 6042 | ** |
| 6043 | ** SQLITE_PROXY_DEBUG |
| 6044 | ** |
| 6045 | ** Enables the logging of error messages during host id file |
| 6046 | ** retrieval and creation |
| 6047 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6048 | ** LOCKPROXYDIR |
| 6049 | ** |
| 6050 | ** Overrides the default directory used for lock proxy files that |
| 6051 | ** are named automatically via the ":auto:" setting |
| 6052 | ** |
| 6053 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 6054 | ** |
| 6055 | ** Permissions to use when creating a directory for storing the |
| 6056 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 6057 | ** |
| 6058 | ** |
| 6059 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 6060 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 6061 | ** force proxy locking to be used for every database file opened, and 0 |
| 6062 | ** will force automatic proxy locking to be disabled for all database |
| 6063 | ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or |
| 6064 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 6065 | */ |
| 6066 | |
| 6067 | /* |
| 6068 | ** Proxy locking is only available on MacOSX |
| 6069 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6070 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6071 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6072 | /* |
| 6073 | ** The proxyLockingContext has the path and file structures for the remote |
| 6074 | ** and local proxy files in it |
| 6075 | */ |
| 6076 | typedef struct proxyLockingContext proxyLockingContext; |
| 6077 | struct proxyLockingContext { |
| 6078 | unixFile *conchFile; /* Open conch file */ |
| 6079 | char *conchFilePath; /* Name of the conch file */ |
| 6080 | unixFile *lockProxy; /* Open proxy lock file */ |
| 6081 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 6082 | char *dbPath; /* Name of the open file */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6083 | int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6084 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 6085 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 6086 | }; |
| 6087 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6088 | /* |
| 6089 | ** The proxy lock file path for the database at dbPath is written into lPath, |
| 6090 | ** which must point to valid, writable memory large enough for a maxLen length |
| 6091 | ** file path. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6092 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6093 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 6094 | int len; |
| 6095 | int dbLen; |
| 6096 | int i; |
| 6097 | |
| 6098 | #ifdef LOCKPROXYDIR |
| 6099 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 6100 | #else |
| 6101 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 6102 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6103 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6104 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
| 6105 | lPath, errno, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6106 | return SQLITE_IOERR_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6107 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6108 | len = strlcat(lPath, "sqliteplocks", maxLen); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6109 | } |
| 6110 | # else |
| 6111 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 6112 | # endif |
| 6113 | #endif |
| 6114 | |
| 6115 | if( lPath[len-1]!='/' ){ |
| 6116 | len = strlcat(lPath, "/", maxLen); |
| 6117 | } |
| 6118 | |
| 6119 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6120 | dbLen = (int)strlen(dbPath); |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6121 | for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6122 | char c = dbPath[i]; |
| 6123 | lPath[i+len] = (c=='/')?'_':c; |
| 6124 | } |
| 6125 | lPath[i+len]='\0'; |
| 6126 | strlcat(lPath, ":auto:", maxLen); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6127 | OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6128 | return SQLITE_OK; |
| 6129 | } |
| 6130 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6131 | /* |
| 6132 | ** Creates the lock file and any missing directories in lockPath |
| 6133 | */ |
| 6134 | static int proxyCreateLockPath(const char *lockPath){ |
| 6135 | int i, len; |
| 6136 | char buf[MAXPATHLEN]; |
| 6137 | int start = 0; |
| 6138 | |
| 6139 | assert(lockPath!=NULL); |
| 6140 | /* try to create all the intermediate directories */ |
| 6141 | len = (int)strlen(lockPath); |
| 6142 | buf[0] = lockPath[0]; |
| 6143 | for( i=1; i<len; i++ ){ |
| 6144 | if( lockPath[i] == '/' && (i - start > 0) ){ |
| 6145 | /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 6146 | if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 6147 | || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 6148 | buf[i]='\0'; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 6149 | if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6150 | int err=errno; |
| 6151 | if( err!=EEXIST ) { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6152 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6153 | "'%s' proxy lock path=%s pid=%d\n", |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6154 | buf, strerror(err), lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6155 | return err; |
| 6156 | } |
| 6157 | } |
| 6158 | } |
| 6159 | start=i+1; |
| 6160 | } |
| 6161 | buf[i] = lockPath[i]; |
| 6162 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6163 | OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6164 | return 0; |
| 6165 | } |
| 6166 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6167 | /* |
| 6168 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 6169 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 6170 | ** |
| 6171 | ** The caller is responsible not only for closing the file descriptor |
| 6172 | ** but also for freeing the memory associated with the file descriptor. |
| 6173 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6174 | static int proxyCreateUnixFile( |
| 6175 | const char *path, /* path for the new unixFile */ |
| 6176 | unixFile **ppFile, /* unixFile created and returned by ref */ |
| 6177 | int islockfile /* if non zero missing dirs will be created */ |
| 6178 | ) { |
| 6179 | int fd = -1; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6180 | unixFile *pNew; |
| 6181 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6182 | int openFlags = O_RDWR | O_CREAT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6183 | sqlite3_vfs dummyVfs; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6184 | int terrno = 0; |
| 6185 | UnixUnusedFd *pUnused = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6186 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6187 | /* 1. first try to open/create the file |
| 6188 | ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 6189 | ** the parent directories and then try again. |
| 6190 | ** 3. if that fails, try to open the file read-only |
| 6191 | ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 6192 | */ |
| 6193 | pUnused = findReusableFd(path, openFlags); |
| 6194 | if( pUnused ){ |
| 6195 | fd = pUnused->fd; |
| 6196 | }else{ |
| 6197 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
| 6198 | if( !pUnused ){ |
| 6199 | return SQLITE_NOMEM; |
| 6200 | } |
| 6201 | } |
| 6202 | if( fd<0 ){ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6203 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6204 | terrno = errno; |
| 6205 | if( fd<0 && errno==ENOENT && islockfile ){ |
| 6206 | if( proxyCreateLockPath(path) == SQLITE_OK ){ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6207 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6208 | } |
| 6209 | } |
| 6210 | } |
| 6211 | if( fd<0 ){ |
| 6212 | openFlags = O_RDONLY; |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6213 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6214 | terrno = errno; |
| 6215 | } |
| 6216 | if( fd<0 ){ |
| 6217 | if( islockfile ){ |
| 6218 | return SQLITE_BUSY; |
| 6219 | } |
| 6220 | switch (terrno) { |
| 6221 | case EACCES: |
| 6222 | return SQLITE_PERM; |
| 6223 | case EIO: |
| 6224 | return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 6225 | default: |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6226 | return SQLITE_CANTOPEN_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6227 | } |
| 6228 | } |
| 6229 | |
| 6230 | pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew)); |
| 6231 | if( pNew==NULL ){ |
| 6232 | rc = SQLITE_NOMEM; |
| 6233 | goto end_create_proxy; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6234 | } |
| 6235 | memset(pNew, 0, sizeof(unixFile)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6236 | pNew->openFlags = openFlags; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 6237 | memset(&dummyVfs, 0, sizeof(dummyVfs)); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6238 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 6239 | dummyVfs.zName = "dummy"; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6240 | pUnused->fd = fd; |
| 6241 | pUnused->flags = openFlags; |
| 6242 | pNew->pUnused = pUnused; |
| 6243 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6244 | rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6245 | if( rc==SQLITE_OK ){ |
| 6246 | *ppFile = pNew; |
| 6247 | return SQLITE_OK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6248 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6249 | end_create_proxy: |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6250 | robust_close(pNew, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6251 | sqlite3_free(pNew); |
| 6252 | sqlite3_free(pUnused); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6253 | return rc; |
| 6254 | } |
| 6255 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6256 | #ifdef SQLITE_TEST |
| 6257 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 6258 | int sqlite3_hostid_num = 0; |
| 6259 | #endif |
| 6260 | |
| 6261 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 6262 | |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6263 | /* Not always defined in the headers as it ought to be */ |
| 6264 | extern int gethostuuid(uuid_t id, const struct timespec *wait); |
| 6265 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6266 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 6267 | ** bytes of writable memory. |
| 6268 | */ |
| 6269 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6270 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 6271 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 6272 | #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\ |
| 6273 | && __MAC_OS_X_VERSION_MIN_REQUIRED<1050 |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 6274 | { |
| 6275 | static const struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
| 6276 | if( gethostuuid(pHostID, &timeout) ){ |
| 6277 | int err = errno; |
| 6278 | if( pError ){ |
| 6279 | *pError = err; |
| 6280 | } |
| 6281 | return SQLITE_IOERR; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6282 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6283 | } |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6284 | #else |
| 6285 | UNUSED_PARAMETER(pError); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 6286 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6287 | #ifdef SQLITE_TEST |
| 6288 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 6289 | if( sqlite3_hostid_num != 0){ |
| 6290 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 6291 | } |
| 6292 | #endif |
| 6293 | |
| 6294 | return SQLITE_OK; |
| 6295 | } |
| 6296 | |
| 6297 | /* The conch file contains the header, host id and lock file path |
| 6298 | */ |
| 6299 | #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 6300 | #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 6301 | #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 6302 | #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 6303 | |
| 6304 | /* |
| 6305 | ** Takes an open conch file, copies the contents to a new path and then moves |
| 6306 | ** it back. The newly created file's file descriptor is assigned to the |
| 6307 | ** conch file structure and finally the original conch file descriptor is |
| 6308 | ** closed. Returns zero if successful. |
| 6309 | */ |
| 6310 | static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 6311 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6312 | unixFile *conchFile = pCtx->conchFile; |
| 6313 | char tPath[MAXPATHLEN]; |
| 6314 | char buf[PROXY_MAXCONCHLEN]; |
| 6315 | char *cPath = pCtx->conchFilePath; |
| 6316 | size_t readLen = 0; |
| 6317 | size_t pathLen = 0; |
| 6318 | char errmsg[64] = ""; |
| 6319 | int fd = -1; |
| 6320 | int rc = -1; |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6321 | UNUSED_PARAMETER(myHostID); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6322 | |
| 6323 | /* create a new path by replace the trailing '-conch' with '-break' */ |
| 6324 | pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 6325 | if( pathLen>MAXPATHLEN || pathLen<6 || |
| 6326 | (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6327 | sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6328 | goto end_breaklock; |
| 6329 | } |
| 6330 | /* read the conch content */ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6331 | readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6332 | if( readLen<PROXY_PATHINDEX ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6333 | sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6334 | goto end_breaklock; |
| 6335 | } |
| 6336 | /* write it out to the temporary break file */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6337 | fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6338 | if( fd<0 ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6339 | sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6340 | goto end_breaklock; |
| 6341 | } |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6342 | if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6343 | sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6344 | goto end_breaklock; |
| 6345 | } |
| 6346 | if( rename(tPath, cPath) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6347 | sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6348 | goto end_breaklock; |
| 6349 | } |
| 6350 | rc = 0; |
| 6351 | fprintf(stderr, "broke stale lock on %s\n", cPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6352 | robust_close(pFile, conchFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6353 | conchFile->h = fd; |
| 6354 | conchFile->openFlags = O_RDWR | O_CREAT; |
| 6355 | |
| 6356 | end_breaklock: |
| 6357 | if( rc ){ |
| 6358 | if( fd>=0 ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 6359 | osUnlink(tPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6360 | robust_close(pFile, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6361 | } |
| 6362 | fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 6363 | } |
| 6364 | return rc; |
| 6365 | } |
| 6366 | |
| 6367 | /* Take the requested lock on the conch file and break a stale lock if the |
| 6368 | ** host id matches. |
| 6369 | */ |
| 6370 | static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 6371 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6372 | unixFile *conchFile = pCtx->conchFile; |
| 6373 | int rc = SQLITE_OK; |
| 6374 | int nTries = 0; |
| 6375 | struct timespec conchModTime; |
| 6376 | |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6377 | memset(&conchModTime, 0, sizeof(conchModTime)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6378 | do { |
| 6379 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 6380 | nTries ++; |
| 6381 | if( rc==SQLITE_BUSY ){ |
| 6382 | /* If the lock failed (busy): |
| 6383 | * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 6384 | * 2nd try: fail if the mod time changed or host id is different, wait |
| 6385 | * 10 sec and try again |
| 6386 | * 3rd try: break the lock unless the mod time has changed. |
| 6387 | */ |
| 6388 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6389 | if( osFstat(conchFile->h, &buf) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6390 | pFile->lastErrno = errno; |
| 6391 | return SQLITE_IOERR_LOCK; |
| 6392 | } |
| 6393 | |
| 6394 | if( nTries==1 ){ |
| 6395 | conchModTime = buf.st_mtimespec; |
| 6396 | usleep(500000); /* wait 0.5 sec and try the lock again*/ |
| 6397 | continue; |
| 6398 | } |
| 6399 | |
| 6400 | assert( nTries>1 ); |
| 6401 | if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 6402 | conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 6403 | return SQLITE_BUSY; |
| 6404 | } |
| 6405 | |
| 6406 | if( nTries==2 ){ |
| 6407 | char tBuf[PROXY_MAXCONCHLEN]; |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6408 | int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6409 | if( len<0 ){ |
| 6410 | pFile->lastErrno = errno; |
| 6411 | return SQLITE_IOERR_LOCK; |
| 6412 | } |
| 6413 | if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 6414 | /* don't break the lock if the host id doesn't match */ |
| 6415 | if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 6416 | return SQLITE_BUSY; |
| 6417 | } |
| 6418 | }else{ |
| 6419 | /* don't break the lock on short read or a version mismatch */ |
| 6420 | return SQLITE_BUSY; |
| 6421 | } |
| 6422 | usleep(10000000); /* wait 10 sec and try the lock again */ |
| 6423 | continue; |
| 6424 | } |
| 6425 | |
| 6426 | assert( nTries==3 ); |
| 6427 | if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 6428 | rc = SQLITE_OK; |
| 6429 | if( lockType==EXCLUSIVE_LOCK ){ |
| 6430 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 6431 | } |
| 6432 | if( !rc ){ |
| 6433 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 6434 | } |
| 6435 | } |
| 6436 | } |
| 6437 | } while( rc==SQLITE_BUSY && nTries<3 ); |
| 6438 | |
| 6439 | return rc; |
| 6440 | } |
| 6441 | |
| 6442 | /* 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] | 6443 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 6444 | ** lockPath means that the lockPath in the conch file will be used if the |
| 6445 | ** host IDs match, or a new lock path will be generated automatically |
| 6446 | ** and written to the conch file. |
| 6447 | */ |
| 6448 | static int proxyTakeConch(unixFile *pFile){ |
| 6449 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6450 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6451 | if( pCtx->conchHeld!=0 ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6452 | return SQLITE_OK; |
| 6453 | }else{ |
| 6454 | unixFile *conchFile = pCtx->conchFile; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6455 | uuid_t myHostID; |
| 6456 | int pError = 0; |
| 6457 | char readBuf[PROXY_MAXCONCHLEN]; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6458 | char lockPath[MAXPATHLEN]; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6459 | char *tempLockPath = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6460 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6461 | int createConch = 0; |
| 6462 | int hostIdMatch = 0; |
| 6463 | int readLen = 0; |
| 6464 | int tryOldLockPath = 0; |
| 6465 | int forceNewLockPath = 0; |
| 6466 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6467 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
| 6468 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6469 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6470 | rc = proxyGetHostID(myHostID, &pError); |
| 6471 | if( (rc&0xff)==SQLITE_IOERR ){ |
| 6472 | pFile->lastErrno = pError; |
| 6473 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6474 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6475 | rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6476 | if( rc!=SQLITE_OK ){ |
| 6477 | goto end_takeconch; |
| 6478 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6479 | /* read the existing conch file */ |
| 6480 | readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 6481 | if( readLen<0 ){ |
| 6482 | /* I/O error: lastErrno set by seekAndRead */ |
| 6483 | pFile->lastErrno = conchFile->lastErrno; |
| 6484 | rc = SQLITE_IOERR_READ; |
| 6485 | goto end_takeconch; |
| 6486 | }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 6487 | readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 6488 | /* a short read or version format mismatch means we need to create a new |
| 6489 | ** conch file. |
| 6490 | */ |
| 6491 | createConch = 1; |
| 6492 | } |
| 6493 | /* if the host id matches and the lock path already exists in the conch |
| 6494 | ** we'll try to use the path there, if we can't open that path, we'll |
| 6495 | ** retry with a new auto-generated path |
| 6496 | */ |
| 6497 | do { /* in case we need to try again for an :auto: named lock file */ |
| 6498 | |
| 6499 | if( !createConch && !forceNewLockPath ){ |
| 6500 | hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 6501 | PROXY_HOSTIDLEN); |
| 6502 | /* if the conch has data compare the contents */ |
| 6503 | if( !pCtx->lockProxyPath ){ |
| 6504 | /* for auto-named local lock file, just check the host ID and we'll |
| 6505 | ** use the local lock file path that's already in there |
| 6506 | */ |
| 6507 | if( hostIdMatch ){ |
| 6508 | size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 6509 | |
| 6510 | if( pathLen>=MAXPATHLEN ){ |
| 6511 | pathLen=MAXPATHLEN-1; |
| 6512 | } |
| 6513 | memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 6514 | lockPath[pathLen] = 0; |
| 6515 | tempLockPath = lockPath; |
| 6516 | tryOldLockPath = 1; |
| 6517 | /* create a copy of the lock path if the conch is taken */ |
| 6518 | goto end_takeconch; |
| 6519 | } |
| 6520 | }else if( hostIdMatch |
| 6521 | && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 6522 | readLen-PROXY_PATHINDEX) |
| 6523 | ){ |
| 6524 | /* conch host and lock path match */ |
| 6525 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6526 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6527 | } |
| 6528 | |
| 6529 | /* if the conch isn't writable and doesn't match, we can't take it */ |
| 6530 | if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 6531 | rc = SQLITE_BUSY; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6532 | goto end_takeconch; |
| 6533 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6534 | |
| 6535 | /* 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] | 6536 | if( !pCtx->lockProxyPath ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6537 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 6538 | tempLockPath = lockPath; |
| 6539 | /* create a copy of the lock path _only_ if the conch is taken */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6540 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6541 | |
| 6542 | /* update conch with host and path (this will fail if other process |
| 6543 | ** has a shared lock already), if the host id matches, use the big |
| 6544 | ** stick. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6545 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6546 | futimes(conchFile->h, NULL); |
| 6547 | if( hostIdMatch && !createConch ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 6548 | if( conchFile->pInode && conchFile->pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6549 | /* We are trying for an exclusive lock but another thread in this |
| 6550 | ** same process is still holding a shared lock. */ |
| 6551 | rc = SQLITE_BUSY; |
| 6552 | } else { |
| 6553 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6554 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6555 | }else{ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6556 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6557 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6558 | if( rc==SQLITE_OK ){ |
| 6559 | char writeBuffer[PROXY_MAXCONCHLEN]; |
| 6560 | int writeSize = 0; |
| 6561 | |
| 6562 | writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 6563 | memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 6564 | if( pCtx->lockProxyPath!=NULL ){ |
| 6565 | strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN); |
| 6566 | }else{ |
| 6567 | strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 6568 | } |
| 6569 | writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6570 | robust_ftruncate(conchFile->h, writeSize); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6571 | rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
| 6572 | fsync(conchFile->h); |
| 6573 | /* If we created a new conch file (not just updated the contents of a |
| 6574 | ** valid conch file), try to match the permissions of the database |
| 6575 | */ |
| 6576 | if( rc==SQLITE_OK && createConch ){ |
| 6577 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6578 | int err = osFstat(pFile->h, &buf); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6579 | if( err==0 ){ |
| 6580 | mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 6581 | S_IROTH|S_IWOTH); |
| 6582 | /* try to match the database file R/W permissions, ignore failure */ |
| 6583 | #ifndef SQLITE_PROXY_DEBUG |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6584 | osFchmod(conchFile->h, cmode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6585 | #else |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6586 | do{ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6587 | rc = osFchmod(conchFile->h, cmode); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6588 | }while( rc==(-1) && errno==EINTR ); |
| 6589 | if( rc!=0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6590 | int code = errno; |
| 6591 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 6592 | cmode, code, strerror(code)); |
| 6593 | } else { |
| 6594 | fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 6595 | } |
| 6596 | }else{ |
| 6597 | int code = errno; |
| 6598 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 6599 | err, code, strerror(code)); |
| 6600 | #endif |
| 6601 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6602 | } |
| 6603 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6604 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 6605 | |
| 6606 | end_takeconch: |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6607 | OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6608 | if( rc==SQLITE_OK && pFile->openFlags ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6609 | int fd; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6610 | if( pFile->h>=0 ){ |
drh | e84009f | 2011-03-02 17:54:32 +0000 | [diff] [blame] | 6611 | robust_close(pFile, pFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6612 | } |
| 6613 | pFile->h = -1; |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6614 | fd = robust_open(pCtx->dbPath, pFile->openFlags, 0); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6615 | OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6616 | if( fd>=0 ){ |
| 6617 | pFile->h = fd; |
| 6618 | }else{ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6619 | rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6620 | during locking */ |
| 6621 | } |
| 6622 | } |
| 6623 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 6624 | char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 6625 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 6626 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 6627 | /* we couldn't create the proxy lock file with the old lock file path |
| 6628 | ** so try again via auto-naming |
| 6629 | */ |
| 6630 | forceNewLockPath = 1; |
| 6631 | tryOldLockPath = 0; |
dan | 2b0ef47 | 2010-02-16 12:18:47 +0000 | [diff] [blame] | 6632 | continue; /* go back to the do {} while start point, try again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6633 | } |
| 6634 | } |
| 6635 | if( rc==SQLITE_OK ){ |
| 6636 | /* Need to make a copy of path if we extracted the value |
| 6637 | ** from the conch file or the path was allocated on the stack |
| 6638 | */ |
| 6639 | if( tempLockPath ){ |
| 6640 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 6641 | if( !pCtx->lockProxyPath ){ |
| 6642 | rc = SQLITE_NOMEM; |
| 6643 | } |
| 6644 | } |
| 6645 | } |
| 6646 | if( rc==SQLITE_OK ){ |
| 6647 | pCtx->conchHeld = 1; |
| 6648 | |
| 6649 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 6650 | afpLockingContext *afpCtx; |
| 6651 | afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 6652 | afpCtx->dbPath = pCtx->lockProxyPath; |
| 6653 | } |
| 6654 | } else { |
| 6655 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 6656 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6657 | OSTRACE(("TAKECONCH %d %s\n", conchFile->h, |
| 6658 | rc==SQLITE_OK?"ok":"failed")); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6659 | return rc; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6660 | } while (1); /* in case we need to retry the :auto: lock file - |
| 6661 | ** we should never get here except via the 'continue' call. */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6662 | } |
| 6663 | } |
| 6664 | |
| 6665 | /* |
| 6666 | ** If pFile holds a lock on a conch file, then release that lock. |
| 6667 | */ |
| 6668 | static int proxyReleaseConch(unixFile *pFile){ |
drh | 1c5bb4d | 2010-05-10 17:29:28 +0000 | [diff] [blame] | 6669 | int rc = SQLITE_OK; /* Subroutine return code */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6670 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 6671 | unixFile *conchFile; /* Name of the conch file */ |
| 6672 | |
| 6673 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6674 | conchFile = pCtx->conchFile; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6675 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6676 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6677 | getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6678 | if( pCtx->conchHeld>0 ){ |
| 6679 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 6680 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6681 | pCtx->conchHeld = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6682 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| 6683 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6684 | return rc; |
| 6685 | } |
| 6686 | |
| 6687 | /* |
| 6688 | ** Given the name of a database file, compute the name of its conch file. |
| 6689 | ** Store the conch filename in memory obtained from sqlite3_malloc(). |
| 6690 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 6691 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 6692 | ** |
| 6693 | ** The caller is responsible for ensuring that the allocated memory |
| 6694 | ** space is eventually freed. |
| 6695 | ** |
| 6696 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 6697 | */ |
| 6698 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 6699 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6700 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6701 | char *conchPath; /* buffer in which to construct conch name */ |
| 6702 | |
| 6703 | /* Allocate space for the conch filename and initialize the name to |
| 6704 | ** the name of the original database file. */ |
| 6705 | *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8); |
| 6706 | if( conchPath==0 ){ |
| 6707 | return SQLITE_NOMEM; |
| 6708 | } |
| 6709 | memcpy(conchPath, dbPath, len+1); |
| 6710 | |
| 6711 | /* now insert a "." before the last / character */ |
| 6712 | for( i=(len-1); i>=0; i-- ){ |
| 6713 | if( conchPath[i]=='/' ){ |
| 6714 | i++; |
| 6715 | break; |
| 6716 | } |
| 6717 | } |
| 6718 | conchPath[i]='.'; |
| 6719 | while ( i<len ){ |
| 6720 | conchPath[i+1]=dbPath[i]; |
| 6721 | i++; |
| 6722 | } |
| 6723 | |
| 6724 | /* append the "-conch" suffix to the file */ |
| 6725 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6726 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6727 | |
| 6728 | return SQLITE_OK; |
| 6729 | } |
| 6730 | |
| 6731 | |
| 6732 | /* Takes a fully configured proxy locking-style unix file and switches |
| 6733 | ** the local lock file path |
| 6734 | */ |
| 6735 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 6736 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 6737 | char *oldPath = pCtx->lockProxyPath; |
| 6738 | int rc = SQLITE_OK; |
| 6739 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6740 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6741 | return SQLITE_BUSY; |
| 6742 | } |
| 6743 | |
| 6744 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 6745 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 6746 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 6747 | return SQLITE_OK; |
| 6748 | }else{ |
| 6749 | unixFile *lockProxy = pCtx->lockProxy; |
| 6750 | pCtx->lockProxy=NULL; |
| 6751 | pCtx->conchHeld = 0; |
| 6752 | if( lockProxy!=NULL ){ |
| 6753 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 6754 | if( rc ) return rc; |
| 6755 | sqlite3_free(lockProxy); |
| 6756 | } |
| 6757 | sqlite3_free(oldPath); |
| 6758 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 6759 | } |
| 6760 | |
| 6761 | return rc; |
| 6762 | } |
| 6763 | |
| 6764 | /* |
| 6765 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 6766 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 6767 | ** |
| 6768 | ** This routine find the filename associated with pFile and writes it |
| 6769 | ** int dbPath. |
| 6770 | */ |
| 6771 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6772 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6773 | if( pFile->pMethod == &afpIoMethods ){ |
| 6774 | /* afp style keeps a reference to the db path in the filePath field |
| 6775 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6776 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6777 | strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN); |
| 6778 | } else |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6779 | #endif |
| 6780 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 6781 | /* dot lock style uses the locking context to store the dot lock |
| 6782 | ** file path */ |
| 6783 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 6784 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 6785 | }else{ |
| 6786 | /* all other styles use the locking context to store the db file path */ |
| 6787 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6788 | strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6789 | } |
| 6790 | return SQLITE_OK; |
| 6791 | } |
| 6792 | |
| 6793 | /* |
| 6794 | ** Takes an already filled in unix file and alters it so all file locking |
| 6795 | ** will be performed on the local proxy lock file. The following fields |
| 6796 | ** are preserved in the locking context so that they can be restored and |
| 6797 | ** the unix structure properly cleaned up at close time: |
| 6798 | ** ->lockingContext |
| 6799 | ** ->pMethod |
| 6800 | */ |
| 6801 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 6802 | proxyLockingContext *pCtx; |
| 6803 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 6804 | char *lockPath=NULL; |
| 6805 | int rc = SQLITE_OK; |
| 6806 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6807 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6808 | return SQLITE_BUSY; |
| 6809 | } |
| 6810 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 6811 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 6812 | lockPath=NULL; |
| 6813 | }else{ |
| 6814 | lockPath=(char *)path; |
| 6815 | } |
| 6816 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6817 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
| 6818 | (lockPath ? lockPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6819 | |
| 6820 | pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 6821 | if( pCtx==0 ){ |
| 6822 | return SQLITE_NOMEM; |
| 6823 | } |
| 6824 | memset(pCtx, 0, sizeof(*pCtx)); |
| 6825 | |
| 6826 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 6827 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6828 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 6829 | if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 6830 | /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 6831 | ** (c) the file system is read-only, then enable no-locking access. |
| 6832 | ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 6833 | ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 6834 | */ |
| 6835 | struct statfs fsInfo; |
| 6836 | struct stat conchInfo; |
| 6837 | int goLockless = 0; |
| 6838 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6839 | if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6840 | int err = errno; |
| 6841 | if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 6842 | goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 6843 | } |
| 6844 | } |
| 6845 | if( goLockless ){ |
| 6846 | pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 6847 | rc = SQLITE_OK; |
| 6848 | } |
| 6849 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6850 | } |
| 6851 | if( rc==SQLITE_OK && lockPath ){ |
| 6852 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 6853 | } |
| 6854 | |
| 6855 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6856 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 6857 | if( pCtx->dbPath==NULL ){ |
| 6858 | rc = SQLITE_NOMEM; |
| 6859 | } |
| 6860 | } |
| 6861 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6862 | /* all memory is allocated, proxys are created and assigned, |
| 6863 | ** switch the locking context and pMethod then return. |
| 6864 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6865 | pCtx->oldLockingContext = pFile->lockingContext; |
| 6866 | pFile->lockingContext = pCtx; |
| 6867 | pCtx->pOldMethod = pFile->pMethod; |
| 6868 | pFile->pMethod = &proxyIoMethods; |
| 6869 | }else{ |
| 6870 | if( pCtx->conchFile ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6871 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6872 | sqlite3_free(pCtx->conchFile); |
| 6873 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 6874 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6875 | sqlite3_free(pCtx->conchFilePath); |
| 6876 | sqlite3_free(pCtx); |
| 6877 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6878 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 6879 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6880 | return rc; |
| 6881 | } |
| 6882 | |
| 6883 | |
| 6884 | /* |
| 6885 | ** This routine handles sqlite3_file_control() calls that are specific |
| 6886 | ** to proxy locking. |
| 6887 | */ |
| 6888 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 6889 | switch( op ){ |
| 6890 | case SQLITE_GET_LOCKPROXYFILE: { |
| 6891 | unixFile *pFile = (unixFile*)id; |
| 6892 | if( pFile->pMethod == &proxyIoMethods ){ |
| 6893 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 6894 | proxyTakeConch(pFile); |
| 6895 | if( pCtx->lockProxyPath ){ |
| 6896 | *(const char **)pArg = pCtx->lockProxyPath; |
| 6897 | }else{ |
| 6898 | *(const char **)pArg = ":auto: (not held)"; |
| 6899 | } |
| 6900 | } else { |
| 6901 | *(const char **)pArg = NULL; |
| 6902 | } |
| 6903 | return SQLITE_OK; |
| 6904 | } |
| 6905 | case SQLITE_SET_LOCKPROXYFILE: { |
| 6906 | unixFile *pFile = (unixFile*)id; |
| 6907 | int rc = SQLITE_OK; |
| 6908 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 6909 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 6910 | if( isProxyStyle ){ |
| 6911 | /* turn off proxy locking - not supported */ |
| 6912 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 6913 | }else{ |
| 6914 | /* turn off proxy locking - already off - NOOP */ |
| 6915 | rc = SQLITE_OK; |
| 6916 | } |
| 6917 | }else{ |
| 6918 | const char *proxyPath = (const char *)pArg; |
| 6919 | if( isProxyStyle ){ |
| 6920 | proxyLockingContext *pCtx = |
| 6921 | (proxyLockingContext*)pFile->lockingContext; |
| 6922 | if( !strcmp(pArg, ":auto:") |
| 6923 | || (pCtx->lockProxyPath && |
| 6924 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 6925 | ){ |
| 6926 | rc = SQLITE_OK; |
| 6927 | }else{ |
| 6928 | rc = switchLockProxyPath(pFile, proxyPath); |
| 6929 | } |
| 6930 | }else{ |
| 6931 | /* turn on proxy file locking */ |
| 6932 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 6933 | } |
| 6934 | } |
| 6935 | return rc; |
| 6936 | } |
| 6937 | default: { |
| 6938 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 6939 | } |
| 6940 | } |
| 6941 | /*NOTREACHED*/ |
| 6942 | return SQLITE_ERROR; |
| 6943 | } |
| 6944 | |
| 6945 | /* |
| 6946 | ** Within this division (the proxying locking implementation) the procedures |
| 6947 | ** above this point are all utilities. The lock-related methods of the |
| 6948 | ** proxy-locking sqlite3_io_method object follow. |
| 6949 | */ |
| 6950 | |
| 6951 | |
| 6952 | /* |
| 6953 | ** This routine checks if there is a RESERVED lock held on the specified |
| 6954 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 6955 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 6956 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 6957 | */ |
| 6958 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 6959 | unixFile *pFile = (unixFile*)id; |
| 6960 | int rc = proxyTakeConch(pFile); |
| 6961 | if( rc==SQLITE_OK ){ |
| 6962 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6963 | if( pCtx->conchHeld>0 ){ |
| 6964 | unixFile *proxy = pCtx->lockProxy; |
| 6965 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 6966 | }else{ /* conchHeld < 0 is lockless */ |
| 6967 | pResOut=0; |
| 6968 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6969 | } |
| 6970 | return rc; |
| 6971 | } |
| 6972 | |
| 6973 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6974 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6975 | ** of the following: |
| 6976 | ** |
| 6977 | ** (1) SHARED_LOCK |
| 6978 | ** (2) RESERVED_LOCK |
| 6979 | ** (3) PENDING_LOCK |
| 6980 | ** (4) EXCLUSIVE_LOCK |
| 6981 | ** |
| 6982 | ** Sometimes when requesting one lock state, additional lock states |
| 6983 | ** are inserted in between. The locking might fail on one of the later |
| 6984 | ** transitions leaving the lock state different from what it started but |
| 6985 | ** still short of its goal. The following chart shows the allowed |
| 6986 | ** transitions and the inserted intermediate states: |
| 6987 | ** |
| 6988 | ** UNLOCKED -> SHARED |
| 6989 | ** SHARED -> RESERVED |
| 6990 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 6991 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 6992 | ** PENDING -> EXCLUSIVE |
| 6993 | ** |
| 6994 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 6995 | ** routine to lower a locking level. |
| 6996 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6997 | static int proxyLock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6998 | unixFile *pFile = (unixFile*)id; |
| 6999 | int rc = proxyTakeConch(pFile); |
| 7000 | if( rc==SQLITE_OK ){ |
| 7001 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7002 | if( pCtx->conchHeld>0 ){ |
| 7003 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7004 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); |
| 7005 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7006 | }else{ |
| 7007 | /* conchHeld < 0 is lockless */ |
| 7008 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7009 | } |
| 7010 | return rc; |
| 7011 | } |
| 7012 | |
| 7013 | |
| 7014 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7015 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7016 | ** must be either NO_LOCK or SHARED_LOCK. |
| 7017 | ** |
| 7018 | ** If the locking level of the file descriptor is already at or below |
| 7019 | ** the requested locking level, this routine is a no-op. |
| 7020 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7021 | static int proxyUnlock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7022 | unixFile *pFile = (unixFile*)id; |
| 7023 | int rc = proxyTakeConch(pFile); |
| 7024 | if( rc==SQLITE_OK ){ |
| 7025 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7026 | if( pCtx->conchHeld>0 ){ |
| 7027 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7028 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); |
| 7029 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7030 | }else{ |
| 7031 | /* conchHeld < 0 is lockless */ |
| 7032 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7033 | } |
| 7034 | return rc; |
| 7035 | } |
| 7036 | |
| 7037 | /* |
| 7038 | ** Close a file that uses proxy locks. |
| 7039 | */ |
| 7040 | static int proxyClose(sqlite3_file *id) { |
| 7041 | if( id ){ |
| 7042 | unixFile *pFile = (unixFile*)id; |
| 7043 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7044 | unixFile *lockProxy = pCtx->lockProxy; |
| 7045 | unixFile *conchFile = pCtx->conchFile; |
| 7046 | int rc = SQLITE_OK; |
| 7047 | |
| 7048 | if( lockProxy ){ |
| 7049 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 7050 | if( rc ) return rc; |
| 7051 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 7052 | if( rc ) return rc; |
| 7053 | sqlite3_free(lockProxy); |
| 7054 | pCtx->lockProxy = 0; |
| 7055 | } |
| 7056 | if( conchFile ){ |
| 7057 | if( pCtx->conchHeld ){ |
| 7058 | rc = proxyReleaseConch(pFile); |
| 7059 | if( rc ) return rc; |
| 7060 | } |
| 7061 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 7062 | if( rc ) return rc; |
| 7063 | sqlite3_free(conchFile); |
| 7064 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7065 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7066 | sqlite3_free(pCtx->conchFilePath); |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7067 | sqlite3DbFree(0, pCtx->dbPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7068 | /* restore the original locking context and pMethod then close it */ |
| 7069 | pFile->lockingContext = pCtx->oldLockingContext; |
| 7070 | pFile->pMethod = pCtx->pOldMethod; |
| 7071 | sqlite3_free(pCtx); |
| 7072 | return pFile->pMethod->xClose(id); |
| 7073 | } |
| 7074 | return SQLITE_OK; |
| 7075 | } |
| 7076 | |
| 7077 | |
| 7078 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7079 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7080 | /* |
| 7081 | ** The proxy locking style is intended for use with AFP filesystems. |
| 7082 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 7083 | ** restricted to MacOSX. |
| 7084 | ** |
| 7085 | ** |
| 7086 | ******************* End of the proxy lock implementation ********************** |
| 7087 | ******************************************************************************/ |
| 7088 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7089 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7090 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7091 | ** |
| 7092 | ** This routine registers all VFS implementations for unix-like operating |
| 7093 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 7094 | ** should be the only routines in this file that are visible from other |
| 7095 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7096 | ** |
| 7097 | ** This routine is called once during SQLite initialization and by a |
| 7098 | ** single thread. The memory allocation and mutex subsystems have not |
| 7099 | ** necessarily been initialized when this routine is called, and so they |
| 7100 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7101 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7102 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7103 | /* |
| 7104 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7105 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 7106 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 7107 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 7108 | ** and so we have to go through the intermediate pointer to avoid problems |
| 7109 | ** when compiling with -pedantic-errors on GCC.) |
| 7110 | ** |
| 7111 | ** 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] | 7112 | ** finder-function. The finder-function returns a pointer to the |
| 7113 | ** sqlite_io_methods object that implements the desired locking |
| 7114 | ** behaviors. See the division above that contains the IOMETHODS |
| 7115 | ** macro for addition information on finder-functions. |
| 7116 | ** |
| 7117 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 7118 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 7119 | ** more than that; it looks at the filesystem type that hosts the |
| 7120 | ** database file and tries to choose an locking method appropriate for |
| 7121 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7122 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7123 | #define UNIXVFS(VFSNAME, FINDER) { \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7124 | 3, /* iVersion */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7125 | sizeof(unixFile), /* szOsFile */ \ |
| 7126 | MAX_PATHNAME, /* mxPathname */ \ |
| 7127 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7128 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7129 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7130 | unixOpen, /* xOpen */ \ |
| 7131 | unixDelete, /* xDelete */ \ |
| 7132 | unixAccess, /* xAccess */ \ |
| 7133 | unixFullPathname, /* xFullPathname */ \ |
| 7134 | unixDlOpen, /* xDlOpen */ \ |
| 7135 | unixDlError, /* xDlError */ \ |
| 7136 | unixDlSym, /* xDlSym */ \ |
| 7137 | unixDlClose, /* xDlClose */ \ |
| 7138 | unixRandomness, /* xRandomness */ \ |
| 7139 | unixSleep, /* xSleep */ \ |
| 7140 | unixCurrentTime, /* xCurrentTime */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 7141 | unixGetLastError, /* xGetLastError */ \ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 7142 | unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7143 | unixSetSystemCall, /* xSetSystemCall */ \ |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 7144 | unixGetSystemCall, /* xGetSystemCall */ \ |
| 7145 | unixNextSystemCall, /* xNextSystemCall */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7146 | } |
| 7147 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7148 | /* |
| 7149 | ** All default VFSes for unix are contained in the following array. |
| 7150 | ** |
| 7151 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 7152 | ** by the SQLite core when the VFS is registered. So the following |
| 7153 | ** array cannot be const. |
| 7154 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7155 | static sqlite3_vfs aVfs[] = { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 7156 | #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__)) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7157 | UNIXVFS("unix", autolockIoFinder ), |
| 7158 | #else |
| 7159 | UNIXVFS("unix", posixIoFinder ), |
| 7160 | #endif |
| 7161 | UNIXVFS("unix-none", nolockIoFinder ), |
| 7162 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7163 | UNIXVFS("unix-excl", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7164 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7165 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7166 | #endif |
| 7167 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7168 | UNIXVFS("unix-posix", posixIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 7169 | #if !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7170 | UNIXVFS("unix-flock", flockIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7171 | #endif |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 7172 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7173 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7174 | UNIXVFS("unix-afp", afpIoFinder ), |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7175 | UNIXVFS("unix-nfs", nfsIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7176 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7177 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7178 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7179 | unsigned int i; /* Loop counter */ |
| 7180 | |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 7181 | /* Double-check that the aSyscall[] array has been constructed |
| 7182 | ** correctly. See ticket [bb3a86e890c8e96ab] */ |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 7183 | assert( ArraySize(aSyscall)==21 ); |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 7184 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7185 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7186 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7187 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7188 | } |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7189 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7190 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7191 | |
| 7192 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7193 | ** Shutdown the operating system interface. |
| 7194 | ** |
| 7195 | ** Some operating systems might need to do some cleanup in this routine, |
| 7196 | ** to release dynamically allocated objects. But not on unix. |
| 7197 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7198 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7199 | int sqlite3_os_end(void){ |
| 7200 | return SQLITE_OK; |
| 7201 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 7202 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 7203 | #endif /* SQLITE_OS_UNIX */ |