drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2004 May 22 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 13 | ** This file contains the VFS implementation for unix-like operating systems |
| 14 | ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others. |
danielk1977 | 822a516 | 2008-05-16 04:51:54 +0000 | [diff] [blame] | 15 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 16 | ** There are actually several different VFS implementations in this file. |
| 17 | ** The differences are in the way that file locking is done. The default |
| 18 | ** implementation uses Posix Advisory Locks. Alternative implementations |
| 19 | ** use flock(), dot-files, various proprietary locking schemas, or simply |
| 20 | ** skip locking all together. |
| 21 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 22 | ** This source file is organized into divisions where the logic for various |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 23 | ** subfunctions is contained within the appropriate division. PLEASE |
| 24 | ** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed |
| 25 | ** in the correct division and should be clearly labeled. |
| 26 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 27 | ** The layout of divisions is as follows: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 28 | ** |
| 29 | ** * General-purpose declarations and utility functions. |
| 30 | ** * Unique file ID logic used by VxWorks. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 31 | ** * Various locking primitive implementations (all except proxy locking): |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 32 | ** + for Posix Advisory Locks |
| 33 | ** + for no-op locks |
| 34 | ** + for dot-file locks |
| 35 | ** + for flock() locking |
| 36 | ** + for named semaphore locks (VxWorks only) |
| 37 | ** + for AFP filesystem locks (MacOSX only) |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 38 | ** * sqlite3_file methods not associated with locking. |
| 39 | ** * Definitions of sqlite3_io_methods objects for all locking |
| 40 | ** methods plus "finder" functions for each locking method. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 41 | ** * sqlite3_vfs method implementations. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 42 | ** * Locking primitives for the proxy uber-locking-method. (MacOSX only) |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 43 | ** * Definitions of sqlite3_vfs objects for all locking methods |
| 44 | ** plus implementations of sqlite3_os_init() and sqlite3_os_end(). |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 45 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 46 | #include "sqliteInt.h" |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 47 | #if SQLITE_OS_UNIX /* This file is used on unix only */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 48 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 49 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 50 | ** There are various methods for file locking used for concurrency |
| 51 | ** control: |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 52 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 53 | ** 1. POSIX locking (the default), |
| 54 | ** 2. No locking, |
| 55 | ** 3. Dot-file locking, |
| 56 | ** 4. flock() locking, |
| 57 | ** 5. AFP locking (OSX only), |
| 58 | ** 6. Named POSIX semaphores (VXWorks only), |
| 59 | ** 7. proxy locking. (OSX only) |
| 60 | ** |
| 61 | ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE |
| 62 | ** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic |
| 63 | ** selection of the appropriate locking style based on the filesystem |
| 64 | ** where the database is located. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 65 | */ |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 66 | #if !defined(SQLITE_ENABLE_LOCKING_STYLE) |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 67 | # if defined(__APPLE__) |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 68 | # define SQLITE_ENABLE_LOCKING_STYLE 1 |
| 69 | # else |
| 70 | # define SQLITE_ENABLE_LOCKING_STYLE 0 |
| 71 | # endif |
| 72 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 73 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 74 | /* |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 75 | ** Define the OS_VXWORKS pre-processor macro to 1 if building on |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 76 | ** vxworks, or 0 otherwise. |
| 77 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 78 | #ifndef OS_VXWORKS |
| 79 | # if defined(__RTP__) || defined(_WRS_KERNEL) |
| 80 | # define OS_VXWORKS 1 |
| 81 | # else |
| 82 | # define OS_VXWORKS 0 |
| 83 | # endif |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 84 | #endif |
| 85 | |
| 86 | /* |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 87 | ** These #defines should enable >2GB file support on Posix if the |
| 88 | ** underlying operating system supports it. If the OS lacks |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 89 | ** large file support, these should be no-ops. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 90 | ** |
| 91 | ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch |
| 92 | ** on the compiler command line. This is necessary if you are compiling |
| 93 | ** on a recent machine (ex: RedHat 7.2) but you want your code to work |
| 94 | ** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2 |
| 95 | ** without this option, LFS is enable. But LFS does not exist in the kernel |
| 96 | ** in RedHat 6.0, so the code won't work. Hence, for maximum binary |
| 97 | ** portability you should omit LFS. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 98 | ** |
| 99 | ** The previous paragraph was written in 2005. (This paragraph is written |
| 100 | ** on 2008-11-28.) These days, all Linux kernels support large files, so |
| 101 | ** you should probably leave LFS enabled. But some embedded platforms might |
| 102 | ** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 103 | */ |
| 104 | #ifndef SQLITE_DISABLE_LFS |
| 105 | # define _LARGE_FILE 1 |
| 106 | # ifndef _FILE_OFFSET_BITS |
| 107 | # define _FILE_OFFSET_BITS 64 |
| 108 | # endif |
| 109 | # define _LARGEFILE_SOURCE 1 |
| 110 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 111 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 112 | /* |
| 113 | ** standard include files. |
| 114 | */ |
| 115 | #include <sys/types.h> |
| 116 | #include <sys/stat.h> |
| 117 | #include <fcntl.h> |
| 118 | #include <unistd.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 119 | #include <time.h> |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 120 | #include <sys/time.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 121 | #include <errno.h> |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 122 | #include <sys/mman.h> |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 123 | |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 124 | #if SQLITE_ENABLE_LOCKING_STYLE |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 125 | # include <sys/ioctl.h> |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 126 | # if OS_VXWORKS |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 127 | # include <semaphore.h> |
| 128 | # include <limits.h> |
| 129 | # else |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 130 | # include <sys/file.h> |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 131 | # include <sys/param.h> |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 132 | # endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 133 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 134 | |
drh | f8b4d8c | 2010-03-05 13:53:22 +0000 | [diff] [blame] | 135 | #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS) |
drh | 84a2bf6 | 2010-03-05 13:41:06 +0000 | [diff] [blame] | 136 | # include <sys/mount.h> |
| 137 | #endif |
| 138 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 139 | /* |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 140 | ** Allowed values of unixFile.fsFlags |
| 141 | */ |
| 142 | #define SQLITE_FSFLAGS_IS_MSDOS 0x1 |
| 143 | |
| 144 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 145 | ** If we are to be thread-safe, include the pthreads header and define |
| 146 | ** the SQLITE_UNIX_THREADS macro. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 147 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 148 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 149 | # include <pthread.h> |
| 150 | # define SQLITE_UNIX_THREADS 1 |
| 151 | #endif |
| 152 | |
| 153 | /* |
| 154 | ** Default permissions when creating a new file |
| 155 | */ |
| 156 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 157 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 158 | #endif |
| 159 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 160 | /* |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 161 | ** Default permissions when creating auto proxy dir |
| 162 | */ |
| 163 | #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 164 | # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 |
| 165 | #endif |
| 166 | |
| 167 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 168 | ** Maximum supported path-length. |
| 169 | */ |
| 170 | #define MAX_PATHNAME 512 |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 171 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 172 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 173 | ** Only set the lastErrno if the error code is a real error and not |
| 174 | ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 175 | */ |
| 176 | #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |
| 177 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 178 | |
| 179 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 180 | ** Sometimes, after a file handle is closed by SQLite, the file descriptor |
| 181 | ** cannot be closed immediately. In these cases, instances of the following |
| 182 | ** structure are used to store the file descriptor while waiting for an |
| 183 | ** opportunity to either close or reuse it. |
| 184 | */ |
| 185 | typedef struct UnixUnusedFd UnixUnusedFd; |
| 186 | struct UnixUnusedFd { |
| 187 | int fd; /* File descriptor to close */ |
| 188 | int flags; /* Flags this file descriptor was opened with */ |
| 189 | UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ |
| 190 | }; |
| 191 | |
| 192 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 193 | ** The unixFile structure is subclass of sqlite3_file specific to the unix |
| 194 | ** VFS implementations. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 195 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 196 | typedef struct unixFile unixFile; |
| 197 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 198 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 199 | struct unixOpenCnt *pOpen; /* Info about all open fd's on this inode */ |
| 200 | struct unixLockInfo *pLock; /* Info about locks on this inode */ |
| 201 | int h; /* The file descriptor */ |
| 202 | int dirfd; /* File descriptor for the directory */ |
| 203 | unsigned char locktype; /* The type of lock held on this fd */ |
| 204 | int lastErrno; /* The unix errno from the last I/O error */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 205 | void *lockingContext; /* Locking style specific state */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 206 | UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 207 | int fileFlags; /* Miscellanous flags */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 208 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 209 | int openFlags; /* The flags specified at open() */ |
| 210 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 211 | #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
| 212 | unsigned fsFlags; /* cached details from statfs() */ |
| 213 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 214 | #if SQLITE_THREADSAFE && defined(__linux__) |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 215 | pthread_t tid; /* The thread that "owns" this unixFile */ |
| 216 | #endif |
| 217 | #if OS_VXWORKS |
| 218 | int isDelete; /* Delete on close if true */ |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 219 | struct vxworksFileId *pId; /* Unique file ID */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 220 | #endif |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 221 | #ifndef NDEBUG |
| 222 | /* The next group of variables are used to track whether or not the |
| 223 | ** transaction counter in bytes 24-27 of database files are updated |
| 224 | ** whenever any part of the database changes. An assertion fault will |
| 225 | ** occur if a file is updated without also updating the transaction |
| 226 | ** counter. This test is made to avoid new problems similar to the |
| 227 | ** one described by ticket #3584. |
| 228 | */ |
| 229 | unsigned char transCntrChng; /* True if the transaction counter changed */ |
| 230 | unsigned char dbUpdate; /* True if any part of database file changed */ |
| 231 | unsigned char inNormalWrite; /* True if in a normal write operation */ |
| 232 | #endif |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 233 | #ifdef SQLITE_TEST |
| 234 | /* In test mode, increase the size of this structure a bit so that |
| 235 | ** it is larger than the struct CrashFile defined in test6.c. |
| 236 | */ |
| 237 | char aPadding[32]; |
| 238 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 239 | }; |
| 240 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 241 | /* |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 242 | ** The following macros define bits in unixFile.fileFlags |
| 243 | */ |
| 244 | #define SQLITE_WHOLE_FILE_LOCKING 0x0001 /* Use whole-file locking */ |
| 245 | |
| 246 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 247 | ** Include code that is common to all os_*.c files |
| 248 | */ |
| 249 | #include "os_common.h" |
| 250 | |
| 251 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 252 | ** Define various macros that are missing from some systems. |
| 253 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 254 | #ifndef O_LARGEFILE |
| 255 | # define O_LARGEFILE 0 |
| 256 | #endif |
| 257 | #ifdef SQLITE_DISABLE_LFS |
| 258 | # undef O_LARGEFILE |
| 259 | # define O_LARGEFILE 0 |
| 260 | #endif |
| 261 | #ifndef O_NOFOLLOW |
| 262 | # define O_NOFOLLOW 0 |
| 263 | #endif |
| 264 | #ifndef O_BINARY |
| 265 | # define O_BINARY 0 |
| 266 | #endif |
| 267 | |
| 268 | /* |
| 269 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 270 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 271 | ** that always succeeds. This means that locking does not occur under |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 272 | ** DJGPP. But it is DOS - what did you expect? |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 273 | */ |
| 274 | #ifdef __DJGPP__ |
| 275 | # define fcntl(A,B,C) 0 |
| 276 | #endif |
| 277 | |
| 278 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 279 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 280 | ** testing and debugging only. |
| 281 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 282 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 283 | #define threadid pthread_self() |
| 284 | #else |
| 285 | #define threadid 0 |
| 286 | #endif |
| 287 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 288 | |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 289 | /* |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 290 | ** Helper functions to obtain and relinquish the global mutex. The |
| 291 | ** global mutex is used to protect the unixOpenCnt, unixLockInfo and |
| 292 | ** vxworksFileId objects used by this file, all of which may be |
| 293 | ** shared by multiple threads. |
| 294 | ** |
| 295 | ** Function unixMutexHeld() is used to assert() that the global mutex |
| 296 | ** is held when required. This function is only used as part of assert() |
| 297 | ** statements. e.g. |
| 298 | ** |
| 299 | ** unixEnterMutex() |
| 300 | ** assert( unixMutexHeld() ); |
| 301 | ** unixEnterLeave() |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 302 | */ |
| 303 | static void unixEnterMutex(void){ |
| 304 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 305 | } |
| 306 | static void unixLeaveMutex(void){ |
| 307 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 308 | } |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 309 | #ifdef SQLITE_DEBUG |
| 310 | static int unixMutexHeld(void) { |
| 311 | return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 312 | } |
| 313 | #endif |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 314 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 315 | |
| 316 | #ifdef SQLITE_DEBUG |
| 317 | /* |
| 318 | ** Helper function for printing out trace information from debugging |
| 319 | ** binaries. This returns the string represetation of the supplied |
| 320 | ** integer lock-type. |
| 321 | */ |
| 322 | static const char *locktypeName(int locktype){ |
| 323 | switch( locktype ){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 324 | case NO_LOCK: return "NONE"; |
| 325 | case SHARED_LOCK: return "SHARED"; |
| 326 | case RESERVED_LOCK: return "RESERVED"; |
| 327 | case PENDING_LOCK: return "PENDING"; |
| 328 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 329 | } |
| 330 | return "ERROR"; |
| 331 | } |
| 332 | #endif |
| 333 | |
| 334 | #ifdef SQLITE_LOCK_TRACE |
| 335 | /* |
| 336 | ** Print out information about all locking operations. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 337 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 338 | ** This routine is used for troubleshooting locks on multithreaded |
| 339 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 340 | ** command-line option on the compiler. This code is normally |
| 341 | ** turned off. |
| 342 | */ |
| 343 | static int lockTrace(int fd, int op, struct flock *p){ |
| 344 | char *zOpName, *zType; |
| 345 | int s; |
| 346 | int savedErrno; |
| 347 | if( op==F_GETLK ){ |
| 348 | zOpName = "GETLK"; |
| 349 | }else if( op==F_SETLK ){ |
| 350 | zOpName = "SETLK"; |
| 351 | }else{ |
| 352 | s = fcntl(fd, op, p); |
| 353 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 354 | return s; |
| 355 | } |
| 356 | if( p->l_type==F_RDLCK ){ |
| 357 | zType = "RDLCK"; |
| 358 | }else if( p->l_type==F_WRLCK ){ |
| 359 | zType = "WRLCK"; |
| 360 | }else if( p->l_type==F_UNLCK ){ |
| 361 | zType = "UNLCK"; |
| 362 | }else{ |
| 363 | assert( 0 ); |
| 364 | } |
| 365 | assert( p->l_whence==SEEK_SET ); |
| 366 | s = fcntl(fd, op, p); |
| 367 | savedErrno = errno; |
| 368 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 369 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 370 | (int)p->l_pid, s); |
| 371 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 372 | struct flock l2; |
| 373 | l2 = *p; |
| 374 | fcntl(fd, F_GETLK, &l2); |
| 375 | if( l2.l_type==F_RDLCK ){ |
| 376 | zType = "RDLCK"; |
| 377 | }else if( l2.l_type==F_WRLCK ){ |
| 378 | zType = "WRLCK"; |
| 379 | }else if( l2.l_type==F_UNLCK ){ |
| 380 | zType = "UNLCK"; |
| 381 | }else{ |
| 382 | assert( 0 ); |
| 383 | } |
| 384 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 385 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 386 | } |
| 387 | errno = savedErrno; |
| 388 | return s; |
| 389 | } |
| 390 | #define fcntl lockTrace |
| 391 | #endif /* SQLITE_LOCK_TRACE */ |
| 392 | |
| 393 | |
| 394 | |
| 395 | /* |
| 396 | ** This routine translates a standard POSIX errno code into something |
| 397 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 398 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 399 | ** and a variety of "please close the file descriptor NOW" errors into |
| 400 | ** SQLITE_IOERR |
| 401 | ** |
| 402 | ** Errors during initialization of locks, or file system support for locks, |
| 403 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 404 | */ |
| 405 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
| 406 | switch (posixError) { |
| 407 | case 0: |
| 408 | return SQLITE_OK; |
| 409 | |
| 410 | case EAGAIN: |
| 411 | case ETIMEDOUT: |
| 412 | case EBUSY: |
| 413 | case EINTR: |
| 414 | case ENOLCK: |
| 415 | /* random NFS retry error, unless during file system support |
| 416 | * introspection, in which it actually means what it says */ |
| 417 | return SQLITE_BUSY; |
| 418 | |
| 419 | case EACCES: |
| 420 | /* EACCES is like EAGAIN during locking operations, but not any other time*/ |
| 421 | if( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
| 422 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 423 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 424 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){ |
| 425 | return SQLITE_BUSY; |
| 426 | } |
| 427 | /* else fall through */ |
| 428 | case EPERM: |
| 429 | return SQLITE_PERM; |
| 430 | |
| 431 | case EDEADLK: |
| 432 | return SQLITE_IOERR_BLOCKED; |
| 433 | |
| 434 | #if EOPNOTSUPP!=ENOTSUP |
| 435 | case EOPNOTSUPP: |
| 436 | /* something went terribly awry, unless during file system support |
| 437 | * introspection, in which it actually means what it says */ |
| 438 | #endif |
| 439 | #ifdef ENOTSUP |
| 440 | case ENOTSUP: |
| 441 | /* invalid fd, unless during file system support introspection, in which |
| 442 | * it actually means what it says */ |
| 443 | #endif |
| 444 | case EIO: |
| 445 | case EBADF: |
| 446 | case EINVAL: |
| 447 | case ENOTCONN: |
| 448 | case ENODEV: |
| 449 | case ENXIO: |
| 450 | case ENOENT: |
| 451 | case ESTALE: |
| 452 | case ENOSYS: |
| 453 | /* these should force the client to close the file and reconnect */ |
| 454 | |
| 455 | default: |
| 456 | return sqliteIOErr; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | |
| 461 | |
| 462 | /****************************************************************************** |
| 463 | ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 464 | ** |
| 465 | ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 466 | ** the device number and the inode number. But this does not work on VxWorks. |
| 467 | ** On VxWorks, a unique file id must be based on the canonical filename. |
| 468 | ** |
| 469 | ** A pointer to an instance of the following structure can be used as a |
| 470 | ** unique file ID in VxWorks. Each instance of this structure contains |
| 471 | ** a copy of the canonical filename. There is also a reference count. |
| 472 | ** The structure is reclaimed when the number of pointers to it drops to |
| 473 | ** zero. |
| 474 | ** |
| 475 | ** There are never very many files open at one time and lookups are not |
| 476 | ** a performance-critical path, so it is sufficient to put these |
| 477 | ** structures on a linked list. |
| 478 | */ |
| 479 | struct vxworksFileId { |
| 480 | struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 481 | int nRef; /* Number of references to this one */ |
| 482 | int nName; /* Length of the zCanonicalName[] string */ |
| 483 | char *zCanonicalName; /* Canonical filename */ |
| 484 | }; |
| 485 | |
| 486 | #if OS_VXWORKS |
| 487 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 488 | ** All unique filenames are held on a linked list headed by this |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 489 | ** variable: |
| 490 | */ |
| 491 | static struct vxworksFileId *vxworksFileList = 0; |
| 492 | |
| 493 | /* |
| 494 | ** Simplify a filename into its canonical form |
| 495 | ** by making the following changes: |
| 496 | ** |
| 497 | ** * removing any trailing and duplicate / |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 498 | ** * convert /./ into just / |
| 499 | ** * convert /A/../ where A is any simple name into just / |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 500 | ** |
| 501 | ** Changes are made in-place. Return the new name length. |
| 502 | ** |
| 503 | ** The original filename is in z[0..n-1]. Return the number of |
| 504 | ** characters in the simplified name. |
| 505 | */ |
| 506 | static int vxworksSimplifyName(char *z, int n){ |
| 507 | int i, j; |
| 508 | while( n>1 && z[n-1]=='/' ){ n--; } |
| 509 | for(i=j=0; i<n; i++){ |
| 510 | if( z[i]=='/' ){ |
| 511 | if( z[i+1]=='/' ) continue; |
| 512 | if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 513 | i += 1; |
| 514 | continue; |
| 515 | } |
| 516 | if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 517 | while( j>0 && z[j-1]!='/' ){ j--; } |
| 518 | if( j>0 ){ j--; } |
| 519 | i += 2; |
| 520 | continue; |
| 521 | } |
| 522 | } |
| 523 | z[j++] = z[i]; |
| 524 | } |
| 525 | z[j] = 0; |
| 526 | return j; |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | ** Find a unique file ID for the given absolute pathname. Return |
| 531 | ** a pointer to the vxworksFileId object. This pointer is the unique |
| 532 | ** file ID. |
| 533 | ** |
| 534 | ** The nRef field of the vxworksFileId object is incremented before |
| 535 | ** the object is returned. A new vxworksFileId object is created |
| 536 | ** and added to the global list if necessary. |
| 537 | ** |
| 538 | ** If a memory allocation error occurs, return NULL. |
| 539 | */ |
| 540 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 541 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 542 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 543 | int n; /* Length of zAbsoluteName string */ |
| 544 | |
| 545 | assert( zAbsoluteName[0]=='/' ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 546 | n = (int)strlen(zAbsoluteName); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 547 | pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) ); |
| 548 | if( pNew==0 ) return 0; |
| 549 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 550 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 551 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 552 | |
| 553 | /* Search for an existing entry that matching the canonical name. |
| 554 | ** If found, increment the reference count and return a pointer to |
| 555 | ** the existing file ID. |
| 556 | */ |
| 557 | unixEnterMutex(); |
| 558 | for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 559 | if( pCandidate->nName==n |
| 560 | && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 561 | ){ |
| 562 | sqlite3_free(pNew); |
| 563 | pCandidate->nRef++; |
| 564 | unixLeaveMutex(); |
| 565 | return pCandidate; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /* No match was found. We will make a new file ID */ |
| 570 | pNew->nRef = 1; |
| 571 | pNew->nName = n; |
| 572 | pNew->pNext = vxworksFileList; |
| 573 | vxworksFileList = pNew; |
| 574 | unixLeaveMutex(); |
| 575 | return pNew; |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | ** Decrement the reference count on a vxworksFileId object. Free |
| 580 | ** the object when the reference count reaches zero. |
| 581 | */ |
| 582 | static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 583 | unixEnterMutex(); |
| 584 | assert( pId->nRef>0 ); |
| 585 | pId->nRef--; |
| 586 | if( pId->nRef==0 ){ |
| 587 | struct vxworksFileId **pp; |
| 588 | for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 589 | assert( *pp==pId ); |
| 590 | *pp = pId->pNext; |
| 591 | sqlite3_free(pId); |
| 592 | } |
| 593 | unixLeaveMutex(); |
| 594 | } |
| 595 | #endif /* OS_VXWORKS */ |
| 596 | /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 597 | ******************************************************************************/ |
| 598 | |
| 599 | |
| 600 | /****************************************************************************** |
| 601 | *************************** Posix Advisory Locking **************************** |
| 602 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 603 | ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 604 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 605 | ** sets or clears a lock, that operation overrides any prior locks set |
| 606 | ** by the same process. It does not explicitly say so, but this implies |
| 607 | ** that it overrides locks set by the same process using a different |
| 608 | ** file descriptor. Consider this test case: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 609 | ** |
| 610 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 611 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 612 | ** |
| 613 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 614 | ** one is a hard or symbolic link to the other) then if you set |
| 615 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 616 | ** on fd2, it works. I would have expected the second lock to |
| 617 | ** fail since there was already a lock on the file due to fd1. |
| 618 | ** But not so. Since both locks came from the same process, the |
| 619 | ** second overrides the first, even though they were on different |
| 620 | ** file descriptors opened on different file names. |
| 621 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 622 | ** This means that we cannot use POSIX locks to synchronize file access |
| 623 | ** among competing threads of the same process. POSIX locks will work fine |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 624 | ** to synchronize access for threads in separate processes, but not |
| 625 | ** threads within the same process. |
| 626 | ** |
| 627 | ** To work around the problem, SQLite has to manage file locks internally |
| 628 | ** on its own. Whenever a new database is opened, we have to find the |
| 629 | ** specific inode of the database file (the inode is determined by the |
| 630 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 631 | ** and check for locks already existing on that inode. When locks are |
| 632 | ** created or removed, we have to look at our own internal record of the |
| 633 | ** locks to see if another thread has previously set a lock on that same |
| 634 | ** inode. |
| 635 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 636 | ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 637 | ** For VxWorks, we have to use the alternative unique ID system based on |
| 638 | ** canonical filename and implemented in the previous division.) |
| 639 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 640 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 641 | ** descriptor. It is now a structure that holds the integer file |
| 642 | ** descriptor and a pointer to a structure that describes the internal |
| 643 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 644 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 645 | ** point to the same locking structure. The locking structure keeps |
| 646 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 647 | ** field that tells us its internal lock status. cnt==0 means the |
| 648 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 649 | ** cnt>0 means there are cnt shared locks on the file. |
| 650 | ** |
| 651 | ** Any attempt to lock or unlock a file first checks the locking |
| 652 | ** structure. The fcntl() system call is only invoked to set a |
| 653 | ** POSIX lock if the internal lock structure transitions between |
| 654 | ** a locked and an unlocked state. |
| 655 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 656 | ** But wait: there are yet more problems with POSIX advisory locks. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 657 | ** |
| 658 | ** If you close a file descriptor that points to a file that has locks, |
| 659 | ** all locks on that file that are owned by the current process are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 660 | ** released. To work around this problem, each unixFile structure contains |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 661 | ** a pointer to an unixOpenCnt structure. There is one unixOpenCnt structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 662 | ** per open inode, which means that multiple unixFile can point to a single |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 663 | ** unixOpenCnt. When an attempt is made to close an unixFile, if there are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 664 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 665 | ** to close() the file descriptor is deferred until all of the locks clear. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 666 | ** The unixOpenCnt structure keeps a list of file descriptors that need to |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 667 | ** be closed and that list is walked (and cleared) when the last lock |
| 668 | ** clears. |
| 669 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 670 | ** Yet another problem: LinuxThreads do not play well with posix locks. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 671 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 672 | ** Many older versions of linux use the LinuxThreads library which is |
| 673 | ** not posix compliant. Under LinuxThreads, a lock created by thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 674 | ** A cannot be modified or overridden by a different thread B. |
| 675 | ** Only thread A can modify the lock. Locking behavior is correct |
| 676 | ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 677 | ** on linux - with NPTL a lock created by thread A can override locks |
| 678 | ** in thread B. But there is no way to know at compile-time which |
| 679 | ** threading library is being used. So there is no way to know at |
| 680 | ** compile-time whether or not thread A can override locks on thread B. |
| 681 | ** We have to do a run-time check to discover the behavior of the |
| 682 | ** current process. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 683 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 684 | ** On systems where thread A is unable to modify locks created by |
| 685 | ** thread B, we have to keep track of which thread created each |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 686 | ** lock. Hence there is an extra field in the key to the unixLockInfo |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 687 | ** structure to record this information. And on those systems it |
| 688 | ** is illegal to begin a transaction in one thread and finish it |
| 689 | ** in another. For this latter restriction, there is no work-around. |
| 690 | ** It is a limitation of LinuxThreads. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 691 | */ |
| 692 | |
| 693 | /* |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 694 | ** Set or check the unixFile.tid field. This field is set when an unixFile |
| 695 | ** is first opened. All subsequent uses of the unixFile verify that the |
| 696 | ** same thread is operating on the unixFile. Some operating systems do |
| 697 | ** not allow locks to be overridden by other threads and that restriction |
| 698 | ** means that sqlite3* database handles cannot be moved from one thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 699 | ** to another while locks are held. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 700 | ** |
| 701 | ** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to |
| 702 | ** another as long as we are running on a system that supports threads |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 703 | ** overriding each others locks (which is now the most common behavior) |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 704 | ** or if no locks are held. But the unixFile.pLock field needs to be |
| 705 | ** recomputed because its key includes the thread-id. See the |
| 706 | ** transferOwnership() function below for additional information |
| 707 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 708 | #if SQLITE_THREADSAFE && defined(__linux__) |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 709 | # define SET_THREADID(X) (X)->tid = pthread_self() |
| 710 | # define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \ |
| 711 | !pthread_equal((X)->tid, pthread_self())) |
| 712 | #else |
| 713 | # define SET_THREADID(X) |
| 714 | # define CHECK_THREADID(X) 0 |
| 715 | #endif |
| 716 | |
| 717 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 718 | ** An instance of the following structure serves as the key used |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 719 | ** to locate a particular unixOpenCnt structure given its inode. This |
| 720 | ** is the same as the unixLockKey except that the thread ID is omitted. |
| 721 | */ |
| 722 | struct unixFileId { |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 723 | dev_t dev; /* Device number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 724 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 725 | struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 726 | #else |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 727 | ino_t ino; /* Inode number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 728 | #endif |
| 729 | }; |
| 730 | |
| 731 | /* |
| 732 | ** An instance of the following structure serves as the key used |
| 733 | ** to locate a particular unixLockInfo structure given its inode. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 734 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 735 | ** If threads cannot override each others locks (LinuxThreads), then we |
| 736 | ** set the unixLockKey.tid field to the thread ID. If threads can override |
| 737 | ** each others locks (Posix and NPTL) then tid is always set to zero. |
| 738 | ** tid is omitted if we compile without threading support or on an OS |
| 739 | ** other than linux. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 740 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 741 | struct unixLockKey { |
| 742 | struct unixFileId fid; /* Unique identifier for the file */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 743 | #if SQLITE_THREADSAFE && defined(__linux__) |
| 744 | pthread_t tid; /* Thread ID of lock owner. Zero if not using LinuxThreads */ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 745 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 746 | }; |
| 747 | |
| 748 | /* |
| 749 | ** An instance of the following structure is allocated for each open |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 750 | ** inode. Or, on LinuxThreads, there is one of these structures for |
| 751 | ** each inode opened by each thread. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 752 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 753 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 754 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 755 | ** object keeps a count of the number of unixFile pointing to it. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 756 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 757 | struct unixLockInfo { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 758 | struct unixLockKey lockKey; /* The lookup key */ |
| 759 | int cnt; /* Number of SHARED locks held */ |
| 760 | int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
| 761 | int nRef; /* Number of pointers to this structure */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 762 | #if defined(SQLITE_ENABLE_LOCKING_STYLE) |
| 763 | unsigned long long sharedByte; /* for AFP simulated shared lock */ |
| 764 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 765 | struct unixLockInfo *pNext; /* List of all unixLockInfo objects */ |
| 766 | struct unixLockInfo *pPrev; /* .... doubly linked */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 767 | }; |
| 768 | |
| 769 | /* |
| 770 | ** An instance of the following structure is allocated for each open |
| 771 | ** inode. This structure keeps track of the number of locks on that |
| 772 | ** inode. If a close is attempted against an inode that is holding |
| 773 | ** locks, the close is deferred until all locks clear by adding the |
| 774 | ** file descriptor to be closed to the pending list. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 775 | ** |
| 776 | ** TODO: Consider changing this so that there is only a single file |
| 777 | ** descriptor for each open file, even when it is opened multiple times. |
| 778 | ** The close() system call would only occur when the last database |
| 779 | ** using the file closes. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 780 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 781 | struct unixOpenCnt { |
| 782 | struct unixFileId fileId; /* The lookup key */ |
| 783 | int nRef; /* Number of pointers to this structure */ |
| 784 | int nLock; /* Number of outstanding locks */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 785 | UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 786 | #if OS_VXWORKS |
| 787 | sem_t *pSem; /* Named POSIX semaphore */ |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 788 | char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 789 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 790 | struct unixOpenCnt *pNext, *pPrev; /* List of all unixOpenCnt objects */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 791 | }; |
| 792 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 793 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 794 | ** Lists of all unixLockInfo and unixOpenCnt objects. These used to be hash |
| 795 | ** tables. But the number of objects is rarely more than a dozen and |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 796 | ** never exceeds a few thousand. And lookup is not on a critical |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 797 | ** path so a simple linked list will suffice. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 798 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 799 | static struct unixLockInfo *lockList = 0; |
| 800 | static struct unixOpenCnt *openList = 0; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 801 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 802 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 803 | ** This variable remembers whether or not threads can override each others |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 804 | ** locks. |
| 805 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 806 | ** 0: No. Threads cannot override each others locks. (LinuxThreads) |
| 807 | ** 1: Yes. Threads can override each others locks. (Posix & NLPT) |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 808 | ** -1: We don't know yet. |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 809 | ** |
drh | 5062d3a | 2006-01-31 23:03:35 +0000 | [diff] [blame] | 810 | ** On some systems, we know at compile-time if threads can override each |
| 811 | ** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro |
| 812 | ** will be set appropriately. On other systems, we have to check at |
| 813 | ** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is |
| 814 | ** undefined. |
| 815 | ** |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 816 | ** This variable normally has file scope only. But during testing, we make |
| 817 | ** it a global so that the test code can change its value in order to verify |
| 818 | ** that the right stuff happens in either case. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 819 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 820 | #if SQLITE_THREADSAFE && defined(__linux__) |
| 821 | # ifndef SQLITE_THREAD_OVERRIDE_LOCK |
| 822 | # define SQLITE_THREAD_OVERRIDE_LOCK -1 |
| 823 | # endif |
| 824 | # ifdef SQLITE_TEST |
drh | 5062d3a | 2006-01-31 23:03:35 +0000 | [diff] [blame] | 825 | int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 826 | # else |
drh | 5062d3a | 2006-01-31 23:03:35 +0000 | [diff] [blame] | 827 | static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 828 | # endif |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 829 | #endif |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 830 | |
| 831 | /* |
| 832 | ** This structure holds information passed into individual test |
| 833 | ** threads by the testThreadLockingBehavior() routine. |
| 834 | */ |
| 835 | struct threadTestData { |
| 836 | int fd; /* File to be locked */ |
| 837 | struct flock lock; /* The locking operation */ |
| 838 | int result; /* Result of the locking operation */ |
| 839 | }; |
| 840 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 841 | #if SQLITE_THREADSAFE && defined(__linux__) |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 842 | /* |
danielk1977 | 41a6a61 | 2008-11-11 18:34:35 +0000 | [diff] [blame] | 843 | ** This function is used as the main routine for a thread launched by |
| 844 | ** testThreadLockingBehavior(). It tests whether the shared-lock obtained |
| 845 | ** by the main thread in testThreadLockingBehavior() conflicts with a |
| 846 | ** hypothetical write-lock obtained by this thread on the same file. |
| 847 | ** |
| 848 | ** The write-lock is not actually acquired, as this is not possible if |
| 849 | ** the file is open in read-only mode (see ticket #3472). |
| 850 | */ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 851 | static void *threadLockingTest(void *pArg){ |
| 852 | struct threadTestData *pData = (struct threadTestData*)pArg; |
danielk1977 | 41a6a61 | 2008-11-11 18:34:35 +0000 | [diff] [blame] | 853 | pData->result = fcntl(pData->fd, F_GETLK, &pData->lock); |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 854 | return pArg; |
| 855 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 856 | #endif /* SQLITE_THREADSAFE && defined(__linux__) */ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 857 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 858 | |
| 859 | #if SQLITE_THREADSAFE && defined(__linux__) |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 860 | /* |
| 861 | ** This procedure attempts to determine whether or not threads |
| 862 | ** can override each others locks then sets the |
| 863 | ** threadsOverrideEachOthersLocks variable appropriately. |
| 864 | */ |
danielk1977 | 4d5238f | 2006-01-27 06:32:00 +0000 | [diff] [blame] | 865 | static void testThreadLockingBehavior(int fd_orig){ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 866 | int fd; |
danielk1977 | 41a6a61 | 2008-11-11 18:34:35 +0000 | [diff] [blame] | 867 | int rc; |
| 868 | struct threadTestData d; |
| 869 | struct flock l; |
| 870 | pthread_t t; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 871 | |
| 872 | fd = dup(fd_orig); |
| 873 | if( fd<0 ) return; |
danielk1977 | 41a6a61 | 2008-11-11 18:34:35 +0000 | [diff] [blame] | 874 | memset(&l, 0, sizeof(l)); |
| 875 | l.l_type = F_RDLCK; |
| 876 | l.l_len = 1; |
| 877 | l.l_start = 0; |
| 878 | l.l_whence = SEEK_SET; |
| 879 | rc = fcntl(fd_orig, F_SETLK, &l); |
| 880 | if( rc!=0 ) return; |
| 881 | memset(&d, 0, sizeof(d)); |
| 882 | d.fd = fd; |
| 883 | d.lock = l; |
| 884 | d.lock.l_type = F_WRLCK; |
drh | 06150f9 | 2009-07-03 12:57:58 +0000 | [diff] [blame] | 885 | if( pthread_create(&t, 0, threadLockingTest, &d)==0 ){ |
| 886 | pthread_join(t, 0); |
| 887 | } |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 888 | close(fd); |
danielk1977 | 41a6a61 | 2008-11-11 18:34:35 +0000 | [diff] [blame] | 889 | if( d.result!=0 ) return; |
| 890 | threadsOverrideEachOthersLocks = (d.lock.l_type==F_UNLCK); |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 891 | } |
drh | 06150f9 | 2009-07-03 12:57:58 +0000 | [diff] [blame] | 892 | #endif /* SQLITE_THREADSAFE && defined(__linux__) */ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 893 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 894 | /* |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 895 | ** Release a unixLockInfo structure previously allocated by findLockInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 896 | ** |
| 897 | ** The mutex entered using the unixEnterMutex() function must be held |
| 898 | ** when this function is called. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 899 | */ |
| 900 | static void releaseLockInfo(struct unixLockInfo *pLock){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 901 | assert( unixMutexHeld() ); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 902 | if( pLock ){ |
| 903 | pLock->nRef--; |
| 904 | if( pLock->nRef==0 ){ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 905 | if( pLock->pPrev ){ |
| 906 | assert( pLock->pPrev->pNext==pLock ); |
| 907 | pLock->pPrev->pNext = pLock->pNext; |
| 908 | }else{ |
| 909 | assert( lockList==pLock ); |
| 910 | lockList = pLock->pNext; |
| 911 | } |
| 912 | if( pLock->pNext ){ |
| 913 | assert( pLock->pNext->pPrev==pLock ); |
| 914 | pLock->pNext->pPrev = pLock->pPrev; |
| 915 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 916 | sqlite3_free(pLock); |
| 917 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | |
| 921 | /* |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 922 | ** Release a unixOpenCnt structure previously allocated by findLockInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 923 | ** |
| 924 | ** The mutex entered using the unixEnterMutex() function must be held |
| 925 | ** when this function is called. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 926 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 927 | static void releaseOpenCnt(struct unixOpenCnt *pOpen){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 928 | assert( unixMutexHeld() ); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 929 | if( pOpen ){ |
| 930 | pOpen->nRef--; |
| 931 | if( pOpen->nRef==0 ){ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 932 | if( pOpen->pPrev ){ |
| 933 | assert( pOpen->pPrev->pNext==pOpen ); |
| 934 | pOpen->pPrev->pNext = pOpen->pNext; |
| 935 | }else{ |
| 936 | assert( openList==pOpen ); |
| 937 | openList = pOpen->pNext; |
| 938 | } |
| 939 | if( pOpen->pNext ){ |
| 940 | assert( pOpen->pNext->pPrev==pOpen ); |
| 941 | pOpen->pNext->pPrev = pOpen->pPrev; |
| 942 | } |
drh | 08da4bb | 2009-09-10 19:20:03 +0000 | [diff] [blame] | 943 | #if SQLITE_THREADSAFE && defined(__linux__) |
dan | 11b3879 | 2009-09-09 18:46:52 +0000 | [diff] [blame] | 944 | assert( !pOpen->pUnused || threadsOverrideEachOthersLocks==0 ); |
drh | 08da4bb | 2009-09-10 19:20:03 +0000 | [diff] [blame] | 945 | #endif |
dan | 11b3879 | 2009-09-09 18:46:52 +0000 | [diff] [blame] | 946 | |
| 947 | /* If pOpen->pUnused is not null, then memory and file-descriptors |
| 948 | ** are leaked. |
| 949 | ** |
| 950 | ** This will only happen if, under Linuxthreads, the user has opened |
| 951 | ** a transaction in one thread, then attempts to close the database |
| 952 | ** handle from another thread (without first unlocking the db file). |
| 953 | ** This is a misuse. */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 954 | sqlite3_free(pOpen); |
| 955 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 959 | /* |
| 960 | ** Given a file descriptor, locate unixLockInfo and unixOpenCnt structures that |
| 961 | ** describes that file descriptor. Create new ones if necessary. The |
| 962 | ** return values might be uninitialized if an error occurs. |
| 963 | ** |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 964 | ** The mutex entered using the unixEnterMutex() function must be held |
| 965 | ** when this function is called. |
| 966 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 967 | ** Return an appropriate error code. |
| 968 | */ |
| 969 | static int findLockInfo( |
| 970 | unixFile *pFile, /* Unix file with file desc used in the key */ |
| 971 | struct unixLockInfo **ppLock, /* Return the unixLockInfo structure here */ |
| 972 | struct unixOpenCnt **ppOpen /* Return the unixOpenCnt structure here */ |
| 973 | ){ |
| 974 | int rc; /* System call return code */ |
| 975 | int fd; /* The file descriptor for pFile */ |
| 976 | struct unixLockKey lockKey; /* Lookup key for the unixLockInfo structure */ |
| 977 | struct unixFileId fileId; /* Lookup key for the unixOpenCnt struct */ |
| 978 | struct stat statbuf; /* Low-level file information */ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 979 | struct unixLockInfo *pLock = 0;/* Candidate unixLockInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 980 | struct unixOpenCnt *pOpen; /* Candidate unixOpenCnt object */ |
| 981 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 982 | assert( unixMutexHeld() ); |
| 983 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 984 | /* Get low-level information about the file that we can used to |
| 985 | ** create a unique name for the file. |
| 986 | */ |
| 987 | fd = pFile->h; |
| 988 | rc = fstat(fd, &statbuf); |
| 989 | if( rc!=0 ){ |
| 990 | pFile->lastErrno = errno; |
| 991 | #ifdef EOVERFLOW |
| 992 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 993 | #endif |
| 994 | return SQLITE_IOERR; |
| 995 | } |
| 996 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 997 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 998 | /* On OS X on an msdos filesystem, the inode number is reported |
| 999 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 1000 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 1001 | ** we always increase the file size to 1 by writing a single byte |
| 1002 | ** prior to accessing the inode number. The one byte written is |
| 1003 | ** an ASCII 'S' character which also happens to be the first byte |
| 1004 | ** in the header of every SQLite database. In this way, if there |
| 1005 | ** is a race condition such that another thread has already populated |
| 1006 | ** the first page of the database, no damage is done. |
| 1007 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1008 | if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1009 | rc = write(fd, "S", 1); |
| 1010 | if( rc!=1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1011 | pFile->lastErrno = errno; |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1012 | return SQLITE_IOERR; |
| 1013 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1014 | rc = fstat(fd, &statbuf); |
| 1015 | if( rc!=0 ){ |
| 1016 | pFile->lastErrno = errno; |
| 1017 | return SQLITE_IOERR; |
| 1018 | } |
| 1019 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1020 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1021 | |
| 1022 | memset(&lockKey, 0, sizeof(lockKey)); |
| 1023 | lockKey.fid.dev = statbuf.st_dev; |
| 1024 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1025 | lockKey.fid.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1026 | #else |
| 1027 | lockKey.fid.ino = statbuf.st_ino; |
| 1028 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1029 | #if SQLITE_THREADSAFE && defined(__linux__) |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1030 | if( threadsOverrideEachOthersLocks<0 ){ |
| 1031 | testThreadLockingBehavior(fd); |
| 1032 | } |
| 1033 | lockKey.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self(); |
| 1034 | #endif |
| 1035 | fileId = lockKey.fid; |
| 1036 | if( ppLock!=0 ){ |
| 1037 | pLock = lockList; |
| 1038 | while( pLock && memcmp(&lockKey, &pLock->lockKey, sizeof(lockKey)) ){ |
| 1039 | pLock = pLock->pNext; |
| 1040 | } |
| 1041 | if( pLock==0 ){ |
| 1042 | pLock = sqlite3_malloc( sizeof(*pLock) ); |
| 1043 | if( pLock==0 ){ |
| 1044 | rc = SQLITE_NOMEM; |
| 1045 | goto exit_findlockinfo; |
| 1046 | } |
drh | 9b5db1d | 2009-10-07 23:42:25 +0000 | [diff] [blame] | 1047 | memcpy(&pLock->lockKey,&lockKey,sizeof(lockKey)); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1048 | pLock->nRef = 1; |
| 1049 | pLock->cnt = 0; |
| 1050 | pLock->locktype = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1051 | #if defined(SQLITE_ENABLE_LOCKING_STYLE) |
| 1052 | pLock->sharedByte = 0; |
| 1053 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1054 | pLock->pNext = lockList; |
| 1055 | pLock->pPrev = 0; |
| 1056 | if( lockList ) lockList->pPrev = pLock; |
| 1057 | lockList = pLock; |
| 1058 | }else{ |
| 1059 | pLock->nRef++; |
| 1060 | } |
| 1061 | *ppLock = pLock; |
| 1062 | } |
| 1063 | if( ppOpen!=0 ){ |
| 1064 | pOpen = openList; |
| 1065 | while( pOpen && memcmp(&fileId, &pOpen->fileId, sizeof(fileId)) ){ |
| 1066 | pOpen = pOpen->pNext; |
| 1067 | } |
| 1068 | if( pOpen==0 ){ |
| 1069 | pOpen = sqlite3_malloc( sizeof(*pOpen) ); |
| 1070 | if( pOpen==0 ){ |
| 1071 | releaseLockInfo(pLock); |
| 1072 | rc = SQLITE_NOMEM; |
| 1073 | goto exit_findlockinfo; |
| 1074 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1075 | memset(pOpen, 0, sizeof(*pOpen)); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1076 | pOpen->fileId = fileId; |
| 1077 | pOpen->nRef = 1; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1078 | pOpen->pNext = openList; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1079 | if( openList ) openList->pPrev = pOpen; |
| 1080 | openList = pOpen; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1081 | }else{ |
| 1082 | pOpen->nRef++; |
| 1083 | } |
| 1084 | *ppOpen = pOpen; |
| 1085 | } |
| 1086 | |
| 1087 | exit_findlockinfo: |
| 1088 | return rc; |
| 1089 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1090 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1091 | /* |
| 1092 | ** If we are currently in a different thread than the thread that the |
| 1093 | ** unixFile argument belongs to, then transfer ownership of the unixFile |
| 1094 | ** over to the current thread. |
| 1095 | ** |
| 1096 | ** A unixFile is only owned by a thread on systems that use LinuxThreads. |
| 1097 | ** |
| 1098 | ** Ownership transfer is only allowed if the unixFile is currently unlocked. |
| 1099 | ** If the unixFile is locked and an ownership is wrong, then return |
| 1100 | ** SQLITE_MISUSE. SQLITE_OK is returned if everything works. |
| 1101 | */ |
| 1102 | #if SQLITE_THREADSAFE && defined(__linux__) |
| 1103 | static int transferOwnership(unixFile *pFile){ |
| 1104 | int rc; |
| 1105 | pthread_t hSelf; |
| 1106 | if( threadsOverrideEachOthersLocks ){ |
| 1107 | /* Ownership transfers not needed on this system */ |
| 1108 | return SQLITE_OK; |
| 1109 | } |
| 1110 | hSelf = pthread_self(); |
| 1111 | if( pthread_equal(pFile->tid, hSelf) ){ |
| 1112 | /* We are still in the same thread */ |
| 1113 | OSTRACE1("No-transfer, same thread\n"); |
| 1114 | return SQLITE_OK; |
| 1115 | } |
| 1116 | if( pFile->locktype!=NO_LOCK ){ |
| 1117 | /* We cannot change ownership while we are holding a lock! */ |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 1118 | return SQLITE_MISUSE_BKPT; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1119 | } |
| 1120 | OSTRACE4("Transfer ownership of %d from %d to %d\n", |
| 1121 | pFile->h, pFile->tid, hSelf); |
| 1122 | pFile->tid = hSelf; |
| 1123 | if (pFile->pLock != NULL) { |
| 1124 | releaseLockInfo(pFile->pLock); |
| 1125 | rc = findLockInfo(pFile, &pFile->pLock, 0); |
| 1126 | OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h, |
| 1127 | locktypeName(pFile->locktype), |
| 1128 | locktypeName(pFile->pLock->locktype), pFile->pLock->cnt); |
| 1129 | return rc; |
| 1130 | } else { |
| 1131 | return SQLITE_OK; |
| 1132 | } |
| 1133 | } |
| 1134 | #else /* if not SQLITE_THREADSAFE */ |
| 1135 | /* On single-threaded builds, ownership transfer is a no-op */ |
| 1136 | # define transferOwnership(X) SQLITE_OK |
| 1137 | #endif /* SQLITE_THREADSAFE */ |
| 1138 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1139 | |
| 1140 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1141 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1142 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1143 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1144 | ** 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] | 1145 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1146 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1147 | int rc = SQLITE_OK; |
| 1148 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1149 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1150 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1151 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1152 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1153 | assert( pFile ); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1154 | unixEnterMutex(); /* Because pFile->pLock is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1155 | |
| 1156 | /* Check if a thread in this process holds such a lock */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1157 | if( pFile->pLock->locktype>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1158 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1161 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1162 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1163 | #ifndef __DJGPP__ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1164 | if( !reserved ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1165 | struct flock lock; |
| 1166 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1167 | lock.l_start = RESERVED_BYTE; |
| 1168 | lock.l_len = 1; |
| 1169 | lock.l_type = F_WRLCK; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1170 | if (-1 == fcntl(pFile->h, F_GETLK, &lock)) { |
| 1171 | int tErrno = errno; |
| 1172 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 1173 | pFile->lastErrno = tErrno; |
| 1174 | } else if( lock.l_type!=F_UNLCK ){ |
| 1175 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1176 | } |
| 1177 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1178 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1179 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1180 | unixLeaveMutex(); |
drh | 476bda7 | 2009-12-04 14:25:18 +0000 | [diff] [blame] | 1181 | OSTRACE4("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1182 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1183 | *pResOut = reserved; |
| 1184 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | /* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1188 | ** Lock the file with the lock specified by parameter locktype - one |
| 1189 | ** of the following: |
| 1190 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1191 | ** (1) SHARED_LOCK |
| 1192 | ** (2) RESERVED_LOCK |
| 1193 | ** (3) PENDING_LOCK |
| 1194 | ** (4) EXCLUSIVE_LOCK |
| 1195 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1196 | ** Sometimes when requesting one lock state, additional lock states |
| 1197 | ** are inserted in between. The locking might fail on one of the later |
| 1198 | ** transitions leaving the lock state different from what it started but |
| 1199 | ** still short of its goal. The following chart shows the allowed |
| 1200 | ** transitions and the inserted intermediate states: |
| 1201 | ** |
| 1202 | ** UNLOCKED -> SHARED |
| 1203 | ** SHARED -> RESERVED |
| 1204 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1205 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1206 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1207 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1208 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1209 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1210 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1211 | static int unixLock(sqlite3_file *id, int locktype){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1212 | /* The following describes the implementation of the various locks and |
| 1213 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1214 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1215 | ** confusion with SQLite lock names). The algorithms are complicated |
| 1216 | ** slightly in order to be compatible with windows systems simultaneously |
| 1217 | ** accessing the same database file, in case that is ever required. |
| 1218 | ** |
| 1219 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1220 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1221 | ** range', a range of 510 bytes at a well known offset. |
| 1222 | ** |
| 1223 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 1224 | ** byte'. If this is successful, a random byte from the 'shared byte |
| 1225 | ** range' is read-locked and the lock on the 'pending byte' released. |
| 1226 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1227 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1228 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1229 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1230 | ** |
| 1231 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1232 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1233 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1234 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1235 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1236 | ** This property is used by the algorithm for rolling back a journal file |
| 1237 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1238 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1239 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1240 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1241 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1242 | ** within this range, this ensures that no other locks are held on the |
| 1243 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1244 | ** |
| 1245 | ** The reason a single byte cannot be used instead of the 'shared byte |
| 1246 | ** range' is that some versions of windows do not support read-locks. By |
| 1247 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 1248 | ** even if the locking primitive used is always a write-lock. |
| 1249 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1250 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1251 | unixFile *pFile = (unixFile*)id; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1252 | struct unixLockInfo *pLock = pFile->pLock; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1253 | struct flock lock; |
drh | 3f02218 | 2009-09-09 16:10:50 +0000 | [diff] [blame] | 1254 | int s = 0; |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 1255 | int tErrno = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1256 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1257 | assert( pFile ); |
drh | 476bda7 | 2009-12-04 14:25:18 +0000 | [diff] [blame] | 1258 | OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1259 | locktypeName(locktype), locktypeName(pFile->locktype), |
| 1260 | locktypeName(pLock->locktype), pLock->cnt , getpid()); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1261 | |
| 1262 | /* 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] | 1263 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1264 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1265 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1266 | if( pFile->locktype>=locktype ){ |
drh | 476bda7 | 2009-12-04 14:25:18 +0000 | [diff] [blame] | 1267 | OSTRACE3("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1268 | locktypeName(locktype)); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1269 | return SQLITE_OK; |
| 1270 | } |
| 1271 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1272 | /* Make sure the locking sequence is correct. |
| 1273 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 1274 | ** (2) SQLite never explicitly requests a pendig lock. |
| 1275 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1276 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1277 | assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1278 | assert( locktype!=PENDING_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1279 | assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1280 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1281 | /* This mutex is needed because pFile->pLock is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1282 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1283 | unixEnterMutex(); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1284 | |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1285 | /* Make sure the current thread owns the pFile. |
| 1286 | */ |
| 1287 | rc = transferOwnership(pFile); |
| 1288 | if( rc!=SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1289 | unixLeaveMutex(); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1290 | return rc; |
| 1291 | } |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 1292 | pLock = pFile->pLock; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1293 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1294 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1295 | ** handle that precludes the requested lock, return BUSY. |
| 1296 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1297 | if( (pFile->locktype!=pLock->locktype && |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1298 | (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1299 | ){ |
| 1300 | rc = SQLITE_BUSY; |
| 1301 | goto end_lock; |
| 1302 | } |
| 1303 | |
| 1304 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1305 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1306 | ** return SQLITE_OK. |
| 1307 | */ |
| 1308 | if( locktype==SHARED_LOCK && |
| 1309 | (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){ |
| 1310 | assert( locktype==SHARED_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1311 | assert( pFile->locktype==0 ); |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1312 | assert( pLock->cnt>0 ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1313 | pFile->locktype = SHARED_LOCK; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1314 | pLock->cnt++; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1315 | pFile->pOpen->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1316 | goto end_lock; |
| 1317 | } |
| 1318 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1319 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1320 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1321 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1322 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1323 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1324 | lock.l_len = 1L; |
| 1325 | lock.l_whence = SEEK_SET; |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1326 | if( locktype==SHARED_LOCK |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1327 | || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1328 | ){ |
danielk1977 | 489468c | 2004-06-28 08:25:47 +0000 | [diff] [blame] | 1329 | lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1330 | lock.l_start = PENDING_BYTE; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1331 | s = fcntl(pFile->h, F_SETLK, &lock); |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1332 | if( s==(-1) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1333 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1334 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1335 | if( IS_LOCK_ERROR(rc) ){ |
| 1336 | pFile->lastErrno = tErrno; |
| 1337 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1338 | goto end_lock; |
| 1339 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | |
| 1343 | /* If control gets to this point, then actually go ahead and make |
| 1344 | ** operating system calls for the specified lock. |
| 1345 | */ |
| 1346 | if( locktype==SHARED_LOCK ){ |
| 1347 | assert( pLock->cnt==0 ); |
| 1348 | assert( pLock->locktype==0 ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1349 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1350 | /* Now get the read-lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1351 | lock.l_start = SHARED_FIRST; |
| 1352 | lock.l_len = SHARED_SIZE; |
| 1353 | if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){ |
| 1354 | tErrno = errno; |
| 1355 | } |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1356 | /* Drop the temporary PENDING lock */ |
| 1357 | lock.l_start = PENDING_BYTE; |
| 1358 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1359 | lock.l_type = F_UNLCK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1360 | if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1361 | if( s != -1 ){ |
| 1362 | /* This could happen with a network mount */ |
| 1363 | tErrno = errno; |
| 1364 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1365 | if( IS_LOCK_ERROR(rc) ){ |
| 1366 | pFile->lastErrno = tErrno; |
| 1367 | } |
| 1368 | goto end_lock; |
| 1369 | } |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1370 | } |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1371 | if( s==(-1) ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1372 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1373 | if( IS_LOCK_ERROR(rc) ){ |
| 1374 | pFile->lastErrno = tErrno; |
| 1375 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1376 | }else{ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1377 | pFile->locktype = SHARED_LOCK; |
| 1378 | pFile->pOpen->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1379 | pLock->cnt = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1380 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1381 | }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){ |
| 1382 | /* We are trying for an exclusive lock but another thread in this |
| 1383 | ** same process is still holding a shared lock. */ |
| 1384 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1385 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1386 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1387 | ** assumed that there is a SHARED or greater lock on the file |
| 1388 | ** already. |
| 1389 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1390 | assert( 0!=pFile->locktype ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1391 | lock.l_type = F_WRLCK; |
| 1392 | switch( locktype ){ |
| 1393 | case RESERVED_LOCK: |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1394 | lock.l_start = RESERVED_BYTE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1395 | break; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1396 | case EXCLUSIVE_LOCK: |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1397 | lock.l_start = SHARED_FIRST; |
| 1398 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1399 | break; |
| 1400 | default: |
| 1401 | assert(0); |
| 1402 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1403 | s = fcntl(pFile->h, F_SETLK, &lock); |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1404 | if( s==(-1) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1405 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1406 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1407 | if( IS_LOCK_ERROR(rc) ){ |
| 1408 | pFile->lastErrno = tErrno; |
| 1409 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1410 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1411 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1412 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1413 | |
| 1414 | #ifndef NDEBUG |
| 1415 | /* Set up the transaction-counter change checking flags when |
| 1416 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1417 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1418 | ** write operation (not a hot journal rollback). |
| 1419 | */ |
| 1420 | if( rc==SQLITE_OK |
| 1421 | && pFile->locktype<=SHARED_LOCK |
| 1422 | && locktype==RESERVED_LOCK |
| 1423 | ){ |
| 1424 | pFile->transCntrChng = 0; |
| 1425 | pFile->dbUpdate = 0; |
| 1426 | pFile->inNormalWrite = 1; |
| 1427 | } |
| 1428 | #endif |
| 1429 | |
| 1430 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1431 | if( rc==SQLITE_OK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1432 | pFile->locktype = locktype; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1433 | pLock->locktype = locktype; |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1434 | }else if( locktype==EXCLUSIVE_LOCK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1435 | pFile->locktype = PENDING_LOCK; |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1436 | pLock->locktype = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1437 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1438 | |
| 1439 | end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1440 | unixLeaveMutex(); |
drh | 476bda7 | 2009-12-04 14:25:18 +0000 | [diff] [blame] | 1441 | OSTRACE4("LOCK %d %s %s (unix)\n", pFile->h, locktypeName(locktype), |
danielk1977 | 2b44485 | 2004-06-29 07:45:33 +0000 | [diff] [blame] | 1442 | rc==SQLITE_OK ? "ok" : "failed"); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1443 | return rc; |
| 1444 | } |
| 1445 | |
| 1446 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1447 | ** Close all file descriptors accumuated in the unixOpenCnt->pUnused list. |
| 1448 | ** If all such file descriptors are closed without error, the list is |
| 1449 | ** cleared and SQLITE_OK returned. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1450 | ** |
| 1451 | ** Otherwise, if an error occurs, then successfully closed file descriptor |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1452 | ** entries are removed from the list, and SQLITE_IOERR_CLOSE returned. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1453 | ** not deleted and SQLITE_IOERR_CLOSE returned. |
| 1454 | */ |
| 1455 | static int closePendingFds(unixFile *pFile){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1456 | int rc = SQLITE_OK; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1457 | struct unixOpenCnt *pOpen = pFile->pOpen; |
| 1458 | UnixUnusedFd *pError = 0; |
| 1459 | UnixUnusedFd *p; |
| 1460 | UnixUnusedFd *pNext; |
| 1461 | for(p=pOpen->pUnused; p; p=pNext){ |
| 1462 | pNext = p->pNext; |
| 1463 | if( close(p->fd) ){ |
| 1464 | pFile->lastErrno = errno; |
| 1465 | rc = SQLITE_IOERR_CLOSE; |
| 1466 | p->pNext = pError; |
| 1467 | pError = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1468 | }else{ |
| 1469 | sqlite3_free(p); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1470 | } |
| 1471 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1472 | pOpen->pUnused = pError; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1473 | return rc; |
| 1474 | } |
| 1475 | |
| 1476 | /* |
| 1477 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1478 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1479 | */ |
| 1480 | static void setPendingFd(unixFile *pFile){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1481 | struct unixOpenCnt *pOpen = pFile->pOpen; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1482 | UnixUnusedFd *p = pFile->pUnused; |
| 1483 | p->pNext = pOpen->pUnused; |
| 1484 | pOpen->pUnused = p; |
| 1485 | pFile->h = -1; |
| 1486 | pFile->pUnused = 0; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | /* |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1490 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1491 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1492 | ** |
| 1493 | ** If the locking level of the file descriptor is already at or below |
| 1494 | ** the requested locking level, this routine is a no-op. |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1495 | ** |
| 1496 | ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 1497 | ** the byte range is divided into 2 parts and the first part is unlocked then |
| 1498 | ** set to a read lock, then the other part is simply unlocked. This works |
| 1499 | ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 1500 | ** remove the write lock on a region when a read lock is set. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1501 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1502 | static int _posixUnlock(sqlite3_file *id, int locktype, int handleNFSUnlock){ |
| 1503 | unixFile *pFile = (unixFile*)id; |
| 1504 | struct unixLockInfo *pLock; |
| 1505 | struct flock lock; |
| 1506 | int rc = SQLITE_OK; |
| 1507 | int h; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1508 | int tErrno; /* Error code from system call errors */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1509 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1510 | assert( pFile ); |
drh | 476bda7 | 2009-12-04 14:25:18 +0000 | [diff] [blame] | 1511 | OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, locktype, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1512 | pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid()); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1513 | |
| 1514 | assert( locktype<=SHARED_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1515 | if( pFile->locktype<=locktype ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1516 | return SQLITE_OK; |
| 1517 | } |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 1518 | if( CHECK_THREADID(pFile) ){ |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 1519 | return SQLITE_MISUSE_BKPT; |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 1520 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1521 | unixEnterMutex(); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1522 | h = pFile->h; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1523 | pLock = pFile->pLock; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1524 | assert( pLock->cnt!=0 ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1525 | if( pFile->locktype>SHARED_LOCK ){ |
| 1526 | assert( pLock->locktype==pFile->locktype ); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1527 | SimulateIOErrorBenign(1); |
| 1528 | SimulateIOError( h=(-1) ) |
| 1529 | SimulateIOErrorBenign(0); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1530 | |
| 1531 | #ifndef NDEBUG |
| 1532 | /* When reducing a lock such that other processes can start |
| 1533 | ** reading the database file again, make sure that the |
| 1534 | ** transaction counter was updated if any part of the database |
| 1535 | ** file changed. If the transaction counter is not updated, |
| 1536 | ** other connections to the same file might not realize that |
| 1537 | ** the file has changed and hence might not know to flush their |
| 1538 | ** cache. The use of a stale cache can lead to database corruption. |
| 1539 | */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1540 | #if 0 |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1541 | assert( pFile->inNormalWrite==0 |
| 1542 | || pFile->dbUpdate==0 |
| 1543 | || pFile->transCntrChng==1 ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1544 | #endif |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1545 | pFile->inNormalWrite = 0; |
| 1546 | #endif |
| 1547 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1548 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 1549 | ** before establishing the readlock - to avoid a race condition we downgrade |
| 1550 | ** the lock in 2 blocks, so that part of the range will be covered by a |
| 1551 | ** write lock until the rest is covered by a read lock: |
| 1552 | ** 1: [WWWWW] |
| 1553 | ** 2: [....W] |
| 1554 | ** 3: [RRRRW] |
| 1555 | ** 4: [RRRR.] |
| 1556 | */ |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1557 | if( locktype==SHARED_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1558 | if( handleNFSUnlock ){ |
| 1559 | off_t divSize = SHARED_SIZE - 1; |
| 1560 | |
| 1561 | lock.l_type = F_UNLCK; |
| 1562 | lock.l_whence = SEEK_SET; |
| 1563 | lock.l_start = SHARED_FIRST; |
| 1564 | lock.l_len = divSize; |
| 1565 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1566 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1567 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1568 | if( IS_LOCK_ERROR(rc) ){ |
| 1569 | pFile->lastErrno = tErrno; |
| 1570 | } |
| 1571 | goto end_unlock; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1572 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1573 | lock.l_type = F_RDLCK; |
| 1574 | lock.l_whence = SEEK_SET; |
| 1575 | lock.l_start = SHARED_FIRST; |
| 1576 | lock.l_len = divSize; |
| 1577 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1578 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1579 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1580 | if( IS_LOCK_ERROR(rc) ){ |
| 1581 | pFile->lastErrno = tErrno; |
| 1582 | } |
| 1583 | goto end_unlock; |
| 1584 | } |
| 1585 | lock.l_type = F_UNLCK; |
| 1586 | lock.l_whence = SEEK_SET; |
| 1587 | lock.l_start = SHARED_FIRST+divSize; |
| 1588 | lock.l_len = SHARED_SIZE-divSize; |
| 1589 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1590 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1591 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1592 | if( IS_LOCK_ERROR(rc) ){ |
| 1593 | pFile->lastErrno = tErrno; |
| 1594 | } |
| 1595 | goto end_unlock; |
| 1596 | } |
| 1597 | }else{ |
| 1598 | lock.l_type = F_RDLCK; |
| 1599 | lock.l_whence = SEEK_SET; |
| 1600 | lock.l_start = SHARED_FIRST; |
| 1601 | lock.l_len = SHARED_SIZE; |
| 1602 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1603 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1604 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1605 | if( IS_LOCK_ERROR(rc) ){ |
| 1606 | pFile->lastErrno = tErrno; |
| 1607 | } |
| 1608 | goto end_unlock; |
| 1609 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1610 | } |
| 1611 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1612 | lock.l_type = F_UNLCK; |
| 1613 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1614 | lock.l_start = PENDING_BYTE; |
| 1615 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1616 | if( fcntl(h, F_SETLK, &lock)!=(-1) ){ |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1617 | pLock->locktype = SHARED_LOCK; |
| 1618 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1619 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1620 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1621 | if( IS_LOCK_ERROR(rc) ){ |
| 1622 | pFile->lastErrno = tErrno; |
| 1623 | } |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 1624 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1625 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1626 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1627 | if( locktype==NO_LOCK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1628 | struct unixOpenCnt *pOpen; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1629 | |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1630 | /* Decrement the shared lock counter. Release the lock using an |
| 1631 | ** OS call only when all threads in this same process have released |
| 1632 | ** the lock. |
| 1633 | */ |
| 1634 | pLock->cnt--; |
| 1635 | if( pLock->cnt==0 ){ |
| 1636 | lock.l_type = F_UNLCK; |
| 1637 | lock.l_whence = SEEK_SET; |
| 1638 | lock.l_start = lock.l_len = 0L; |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1639 | SimulateIOErrorBenign(1); |
| 1640 | SimulateIOError( h=(-1) ) |
| 1641 | SimulateIOErrorBenign(0); |
| 1642 | if( fcntl(h, F_SETLK, &lock)!=(-1) ){ |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1643 | pLock->locktype = NO_LOCK; |
| 1644 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1645 | tErrno = errno; |
danielk1977 | 5ad6a88 | 2008-09-15 04:20:31 +0000 | [diff] [blame] | 1646 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1647 | if( IS_LOCK_ERROR(rc) ){ |
| 1648 | pFile->lastErrno = tErrno; |
| 1649 | } |
drh | f48f9ca | 2009-03-28 23:47:10 +0000 | [diff] [blame] | 1650 | pLock->locktype = NO_LOCK; |
| 1651 | pFile->locktype = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1652 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1655 | /* Decrement the count of locks against this same file. When the |
| 1656 | ** count reaches zero, close any other file descriptors whose close |
| 1657 | ** was deferred because of outstanding locks. |
| 1658 | */ |
danielk1977 | 64a54c5 | 2009-03-30 07:39:35 +0000 | [diff] [blame] | 1659 | pOpen = pFile->pOpen; |
| 1660 | pOpen->nLock--; |
| 1661 | assert( pOpen->nLock>=0 ); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1662 | if( pOpen->nLock==0 ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1663 | int rc2 = closePendingFds(pFile); |
| 1664 | if( rc==SQLITE_OK ){ |
| 1665 | rc = rc2; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1666 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1667 | } |
| 1668 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1669 | |
| 1670 | end_unlock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1671 | unixLeaveMutex(); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1672 | if( rc==SQLITE_OK ) pFile->locktype = locktype; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1673 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | /* |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1677 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
| 1678 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1679 | ** |
| 1680 | ** If the locking level of the file descriptor is already at or below |
| 1681 | ** the requested locking level, this routine is a no-op. |
| 1682 | */ |
| 1683 | static int unixUnlock(sqlite3_file *id, int locktype){ |
| 1684 | return _posixUnlock(id, locktype, 0); |
| 1685 | } |
| 1686 | |
| 1687 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1688 | ** This function performs the parts of the "close file" operation |
| 1689 | ** common to all locking schemes. It closes the directory and file |
| 1690 | ** handles, if they are valid, and sets all fields of the unixFile |
| 1691 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1692 | ** |
| 1693 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 1694 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 1695 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1696 | */ |
| 1697 | static int closeUnixFile(sqlite3_file *id){ |
| 1698 | unixFile *pFile = (unixFile*)id; |
| 1699 | if( pFile ){ |
| 1700 | if( pFile->dirfd>=0 ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1701 | int err = close(pFile->dirfd); |
| 1702 | if( err ){ |
| 1703 | pFile->lastErrno = errno; |
| 1704 | return SQLITE_IOERR_DIR_CLOSE; |
| 1705 | }else{ |
| 1706 | pFile->dirfd=-1; |
| 1707 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1708 | } |
| 1709 | if( pFile->h>=0 ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1710 | int err = close(pFile->h); |
| 1711 | if( err ){ |
| 1712 | pFile->lastErrno = errno; |
| 1713 | return SQLITE_IOERR_CLOSE; |
| 1714 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1715 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1716 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1717 | if( pFile->pId ){ |
| 1718 | if( pFile->isDelete ){ |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1719 | unlink(pFile->pId->zCanonicalName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1720 | } |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1721 | vxworksReleaseFileId(pFile->pId); |
| 1722 | pFile->pId = 0; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1723 | } |
| 1724 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1725 | OSTRACE2("CLOSE %-3d\n", pFile->h); |
| 1726 | OpenCounter(-1); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1727 | sqlite3_free(pFile->pUnused); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1728 | memset(pFile, 0, sizeof(unixFile)); |
| 1729 | } |
| 1730 | return SQLITE_OK; |
| 1731 | } |
| 1732 | |
| 1733 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1734 | ** Close a file. |
| 1735 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1736 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1737 | int rc = SQLITE_OK; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1738 | if( id ){ |
| 1739 | unixFile *pFile = (unixFile *)id; |
| 1740 | unixUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1741 | unixEnterMutex(); |
danielk1977 | 6cb427f | 2008-06-30 10:16:04 +0000 | [diff] [blame] | 1742 | if( pFile->pOpen && pFile->pOpen->nLock ){ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1743 | /* If there are outstanding locks, do not actually close the file just |
| 1744 | ** yet because that would clear those locks. Instead, add the file |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1745 | ** descriptor to pOpen->pUnused list. It will be automatically closed |
| 1746 | ** when the last lock is cleared. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1747 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1748 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1749 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1750 | releaseLockInfo(pFile->pLock); |
| 1751 | releaseOpenCnt(pFile->pOpen); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1752 | rc = closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1753 | unixLeaveMutex(); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1754 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1755 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1758 | /************** End of the posix advisory lock implementation ***************** |
| 1759 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1760 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1761 | /****************************************************************************** |
| 1762 | ****************************** No-op Locking ********************************** |
| 1763 | ** |
| 1764 | ** Of the various locking implementations available, this is by far the |
| 1765 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 1766 | ** file for reading or writing. |
| 1767 | ** |
| 1768 | ** This locking mode is appropriate for use on read-only databases |
| 1769 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 1770 | ** also be used if the application employs some external mechanism to |
| 1771 | ** prevent simultaneous access of the same database by two or more |
| 1772 | ** database connections. But there is a serious risk of database |
| 1773 | ** corruption if this locking mode is used in situations where multiple |
| 1774 | ** database connections are accessing the same database file at the same |
| 1775 | ** time and one or more of those connections are writing. |
| 1776 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1777 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1778 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 1779 | UNUSED_PARAMETER(NotUsed); |
| 1780 | *pResOut = 0; |
| 1781 | return SQLITE_OK; |
| 1782 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1783 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1784 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1785 | return SQLITE_OK; |
| 1786 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1787 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1788 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1789 | return SQLITE_OK; |
| 1790 | } |
| 1791 | |
| 1792 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1793 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1794 | */ |
| 1795 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1796 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | /******************* End of the no-op lock implementation ********************* |
| 1800 | ******************************************************************************/ |
| 1801 | |
| 1802 | /****************************************************************************** |
| 1803 | ************************* Begin dot-file Locking ****************************** |
| 1804 | ** |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1805 | ** The dotfile locking implementation uses the existance of separate lock |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1806 | ** files in order to control access to the database. This works on just |
| 1807 | ** about every filesystem imaginable. But there are serious downsides: |
| 1808 | ** |
| 1809 | ** (1) There is zero concurrency. A single reader blocks all other |
| 1810 | ** connections from reading or writing the database. |
| 1811 | ** |
| 1812 | ** (2) An application crash or power loss can leave stale lock files |
| 1813 | ** sitting around that need to be cleared manually. |
| 1814 | ** |
| 1815 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 1816 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1817 | ** |
| 1818 | ** Dotfile locking works by creating a file in the same directory as the |
| 1819 | ** database and with the same name but with a ".lock" extension added. |
| 1820 | ** The existance of a lock file implies an EXCLUSIVE lock. All other lock |
| 1821 | ** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1822 | */ |
| 1823 | |
| 1824 | /* |
| 1825 | ** The file suffix added to the data base filename in order to create the |
| 1826 | ** lock file. |
| 1827 | */ |
| 1828 | #define DOTLOCK_SUFFIX ".lock" |
| 1829 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1830 | /* |
| 1831 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1832 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1833 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1834 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1835 | ** |
| 1836 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 1837 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 1838 | ** is held on the file and false if the file is unlocked. |
| 1839 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1840 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 1841 | int rc = SQLITE_OK; |
| 1842 | int reserved = 0; |
| 1843 | unixFile *pFile = (unixFile*)id; |
| 1844 | |
| 1845 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1846 | |
| 1847 | assert( pFile ); |
| 1848 | |
| 1849 | /* Check if a thread in this process holds such a lock */ |
| 1850 | if( pFile->locktype>SHARED_LOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1851 | /* Either this connection or some other connection in the same process |
| 1852 | ** holds a lock on the file. No need to check further. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1853 | reserved = 1; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1854 | }else{ |
| 1855 | /* The lock is held if and only if the lockfile exists */ |
| 1856 | const char *zLockFile = (const char*)pFile->lockingContext; |
| 1857 | reserved = access(zLockFile, 0)==0; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1858 | } |
drh | 476bda7 | 2009-12-04 14:25:18 +0000 | [diff] [blame] | 1859 | OSTRACE4("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1860 | *pResOut = reserved; |
| 1861 | return rc; |
| 1862 | } |
| 1863 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1864 | /* |
| 1865 | ** Lock the file with the lock specified by parameter locktype - one |
| 1866 | ** of the following: |
| 1867 | ** |
| 1868 | ** (1) SHARED_LOCK |
| 1869 | ** (2) RESERVED_LOCK |
| 1870 | ** (3) PENDING_LOCK |
| 1871 | ** (4) EXCLUSIVE_LOCK |
| 1872 | ** |
| 1873 | ** Sometimes when requesting one lock state, additional lock states |
| 1874 | ** are inserted in between. The locking might fail on one of the later |
| 1875 | ** transitions leaving the lock state different from what it started but |
| 1876 | ** still short of its goal. The following chart shows the allowed |
| 1877 | ** transitions and the inserted intermediate states: |
| 1878 | ** |
| 1879 | ** UNLOCKED -> SHARED |
| 1880 | ** SHARED -> RESERVED |
| 1881 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1882 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1883 | ** PENDING -> EXCLUSIVE |
| 1884 | ** |
| 1885 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1886 | ** routine to lower a locking level. |
| 1887 | ** |
| 1888 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 1889 | ** But we track the other locking levels internally. |
| 1890 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1891 | static int dotlockLock(sqlite3_file *id, int locktype) { |
| 1892 | unixFile *pFile = (unixFile*)id; |
| 1893 | int fd; |
| 1894 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1895 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1896 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1897 | |
| 1898 | /* If we have any lock, then the lock file already exists. All we have |
| 1899 | ** to do is adjust our internal record of the lock level. |
| 1900 | */ |
| 1901 | if( pFile->locktype > NO_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1902 | pFile->locktype = locktype; |
| 1903 | #if !OS_VXWORKS |
| 1904 | /* Always update the timestamp on the old file */ |
| 1905 | utimes(zLockFile, NULL); |
| 1906 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1907 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | /* grab an exclusive lock */ |
| 1911 | fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600); |
| 1912 | if( fd<0 ){ |
| 1913 | /* failed to open/create the file, someone else may have stolen the lock */ |
| 1914 | int tErrno = errno; |
| 1915 | if( EEXIST == tErrno ){ |
| 1916 | rc = SQLITE_BUSY; |
| 1917 | } else { |
| 1918 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1919 | if( IS_LOCK_ERROR(rc) ){ |
| 1920 | pFile->lastErrno = tErrno; |
| 1921 | } |
| 1922 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1923 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1924 | } |
| 1925 | if( close(fd) ){ |
| 1926 | pFile->lastErrno = errno; |
| 1927 | rc = SQLITE_IOERR_CLOSE; |
| 1928 | } |
| 1929 | |
| 1930 | /* got it, set the type and return ok */ |
| 1931 | pFile->locktype = locktype; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1932 | return rc; |
| 1933 | } |
| 1934 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1935 | /* |
| 1936 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
| 1937 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1938 | ** |
| 1939 | ** If the locking level of the file descriptor is already at or below |
| 1940 | ** the requested locking level, this routine is a no-op. |
| 1941 | ** |
| 1942 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 1943 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1944 | static int dotlockUnlock(sqlite3_file *id, int locktype) { |
| 1945 | unixFile *pFile = (unixFile*)id; |
| 1946 | char *zLockFile = (char *)pFile->lockingContext; |
| 1947 | |
| 1948 | assert( pFile ); |
drh | 476bda7 | 2009-12-04 14:25:18 +0000 | [diff] [blame] | 1949 | OSTRACE5("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, locktype, |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1950 | pFile->locktype, getpid()); |
| 1951 | assert( locktype<=SHARED_LOCK ); |
| 1952 | |
| 1953 | /* no-op if possible */ |
| 1954 | if( pFile->locktype==locktype ){ |
| 1955 | return SQLITE_OK; |
| 1956 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1957 | |
| 1958 | /* To downgrade to shared, simply update our internal notion of the |
| 1959 | ** lock state. No need to mess with the file on disk. |
| 1960 | */ |
| 1961 | if( locktype==SHARED_LOCK ){ |
| 1962 | pFile->locktype = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1963 | return SQLITE_OK; |
| 1964 | } |
| 1965 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1966 | /* To fully unlock the database, delete the lock file */ |
| 1967 | assert( locktype==NO_LOCK ); |
| 1968 | if( unlink(zLockFile) ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 1969 | int rc = 0; |
| 1970 | int tErrno = errno; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1971 | if( ENOENT != tErrno ){ |
| 1972 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1973 | } |
| 1974 | if( IS_LOCK_ERROR(rc) ){ |
| 1975 | pFile->lastErrno = tErrno; |
| 1976 | } |
| 1977 | return rc; |
| 1978 | } |
| 1979 | pFile->locktype = NO_LOCK; |
| 1980 | return SQLITE_OK; |
| 1981 | } |
| 1982 | |
| 1983 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1984 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1985 | */ |
| 1986 | static int dotlockClose(sqlite3_file *id) { |
| 1987 | int rc; |
| 1988 | if( id ){ |
| 1989 | unixFile *pFile = (unixFile*)id; |
| 1990 | dotlockUnlock(id, NO_LOCK); |
| 1991 | sqlite3_free(pFile->lockingContext); |
| 1992 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1993 | rc = closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1994 | return rc; |
| 1995 | } |
| 1996 | /****************** End of the dot-file lock implementation ******************* |
| 1997 | ******************************************************************************/ |
| 1998 | |
| 1999 | /****************************************************************************** |
| 2000 | ************************** Begin flock Locking ******************************** |
| 2001 | ** |
| 2002 | ** Use the flock() system call to do file locking. |
| 2003 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2004 | ** flock() locking is like dot-file locking in that the various |
| 2005 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 2006 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 2007 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 2008 | ** still works when you do this, but concurrency is reduced since |
| 2009 | ** only a single process can be reading the database at a time. |
| 2010 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2011 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if |
| 2012 | ** compiling for VXWORKS. |
| 2013 | */ |
| 2014 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2015 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2016 | /* |
| 2017 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2018 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2019 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2020 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2021 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2022 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 2023 | int rc = SQLITE_OK; |
| 2024 | int reserved = 0; |
| 2025 | unixFile *pFile = (unixFile*)id; |
| 2026 | |
| 2027 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2028 | |
| 2029 | assert( pFile ); |
| 2030 | |
| 2031 | /* Check if a thread in this process holds such a lock */ |
| 2032 | if( pFile->locktype>SHARED_LOCK ){ |
| 2033 | reserved = 1; |
| 2034 | } |
| 2035 | |
| 2036 | /* Otherwise see if some other process holds it. */ |
| 2037 | if( !reserved ){ |
| 2038 | /* attempt to get the lock */ |
| 2039 | int lrc = flock(pFile->h, LOCK_EX | LOCK_NB); |
| 2040 | if( !lrc ){ |
| 2041 | /* got the lock, unlock it */ |
| 2042 | lrc = flock(pFile->h, LOCK_UN); |
| 2043 | if ( lrc ) { |
| 2044 | int tErrno = errno; |
| 2045 | /* unlock failed with an error */ |
| 2046 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2047 | if( IS_LOCK_ERROR(lrc) ){ |
| 2048 | pFile->lastErrno = tErrno; |
| 2049 | rc = lrc; |
| 2050 | } |
| 2051 | } |
| 2052 | } else { |
| 2053 | int tErrno = errno; |
| 2054 | reserved = 1; |
| 2055 | /* someone else might have it reserved */ |
| 2056 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2057 | if( IS_LOCK_ERROR(lrc) ){ |
| 2058 | pFile->lastErrno = tErrno; |
| 2059 | rc = lrc; |
| 2060 | } |
| 2061 | } |
| 2062 | } |
drh | 476bda7 | 2009-12-04 14:25:18 +0000 | [diff] [blame] | 2063 | OSTRACE4("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2064 | |
| 2065 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2066 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2067 | rc = SQLITE_OK; |
| 2068 | reserved=1; |
| 2069 | } |
| 2070 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2071 | *pResOut = reserved; |
| 2072 | return rc; |
| 2073 | } |
| 2074 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2075 | /* |
| 2076 | ** Lock the file with the lock specified by parameter locktype - one |
| 2077 | ** of the following: |
| 2078 | ** |
| 2079 | ** (1) SHARED_LOCK |
| 2080 | ** (2) RESERVED_LOCK |
| 2081 | ** (3) PENDING_LOCK |
| 2082 | ** (4) EXCLUSIVE_LOCK |
| 2083 | ** |
| 2084 | ** Sometimes when requesting one lock state, additional lock states |
| 2085 | ** are inserted in between. The locking might fail on one of the later |
| 2086 | ** transitions leaving the lock state different from what it started but |
| 2087 | ** still short of its goal. The following chart shows the allowed |
| 2088 | ** transitions and the inserted intermediate states: |
| 2089 | ** |
| 2090 | ** UNLOCKED -> SHARED |
| 2091 | ** SHARED -> RESERVED |
| 2092 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2093 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2094 | ** PENDING -> EXCLUSIVE |
| 2095 | ** |
| 2096 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 2097 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2098 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2099 | ** access the file. |
| 2100 | ** |
| 2101 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2102 | ** routine to lower a locking level. |
| 2103 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2104 | static int flockLock(sqlite3_file *id, int locktype) { |
| 2105 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2106 | unixFile *pFile = (unixFile*)id; |
| 2107 | |
| 2108 | assert( pFile ); |
| 2109 | |
| 2110 | /* if we already have a lock, it is exclusive. |
| 2111 | ** Just adjust level and punt on outta here. */ |
| 2112 | if (pFile->locktype > NO_LOCK) { |
| 2113 | pFile->locktype = locktype; |
| 2114 | return SQLITE_OK; |
| 2115 | } |
| 2116 | |
| 2117 | /* grab an exclusive lock */ |
| 2118 | |
| 2119 | if (flock(pFile->h, LOCK_EX | LOCK_NB)) { |
| 2120 | int tErrno = errno; |
| 2121 | /* didn't get, must be busy */ |
| 2122 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2123 | if( IS_LOCK_ERROR(rc) ){ |
| 2124 | pFile->lastErrno = tErrno; |
| 2125 | } |
| 2126 | } else { |
| 2127 | /* got it, set the type and return ok */ |
| 2128 | pFile->locktype = locktype; |
| 2129 | } |
drh | 476bda7 | 2009-12-04 14:25:18 +0000 | [diff] [blame] | 2130 | OSTRACE4("LOCK %d %s %s (flock)\n", pFile->h, locktypeName(locktype), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2131 | rc==SQLITE_OK ? "ok" : "failed"); |
| 2132 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2133 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2134 | rc = SQLITE_BUSY; |
| 2135 | } |
| 2136 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2137 | return rc; |
| 2138 | } |
| 2139 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2140 | |
| 2141 | /* |
| 2142 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
| 2143 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2144 | ** |
| 2145 | ** If the locking level of the file descriptor is already at or below |
| 2146 | ** the requested locking level, this routine is a no-op. |
| 2147 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2148 | static int flockUnlock(sqlite3_file *id, int locktype) { |
| 2149 | unixFile *pFile = (unixFile*)id; |
| 2150 | |
| 2151 | assert( pFile ); |
drh | 476bda7 | 2009-12-04 14:25:18 +0000 | [diff] [blame] | 2152 | OSTRACE5("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, locktype, |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2153 | pFile->locktype, getpid()); |
| 2154 | assert( locktype<=SHARED_LOCK ); |
| 2155 | |
| 2156 | /* no-op if possible */ |
| 2157 | if( pFile->locktype==locktype ){ |
| 2158 | return SQLITE_OK; |
| 2159 | } |
| 2160 | |
| 2161 | /* shared can just be set because we always have an exclusive */ |
| 2162 | if (locktype==SHARED_LOCK) { |
| 2163 | pFile->locktype = locktype; |
| 2164 | return SQLITE_OK; |
| 2165 | } |
| 2166 | |
| 2167 | /* no, really, unlock. */ |
| 2168 | int rc = flock(pFile->h, LOCK_UN); |
| 2169 | if (rc) { |
| 2170 | int r, tErrno = errno; |
| 2171 | r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2172 | if( IS_LOCK_ERROR(r) ){ |
| 2173 | pFile->lastErrno = tErrno; |
| 2174 | } |
| 2175 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2176 | if( (r & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2177 | r = SQLITE_BUSY; |
| 2178 | } |
| 2179 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2180 | |
| 2181 | return r; |
| 2182 | } else { |
| 2183 | pFile->locktype = NO_LOCK; |
| 2184 | return SQLITE_OK; |
| 2185 | } |
| 2186 | } |
| 2187 | |
| 2188 | /* |
| 2189 | ** Close a file. |
| 2190 | */ |
| 2191 | static int flockClose(sqlite3_file *id) { |
| 2192 | if( id ){ |
| 2193 | flockUnlock(id, NO_LOCK); |
| 2194 | } |
| 2195 | return closeUnixFile(id); |
| 2196 | } |
| 2197 | |
| 2198 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 2199 | |
| 2200 | /******************* End of the flock lock implementation ********************* |
| 2201 | ******************************************************************************/ |
| 2202 | |
| 2203 | /****************************************************************************** |
| 2204 | ************************ Begin Named Semaphore Locking ************************ |
| 2205 | ** |
| 2206 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2207 | ** |
| 2208 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 2209 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 2210 | ** the database file at a time. This reduces potential concurrency, but |
| 2211 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2212 | */ |
| 2213 | #if OS_VXWORKS |
| 2214 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2215 | /* |
| 2216 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2217 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2218 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2219 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2220 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2221 | static int semCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 2222 | int rc = SQLITE_OK; |
| 2223 | int reserved = 0; |
| 2224 | unixFile *pFile = (unixFile*)id; |
| 2225 | |
| 2226 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2227 | |
| 2228 | assert( pFile ); |
| 2229 | |
| 2230 | /* Check if a thread in this process holds such a lock */ |
| 2231 | if( pFile->locktype>SHARED_LOCK ){ |
| 2232 | reserved = 1; |
| 2233 | } |
| 2234 | |
| 2235 | /* Otherwise see if some other process holds it. */ |
| 2236 | if( !reserved ){ |
| 2237 | sem_t *pSem = pFile->pOpen->pSem; |
| 2238 | struct stat statBuf; |
| 2239 | |
| 2240 | if( sem_trywait(pSem)==-1 ){ |
| 2241 | int tErrno = errno; |
| 2242 | if( EAGAIN != tErrno ){ |
| 2243 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 2244 | pFile->lastErrno = tErrno; |
| 2245 | } else { |
| 2246 | /* someone else has the lock when we are in NO_LOCK */ |
| 2247 | reserved = (pFile->locktype < SHARED_LOCK); |
| 2248 | } |
| 2249 | }else{ |
| 2250 | /* we could have it if we want it */ |
| 2251 | sem_post(pSem); |
| 2252 | } |
| 2253 | } |
drh | 476bda7 | 2009-12-04 14:25:18 +0000 | [diff] [blame] | 2254 | OSTRACE4("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2255 | |
| 2256 | *pResOut = reserved; |
| 2257 | return rc; |
| 2258 | } |
| 2259 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2260 | /* |
| 2261 | ** Lock the file with the lock specified by parameter locktype - one |
| 2262 | ** of the following: |
| 2263 | ** |
| 2264 | ** (1) SHARED_LOCK |
| 2265 | ** (2) RESERVED_LOCK |
| 2266 | ** (3) PENDING_LOCK |
| 2267 | ** (4) EXCLUSIVE_LOCK |
| 2268 | ** |
| 2269 | ** Sometimes when requesting one lock state, additional lock states |
| 2270 | ** are inserted in between. The locking might fail on one of the later |
| 2271 | ** transitions leaving the lock state different from what it started but |
| 2272 | ** still short of its goal. The following chart shows the allowed |
| 2273 | ** transitions and the inserted intermediate states: |
| 2274 | ** |
| 2275 | ** UNLOCKED -> SHARED |
| 2276 | ** SHARED -> RESERVED |
| 2277 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2278 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2279 | ** PENDING -> EXCLUSIVE |
| 2280 | ** |
| 2281 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 2282 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2283 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2284 | ** access the file. |
| 2285 | ** |
| 2286 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2287 | ** routine to lower a locking level. |
| 2288 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2289 | static int semLock(sqlite3_file *id, int locktype) { |
| 2290 | unixFile *pFile = (unixFile*)id; |
| 2291 | int fd; |
| 2292 | sem_t *pSem = pFile->pOpen->pSem; |
| 2293 | int rc = SQLITE_OK; |
| 2294 | |
| 2295 | /* if we already have a lock, it is exclusive. |
| 2296 | ** Just adjust level and punt on outta here. */ |
| 2297 | if (pFile->locktype > NO_LOCK) { |
| 2298 | pFile->locktype = locktype; |
| 2299 | rc = SQLITE_OK; |
| 2300 | goto sem_end_lock; |
| 2301 | } |
| 2302 | |
| 2303 | /* lock semaphore now but bail out when already locked. */ |
| 2304 | if( sem_trywait(pSem)==-1 ){ |
| 2305 | rc = SQLITE_BUSY; |
| 2306 | goto sem_end_lock; |
| 2307 | } |
| 2308 | |
| 2309 | /* got it, set the type and return ok */ |
| 2310 | pFile->locktype = locktype; |
| 2311 | |
| 2312 | sem_end_lock: |
| 2313 | return rc; |
| 2314 | } |
| 2315 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2316 | /* |
| 2317 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
| 2318 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2319 | ** |
| 2320 | ** If the locking level of the file descriptor is already at or below |
| 2321 | ** the requested locking level, this routine is a no-op. |
| 2322 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2323 | static int semUnlock(sqlite3_file *id, int locktype) { |
| 2324 | unixFile *pFile = (unixFile*)id; |
| 2325 | sem_t *pSem = pFile->pOpen->pSem; |
| 2326 | |
| 2327 | assert( pFile ); |
| 2328 | assert( pSem ); |
drh | 476bda7 | 2009-12-04 14:25:18 +0000 | [diff] [blame] | 2329 | OSTRACE5("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, locktype, |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2330 | pFile->locktype, getpid()); |
| 2331 | assert( locktype<=SHARED_LOCK ); |
| 2332 | |
| 2333 | /* no-op if possible */ |
| 2334 | if( pFile->locktype==locktype ){ |
| 2335 | return SQLITE_OK; |
| 2336 | } |
| 2337 | |
| 2338 | /* shared can just be set because we always have an exclusive */ |
| 2339 | if (locktype==SHARED_LOCK) { |
| 2340 | pFile->locktype = locktype; |
| 2341 | return SQLITE_OK; |
| 2342 | } |
| 2343 | |
| 2344 | /* no, really unlock. */ |
| 2345 | if ( sem_post(pSem)==-1 ) { |
| 2346 | int rc, tErrno = errno; |
| 2347 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2348 | if( IS_LOCK_ERROR(rc) ){ |
| 2349 | pFile->lastErrno = tErrno; |
| 2350 | } |
| 2351 | return rc; |
| 2352 | } |
| 2353 | pFile->locktype = NO_LOCK; |
| 2354 | return SQLITE_OK; |
| 2355 | } |
| 2356 | |
| 2357 | /* |
| 2358 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2359 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2360 | static int semClose(sqlite3_file *id) { |
| 2361 | if( id ){ |
| 2362 | unixFile *pFile = (unixFile*)id; |
| 2363 | semUnlock(id, NO_LOCK); |
| 2364 | assert( pFile ); |
| 2365 | unixEnterMutex(); |
| 2366 | releaseLockInfo(pFile->pLock); |
| 2367 | releaseOpenCnt(pFile->pOpen); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2368 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2369 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2370 | } |
| 2371 | return SQLITE_OK; |
| 2372 | } |
| 2373 | |
| 2374 | #endif /* OS_VXWORKS */ |
| 2375 | /* |
| 2376 | ** Named semaphore locking is only available on VxWorks. |
| 2377 | ** |
| 2378 | *************** End of the named semaphore lock implementation **************** |
| 2379 | ******************************************************************************/ |
| 2380 | |
| 2381 | |
| 2382 | /****************************************************************************** |
| 2383 | *************************** Begin AFP Locking ********************************* |
| 2384 | ** |
| 2385 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2386 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2387 | ** |
| 2388 | ** Third-party implementations of AFP are available. But this code here |
| 2389 | ** only works on OSX. |
| 2390 | */ |
| 2391 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2392 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2393 | /* |
| 2394 | ** The afpLockingContext structure contains all afp lock specific state |
| 2395 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2396 | typedef struct afpLockingContext afpLockingContext; |
| 2397 | struct afpLockingContext { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2398 | int reserved; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2399 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2400 | }; |
| 2401 | |
| 2402 | struct ByteRangeLockPB2 |
| 2403 | { |
| 2404 | unsigned long long offset; /* offset to first byte to lock */ |
| 2405 | unsigned long long length; /* nbr of bytes to lock */ |
| 2406 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2407 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2408 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2409 | int fd; /* file desc to assoc this lock with */ |
| 2410 | }; |
| 2411 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2412 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2413 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2414 | /* |
| 2415 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2416 | ** AFP filesystem. |
| 2417 | ** |
| 2418 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2419 | */ |
| 2420 | static int afpSetLock( |
| 2421 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2422 | unixFile *pFile, /* Open file descriptor on path */ |
| 2423 | unsigned long long offset, /* First byte to be locked */ |
| 2424 | unsigned long long length, /* Number of bytes to lock */ |
| 2425 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2426 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2427 | struct ByteRangeLockPB2 pb; |
| 2428 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2429 | |
| 2430 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2431 | pb.startEndFlag = 0; |
| 2432 | pb.offset = offset; |
| 2433 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2434 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2435 | |
| 2436 | OSTRACE6("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2437 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
| 2438 | offset, length); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2439 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2440 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2441 | int rc; |
| 2442 | int tErrno = errno; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2443 | OSTRACE4("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2444 | path, tErrno, strerror(tErrno)); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2445 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2446 | rc = SQLITE_BUSY; |
| 2447 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2448 | rc = sqliteErrorFromPosixError(tErrno, |
| 2449 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2450 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2451 | if( IS_LOCK_ERROR(rc) ){ |
| 2452 | pFile->lastErrno = tErrno; |
| 2453 | } |
| 2454 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2455 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2456 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2457 | } |
| 2458 | } |
| 2459 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2460 | /* |
| 2461 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2462 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2463 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2464 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2465 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2466 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2467 | int rc = SQLITE_OK; |
| 2468 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2469 | unixFile *pFile = (unixFile*)id; |
| 2470 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2471 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2472 | |
| 2473 | assert( pFile ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2474 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2475 | if( context->reserved ){ |
| 2476 | *pResOut = 1; |
| 2477 | return SQLITE_OK; |
| 2478 | } |
| 2479 | unixEnterMutex(); /* Because pFile->pLock is shared across threads */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2480 | |
| 2481 | /* Check if a thread in this process holds such a lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2482 | if( pFile->pLock->locktype>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2483 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2484 | } |
| 2485 | |
| 2486 | /* Otherwise see if some other process holds it. |
| 2487 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2488 | if( !reserved ){ |
| 2489 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2490 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2491 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2492 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2493 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2494 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2495 | } else { |
| 2496 | /* if we failed to get the lock then someone else must have it */ |
| 2497 | reserved = 1; |
| 2498 | } |
| 2499 | if( IS_LOCK_ERROR(lrc) ){ |
| 2500 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2501 | } |
| 2502 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2503 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2504 | unixLeaveMutex(); |
drh | 476bda7 | 2009-12-04 14:25:18 +0000 | [diff] [blame] | 2505 | OSTRACE4("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2506 | |
| 2507 | *pResOut = reserved; |
| 2508 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2509 | } |
| 2510 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2511 | /* |
| 2512 | ** Lock the file with the lock specified by parameter locktype - one |
| 2513 | ** of the following: |
| 2514 | ** |
| 2515 | ** (1) SHARED_LOCK |
| 2516 | ** (2) RESERVED_LOCK |
| 2517 | ** (3) PENDING_LOCK |
| 2518 | ** (4) EXCLUSIVE_LOCK |
| 2519 | ** |
| 2520 | ** Sometimes when requesting one lock state, additional lock states |
| 2521 | ** are inserted in between. The locking might fail on one of the later |
| 2522 | ** transitions leaving the lock state different from what it started but |
| 2523 | ** still short of its goal. The following chart shows the allowed |
| 2524 | ** transitions and the inserted intermediate states: |
| 2525 | ** |
| 2526 | ** UNLOCKED -> SHARED |
| 2527 | ** SHARED -> RESERVED |
| 2528 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2529 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2530 | ** PENDING -> EXCLUSIVE |
| 2531 | ** |
| 2532 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2533 | ** routine to lower a locking level. |
| 2534 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2535 | static int afpLock(sqlite3_file *id, int locktype){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2536 | int rc = SQLITE_OK; |
| 2537 | unixFile *pFile = (unixFile*)id; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2538 | struct unixLockInfo *pLock = pFile->pLock; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2539 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2540 | |
| 2541 | assert( pFile ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2542 | OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 2543 | locktypeName(locktype), locktypeName(pFile->locktype), |
| 2544 | locktypeName(pLock->locktype), pLock->cnt , getpid()); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2545 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2546 | /* 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] | 2547 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2548 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2549 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2550 | if( pFile->locktype>=locktype ){ |
drh | 476bda7 | 2009-12-04 14:25:18 +0000 | [diff] [blame] | 2551 | OSTRACE3("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2552 | locktypeName(locktype)); |
| 2553 | return SQLITE_OK; |
| 2554 | } |
| 2555 | |
| 2556 | /* Make sure the locking sequence is correct |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2557 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 2558 | ** (2) SQLite never explicitly requests a pendig lock. |
| 2559 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2560 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2561 | assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |
| 2562 | assert( locktype!=PENDING_LOCK ); |
| 2563 | assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); |
| 2564 | |
| 2565 | /* This mutex is needed because pFile->pLock is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2566 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2567 | unixEnterMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2568 | |
| 2569 | /* Make sure the current thread owns the pFile. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2570 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2571 | rc = transferOwnership(pFile); |
| 2572 | if( rc!=SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2573 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2574 | return rc; |
| 2575 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2576 | pLock = pFile->pLock; |
| 2577 | |
| 2578 | /* If some thread using this PID has a lock via a different unixFile* |
| 2579 | ** handle that precludes the requested lock, return BUSY. |
| 2580 | */ |
| 2581 | if( (pFile->locktype!=pLock->locktype && |
| 2582 | (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK)) |
| 2583 | ){ |
| 2584 | rc = SQLITE_BUSY; |
| 2585 | goto afp_end_lock; |
| 2586 | } |
| 2587 | |
| 2588 | /* If a SHARED lock is requested, and some thread using this PID already |
| 2589 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 2590 | ** return SQLITE_OK. |
| 2591 | */ |
| 2592 | if( locktype==SHARED_LOCK && |
| 2593 | (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){ |
| 2594 | assert( locktype==SHARED_LOCK ); |
| 2595 | assert( pFile->locktype==0 ); |
| 2596 | assert( pLock->cnt>0 ); |
| 2597 | pFile->locktype = SHARED_LOCK; |
| 2598 | pLock->cnt++; |
| 2599 | pFile->pOpen->nLock++; |
| 2600 | goto afp_end_lock; |
| 2601 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2602 | |
| 2603 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2604 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2605 | ** be released. |
| 2606 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2607 | if( locktype==SHARED_LOCK |
| 2608 | || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2609 | ){ |
| 2610 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2611 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2612 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2613 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2614 | goto afp_end_lock; |
| 2615 | } |
| 2616 | } |
| 2617 | |
| 2618 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2619 | ** operating system calls for the specified lock. |
| 2620 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2621 | if( locktype==SHARED_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2622 | int lrc1, lrc2, lrc1Errno; |
| 2623 | long lk, mask; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2624 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2625 | assert( pLock->cnt==0 ); |
| 2626 | assert( pLock->locktype==0 ); |
| 2627 | |
| 2628 | mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2629 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2630 | /* note that the quality of the randomness doesn't matter that much */ |
| 2631 | lk = random(); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2632 | pLock->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2633 | lrc1 = afpSetLock(context->dbPath, pFile, |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2634 | SHARED_FIRST+pLock->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2635 | if( IS_LOCK_ERROR(lrc1) ){ |
| 2636 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2637 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2638 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2639 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2640 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2641 | if( IS_LOCK_ERROR(lrc1) ) { |
| 2642 | pFile->lastErrno = lrc1Errno; |
| 2643 | rc = lrc1; |
| 2644 | goto afp_end_lock; |
| 2645 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 2646 | rc = lrc2; |
| 2647 | goto afp_end_lock; |
| 2648 | } else if( lrc1 != SQLITE_OK ) { |
| 2649 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2650 | } else { |
| 2651 | pFile->locktype = SHARED_LOCK; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2652 | pFile->pOpen->nLock++; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2653 | pLock->cnt = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2654 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2655 | }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){ |
| 2656 | /* We are trying for an exclusive lock but another thread in this |
| 2657 | ** same process is still holding a shared lock. */ |
| 2658 | rc = SQLITE_BUSY; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2659 | }else{ |
| 2660 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 2661 | ** assumed that there is a SHARED or greater lock on the file |
| 2662 | ** already. |
| 2663 | */ |
| 2664 | int failed = 0; |
| 2665 | assert( 0!=pFile->locktype ); |
| 2666 | if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) { |
| 2667 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2668 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2669 | if( !failed ){ |
| 2670 | context->reserved = 1; |
| 2671 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2672 | } |
| 2673 | if (!failed && locktype == EXCLUSIVE_LOCK) { |
| 2674 | /* Acquire an EXCLUSIVE lock */ |
| 2675 | |
| 2676 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2677 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2678 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2679 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2680 | pLock->sharedByte, 1, 0)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2681 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2682 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2683 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2684 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2685 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2686 | SHARED_FIRST + pLock->sharedByte, 1, 1)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2687 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 2688 | ** a critical I/O error |
| 2689 | */ |
| 2690 | rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : |
| 2691 | SQLITE_IOERR_LOCK; |
| 2692 | goto afp_end_lock; |
| 2693 | } |
| 2694 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2695 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2696 | } |
| 2697 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2698 | if( failed ){ |
| 2699 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2700 | } |
| 2701 | } |
| 2702 | |
| 2703 | if( rc==SQLITE_OK ){ |
| 2704 | pFile->locktype = locktype; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2705 | pLock->locktype = locktype; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2706 | }else if( locktype==EXCLUSIVE_LOCK ){ |
| 2707 | pFile->locktype = PENDING_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2708 | pLock->locktype = PENDING_LOCK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
| 2711 | afp_end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2712 | unixLeaveMutex(); |
drh | 476bda7 | 2009-12-04 14:25:18 +0000 | [diff] [blame] | 2713 | OSTRACE4("LOCK %d %s %s (afp)\n", pFile->h, locktypeName(locktype), |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2714 | rc==SQLITE_OK ? "ok" : "failed"); |
| 2715 | return rc; |
| 2716 | } |
| 2717 | |
| 2718 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2719 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
| 2720 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2721 | ** |
| 2722 | ** If the locking level of the file descriptor is already at or below |
| 2723 | ** the requested locking level, this routine is a no-op. |
| 2724 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2725 | static int afpUnlock(sqlite3_file *id, int locktype) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2726 | int rc = SQLITE_OK; |
| 2727 | unixFile *pFile = (unixFile*)id; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2728 | struct unixLockInfo *pLock; |
| 2729 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 2730 | int skipShared = 0; |
| 2731 | #ifdef SQLITE_TEST |
| 2732 | int h = pFile->h; |
| 2733 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2734 | |
| 2735 | assert( pFile ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2736 | OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, locktype, |
| 2737 | pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid()); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2738 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2739 | assert( locktype<=SHARED_LOCK ); |
| 2740 | if( pFile->locktype<=locktype ){ |
| 2741 | return SQLITE_OK; |
| 2742 | } |
| 2743 | if( CHECK_THREADID(pFile) ){ |
drh | 413c3d3 | 2010-02-23 20:11:56 +0000 | [diff] [blame] | 2744 | return SQLITE_MISUSE_BKPT; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2745 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2746 | unixEnterMutex(); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2747 | pLock = pFile->pLock; |
| 2748 | assert( pLock->cnt!=0 ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2749 | if( pFile->locktype>SHARED_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2750 | assert( pLock->locktype==pFile->locktype ); |
| 2751 | SimulateIOErrorBenign(1); |
| 2752 | SimulateIOError( h=(-1) ) |
| 2753 | SimulateIOErrorBenign(0); |
| 2754 | |
| 2755 | #ifndef NDEBUG |
| 2756 | /* When reducing a lock such that other processes can start |
| 2757 | ** reading the database file again, make sure that the |
| 2758 | ** transaction counter was updated if any part of the database |
| 2759 | ** file changed. If the transaction counter is not updated, |
| 2760 | ** other connections to the same file might not realize that |
| 2761 | ** the file has changed and hence might not know to flush their |
| 2762 | ** cache. The use of a stale cache can lead to database corruption. |
| 2763 | */ |
| 2764 | assert( pFile->inNormalWrite==0 |
| 2765 | || pFile->dbUpdate==0 |
| 2766 | || pFile->transCntrChng==1 ); |
| 2767 | pFile->inNormalWrite = 0; |
| 2768 | #endif |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2769 | |
| 2770 | if( pFile->locktype==EXCLUSIVE_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2771 | rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
| 2772 | if( rc==SQLITE_OK && (locktype==SHARED_LOCK || pLock->cnt>1) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2773 | /* only re-establish the shared lock if necessary */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2774 | int sharedLockByte = SHARED_FIRST+pLock->sharedByte; |
| 2775 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 2776 | } else { |
| 2777 | skipShared = 1; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2778 | } |
| 2779 | } |
| 2780 | if( rc==SQLITE_OK && pFile->locktype>=PENDING_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2781 | rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2782 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2783 | if( rc==SQLITE_OK && pFile->locktype>=RESERVED_LOCK && context->reserved ){ |
| 2784 | rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 2785 | if( !rc ){ |
| 2786 | context->reserved = 0; |
| 2787 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2788 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2789 | if( rc==SQLITE_OK && (locktype==SHARED_LOCK || pLock->cnt>1)){ |
| 2790 | pLock->locktype = SHARED_LOCK; |
| 2791 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2792 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2793 | if( rc==SQLITE_OK && locktype==NO_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2794 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2795 | /* Decrement the shared lock counter. Release the lock using an |
| 2796 | ** OS call only when all threads in this same process have released |
| 2797 | ** the lock. |
| 2798 | */ |
| 2799 | unsigned long long sharedLockByte = SHARED_FIRST+pLock->sharedByte; |
| 2800 | pLock->cnt--; |
| 2801 | if( pLock->cnt==0 ){ |
| 2802 | SimulateIOErrorBenign(1); |
| 2803 | SimulateIOError( h=(-1) ) |
| 2804 | SimulateIOErrorBenign(0); |
| 2805 | if( !skipShared ){ |
| 2806 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 2807 | } |
| 2808 | if( !rc ){ |
| 2809 | pLock->locktype = NO_LOCK; |
| 2810 | pFile->locktype = NO_LOCK; |
| 2811 | } |
| 2812 | } |
| 2813 | if( rc==SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2814 | struct unixOpenCnt *pOpen = pFile->pOpen; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2815 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2816 | pOpen->nLock--; |
| 2817 | assert( pOpen->nLock>=0 ); |
dan | 6aa657f | 2009-08-24 18:57:58 +0000 | [diff] [blame] | 2818 | if( pOpen->nLock==0 ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2819 | rc = closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2820 | } |
| 2821 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2822 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2823 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2824 | unixLeaveMutex(); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2825 | if( rc==SQLITE_OK ) pFile->locktype = locktype; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2826 | return rc; |
| 2827 | } |
| 2828 | |
| 2829 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2830 | ** Close a file & cleanup AFP specific locking context |
| 2831 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2832 | static int afpClose(sqlite3_file *id) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2833 | int rc = SQLITE_OK; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2834 | if( id ){ |
| 2835 | unixFile *pFile = (unixFile*)id; |
| 2836 | afpUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2837 | unixEnterMutex(); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2838 | if( pFile->pOpen && pFile->pOpen->nLock ){ |
| 2839 | /* If there are outstanding locks, do not actually close the file just |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2840 | ** yet because that would clear those locks. Instead, add the file |
| 2841 | ** descriptor to pOpen->aPending. It will be automatically closed when |
| 2842 | ** the last lock is cleared. |
| 2843 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2844 | setPendingFd(pFile); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2845 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2846 | releaseLockInfo(pFile->pLock); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2847 | releaseOpenCnt(pFile->pOpen); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2848 | sqlite3_free(pFile->lockingContext); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2849 | rc = closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2850 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2851 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2852 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2855 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2856 | /* |
| 2857 | ** The code above is the AFP lock implementation. The code is specific |
| 2858 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2859 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 2860 | ** VFS is not available. |
| 2861 | ** |
| 2862 | ********************* End of the AFP lock implementation ********************** |
| 2863 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2864 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2865 | /****************************************************************************** |
| 2866 | *************************** Begin NFS Locking ********************************/ |
| 2867 | |
| 2868 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 2869 | /* |
| 2870 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
| 2871 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2872 | ** |
| 2873 | ** If the locking level of the file descriptor is already at or below |
| 2874 | ** the requested locking level, this routine is a no-op. |
| 2875 | */ |
| 2876 | static int nfsUnlock(sqlite3_file *id, int locktype){ |
| 2877 | return _posixUnlock(id, locktype, 1); |
| 2878 | } |
| 2879 | |
| 2880 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 2881 | /* |
| 2882 | ** The code above is the NFS lock implementation. The code is specific |
| 2883 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2884 | ** is available. |
| 2885 | ** |
| 2886 | ********************* End of the NFS lock implementation ********************** |
| 2887 | ******************************************************************************/ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2888 | |
| 2889 | /****************************************************************************** |
| 2890 | **************** Non-locking sqlite3_file methods ***************************** |
| 2891 | ** |
| 2892 | ** The next division contains implementations for all methods of the |
| 2893 | ** sqlite3_file object other than the locking methods. The locking |
| 2894 | ** methods were defined in divisions above (one locking method per |
| 2895 | ** division). Those methods that are common to all locking modes |
| 2896 | ** are gather together into this division. |
| 2897 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2898 | |
| 2899 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2900 | ** Seek to the offset passed as the second argument, then read cnt |
| 2901 | ** bytes into pBuf. Return the number of bytes actually read. |
| 2902 | ** |
| 2903 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 2904 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 2905 | ** one system to another. Since SQLite does not define USE_PREAD |
| 2906 | ** any any form by default, we will not attempt to define _XOPEN_SOURCE. |
| 2907 | ** See tickets #2741 and #2681. |
| 2908 | ** |
| 2909 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 2910 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2911 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2912 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 2913 | int got; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2914 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2915 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2916 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2917 | TIMER_START; |
| 2918 | #if defined(USE_PREAD) |
| 2919 | got = pread(id->h, pBuf, cnt, offset); |
| 2920 | SimulateIOError( got = -1 ); |
| 2921 | #elif defined(USE_PREAD64) |
| 2922 | got = pread64(id->h, pBuf, cnt, offset); |
| 2923 | SimulateIOError( got = -1 ); |
| 2924 | #else |
| 2925 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 2926 | SimulateIOError( newOffset-- ); |
| 2927 | if( newOffset!=offset ){ |
| 2928 | if( newOffset == -1 ){ |
| 2929 | ((unixFile*)id)->lastErrno = errno; |
| 2930 | }else{ |
| 2931 | ((unixFile*)id)->lastErrno = 0; |
| 2932 | } |
| 2933 | return -1; |
| 2934 | } |
| 2935 | got = read(id->h, pBuf, cnt); |
| 2936 | #endif |
| 2937 | TIMER_END; |
| 2938 | if( got<0 ){ |
| 2939 | ((unixFile*)id)->lastErrno = errno; |
| 2940 | } |
| 2941 | OSTRACE5("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED); |
| 2942 | return got; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2943 | } |
| 2944 | |
| 2945 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2946 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 2947 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 2948 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2949 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2950 | static int unixRead( |
| 2951 | sqlite3_file *id, |
| 2952 | void *pBuf, |
| 2953 | int amt, |
| 2954 | sqlite3_int64 offset |
| 2955 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2956 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2957 | int got; |
| 2958 | assert( id ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2959 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2960 | /* If this is a database file (not a journal, master-journal or temp |
| 2961 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2962 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 2963 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2964 | || offset>=PENDING_BYTE+512 |
| 2965 | || offset+amt<=PENDING_BYTE |
| 2966 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2967 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2968 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2969 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2970 | if( got==amt ){ |
| 2971 | return SQLITE_OK; |
| 2972 | }else if( got<0 ){ |
| 2973 | /* lastErrno set by seekAndRead */ |
| 2974 | return SQLITE_IOERR_READ; |
| 2975 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2976 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2977 | /* Unread parts of the buffer must be zero-filled */ |
| 2978 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 2979 | return SQLITE_IOERR_SHORT_READ; |
| 2980 | } |
| 2981 | } |
| 2982 | |
| 2983 | /* |
| 2984 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 2985 | ** Return the number of bytes actually read. Update the offset. |
| 2986 | ** |
| 2987 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 2988 | ** is set before returning. |
| 2989 | */ |
| 2990 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
| 2991 | int got; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2992 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2993 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2994 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2995 | TIMER_START; |
| 2996 | #if defined(USE_PREAD) |
| 2997 | got = pwrite(id->h, pBuf, cnt, offset); |
| 2998 | #elif defined(USE_PREAD64) |
| 2999 | got = pwrite64(id->h, pBuf, cnt, offset); |
| 3000 | #else |
| 3001 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 3002 | if( newOffset!=offset ){ |
| 3003 | if( newOffset == -1 ){ |
| 3004 | ((unixFile*)id)->lastErrno = errno; |
| 3005 | }else{ |
| 3006 | ((unixFile*)id)->lastErrno = 0; |
| 3007 | } |
| 3008 | return -1; |
| 3009 | } |
| 3010 | got = write(id->h, pBuf, cnt); |
| 3011 | #endif |
| 3012 | TIMER_END; |
| 3013 | if( got<0 ){ |
| 3014 | ((unixFile*)id)->lastErrno = errno; |
| 3015 | } |
| 3016 | |
| 3017 | OSTRACE5("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED); |
| 3018 | return got; |
| 3019 | } |
| 3020 | |
| 3021 | |
| 3022 | /* |
| 3023 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 3024 | ** or some other error code on failure. |
| 3025 | */ |
| 3026 | static int unixWrite( |
| 3027 | sqlite3_file *id, |
| 3028 | const void *pBuf, |
| 3029 | int amt, |
| 3030 | sqlite3_int64 offset |
| 3031 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3032 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3033 | int wrote = 0; |
| 3034 | assert( id ); |
| 3035 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3036 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3037 | /* If this is a database file (not a journal, master-journal or temp |
| 3038 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3039 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3040 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3041 | || offset>=PENDING_BYTE+512 |
| 3042 | || offset+amt<=PENDING_BYTE |
| 3043 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3044 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3045 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3046 | #ifndef NDEBUG |
| 3047 | /* If we are doing a normal write to a database file (as opposed to |
| 3048 | ** doing a hot-journal rollback or a write to some file other than a |
| 3049 | ** normal database file) then record the fact that the database |
| 3050 | ** has changed. If the transaction counter is modified, record that |
| 3051 | ** fact too. |
| 3052 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3053 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3054 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 3055 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3056 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3057 | char oldCntr[4]; |
| 3058 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3059 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3060 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3061 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3062 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 3063 | } |
| 3064 | } |
| 3065 | } |
| 3066 | #endif |
| 3067 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3068 | while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3069 | amt -= wrote; |
| 3070 | offset += wrote; |
| 3071 | pBuf = &((char*)pBuf)[wrote]; |
| 3072 | } |
| 3073 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 3074 | SimulateDiskfullError(( wrote=0, amt=1 )); |
| 3075 | if( amt>0 ){ |
| 3076 | if( wrote<0 ){ |
| 3077 | /* lastErrno set by seekAndWrite */ |
| 3078 | return SQLITE_IOERR_WRITE; |
| 3079 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3080 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3081 | return SQLITE_FULL; |
| 3082 | } |
| 3083 | } |
| 3084 | return SQLITE_OK; |
| 3085 | } |
| 3086 | |
| 3087 | #ifdef SQLITE_TEST |
| 3088 | /* |
| 3089 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3090 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3091 | */ |
| 3092 | int sqlite3_sync_count = 0; |
| 3093 | int sqlite3_fullsync_count = 0; |
| 3094 | #endif |
| 3095 | |
| 3096 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3097 | ** We do not trust systems to provide a working fdatasync(). Some do. |
| 3098 | ** Others do no. To be safe, we will stick with the (slower) fsync(). |
| 3099 | ** If you know that your system does support fdatasync() correctly, |
| 3100 | ** then simply compile with -Dfdatasync=fdatasync |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3101 | */ |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3102 | #if !defined(fdatasync) && !defined(__linux__) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3103 | # define fdatasync fsync |
| 3104 | #endif |
| 3105 | |
| 3106 | /* |
| 3107 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 3108 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 3109 | ** only available on Mac OS X. But that could change. |
| 3110 | */ |
| 3111 | #ifdef F_FULLFSYNC |
| 3112 | # define HAVE_FULLFSYNC 1 |
| 3113 | #else |
| 3114 | # define HAVE_FULLFSYNC 0 |
| 3115 | #endif |
| 3116 | |
| 3117 | |
| 3118 | /* |
| 3119 | ** The fsync() system call does not work as advertised on many |
| 3120 | ** unix systems. The following procedure is an attempt to make |
| 3121 | ** it work better. |
| 3122 | ** |
| 3123 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 3124 | ** for testing when we want to run through the test suite quickly. |
| 3125 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 3126 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 3127 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3128 | ** |
| 3129 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 3130 | ** The idea behind dataOnly is that it should only write the file content |
| 3131 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 3132 | ** unchanged since the file size is part of the inode. However, |
| 3133 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 3134 | ** file size has changed. The only real difference between fdatasync() |
| 3135 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 3136 | ** inode if the mtime or owner or other inode attributes have changed. |
| 3137 | ** We only care about the file size, not the other file attributes, so |
| 3138 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 3139 | ** So, we always use fdatasync() if it is available, regardless of |
| 3140 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3141 | */ |
| 3142 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3143 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3144 | |
| 3145 | /* The following "ifdef/elif/else/" block has the same structure as |
| 3146 | ** the one below. It is replicated here solely to avoid cluttering |
| 3147 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 3148 | */ |
| 3149 | #ifdef SQLITE_NO_SYNC |
| 3150 | UNUSED_PARAMETER(fd); |
| 3151 | UNUSED_PARAMETER(fullSync); |
| 3152 | UNUSED_PARAMETER(dataOnly); |
| 3153 | #elif HAVE_FULLFSYNC |
| 3154 | UNUSED_PARAMETER(dataOnly); |
| 3155 | #else |
| 3156 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3157 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3158 | #endif |
| 3159 | |
| 3160 | /* Record the number of times that we do a normal fsync() and |
| 3161 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 3162 | ** gets called with the correct arguments. |
| 3163 | */ |
| 3164 | #ifdef SQLITE_TEST |
| 3165 | if( fullSync ) sqlite3_fullsync_count++; |
| 3166 | sqlite3_sync_count++; |
| 3167 | #endif |
| 3168 | |
| 3169 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 3170 | ** no-op |
| 3171 | */ |
| 3172 | #ifdef SQLITE_NO_SYNC |
| 3173 | rc = SQLITE_OK; |
| 3174 | #elif HAVE_FULLFSYNC |
| 3175 | if( fullSync ){ |
| 3176 | rc = fcntl(fd, F_FULLFSYNC, 0); |
| 3177 | }else{ |
| 3178 | rc = 1; |
| 3179 | } |
| 3180 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3181 | ** It shouldn't be possible for fullfsync to fail on the local |
| 3182 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 3183 | ** isn't supported for this file system. So, attempt an fsync |
| 3184 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 3185 | ** It'd be better to detect fullfsync support once and avoid |
| 3186 | ** the fcntl call every time sync is called. |
| 3187 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3188 | if( rc ) rc = fsync(fd); |
| 3189 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3190 | #elif defined(__APPLE__) |
| 3191 | /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 3192 | ** so currently we default to the macro that redefines fdatasync to fsync |
| 3193 | */ |
| 3194 | rc = fsync(fd); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3195 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3196 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 3197 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3198 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3199 | rc = fsync(fd); |
| 3200 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3201 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3202 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 3203 | |
| 3204 | if( OS_VXWORKS && rc!= -1 ){ |
| 3205 | rc = 0; |
| 3206 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3207 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3208 | } |
| 3209 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3210 | /* |
| 3211 | ** Make sure all writes to a particular file are committed to disk. |
| 3212 | ** |
| 3213 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 3214 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 3215 | ** file data is synced. |
| 3216 | ** |
| 3217 | ** Under Unix, also make sure that the directory entry for the file |
| 3218 | ** has been created by fsync-ing the directory that contains the file. |
| 3219 | ** If we do not do this and we encounter a power failure, the directory |
| 3220 | ** entry for the journal might not exist after we reboot. The next |
| 3221 | ** SQLite to access the file will not know that the journal exists (because |
| 3222 | ** the directory entry for the journal was never created) and the transaction |
| 3223 | ** will not roll back - possibly leading to database corruption. |
| 3224 | */ |
| 3225 | static int unixSync(sqlite3_file *id, int flags){ |
| 3226 | int rc; |
| 3227 | unixFile *pFile = (unixFile*)id; |
| 3228 | |
| 3229 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 3230 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 3231 | |
| 3232 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 3233 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 3234 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 3235 | ); |
| 3236 | |
| 3237 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 3238 | ** line is to test that doing so does not cause any problems. |
| 3239 | */ |
| 3240 | SimulateDiskfullError( return SQLITE_FULL ); |
| 3241 | |
| 3242 | assert( pFile ); |
| 3243 | OSTRACE2("SYNC %-3d\n", pFile->h); |
| 3244 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 3245 | SimulateIOError( rc=1 ); |
| 3246 | if( rc ){ |
| 3247 | pFile->lastErrno = errno; |
| 3248 | return SQLITE_IOERR_FSYNC; |
| 3249 | } |
| 3250 | if( pFile->dirfd>=0 ){ |
| 3251 | int err; |
| 3252 | OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd, |
| 3253 | HAVE_FULLFSYNC, isFullsync); |
| 3254 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 3255 | /* The directory sync is only attempted if full_fsync is |
| 3256 | ** turned off or unavailable. If a full_fsync occurred above, |
| 3257 | ** then the directory sync is superfluous. |
| 3258 | */ |
| 3259 | if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){ |
| 3260 | /* |
| 3261 | ** We have received multiple reports of fsync() returning |
| 3262 | ** errors when applied to directories on certain file systems. |
| 3263 | ** A failed directory sync is not a big deal. So it seems |
| 3264 | ** better to ignore the error. Ticket #1657 |
| 3265 | */ |
| 3266 | /* pFile->lastErrno = errno; */ |
| 3267 | /* return SQLITE_IOERR; */ |
| 3268 | } |
| 3269 | #endif |
| 3270 | err = close(pFile->dirfd); /* Only need to sync once, so close the */ |
| 3271 | if( err==0 ){ /* directory when we are done */ |
| 3272 | pFile->dirfd = -1; |
| 3273 | }else{ |
| 3274 | pFile->lastErrno = errno; |
| 3275 | rc = SQLITE_IOERR_DIR_CLOSE; |
| 3276 | } |
| 3277 | } |
| 3278 | return rc; |
| 3279 | } |
| 3280 | |
| 3281 | /* |
| 3282 | ** Truncate an open file to a specified size |
| 3283 | */ |
| 3284 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
| 3285 | int rc; |
| 3286 | assert( id ); |
| 3287 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
| 3288 | rc = ftruncate(((unixFile*)id)->h, (off_t)nByte); |
| 3289 | if( rc ){ |
| 3290 | ((unixFile*)id)->lastErrno = errno; |
| 3291 | return SQLITE_IOERR_TRUNCATE; |
| 3292 | }else{ |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3293 | #ifndef NDEBUG |
| 3294 | /* If we are doing a normal write to a database file (as opposed to |
| 3295 | ** doing a hot-journal rollback or a write to some file other than a |
| 3296 | ** normal database file) and we truncate the file to zero length, |
| 3297 | ** that effectively updates the change counter. This might happen |
| 3298 | ** when restoring a database using the backup API from a zero-length |
| 3299 | ** source. |
| 3300 | */ |
| 3301 | if( ((unixFile*)id)->inNormalWrite && nByte==0 ){ |
| 3302 | ((unixFile*)id)->transCntrChng = 1; |
| 3303 | } |
| 3304 | #endif |
| 3305 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3306 | return SQLITE_OK; |
| 3307 | } |
| 3308 | } |
| 3309 | |
| 3310 | /* |
| 3311 | ** Determine the current size of a file in bytes |
| 3312 | */ |
| 3313 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 3314 | int rc; |
| 3315 | struct stat buf; |
| 3316 | assert( id ); |
| 3317 | rc = fstat(((unixFile*)id)->h, &buf); |
| 3318 | SimulateIOError( rc=1 ); |
| 3319 | if( rc!=0 ){ |
| 3320 | ((unixFile*)id)->lastErrno = errno; |
| 3321 | return SQLITE_IOERR_FSTAT; |
| 3322 | } |
| 3323 | *pSize = buf.st_size; |
| 3324 | |
| 3325 | /* When opening a zero-size database, the findLockInfo() procedure |
| 3326 | ** writes a single byte into that file in order to work around a bug |
| 3327 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 3328 | ** layers, we need to report this file size as zero even though it is |
| 3329 | ** really 1. Ticket #3260. |
| 3330 | */ |
| 3331 | if( *pSize==1 ) *pSize = 0; |
| 3332 | |
| 3333 | |
| 3334 | return SQLITE_OK; |
| 3335 | } |
| 3336 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3337 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3338 | /* |
| 3339 | ** Handler for proxy-locking file-control verbs. Defined below in the |
| 3340 | ** proxying locking division. |
| 3341 | */ |
| 3342 | static int proxyFileControl(sqlite3_file*,int,void*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3343 | #endif |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3344 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3345 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3346 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3347 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3348 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3349 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3350 | switch( op ){ |
| 3351 | case SQLITE_FCNTL_LOCKSTATE: { |
| 3352 | *(int*)pArg = ((unixFile*)id)->locktype; |
| 3353 | return SQLITE_OK; |
| 3354 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3355 | case SQLITE_LAST_ERRNO: { |
| 3356 | *(int*)pArg = ((unixFile*)id)->lastErrno; |
| 3357 | return SQLITE_OK; |
| 3358 | } |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3359 | #ifndef NDEBUG |
| 3360 | /* The pager calls this method to signal that it has done |
| 3361 | ** a rollback and that the database is therefore unchanged and |
| 3362 | ** it hence it is OK for the transaction change counter to be |
| 3363 | ** unchanged. |
| 3364 | */ |
| 3365 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 3366 | ((unixFile*)id)->dbUpdate = 0; |
| 3367 | return SQLITE_OK; |
| 3368 | } |
| 3369 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3370 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3371 | case SQLITE_SET_LOCKPROXYFILE: |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3372 | case SQLITE_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3373 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3374 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3375 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3376 | } |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3377 | return SQLITE_ERROR; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 3378 | } |
| 3379 | |
| 3380 | /* |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3381 | ** Return the sector size in bytes of the underlying block device for |
| 3382 | ** the specified file. This is almost always 512 bytes, but may be |
| 3383 | ** larger for some devices. |
| 3384 | ** |
| 3385 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 3386 | ** if two files are created in the same file-system directory (i.e. |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3387 | ** a database and its journal file) that the sector size will be the |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3388 | ** same for both. |
| 3389 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3390 | static int unixSectorSize(sqlite3_file *NotUsed){ |
| 3391 | UNUSED_PARAMETER(NotUsed); |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 3392 | return SQLITE_DEFAULT_SECTOR_SIZE; |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3393 | } |
| 3394 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3395 | /* |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3396 | ** Return the device characteristics for the file. This is always 0 for unix. |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3397 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3398 | static int unixDeviceCharacteristics(sqlite3_file *NotUsed){ |
| 3399 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3400 | return 0; |
| 3401 | } |
| 3402 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3403 | /* |
| 3404 | ** Here ends the implementation of all sqlite3_file methods. |
| 3405 | ** |
| 3406 | ********************** End sqlite3_file Methods ******************************* |
| 3407 | ******************************************************************************/ |
| 3408 | |
| 3409 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3410 | ** This division contains definitions of sqlite3_io_methods objects that |
| 3411 | ** implement various file locking strategies. It also contains definitions |
| 3412 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 3413 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 3414 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 3415 | ** the correct finder-function for that VFS. |
| 3416 | ** |
| 3417 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 3418 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 3419 | ** looks at the filesystem type and tries to guess the best locking |
| 3420 | ** strategy from that. |
| 3421 | ** |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3422 | ** For finder-funtion F, two objects are created: |
| 3423 | ** |
| 3424 | ** (1) The real finder-function named "FImpt()". |
| 3425 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3426 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3427 | ** |
| 3428 | ** |
| 3429 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 3430 | ** objects. We have to do this instead of letting pAppData point |
| 3431 | ** directly at the finder-function since C90 rules prevent a void* |
| 3432 | ** from be cast into a function pointer. |
| 3433 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3434 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3435 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3436 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3437 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 3438 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 3439 | ** |
| 3440 | ** * An I/O method finder function called FINDER that returns a pointer |
| 3441 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3442 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3443 | #define IOMETHODS(FINDER, METHOD, CLOSE, LOCK, UNLOCK, CKLOCK) \ |
| 3444 | static const sqlite3_io_methods METHOD = { \ |
| 3445 | 1, /* iVersion */ \ |
| 3446 | CLOSE, /* xClose */ \ |
| 3447 | unixRead, /* xRead */ \ |
| 3448 | unixWrite, /* xWrite */ \ |
| 3449 | unixTruncate, /* xTruncate */ \ |
| 3450 | unixSync, /* xSync */ \ |
| 3451 | unixFileSize, /* xFileSize */ \ |
| 3452 | LOCK, /* xLock */ \ |
| 3453 | UNLOCK, /* xUnlock */ \ |
| 3454 | CKLOCK, /* xCheckReservedLock */ \ |
| 3455 | unixFileControl, /* xFileControl */ \ |
| 3456 | unixSectorSize, /* xSectorSize */ \ |
| 3457 | unixDeviceCharacteristics /* xDeviceCapabilities */ \ |
| 3458 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3459 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 3460 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3461 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3462 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3463 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3464 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3465 | |
| 3466 | /* |
| 3467 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 3468 | ** locking strategies. Functions that return pointers to these methods |
| 3469 | ** are also created. |
| 3470 | */ |
| 3471 | IOMETHODS( |
| 3472 | posixIoFinder, /* Finder function name */ |
| 3473 | posixIoMethods, /* sqlite3_io_methods object name */ |
| 3474 | unixClose, /* xClose method */ |
| 3475 | unixLock, /* xLock method */ |
| 3476 | unixUnlock, /* xUnlock method */ |
| 3477 | unixCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3478 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3479 | IOMETHODS( |
| 3480 | nolockIoFinder, /* Finder function name */ |
| 3481 | nolockIoMethods, /* sqlite3_io_methods object name */ |
| 3482 | nolockClose, /* xClose method */ |
| 3483 | nolockLock, /* xLock method */ |
| 3484 | nolockUnlock, /* xUnlock method */ |
| 3485 | nolockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3486 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3487 | IOMETHODS( |
| 3488 | dotlockIoFinder, /* Finder function name */ |
| 3489 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
| 3490 | dotlockClose, /* xClose method */ |
| 3491 | dotlockLock, /* xLock method */ |
| 3492 | dotlockUnlock, /* xUnlock method */ |
| 3493 | dotlockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3494 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3495 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3496 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3497 | IOMETHODS( |
| 3498 | flockIoFinder, /* Finder function name */ |
| 3499 | flockIoMethods, /* sqlite3_io_methods object name */ |
| 3500 | flockClose, /* xClose method */ |
| 3501 | flockLock, /* xLock method */ |
| 3502 | flockUnlock, /* xUnlock method */ |
| 3503 | flockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3504 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3505 | #endif |
| 3506 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3507 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3508 | IOMETHODS( |
| 3509 | semIoFinder, /* Finder function name */ |
| 3510 | semIoMethods, /* sqlite3_io_methods object name */ |
| 3511 | semClose, /* xClose method */ |
| 3512 | semLock, /* xLock method */ |
| 3513 | semUnlock, /* xUnlock method */ |
| 3514 | semCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3515 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3516 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3517 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3518 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3519 | IOMETHODS( |
| 3520 | afpIoFinder, /* Finder function name */ |
| 3521 | afpIoMethods, /* sqlite3_io_methods object name */ |
| 3522 | afpClose, /* xClose method */ |
| 3523 | afpLock, /* xLock method */ |
| 3524 | afpUnlock, /* xUnlock method */ |
| 3525 | afpCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3526 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3527 | #endif |
| 3528 | |
| 3529 | /* |
| 3530 | ** The proxy locking method is a "super-method" in the sense that it |
| 3531 | ** opens secondary file descriptors for the conch and lock files and |
| 3532 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 3533 | ** secondary files. For this reason, the division that implements |
| 3534 | ** proxy locking is located much further down in the file. But we need |
| 3535 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 3536 | ** for proxy locking here. So we forward declare the I/O methods. |
| 3537 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3538 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3539 | static int proxyClose(sqlite3_file*); |
| 3540 | static int proxyLock(sqlite3_file*, int); |
| 3541 | static int proxyUnlock(sqlite3_file*, int); |
| 3542 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3543 | IOMETHODS( |
| 3544 | proxyIoFinder, /* Finder function name */ |
| 3545 | proxyIoMethods, /* sqlite3_io_methods object name */ |
| 3546 | proxyClose, /* xClose method */ |
| 3547 | proxyLock, /* xLock method */ |
| 3548 | proxyUnlock, /* xUnlock method */ |
| 3549 | proxyCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3550 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3551 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3552 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3553 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 3554 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 3555 | IOMETHODS( |
| 3556 | nfsIoFinder, /* Finder function name */ |
| 3557 | nfsIoMethods, /* sqlite3_io_methods object name */ |
| 3558 | unixClose, /* xClose method */ |
| 3559 | unixLock, /* xLock method */ |
| 3560 | nfsUnlock, /* xUnlock method */ |
| 3561 | unixCheckReservedLock /* xCheckReservedLock method */ |
| 3562 | ) |
| 3563 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3564 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3565 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3566 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3567 | ** This "finder" function attempts to determine the best locking strategy |
| 3568 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3569 | ** object that implements that strategy. |
| 3570 | ** |
| 3571 | ** This is for MacOSX only. |
| 3572 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3573 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3574 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3575 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3576 | ){ |
| 3577 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3578 | const char *zFilesystem; /* Filesystem type name */ |
| 3579 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3580 | } aMap[] = { |
| 3581 | { "hfs", &posixIoMethods }, |
| 3582 | { "ufs", &posixIoMethods }, |
| 3583 | { "afpfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3584 | { "smbfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3585 | { "webdav", &nolockIoMethods }, |
| 3586 | { 0, 0 } |
| 3587 | }; |
| 3588 | int i; |
| 3589 | struct statfs fsInfo; |
| 3590 | struct flock lockInfo; |
| 3591 | |
| 3592 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3593 | /* If filePath==NULL that means we are dealing with a transient file |
| 3594 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3595 | return &nolockIoMethods; |
| 3596 | } |
| 3597 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 3598 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 3599 | return &nolockIoMethods; |
| 3600 | } |
| 3601 | for(i=0; aMap[i].zFilesystem; i++){ |
| 3602 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 3603 | return aMap[i].pMethods; |
| 3604 | } |
| 3605 | } |
| 3606 | } |
| 3607 | |
| 3608 | /* Default case. Handles, amongst others, "nfs". |
| 3609 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 3610 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3611 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3612 | lockInfo.l_len = 1; |
| 3613 | lockInfo.l_start = 0; |
| 3614 | lockInfo.l_whence = SEEK_SET; |
| 3615 | lockInfo.l_type = F_RDLCK; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3616 | if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3617 | if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 3618 | return &nfsIoMethods; |
| 3619 | } else { |
| 3620 | return &posixIoMethods; |
| 3621 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3622 | }else{ |
| 3623 | return &dotlockIoMethods; |
| 3624 | } |
| 3625 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3626 | static const sqlite3_io_methods |
| 3627 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3628 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3629 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3630 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3631 | #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE |
| 3632 | /* |
| 3633 | ** This "finder" function attempts to determine the best locking strategy |
| 3634 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
| 3635 | ** object that implements that strategy. |
| 3636 | ** |
| 3637 | ** This is for VXWorks only. |
| 3638 | */ |
| 3639 | static const sqlite3_io_methods *autolockIoFinderImpl( |
| 3640 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3641 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3642 | ){ |
| 3643 | struct flock lockInfo; |
| 3644 | |
| 3645 | if( !filePath ){ |
| 3646 | /* If filePath==NULL that means we are dealing with a transient file |
| 3647 | ** that does not need to be locked. */ |
| 3648 | return &nolockIoMethods; |
| 3649 | } |
| 3650 | |
| 3651 | /* Test if fcntl() is supported and use POSIX style locks. |
| 3652 | ** Otherwise fall back to the named semaphore method. |
| 3653 | */ |
| 3654 | lockInfo.l_len = 1; |
| 3655 | lockInfo.l_start = 0; |
| 3656 | lockInfo.l_whence = SEEK_SET; |
| 3657 | lockInfo.l_type = F_RDLCK; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3658 | if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3659 | return &posixIoMethods; |
| 3660 | }else{ |
| 3661 | return &semIoMethods; |
| 3662 | } |
| 3663 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3664 | static const sqlite3_io_methods |
| 3665 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3666 | |
| 3667 | #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */ |
| 3668 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3669 | /* |
| 3670 | ** An abstract type for a pointer to a IO method finder function: |
| 3671 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3672 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3673 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3674 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3675 | /**************************************************************************** |
| 3676 | **************************** sqlite3_vfs methods **************************** |
| 3677 | ** |
| 3678 | ** This division contains the implementation of methods on the |
| 3679 | ** sqlite3_vfs object. |
| 3680 | */ |
| 3681 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3682 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3683 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3684 | */ |
| 3685 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3686 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3687 | int h, /* Open file descriptor of file being opened */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3688 | int dirfd, /* Directory file descriptor */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 3689 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 3690 | const char *zFilename, /* Name of the file being opened */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3691 | int noLock, /* Omit locking if true */ |
| 3692 | int isDelete /* Delete on close if true */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3693 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3694 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 3695 | unixFile *pNew = (unixFile *)pId; |
| 3696 | int rc = SQLITE_OK; |
| 3697 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3698 | assert( pNew->pLock==NULL ); |
| 3699 | assert( pNew->pOpen==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 3700 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3701 | /* Parameter isDelete is only used on vxworks. Express this explicitly |
| 3702 | ** here to prevent compiler warnings about unused parameters. |
danielk1977 | a03396a | 2008-11-19 14:35:46 +0000 | [diff] [blame] | 3703 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3704 | UNUSED_PARAMETER(isDelete); |
danielk1977 | a03396a | 2008-11-19 14:35:46 +0000 | [diff] [blame] | 3705 | |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 3706 | OSTRACE3("OPEN %-3d %s\n", h, zFilename); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3707 | pNew->h = h; |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 3708 | pNew->dirfd = dirfd; |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3709 | SET_THREADID(pNew); |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3710 | pNew->fileFlags = 0; |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3711 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3712 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 3713 | pNew->pId = vxworksFindFileId(zFilename); |
| 3714 | if( pNew->pId==0 ){ |
| 3715 | noLock = 1; |
| 3716 | rc = SQLITE_NOMEM; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3717 | } |
| 3718 | #endif |
| 3719 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 3720 | if( noLock ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3721 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 3722 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3723 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3724 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 3725 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 3726 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 3727 | ** zFilename remains valid until file is closed, to support */ |
| 3728 | pNew->lockingContext = (void*)zFilename; |
| 3729 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 3730 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3731 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3732 | if( pLockingStyle == &posixIoMethods |
| 3733 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 3734 | || pLockingStyle == &nfsIoMethods |
| 3735 | #endif |
| 3736 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3737 | unixEnterMutex(); |
| 3738 | rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3739 | if( rc!=SQLITE_OK ){ |
| 3740 | /* If an error occured in findLockInfo(), close the file descriptor |
| 3741 | ** immediately, before releasing the mutex. findLockInfo() may fail |
| 3742 | ** in two scenarios: |
| 3743 | ** |
| 3744 | ** (a) A call to fstat() failed. |
| 3745 | ** (b) A malloc failed. |
| 3746 | ** |
| 3747 | ** Scenario (b) may only occur if the process is holding no other |
| 3748 | ** file descriptors open on the same file. If there were other file |
| 3749 | ** descriptors on this file, then no malloc would be required by |
| 3750 | ** findLockInfo(). If this is the case, it is quite safe to close |
| 3751 | ** handle h - as it is guaranteed that no posix locks will be released |
| 3752 | ** by doing so. |
| 3753 | ** |
| 3754 | ** If scenario (a) caused the error then things are not so safe. The |
| 3755 | ** implicit assumption here is that if fstat() fails, things are in |
| 3756 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 3757 | */ |
| 3758 | close(h); |
| 3759 | h = -1; |
| 3760 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3761 | unixLeaveMutex(); |
| 3762 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3763 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3764 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 3765 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3766 | /* AFP locking uses the file path so it needs to be included in |
| 3767 | ** the afpLockingContext. |
| 3768 | */ |
| 3769 | afpLockingContext *pCtx; |
| 3770 | pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 3771 | if( pCtx==0 ){ |
| 3772 | rc = SQLITE_NOMEM; |
| 3773 | }else{ |
| 3774 | /* NB: zFilename exists and remains valid until the file is closed |
| 3775 | ** according to requirement F11141. So we do not need to make a |
| 3776 | ** copy of the filename. */ |
| 3777 | pCtx->dbPath = zFilename; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3778 | pCtx->reserved = 0; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3779 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3780 | unixEnterMutex(); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3781 | rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen); |
| 3782 | if( rc!=SQLITE_OK ){ |
| 3783 | sqlite3_free(pNew->lockingContext); |
| 3784 | close(h); |
| 3785 | h = -1; |
| 3786 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3787 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3788 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3789 | } |
| 3790 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3791 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3792 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 3793 | /* Dotfile locking uses the file path so it needs to be included in |
| 3794 | ** the dotlockLockingContext |
| 3795 | */ |
| 3796 | char *zLockFile; |
| 3797 | int nFilename; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 3798 | nFilename = (int)strlen(zFilename) + 6; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3799 | zLockFile = (char *)sqlite3_malloc(nFilename); |
| 3800 | if( zLockFile==0 ){ |
| 3801 | rc = SQLITE_NOMEM; |
| 3802 | }else{ |
| 3803 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3804 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3805 | pNew->lockingContext = zLockFile; |
| 3806 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3807 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3808 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3809 | else if( pLockingStyle == &semIoMethods ){ |
| 3810 | /* Named semaphore locking uses the file path so it needs to be |
| 3811 | ** included in the semLockingContext |
| 3812 | */ |
| 3813 | unixEnterMutex(); |
| 3814 | rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen); |
| 3815 | if( (rc==SQLITE_OK) && (pNew->pOpen->pSem==NULL) ){ |
| 3816 | char *zSemName = pNew->pOpen->aSemName; |
| 3817 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 3818 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3819 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 3820 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3821 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
| 3822 | pNew->pOpen->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 3823 | if( pNew->pOpen->pSem == SEM_FAILED ){ |
| 3824 | rc = SQLITE_NOMEM; |
| 3825 | pNew->pOpen->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3826 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3827 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3828 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3829 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3830 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3831 | |
| 3832 | pNew->lastErrno = 0; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3833 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3834 | if( rc!=SQLITE_OK ){ |
drh | 309e655 | 2010-02-05 18:00:26 +0000 | [diff] [blame] | 3835 | if( h>=0 ) close(h); |
| 3836 | h = -1; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3837 | unlink(zFilename); |
| 3838 | isDelete = 0; |
| 3839 | } |
| 3840 | pNew->isDelete = isDelete; |
| 3841 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3842 | if( rc!=SQLITE_OK ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3843 | if( dirfd>=0 ) close(dirfd); /* silent leak if fail, already in error */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3844 | if( h>=0 ) close(h); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3845 | }else{ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3846 | pNew->pMethod = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3847 | OpenCounter(+1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3848 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3849 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 3850 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 3851 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3852 | /* |
| 3853 | ** Open a file descriptor to the directory containing file zFilename. |
| 3854 | ** If successful, *pFd is set to the opened file descriptor and |
| 3855 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 3856 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 3857 | ** value. |
| 3858 | ** |
| 3859 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 3860 | ** the file descriptor *pFd using close(). |
| 3861 | */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3862 | static int openDirectory(const char *zFilename, int *pFd){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3863 | int ii; |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 3864 | int fd = -1; |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 3865 | char zDirname[MAX_PATHNAME+1]; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3866 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 3867 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
drh | 617634e | 2009-01-08 14:36:20 +0000 | [diff] [blame] | 3868 | for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3869 | if( ii>0 ){ |
| 3870 | zDirname[ii] = '\0'; |
| 3871 | fd = open(zDirname, O_RDONLY|O_BINARY, 0); |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 3872 | if( fd>=0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3873 | #ifdef FD_CLOEXEC |
| 3874 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 3875 | #endif |
| 3876 | OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname); |
| 3877 | } |
| 3878 | } |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3879 | *pFd = fd; |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 3880 | return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN_BKPT); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3881 | } |
| 3882 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3883 | /* |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3884 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 3885 | ** by the calling process and must be big enough to hold at least |
| 3886 | ** pVfs->mxPathname bytes. |
| 3887 | */ |
| 3888 | static int getTempname(int nBuf, char *zBuf){ |
| 3889 | static const char *azDirs[] = { |
| 3890 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3891 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3892 | "/var/tmp", |
| 3893 | "/usr/tmp", |
| 3894 | "/tmp", |
| 3895 | ".", |
| 3896 | }; |
| 3897 | static const unsigned char zChars[] = |
| 3898 | "abcdefghijklmnopqrstuvwxyz" |
| 3899 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 3900 | "0123456789"; |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 3901 | unsigned int i, j; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3902 | struct stat buf; |
| 3903 | const char *zDir = "."; |
| 3904 | |
| 3905 | /* It's odd to simulate an io-error here, but really this is just |
| 3906 | ** using the io-error infrastructure to test that SQLite handles this |
| 3907 | ** function failing. |
| 3908 | */ |
| 3909 | SimulateIOError( return SQLITE_IOERR ); |
| 3910 | |
| 3911 | azDirs[0] = sqlite3_temp_directory; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3912 | if (NULL == azDirs[1]) { |
| 3913 | azDirs[1] = getenv("TMPDIR"); |
| 3914 | } |
| 3915 | |
| 3916 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3917 | if( azDirs[i]==0 ) continue; |
| 3918 | if( stat(azDirs[i], &buf) ) continue; |
| 3919 | if( !S_ISDIR(buf.st_mode) ) continue; |
| 3920 | if( access(azDirs[i], 07) ) continue; |
| 3921 | zDir = azDirs[i]; |
| 3922 | break; |
| 3923 | } |
| 3924 | |
| 3925 | /* Check that the output buffer is large enough for the temporary file |
| 3926 | ** name. If it is not, return SQLITE_ERROR. |
| 3927 | */ |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 3928 | if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3929 | return SQLITE_ERROR; |
| 3930 | } |
| 3931 | |
| 3932 | do{ |
| 3933 | sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 3934 | j = (int)strlen(zBuf); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3935 | sqlite3_randomness(15, &zBuf[j]); |
| 3936 | for(i=0; i<15; i++, j++){ |
| 3937 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 3938 | } |
| 3939 | zBuf[j] = 0; |
| 3940 | }while( access(zBuf,0)==0 ); |
| 3941 | return SQLITE_OK; |
| 3942 | } |
| 3943 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3944 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 3945 | /* |
| 3946 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 3947 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 3948 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 3949 | */ |
| 3950 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3951 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 3952 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3953 | /* |
| 3954 | ** Search for an unused file descriptor that was opened on the database |
| 3955 | ** file (not a journal or master-journal file) identified by pathname |
| 3956 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 3957 | ** argument to this function. |
| 3958 | ** |
| 3959 | ** Such a file descriptor may exist if a database connection was closed |
| 3960 | ** but the associated file descriptor could not be closed because some |
| 3961 | ** other file descriptor open on the same file is holding a file-lock. |
| 3962 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 3963 | ** describing "Posix Advisory Locking" at the start of this file for |
| 3964 | ** further details. Also, ticket #4018. |
| 3965 | ** |
| 3966 | ** If a suitable file descriptor is found, then it is returned. If no |
| 3967 | ** such file descriptor is located, -1 is returned. |
| 3968 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3969 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 3970 | UnixUnusedFd *pUnused = 0; |
| 3971 | |
| 3972 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 3973 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 3974 | ** but because no way to test it is currently available. It is better |
| 3975 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 3976 | ** feature. */ |
| 3977 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3978 | struct stat sStat; /* Results of stat() call */ |
| 3979 | |
| 3980 | /* A stat() call may fail for various reasons. If this happens, it is |
| 3981 | ** almost certain that an open() call on the same path will also fail. |
| 3982 | ** For this reason, if an error occurs in the stat() call here, it is |
| 3983 | ** ignored and -1 is returned. The caller will try to open a new file |
| 3984 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 3985 | ** |
| 3986 | ** Even if a subsequent open() call does succeed, the consequences of |
| 3987 | ** not searching for a resusable file descriptor are not dire. */ |
| 3988 | if( 0==stat(zPath, &sStat) ){ |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 3989 | struct unixOpenCnt *pOpen; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3990 | |
| 3991 | unixEnterMutex(); |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 3992 | pOpen = openList; |
| 3993 | while( pOpen && (pOpen->fileId.dev!=sStat.st_dev |
| 3994 | || pOpen->fileId.ino!=sStat.st_ino) ){ |
| 3995 | pOpen = pOpen->pNext; |
| 3996 | } |
| 3997 | if( pOpen ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3998 | UnixUnusedFd **pp; |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 3999 | for(pp=&pOpen->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4000 | pUnused = *pp; |
| 4001 | if( pUnused ){ |
| 4002 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4003 | } |
| 4004 | } |
| 4005 | unixLeaveMutex(); |
| 4006 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4007 | #endif /* if !OS_VXWORKS */ |
| 4008 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4009 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4010 | |
| 4011 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4012 | ** Open the file zPath. |
| 4013 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4014 | ** Previously, the SQLite OS layer used three functions in place of this |
| 4015 | ** one: |
| 4016 | ** |
| 4017 | ** sqlite3OsOpenReadWrite(); |
| 4018 | ** sqlite3OsOpenReadOnly(); |
| 4019 | ** sqlite3OsOpenExclusive(); |
| 4020 | ** |
| 4021 | ** These calls correspond to the following combinations of flags: |
| 4022 | ** |
| 4023 | ** ReadWrite() -> (READWRITE | CREATE) |
| 4024 | ** ReadOnly() -> (READONLY) |
| 4025 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 4026 | ** |
| 4027 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 4028 | ** true, the file was configured to be automatically deleted when the |
| 4029 | ** file handle closed. To achieve the same effect using this new |
| 4030 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 4031 | ** OpenExclusive(). |
| 4032 | */ |
| 4033 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4034 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 4035 | const char *zPath, /* Pathname of file to be opened */ |
| 4036 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 4037 | int flags, /* Input flags to control the opening */ |
| 4038 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4039 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4040 | unixFile *p = (unixFile *)pFile; |
| 4041 | int fd = -1; /* File descriptor returned by open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4042 | int dirfd = -1; /* Directory file descriptor */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4043 | int openFlags = 0; /* Flags to pass to open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4044 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4045 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4046 | int rc = SQLITE_OK; /* Function Return Code */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4047 | |
| 4048 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 4049 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 4050 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 4051 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 4052 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4053 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4054 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 4055 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4056 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4057 | /* If creating a master or main-file journal, this function will open |
| 4058 | ** a file-descriptor on the directory too. The first time unixSync() |
| 4059 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 4060 | */ |
| 4061 | int isOpenDirectory = (isCreate && |
| 4062 | (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL) |
| 4063 | ); |
| 4064 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4065 | /* If argument zPath is a NULL pointer, this function is required to open |
| 4066 | ** a temporary file. Use this buffer to store the file name in. |
| 4067 | */ |
| 4068 | char zTmpname[MAX_PATHNAME+1]; |
| 4069 | const char *zName = zPath; |
| 4070 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4071 | /* Check the following statements are true: |
| 4072 | ** |
| 4073 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 4074 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 4075 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4076 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4077 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4078 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4079 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4080 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4081 | assert(isDelete==0 || isCreate); |
| 4082 | |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4083 | /* The main DB, main journal, and master journal are never automatically |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4084 | ** deleted. Nor are they ever temporary files. */ |
| 4085 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 4086 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 4087 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4088 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4089 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 4090 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 4091 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 4092 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4093 | || eType==SQLITE_OPEN_TRANSIENT_DB |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4094 | ); |
| 4095 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4096 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4097 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4098 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4099 | UnixUnusedFd *pUnused; |
| 4100 | pUnused = findReusableFd(zName, flags); |
| 4101 | if( pUnused ){ |
| 4102 | fd = pUnused->fd; |
| 4103 | }else{ |
dan | 6aa657f | 2009-08-24 18:57:58 +0000 | [diff] [blame] | 4104 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4105 | if( !pUnused ){ |
| 4106 | return SQLITE_NOMEM; |
| 4107 | } |
| 4108 | } |
| 4109 | p->pUnused = pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4110 | }else if( !zName ){ |
| 4111 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4112 | assert(isDelete && !isOpenDirectory); |
| 4113 | rc = getTempname(MAX_PATHNAME+1, zTmpname); |
| 4114 | if( rc!=SQLITE_OK ){ |
| 4115 | return rc; |
| 4116 | } |
| 4117 | zName = zTmpname; |
| 4118 | } |
| 4119 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4120 | /* Determine the value of the flags parameter passed to POSIX function |
| 4121 | ** open(). These must be calculated even if open() is not called, as |
| 4122 | ** they may be stored as part of the file handle and used by the |
| 4123 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4124 | if( isReadonly ) openFlags |= O_RDONLY; |
| 4125 | if( isReadWrite ) openFlags |= O_RDWR; |
| 4126 | if( isCreate ) openFlags |= O_CREAT; |
| 4127 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 4128 | openFlags |= (O_LARGEFILE|O_BINARY); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4129 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4130 | if( fd<0 ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4131 | mode_t openMode = (isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 4132 | fd = open(zName, openFlags, openMode); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4133 | OSTRACE4("OPENX %-3d %s 0%o\n", fd, zName, openFlags); |
| 4134 | if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ |
| 4135 | /* Failed to open the file for read/write access. Try read-only. */ |
| 4136 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4137 | openFlags &= ~(O_RDWR|O_CREAT); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4138 | flags |= SQLITE_OPEN_READONLY; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4139 | openFlags |= O_RDONLY; |
| 4140 | fd = open(zName, openFlags, openMode); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4141 | } |
| 4142 | if( fd<0 ){ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 4143 | rc = SQLITE_CANTOPEN_BKPT; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4144 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4145 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4146 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4147 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4148 | if( pOutFlags ){ |
| 4149 | *pOutFlags = flags; |
| 4150 | } |
| 4151 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4152 | if( p->pUnused ){ |
| 4153 | p->pUnused->fd = fd; |
| 4154 | p->pUnused->flags = flags; |
| 4155 | } |
| 4156 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4157 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4158 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4159 | zPath = zName; |
| 4160 | #else |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4161 | unlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4162 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4163 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 4164 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4165 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4166 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 4167 | } |
| 4168 | #endif |
| 4169 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4170 | if( isOpenDirectory ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4171 | rc = openDirectory(zPath, &dirfd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4172 | if( rc!=SQLITE_OK ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4173 | /* It is safe to close fd at this point, because it is guaranteed not |
| 4174 | ** to be open on a database file. If it were open on a database file, |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4175 | ** it would not be safe to close as this would release any locks held |
| 4176 | ** on the file by this process. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4177 | assert( eType!=SQLITE_OPEN_MAIN_DB ); |
| 4178 | close(fd); /* silently leak if fail, already in error */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4179 | goto open_finished; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4180 | } |
| 4181 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4182 | |
| 4183 | #ifdef FD_CLOEXEC |
| 4184 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 4185 | #endif |
| 4186 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4187 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4188 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4189 | |
| 4190 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 4191 | struct statfs fsInfo; |
| 4192 | if( fstatfs(fd, &fsInfo) == -1 ){ |
| 4193 | ((unixFile*)pFile)->lastErrno = errno; |
| 4194 | if( dirfd>=0 ) close(dirfd); /* silently leak if fail, in error */ |
| 4195 | close(fd); /* silently leak if fail, in error */ |
| 4196 | return SQLITE_IOERR_ACCESS; |
| 4197 | } |
| 4198 | if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 4199 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 4200 | } |
| 4201 | #endif |
| 4202 | |
| 4203 | #if SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4204 | #if SQLITE_PREFER_PROXY_LOCKING |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4205 | isAutoProxy = 1; |
| 4206 | #endif |
| 4207 | if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4208 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 4209 | int useProxy = 0; |
| 4210 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4211 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 4212 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4213 | if( envforce!=NULL ){ |
| 4214 | useProxy = atoi(envforce)>0; |
| 4215 | }else{ |
| 4216 | struct statfs fsInfo; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4217 | if( statfs(zPath, &fsInfo) == -1 ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4218 | /* In theory, the close(fd) call is sub-optimal. If the file opened |
| 4219 | ** with fd is a database file, and there are other connections open |
| 4220 | ** on that file that are currently holding advisory locks on it, |
| 4221 | ** then the call to close() will cancel those locks. In practice, |
| 4222 | ** we're assuming that statfs() doesn't fail very often. At least |
| 4223 | ** not while other file descriptors opened by the same process on |
| 4224 | ** the same file are working. */ |
| 4225 | p->lastErrno = errno; |
| 4226 | if( dirfd>=0 ){ |
| 4227 | close(dirfd); /* silently leak if fail, in error */ |
| 4228 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4229 | close(fd); /* silently leak if fail, in error */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4230 | rc = SQLITE_IOERR_ACCESS; |
| 4231 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4232 | } |
| 4233 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 4234 | } |
| 4235 | if( useProxy ){ |
| 4236 | rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete); |
| 4237 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4238 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4239 | if( rc!=SQLITE_OK ){ |
| 4240 | /* Use unixClose to clean up the resources added in fillInUnixFile |
| 4241 | ** and clear all the structure's references. Specifically, |
| 4242 | ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 4243 | */ |
| 4244 | unixClose(pFile); |
| 4245 | return rc; |
| 4246 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4247 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4248 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4249 | } |
| 4250 | } |
| 4251 | #endif |
| 4252 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4253 | rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete); |
| 4254 | open_finished: |
| 4255 | if( rc!=SQLITE_OK ){ |
| 4256 | sqlite3_free(p->pUnused); |
| 4257 | } |
| 4258 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4259 | } |
| 4260 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4261 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4262 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4263 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 4264 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4265 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4266 | static int unixDelete( |
| 4267 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 4268 | const char *zPath, /* Name of file to be deleted */ |
| 4269 | int dirSync /* If true, fsync() directory after deleting file */ |
| 4270 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4271 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4272 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4273 | SimulateIOError(return SQLITE_IOERR_DELETE); |
| 4274 | unlink(zPath); |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 4275 | #ifndef SQLITE_DISABLE_DIRSYNC |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4276 | if( dirSync ){ |
| 4277 | int fd; |
| 4278 | rc = openDirectory(zPath, &fd); |
| 4279 | if( rc==SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4280 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4281 | if( fsync(fd)==-1 ) |
| 4282 | #else |
| 4283 | if( fsync(fd) ) |
| 4284 | #endif |
| 4285 | { |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4286 | rc = SQLITE_IOERR_DIR_FSYNC; |
| 4287 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4288 | if( close(fd)&&!rc ){ |
| 4289 | rc = SQLITE_IOERR_DIR_CLOSE; |
| 4290 | } |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4291 | } |
| 4292 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 4293 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4294 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4295 | } |
| 4296 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4297 | /* |
| 4298 | ** Test the existance of or access permissions of file zPath. The |
| 4299 | ** test performed depends on the value of flags: |
| 4300 | ** |
| 4301 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 4302 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 4303 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 4304 | ** |
| 4305 | ** Otherwise return 0. |
| 4306 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4307 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4308 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 4309 | const char *zPath, /* Path of the file to examine */ |
| 4310 | int flags, /* What do we want to learn about the zPath file? */ |
| 4311 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4312 | ){ |
rse | 25c0d1a | 2007-09-20 08:38:14 +0000 | [diff] [blame] | 4313 | int amode = 0; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4314 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4315 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4316 | switch( flags ){ |
| 4317 | case SQLITE_ACCESS_EXISTS: |
| 4318 | amode = F_OK; |
| 4319 | break; |
| 4320 | case SQLITE_ACCESS_READWRITE: |
| 4321 | amode = W_OK|R_OK; |
| 4322 | break; |
drh | 50d3f90 | 2007-08-27 21:10:36 +0000 | [diff] [blame] | 4323 | case SQLITE_ACCESS_READ: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4324 | amode = R_OK; |
| 4325 | break; |
| 4326 | |
| 4327 | default: |
| 4328 | assert(!"Invalid flags argument"); |
| 4329 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4330 | *pResOut = (access(zPath, amode)==0); |
| 4331 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4334 | |
| 4335 | /* |
| 4336 | ** Turn a relative pathname into a full pathname. The relative path |
| 4337 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 4338 | ** zPath. |
| 4339 | ** |
| 4340 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 4341 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 4342 | ** this buffer before returning. |
| 4343 | */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 4344 | static int unixFullPathname( |
| 4345 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 4346 | const char *zPath, /* Possibly relative input path */ |
| 4347 | int nOut, /* Size of output buffer in bytes */ |
| 4348 | char *zOut /* Output buffer */ |
| 4349 | ){ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 4350 | |
| 4351 | /* It's odd to simulate an io-error here, but really this is just |
| 4352 | ** using the io-error infrastructure to test that SQLite handles this |
| 4353 | ** function failing. This function could fail if, for example, the |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4354 | ** current working directory has been unlinked. |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 4355 | */ |
| 4356 | SimulateIOError( return SQLITE_ERROR ); |
| 4357 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4358 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 4359 | UNUSED_PARAMETER(pVfs); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4360 | |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4361 | zOut[nOut-1] = '\0'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4362 | if( zPath[0]=='/' ){ |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4363 | sqlite3_snprintf(nOut, zOut, "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4364 | }else{ |
| 4365 | int nCwd; |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4366 | if( getcwd(zOut, nOut-1)==0 ){ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 4367 | return SQLITE_CANTOPEN_BKPT; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4368 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4369 | nCwd = (int)strlen(zOut); |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4370 | sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4371 | } |
| 4372 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 4375 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4376 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 4377 | /* |
| 4378 | ** Interfaces for opening a shared library, finding entry points |
| 4379 | ** within the shared library, and closing the shared library. |
| 4380 | */ |
| 4381 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4382 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 4383 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4384 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 4385 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 4386 | |
| 4387 | /* |
| 4388 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 4389 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 4390 | ** message is available, it is written to zBufOut. If no error message |
| 4391 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 4392 | ** error message. |
| 4393 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4394 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4395 | char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4396 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4397 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4398 | zErr = dlerror(); |
| 4399 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4400 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4401 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4402 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4403 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4404 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 4405 | /* |
| 4406 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 4407 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 4408 | ** returns a void* which is really a pointer to a function. So how do we |
| 4409 | ** use dlsym() with -pedantic-errors? |
| 4410 | ** |
| 4411 | ** Variable x below is defined to be a pointer to a function taking |
| 4412 | ** parameters void* and const char* and returning a pointer to a function. |
| 4413 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 4414 | ** (That assignment requires a cast.) Then we call the function that |
| 4415 | ** x points to. |
| 4416 | ** |
| 4417 | ** This work-around is unlikely to work correctly on any system where |
| 4418 | ** you really cannot cast a function pointer into void*. But then, on the |
| 4419 | ** other hand, dlsym() will not work on such a system either, so we have |
| 4420 | ** not really lost anything. |
| 4421 | */ |
| 4422 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4423 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4424 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 4425 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4426 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4427 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 4428 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4429 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4430 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4431 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 4432 | #define unixDlOpen 0 |
| 4433 | #define unixDlError 0 |
| 4434 | #define unixDlSym 0 |
| 4435 | #define unixDlClose 0 |
| 4436 | #endif |
| 4437 | |
| 4438 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4439 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4440 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4441 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 4442 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 4443 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4444 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4445 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 4446 | ** errors. The reports issued by valgrind are incorrect - we would |
| 4447 | ** prefer that the randomness be increased by making use of the |
| 4448 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 4449 | ** some users. Rather than argue, it seems easier just to initialize |
| 4450 | ** the whole array and silence valgrind, even if that means less randomness |
| 4451 | ** in the random seed. |
| 4452 | ** |
| 4453 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 4454 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4455 | ** tests repeatable. |
| 4456 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4457 | memset(zBuf, 0, nBuf); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4458 | #if !defined(SQLITE_TEST) |
| 4459 | { |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 4460 | int pid, fd; |
| 4461 | fd = open("/dev/urandom", O_RDONLY); |
| 4462 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 4463 | time_t t; |
| 4464 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4465 | memcpy(zBuf, &t, sizeof(t)); |
| 4466 | pid = getpid(); |
| 4467 | memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid)); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 4468 | assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf ); |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 4469 | nBuf = sizeof(t) + sizeof(pid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 4470 | }else{ |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 4471 | nBuf = read(fd, zBuf, nBuf); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 4472 | close(fd); |
| 4473 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4474 | } |
| 4475 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 4476 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4479 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4480 | /* |
| 4481 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4482 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 4483 | ** The return value is the number of microseconds of sleep actually |
| 4484 | ** requested from the underlying operating system, a number which |
| 4485 | ** might be greater than or equal to the argument, but not less |
| 4486 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4487 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4488 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4489 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4490 | struct timespec sp; |
| 4491 | |
| 4492 | sp.tv_sec = microseconds / 1000000; |
| 4493 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 4494 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 4495 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4496 | return microseconds; |
| 4497 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4498 | usleep(microseconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 4499 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4500 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4501 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4502 | int seconds = (microseconds+999999)/1000000; |
| 4503 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 4504 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 4505 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 4506 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
| 4509 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4510 | ** The following variable, if set to a non-zero value, is interpreted as |
| 4511 | ** the number of seconds since 1970 and is used to set the result of |
| 4512 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4513 | */ |
| 4514 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4515 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4516 | #endif |
| 4517 | |
| 4518 | /* |
| 4519 | ** Find the current time (in Universal Coordinated Time). Write the |
| 4520 | ** current time and date as a Julian Day number into *prNow and |
| 4521 | ** return 0. Return 1 if the time and date cannot be found. |
| 4522 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4523 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 4524 | #if defined(SQLITE_OMIT_FLOATING_POINT) |
| 4525 | time_t t; |
| 4526 | time(&t); |
| 4527 | *prNow = (((sqlite3_int64)t)/8640 + 24405875)/10; |
| 4528 | #elif defined(NO_GETTOD) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4529 | time_t t; |
| 4530 | time(&t); |
| 4531 | *prNow = t/86400.0 + 2440587.5; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4532 | #elif OS_VXWORKS |
| 4533 | struct timespec sNow; |
| 4534 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 4535 | *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_nsec/86400000000000.0; |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 4536 | #else |
| 4537 | struct timeval sNow; |
drh | bdcc276 | 2007-04-02 18:06:57 +0000 | [diff] [blame] | 4538 | gettimeofday(&sNow, 0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 4539 | *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0; |
| 4540 | #endif |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4541 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4542 | #ifdef SQLITE_TEST |
| 4543 | if( sqlite3_current_time ){ |
| 4544 | *prNow = sqlite3_current_time/86400.0 + 2440587.5; |
| 4545 | } |
| 4546 | #endif |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4547 | UNUSED_PARAMETER(NotUsed); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4548 | return 0; |
| 4549 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4550 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4551 | /* |
| 4552 | ** We added the xGetLastError() method with the intention of providing |
| 4553 | ** better low-level error messages when operating-system problems come up |
| 4554 | ** during SQLite operation. But so far, none of that has been implemented |
| 4555 | ** in the core. So this routine is never called. For now, it is merely |
| 4556 | ** a place-holder. |
| 4557 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4558 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 4559 | UNUSED_PARAMETER(NotUsed); |
| 4560 | UNUSED_PARAMETER(NotUsed2); |
| 4561 | UNUSED_PARAMETER(NotUsed3); |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 4562 | return 0; |
| 4563 | } |
| 4564 | |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4565 | /* Forward reference */ |
| 4566 | typedef struct unixShm unixShm; |
| 4567 | typedef struct unixShmFile unixShmFile; |
| 4568 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4569 | /* |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4570 | ** Object used to represent a single file opened and mmapped to provide |
| 4571 | ** shared memory. When multiple threads all reference the same |
| 4572 | ** log-summary, each thread has its own unixFile object, but they all |
| 4573 | ** point to a single instance of this object. In other words, each |
| 4574 | ** log-summary is opened only once per process. |
| 4575 | ** |
| 4576 | ** unixMutexHeld() must be true when creating or destroying |
| 4577 | ** this object or while reading or writing the following fields: |
| 4578 | ** |
| 4579 | ** nRef |
| 4580 | ** pNext |
| 4581 | ** |
| 4582 | ** The following fields are read-only after the object is created: |
| 4583 | ** |
| 4584 | ** fid |
| 4585 | ** zFilename |
| 4586 | ** |
| 4587 | ** Either unixShmFile.mutex must be held or unixShmFile.nRef==0 and |
| 4588 | ** unixMutexHeld() is true when reading or writing any other field |
| 4589 | ** in this structure. |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 4590 | */ |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4591 | struct unixShmFile { |
| 4592 | struct unixFileId fid; /* Unique file identifier */ |
| 4593 | sqlite3_mutex *mutex; /* Mutex to access this object */ |
| 4594 | sqlite3_mutex *mutexBuf; /* Mutex to access zBuf[] */ |
| 4595 | sqlite3_mutex *mutexRecov; /* The RECOVER mutex */ |
| 4596 | char *zFilename; /* Name of the file */ |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4597 | int h; /* Open file descriptor */ |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 4598 | int szMap; /* Size of the mapping of file into memory */ |
| 4599 | char *pMMapBuf; /* Where currently mmapped(). NULL if unmapped */ |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4600 | int nRef; /* Number of unixShm objects pointing to this */ |
| 4601 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
| 4602 | unixShmFile *pNext; /* Next in list of all unixShmFile objects */ |
| 4603 | #ifdef SQLITE_DEBUG |
| 4604 | u8 exclMask; /* Mask of exclusive locks held */ |
| 4605 | u8 sharedMask; /* Mask of shared locks held */ |
| 4606 | u8 nextShmId; /* Next available unixShm.id value */ |
| 4607 | #endif |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 4608 | }; |
| 4609 | |
| 4610 | /* |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4611 | ** A global array of all unixShmFile objects. |
| 4612 | ** |
| 4613 | ** The unixMutexHeld() must be true while reading or writing this list. |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 4614 | */ |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4615 | static unixShmFile *unixShmFileList = 0; |
| 4616 | |
| 4617 | /* |
| 4618 | ** Structure used internally by this VFS to record the state of an |
| 4619 | ** open shared memory connection. |
| 4620 | ** |
| 4621 | ** unixShm.pFile->mutex must be held while reading or writing the |
| 4622 | ** unixShm.pNext and unixShm.locks[] elements. |
| 4623 | ** |
| 4624 | ** The unixShm.pFile element is initialized when the object is created |
| 4625 | ** and is read-only thereafter. |
| 4626 | */ |
| 4627 | struct unixShm { |
| 4628 | unixShmFile *pFile; /* The underlying unixShmFile object */ |
| 4629 | unixShm *pNext; /* Next unixShm with the same unixShmFile */ |
| 4630 | u8 lockState; /* Current lock state */ |
| 4631 | u8 readLock; /* Which of the two read-lock states to use */ |
| 4632 | u8 hasMutex; /* True if holding the unixShmFile mutex */ |
| 4633 | u8 hasMutexBuf; /* True if holding pFile->mutexBuf */ |
| 4634 | u8 hasMutexRecov; /* True if holding pFile->mutexRecov */ |
| 4635 | u8 sharedMask; /* Mask of shared locks held */ |
| 4636 | u8 exclMask; /* Mask of exclusive locks held */ |
| 4637 | #ifdef SQLITE_DEBUG |
| 4638 | u8 id; /* Id of this connection with its unixShmFile */ |
| 4639 | #endif |
| 4640 | }; |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 4641 | |
| 4642 | /* |
| 4643 | ** Size increment by which shared memory grows |
| 4644 | */ |
| 4645 | #define SQLITE_UNIX_SHM_INCR 4096 |
| 4646 | |
| 4647 | /* |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4648 | ** Constants used for locking |
| 4649 | */ |
| 4650 | #define UNIX_SHM_BASE 32 /* Byte offset of the first lock byte */ |
| 4651 | #define UNIX_SHM_MUTEX 0x01 /* Mask for MUTEX lock */ |
| 4652 | #define UNIX_SHM_DMS 0x04 /* Mask for Dead-Man-Switch lock */ |
| 4653 | #define UNIX_SHM_A 0x10 /* Mask for region locks... */ |
| 4654 | #define UNIX_SHM_B 0x20 |
| 4655 | #define UNIX_SHM_C 0x40 |
| 4656 | #define UNIX_SHM_D 0x80 |
| 4657 | |
| 4658 | #ifdef SQLITE_DEBUG |
| 4659 | /* |
| 4660 | ** Return a pointer to a nul-terminated string in static memory that |
| 4661 | ** describes a locking mask. The string is of the form "MSABCD" with |
| 4662 | ** each character representing a lock. "M" for MUTEX, "S" for DMS, |
| 4663 | ** and "A" through "D" for the region locks. If a lock is held, the |
| 4664 | ** letter is shown. If the lock is not held, the letter is converted |
| 4665 | ** to ".". |
| 4666 | ** |
| 4667 | ** This routine is for debugging purposes only and does not appear |
| 4668 | ** in a production build. |
| 4669 | */ |
| 4670 | static const char *unixShmLockString(u8 mask){ |
| 4671 | static char zBuf[48]; |
| 4672 | static int iBuf = 0; |
| 4673 | char *z; |
| 4674 | |
| 4675 | z = &zBuf[iBuf]; |
| 4676 | iBuf += 8; |
| 4677 | if( iBuf>=sizeof(zBuf) ) iBuf = 0; |
| 4678 | |
| 4679 | z[0] = (mask & UNIX_SHM_MUTEX) ? 'M' : '.'; |
| 4680 | z[1] = (mask & UNIX_SHM_DMS) ? 'S' : '.'; |
| 4681 | z[2] = (mask & UNIX_SHM_A) ? 'A' : '.'; |
| 4682 | z[3] = (mask & UNIX_SHM_B) ? 'B' : '.'; |
| 4683 | z[4] = (mask & UNIX_SHM_C) ? 'C' : '.'; |
| 4684 | z[5] = (mask & UNIX_SHM_D) ? 'D' : '.'; |
| 4685 | z[6] = 0; |
| 4686 | return z; |
| 4687 | } |
| 4688 | #endif /* SQLITE_DEBUG */ |
| 4689 | |
| 4690 | /* |
| 4691 | ** Apply posix advisory locks for all bytes identified in lockMask. |
| 4692 | ** |
| 4693 | ** lockMask might contain multiple bits but all bits are guaranteed |
| 4694 | ** to be contiguous. |
| 4695 | ** |
| 4696 | ** Locks block if the UNIX_SHM_MUTEX bit is set and are non-blocking |
| 4697 | ** otherwise. |
| 4698 | */ |
drh | 31cbbba | 2010-04-29 16:40:51 +0000 | [diff] [blame] | 4699 | static int unixShmSystemLock( |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4700 | unixShmFile *pFile, /* Apply locks to this open shared-memory segment */ |
| 4701 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
| 4702 | u8 lockMask /* Which bytes to lock or unlock */ |
| 4703 | ){ |
| 4704 | struct flock f; /* The posix advisory locking structure */ |
| 4705 | int lockOp; /* The opcode for fcntl() */ |
| 4706 | int i; /* Offset into the locking byte range */ |
| 4707 | int rc; /* Result code form fcntl() */ |
| 4708 | u8 mask; /* Mask of bits in lockMask */ |
| 4709 | |
drh | 31cbbba | 2010-04-29 16:40:51 +0000 | [diff] [blame] | 4710 | /* Access to the unixShmFile object is serialized by the caller */ |
drh | 79e6c78 | 2010-04-30 02:13:26 +0000 | [diff] [blame] | 4711 | assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 ); |
drh | 31cbbba | 2010-04-29 16:40:51 +0000 | [diff] [blame] | 4712 | |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4713 | /* Initialize the locking parameters */ |
| 4714 | memset(&f, 0, sizeof(f)); |
| 4715 | f.l_type = lockType; |
| 4716 | f.l_whence = SEEK_SET; |
| 4717 | if( (lockMask & UNIX_SHM_MUTEX)!=0 && lockType!=F_UNLCK ){ |
| 4718 | lockOp = F_SETLKW; |
drh | d749084 | 2010-04-30 15:54:46 +0000 | [diff] [blame] | 4719 | OSTRACE(("SHM-LOCK requesting blocking lock\n")); |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4720 | }else{ |
| 4721 | lockOp = F_SETLK; |
| 4722 | } |
| 4723 | |
| 4724 | /* Find the first bit in lockMask that is set */ |
| 4725 | for(i=0, mask=0x01; mask!=0 && (lockMask&mask)==0; mask <<= 1, i++){} |
| 4726 | assert( mask!=0 ); |
| 4727 | f.l_start = i+UNIX_SHM_BASE; |
| 4728 | f.l_len = 1; |
| 4729 | |
| 4730 | /* Extend the locking range for each additional bit that is set */ |
| 4731 | mask <<= 1; |
| 4732 | while( mask!=0 && (lockMask & mask)!=0 ){ |
| 4733 | f.l_len++; |
drh | 79e6c78 | 2010-04-30 02:13:26 +0000 | [diff] [blame] | 4734 | mask <<= 1; |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | /* Verify that all bits set in lockMask are contiguous */ |
| 4738 | assert( mask==0 || (lockMask & ~(mask | (mask-1)))==0 ); |
| 4739 | |
| 4740 | /* Acquire the system-level lock */ |
drh | 79e6c78 | 2010-04-30 02:13:26 +0000 | [diff] [blame] | 4741 | rc = fcntl(pFile->h, lockOp, &f); |
| 4742 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4743 | |
| 4744 | /* Update the global lock state and do debug tracing */ |
| 4745 | #ifdef SQLITE_DEBUG |
| 4746 | OSTRACE(("SHM-LOCK ")); |
| 4747 | if( rc==SQLITE_OK ){ |
| 4748 | if( lockType==F_UNLCK ){ |
| 4749 | OSTRACE(("unlock ok")); |
| 4750 | pFile->exclMask &= ~lockMask; |
| 4751 | pFile->sharedMask &= ~lockMask; |
| 4752 | }else if( lockType==F_RDLCK ){ |
| 4753 | OSTRACE(("read-lock ok")); |
| 4754 | pFile->exclMask &= ~lockMask; |
| 4755 | pFile->sharedMask |= lockMask; |
| 4756 | }else{ |
| 4757 | assert( lockType==F_WRLCK ); |
| 4758 | OSTRACE(("write-lock ok")); |
| 4759 | pFile->exclMask |= lockMask; |
| 4760 | pFile->sharedMask &= ~lockMask; |
| 4761 | } |
| 4762 | }else{ |
| 4763 | if( lockType==F_UNLCK ){ |
| 4764 | OSTRACE(("unlock failed")); |
| 4765 | }else if( lockType==F_RDLCK ){ |
| 4766 | OSTRACE(("read-lock failed")); |
| 4767 | }else{ |
| 4768 | assert( lockType==F_WRLCK ); |
| 4769 | OSTRACE(("write-lock failed")); |
| 4770 | } |
| 4771 | } |
drh | d749084 | 2010-04-30 15:54:46 +0000 | [diff] [blame] | 4772 | OSTRACE((" - change requested %s - afterwards %s:%s\n", |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4773 | unixShmLockString(lockMask), |
| 4774 | unixShmLockString(pFile->sharedMask), |
| 4775 | unixShmLockString(pFile->exclMask))); |
| 4776 | #endif |
| 4777 | |
| 4778 | return rc; |
| 4779 | } |
| 4780 | |
| 4781 | /* |
| 4782 | ** For connection p, unlock all of the locks identified by the unlockMask |
| 4783 | ** parameter. |
| 4784 | */ |
| 4785 | static int unixShmUnlock( |
| 4786 | unixShmFile *pFile, /* The underlying shared-memory file */ |
| 4787 | unixShm *p, /* The connection to be unlocked */ |
| 4788 | u8 unlockMask /* Mask of locks to be unlocked */ |
| 4789 | ){ |
| 4790 | int rc; /* Result code */ |
| 4791 | unixShm *pX; /* For looping over all sibling connections */ |
| 4792 | u8 allMask; /* Union of locks held by connections other than "p" */ |
| 4793 | |
drh | 31cbbba | 2010-04-29 16:40:51 +0000 | [diff] [blame] | 4794 | /* Access to the unixShmFile object is serialized by the caller */ |
| 4795 | assert( sqlite3_mutex_held(pFile->mutex) ); |
| 4796 | |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4797 | /* Compute locks held by sibling connections */ |
drh | d749084 | 2010-04-30 15:54:46 +0000 | [diff] [blame] | 4798 | allMask = 0; |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4799 | for(pX=pFile->pFirst; pX; pX=pX->pNext){ |
| 4800 | if( pX==p ) continue; |
dan | 4c97b53 | 2010-04-30 09:52:17 +0000 | [diff] [blame] | 4801 | assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4802 | allMask |= pX->sharedMask; |
| 4803 | } |
| 4804 | |
| 4805 | /* Unlock the system-level locks */ |
| 4806 | if( (unlockMask & allMask)!=unlockMask ){ |
drh | 31cbbba | 2010-04-29 16:40:51 +0000 | [diff] [blame] | 4807 | rc = unixShmSystemLock(pFile, F_UNLCK, unlockMask & ~allMask); |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4808 | }else{ |
| 4809 | rc = SQLITE_OK; |
| 4810 | } |
| 4811 | |
| 4812 | /* Undo the local locks */ |
| 4813 | if( rc==SQLITE_OK ){ |
| 4814 | p->exclMask &= ~unlockMask; |
| 4815 | p->sharedMask &= ~unlockMask; |
| 4816 | } |
| 4817 | return rc; |
| 4818 | } |
| 4819 | |
| 4820 | /* |
| 4821 | ** Get reader locks for connection p on all locks in the readMask parameter. |
| 4822 | */ |
| 4823 | static int unixShmSharedLock( |
| 4824 | unixShmFile *pFile, /* The underlying shared-memory file */ |
| 4825 | unixShm *p, /* The connection to get the shared locks */ |
| 4826 | u8 readMask /* Mask of shared locks to be acquired */ |
| 4827 | ){ |
| 4828 | int rc; /* Result code */ |
| 4829 | unixShm *pX; /* For looping over all sibling connections */ |
| 4830 | u8 allShared; /* Union of locks held by connections other than "p" */ |
| 4831 | |
drh | 31cbbba | 2010-04-29 16:40:51 +0000 | [diff] [blame] | 4832 | /* Access to the unixShmFile object is serialized by the caller */ |
| 4833 | assert( sqlite3_mutex_held(pFile->mutex) ); |
| 4834 | |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4835 | /* Find out which shared locks are already held by sibling connections. |
| 4836 | ** If any sibling already holds an exclusive lock, go ahead and return |
| 4837 | ** SQLITE_BUSY. |
| 4838 | */ |
drh | d749084 | 2010-04-30 15:54:46 +0000 | [diff] [blame] | 4839 | allShared = 0; |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4840 | for(pX=pFile->pFirst; pX; pX=pX->pNext){ |
| 4841 | if( pX==p ) continue; |
| 4842 | if( (pX->exclMask & readMask)!=0 ) return SQLITE_BUSY; |
| 4843 | allShared |= pX->sharedMask; |
| 4844 | } |
| 4845 | |
| 4846 | /* Get shared locks at the system level, if necessary */ |
| 4847 | if( (~allShared) & readMask ){ |
drh | 31cbbba | 2010-04-29 16:40:51 +0000 | [diff] [blame] | 4848 | rc = unixShmSystemLock(pFile, F_RDLCK, readMask); |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4849 | }else{ |
| 4850 | rc = SQLITE_OK; |
| 4851 | } |
| 4852 | |
| 4853 | /* Get the local shared locks */ |
| 4854 | if( rc==SQLITE_OK ){ |
| 4855 | p->sharedMask |= readMask; |
| 4856 | } |
| 4857 | return rc; |
| 4858 | } |
| 4859 | |
| 4860 | /* |
| 4861 | ** For connection p, get an exclusive lock on all locks identified in |
| 4862 | ** the writeMask parameter. |
| 4863 | */ |
| 4864 | static int unixShmExclusiveLock( |
| 4865 | unixShmFile *pFile, /* The underlying shared-memory file */ |
| 4866 | unixShm *p, /* The connection to get the exclusive locks */ |
| 4867 | u8 writeMask /* Mask of exclusive locks to be acquired */ |
| 4868 | ){ |
| 4869 | int rc; /* Result code */ |
| 4870 | unixShm *pX; /* For looping over all sibling connections */ |
| 4871 | |
drh | 31cbbba | 2010-04-29 16:40:51 +0000 | [diff] [blame] | 4872 | /* Access to the unixShmFile object is serialized by the caller */ |
| 4873 | assert( sqlite3_mutex_held(pFile->mutex) ); |
| 4874 | |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4875 | /* Make sure no sibling connections hold locks that will block this |
| 4876 | ** lock. If any do, return SQLITE_BUSY right away. |
| 4877 | */ |
| 4878 | for(pX=pFile->pFirst; pX; pX=pX->pNext){ |
| 4879 | if( pX==p ) continue; |
| 4880 | if( (pX->exclMask & writeMask)!=0 ) return SQLITE_BUSY; |
| 4881 | if( (pX->sharedMask & writeMask)!=0 ) return SQLITE_BUSY; |
| 4882 | } |
| 4883 | |
| 4884 | /* Get the exclusive locks at the system level. Then if successful |
| 4885 | ** also mark the local connection as being locked. |
| 4886 | */ |
drh | 31cbbba | 2010-04-29 16:40:51 +0000 | [diff] [blame] | 4887 | rc = unixShmSystemLock(pFile, F_WRLCK, writeMask); |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4888 | if( rc==SQLITE_OK ){ |
| 4889 | p->sharedMask &= ~writeMask; |
| 4890 | p->exclMask |= writeMask; |
| 4891 | } |
| 4892 | return rc; |
| 4893 | } |
| 4894 | |
| 4895 | /* |
| 4896 | ** Purge the unixShmFileList list of all entries with unixShmFile.nRef==0. |
| 4897 | ** |
| 4898 | ** This is not a VFS shared-memory method; it is a utility function called |
| 4899 | ** by VFS shared-memory methods. |
| 4900 | */ |
| 4901 | static void unixShmPurge(void){ |
| 4902 | unixShmFile **pp; |
| 4903 | unixShmFile *p; |
| 4904 | assert( unixMutexHeld() ); |
| 4905 | pp = &unixShmFileList; |
| 4906 | while( (p = *pp)!=0 ){ |
| 4907 | if( p->nRef==0 ){ |
| 4908 | if( p->mutex ) sqlite3_mutex_free(p->mutex); |
| 4909 | if( p->mutexBuf ) sqlite3_mutex_free(p->mutexBuf); |
| 4910 | if( p->mutexRecov ) sqlite3_mutex_free(p->mutexRecov); |
| 4911 | if( p->h>=0 ) close(p->h); |
| 4912 | *pp = p->pNext; |
| 4913 | sqlite3_free(p); |
| 4914 | }else{ |
| 4915 | pp = &p->pNext; |
| 4916 | } |
| 4917 | } |
| 4918 | } |
| 4919 | |
| 4920 | /* |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 4921 | ** Open a shared-memory area. This implementation uses mmapped files. |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4922 | ** |
| 4923 | ** When opening a new shared-memory file, if no other instances of that |
| 4924 | ** file are currently open, in this process or in other processes, then |
| 4925 | ** the file must be truncated to zero length or have its header cleared. |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 4926 | */ |
| 4927 | static int unixShmOpen( |
| 4928 | sqlite3_vfs *pVfs, /* The VFS */ |
| 4929 | const char *zName, /* Name of file to mmap */ |
| 4930 | sqlite3_shm **pShm /* Write the unixShm object created here */ |
| 4931 | ){ |
drh | 31cbbba | 2010-04-29 16:40:51 +0000 | [diff] [blame] | 4932 | struct unixShm *p = 0; /* The connection to be opened */ |
| 4933 | struct unixShmFile *pFile = 0; /* The underlying mmapped file */ |
| 4934 | int rc; /* Result code */ |
| 4935 | struct unixFileId fid; /* Unix file identifier */ |
| 4936 | struct stat sStat; /* Result from stat() an fstat() */ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 4937 | |
drh | 31cbbba | 2010-04-29 16:40:51 +0000 | [diff] [blame] | 4938 | /* Allocate space for the new sqlite3_shm object */ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 4939 | p = sqlite3_malloc( sizeof(*p) ); |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 4940 | if( p==0 ) return SQLITE_NOMEM; |
| 4941 | memset(p, 0, sizeof(*p)); |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 4942 | |
drh | 31cbbba | 2010-04-29 16:40:51 +0000 | [diff] [blame] | 4943 | /* Look to see if there is an existing unixShmFile that can be used. |
| 4944 | ** If no matching unixShmFile currently exists, create a new one. |
| 4945 | */ |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4946 | unixEnterMutex(); |
| 4947 | rc = stat(zName, &sStat); |
| 4948 | if( rc==0 ){ |
| 4949 | memset(&fid, 0, sizeof(fid)); |
| 4950 | fid.dev = sStat.st_dev; |
| 4951 | fid.ino = sStat.st_ino; |
| 4952 | for(pFile = unixShmFileList; pFile; pFile=pFile->pNext){ |
| 4953 | if( memcmp(&pFile->fid, &fid, sizeof(fid))==0 ) break; |
| 4954 | } |
| 4955 | } |
| 4956 | if( pFile==0 ){ |
| 4957 | int nName = strlen(zName); |
| 4958 | pFile = sqlite3_malloc( sizeof(*pFile) + nName + 1 ); |
| 4959 | if( pFile==0 ){ |
| 4960 | rc = SQLITE_NOMEM; |
| 4961 | goto shm_open_err; |
| 4962 | } |
drh | 79e6c78 | 2010-04-30 02:13:26 +0000 | [diff] [blame] | 4963 | memset(pFile, 0, sizeof(*pFile)); |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4964 | pFile->zFilename = (char*)&pFile[1]; |
| 4965 | memcpy(pFile->zFilename, zName, nName+1); |
| 4966 | pFile->h = -1; |
| 4967 | pFile->pNext = unixShmFileList; |
| 4968 | unixShmFileList = pFile; |
| 4969 | |
| 4970 | pFile->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 4971 | if( pFile->mutex==0 ){ |
| 4972 | rc = SQLITE_NOMEM; |
| 4973 | goto shm_open_err; |
| 4974 | } |
| 4975 | pFile->mutexBuf = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 4976 | if( pFile->mutexBuf==0 ){ |
| 4977 | rc = SQLITE_NOMEM; |
| 4978 | goto shm_open_err; |
| 4979 | } |
| 4980 | pFile->mutexRecov = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 4981 | if( pFile->mutexRecov==0 ){ |
| 4982 | rc = SQLITE_NOMEM; |
| 4983 | goto shm_open_err; |
| 4984 | } |
| 4985 | |
drh | 79e6c78 | 2010-04-30 02:13:26 +0000 | [diff] [blame] | 4986 | pFile->h = open(zName, O_RDWR|O_CREAT, 0664); |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4987 | if( pFile->h<0 ){ |
dan | 87bfb51 | 2010-04-30 11:43:28 +0000 | [diff] [blame] | 4988 | rc = SQLITE_CANTOPEN_BKPT; |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4989 | goto shm_open_err; |
| 4990 | } |
| 4991 | |
| 4992 | rc = fstat(pFile->h, &sStat); |
| 4993 | if( rc ){ |
dan | 87bfb51 | 2010-04-30 11:43:28 +0000 | [diff] [blame] | 4994 | rc = SQLITE_CANTOPEN_BKPT; |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 4995 | goto shm_open_err; |
| 4996 | } |
| 4997 | pFile->fid.dev = sStat.st_dev; |
| 4998 | pFile->fid.ino = sStat.st_ino; |
drh | 31cbbba | 2010-04-29 16:40:51 +0000 | [diff] [blame] | 4999 | |
| 5000 | /* Check to see if another process is holding the dead-man switch. |
| 5001 | ** If not, truncate the file to zero length. |
| 5002 | */ |
| 5003 | if( unixShmSystemLock(pFile, F_WRLCK, UNIX_SHM_MUTEX) ){ |
| 5004 | rc = SQLITE_IOERR_LOCK; |
| 5005 | goto shm_open_err; |
| 5006 | } |
| 5007 | if( unixShmSystemLock(pFile, F_WRLCK, UNIX_SHM_DMS)==SQLITE_OK ){ |
| 5008 | if( ftruncate(pFile->h, 0) ){ |
| 5009 | rc = SQLITE_IOERR; |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5010 | } |
| 5011 | } |
drh | d749084 | 2010-04-30 15:54:46 +0000 | [diff] [blame] | 5012 | if( rc==SQLITE_OK ){ |
| 5013 | rc = unixShmSystemLock(pFile, F_RDLCK, UNIX_SHM_DMS); |
| 5014 | } |
drh | 31cbbba | 2010-04-29 16:40:51 +0000 | [diff] [blame] | 5015 | unixShmSystemLock(pFile, F_UNLCK, UNIX_SHM_MUTEX); |
drh | d749084 | 2010-04-30 15:54:46 +0000 | [diff] [blame] | 5016 | if( rc ) goto shm_open_err; |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
drh | 31cbbba | 2010-04-29 16:40:51 +0000 | [diff] [blame] | 5019 | /* Make the new connection a child of the unixShmFile */ |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5020 | p->pFile = pFile; |
| 5021 | p->pNext = pFile->pFirst; |
| 5022 | #ifdef SQLITE_DEBUG |
| 5023 | p->id = pFile->nextShmId++; |
| 5024 | #endif |
| 5025 | pFile->pFirst = p; |
| 5026 | pFile->nRef++; |
| 5027 | *pShm = (sqlite3_shm*)p; |
| 5028 | unixLeaveMutex(); |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5029 | return SQLITE_OK; |
| 5030 | |
drh | 31cbbba | 2010-04-29 16:40:51 +0000 | [diff] [blame] | 5031 | /* Jump here on any error */ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5032 | shm_open_err: |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5033 | unixShmPurge(); |
| 5034 | sqlite3_free(p); |
| 5035 | sqlite3_free(pFile); |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5036 | *pShm = 0; |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5037 | unixLeaveMutex(); |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5038 | return rc; |
| 5039 | } |
| 5040 | |
| 5041 | /* |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5042 | ** Close a connectioon to shared-memory. |
| 5043 | */ |
| 5044 | static int unixShmClose(sqlite3_shm *pSharedMem){ |
| 5045 | unixShm *p; /* The connection to be closed */ |
| 5046 | unixShmFile *pFile; /* The underlying shared-memory file */ |
| 5047 | unixShm **pp; /* For looping over sibling connections */ |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5048 | |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 5049 | if( pSharedMem==0 ) return SQLITE_OK; |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5050 | p = (struct unixShm*)pSharedMem; |
| 5051 | pFile = p->pFile; |
| 5052 | |
| 5053 | /* Verify that the connection being closed holds no locks */ |
| 5054 | assert( p->exclMask==0 ); |
| 5055 | assert( p->sharedMask==0 ); |
| 5056 | |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5057 | /* Remove connection p from the set of connections associated with pFile */ |
| 5058 | sqlite3_mutex_enter(pFile->mutex); |
| 5059 | for(pp=&pFile->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 5060 | *pp = p->pNext; |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5061 | |
| 5062 | /* Free the connection p */ |
| 5063 | sqlite3_free(p); |
| 5064 | sqlite3_mutex_leave(pFile->mutex); |
| 5065 | |
| 5066 | /* If pFile->nRef has reached 0, then close the underlying |
| 5067 | ** shared-memory file, too */ |
dan | e93808d | 2010-04-30 10:06:09 +0000 | [diff] [blame] | 5068 | unixEnterMutex(); |
| 5069 | assert( pFile->nRef>0 ); |
| 5070 | pFile->nRef--; |
| 5071 | if( pFile->nRef==0 ){ |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5072 | unixShmPurge(); |
| 5073 | } |
dan | e93808d | 2010-04-30 10:06:09 +0000 | [diff] [blame] | 5074 | unixLeaveMutex(); |
| 5075 | |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5076 | return SQLITE_OK; |
| 5077 | } |
| 5078 | |
| 5079 | /* |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 5080 | ** Query and/or changes the size of the underlying storage for |
| 5081 | ** a shared-memory segment. The reqSize parameter is the new size |
| 5082 | ** of the underlying storage, or -1 to do just a query. The size |
| 5083 | ** of the underlying storage (after resizing if resizing occurs) is |
| 5084 | ** written into pNewSize. |
drh | af75c86 | 2010-04-27 11:49:27 +0000 | [diff] [blame] | 5085 | ** |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 5086 | ** This routine does not (necessarily) change the size of the mapping |
| 5087 | ** of the underlying storage into memory. Use xShmGet() to change |
| 5088 | ** the mapping size. |
| 5089 | ** |
| 5090 | ** The reqSize parameter is the minimum size requested. The implementation |
| 5091 | ** is free to expand the storage to some larger amount if it chooses. |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5092 | */ |
| 5093 | static int unixShmSize( |
| 5094 | sqlite3_shm *pSharedMem, /* Pointer returned by unixShmOpen() */ |
| 5095 | int reqSize, /* Requested size. -1 for query only */ |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 5096 | int *pNewSize /* Write new size here */ |
| 5097 | ){ |
| 5098 | unixShm *p = (unixShm*)pSharedMem; |
| 5099 | unixShmFile *pFile = p->pFile; |
| 5100 | int rc = SQLITE_OK; |
| 5101 | struct stat sStat; |
| 5102 | |
| 5103 | if( reqSize>=0 ){ |
| 5104 | reqSize = (reqSize + SQLITE_UNIX_SHM_INCR - 1)/SQLITE_UNIX_SHM_INCR; |
| 5105 | reqSize *= SQLITE_UNIX_SHM_INCR; |
| 5106 | rc = ftruncate(pFile->h, reqSize); |
| 5107 | } |
| 5108 | if( fstat(pFile->h, &sStat)==0 ){ |
| 5109 | *pNewSize = (int)sStat.st_size; |
| 5110 | }else{ |
| 5111 | *pNewSize = 0; |
| 5112 | rc = SQLITE_IOERR; |
| 5113 | } |
| 5114 | return rc; |
| 5115 | } |
| 5116 | |
| 5117 | |
| 5118 | /* |
| 5119 | ** Map the shared storage into memory. The minimum size of the |
| 5120 | ** mapping should be reqMapSize if reqMapSize is positive. If |
| 5121 | ** reqMapSize is zero or negative, the implementation can choose |
| 5122 | ** whatever mapping size is convenient. |
| 5123 | ** |
| 5124 | ** *ppBuf is made to point to the memory which is a mapping of the |
| 5125 | ** underlying storage. This segment is locked. unixShmRelease() |
| 5126 | ** must be called to release the lock. |
| 5127 | ** |
| 5128 | ** *pNewMapSize is set to the size of the mapping. |
| 5129 | ** |
| 5130 | ** *ppBuf and *pNewMapSize might be NULL and zero if no space has |
| 5131 | ** yet been allocated to the underlying storage. |
| 5132 | */ |
| 5133 | static int unixShmGet( |
| 5134 | sqlite3_shm *pSharedMem, /* Pointer returned by unixShmOpen() */ |
| 5135 | int reqMapSize, /* Requested size of mapping. -1 means don't care */ |
| 5136 | int *pNewMapSize, /* Write new size of mapping here */ |
| 5137 | void **ppBuf /* Write mapping buffer origin here */ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5138 | ){ |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5139 | unixShm *p = (unixShm*)pSharedMem; |
| 5140 | unixShmFile *pFile = p->pFile; |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5141 | int rc = SQLITE_OK; |
| 5142 | |
drh | 6dea324 | 2010-04-30 17:47:51 +0000 | [diff] [blame^] | 5143 | if( p->lockState!=SQLITE_SHM_CHECKPOINT ){ |
| 5144 | sqlite3_mutex_enter(pFile->mutexBuf); |
| 5145 | p->hasMutexBuf = 1; |
| 5146 | } |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5147 | sqlite3_mutex_enter(pFile->mutex); |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 5148 | if( pFile->szMap==0 || reqMapSize>pFile->szMap ){ |
| 5149 | int actualSize; |
| 5150 | if( unixShmSize(pSharedMem, -1, &actualSize)==SQLITE_OK |
| 5151 | && reqMapSize<actualSize |
| 5152 | ){ |
| 5153 | reqMapSize = actualSize; |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5154 | } |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 5155 | if( pFile->pMMapBuf ){ |
| 5156 | munmap(pFile->pMMapBuf, pFile->szMap); |
| 5157 | } |
| 5158 | pFile->pMMapBuf = mmap(0, reqMapSize, PROT_READ|PROT_WRITE, MAP_SHARED, |
| 5159 | pFile->h, 0); |
| 5160 | pFile->szMap = pFile->pMMapBuf ? reqMapSize : 0; |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5161 | } |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 5162 | *pNewMapSize = pFile->szMap; |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5163 | *ppBuf = pFile->pMMapBuf; |
| 5164 | sqlite3_mutex_leave(pFile->mutex); |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5165 | return rc; |
| 5166 | } |
| 5167 | |
| 5168 | /* |
drh | af75c86 | 2010-04-27 11:49:27 +0000 | [diff] [blame] | 5169 | ** Release the lock held on the shared memory segment to that other |
| 5170 | ** threads are free to resize it if necessary. |
| 5171 | */ |
| 5172 | static int unixShmRelease(sqlite3_shm *pSharedMem){ |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5173 | unixShm *p = (unixShm*)pSharedMem; |
drh | 6dea324 | 2010-04-30 17:47:51 +0000 | [diff] [blame^] | 5174 | if( p->hasMutexBuf ){ |
| 5175 | unixShmFile *pFile = p->pFile; |
| 5176 | sqlite3_mutex_leave(pFile->mutexBuf); |
| 5177 | p->hasMutexBuf = 0; |
| 5178 | } |
drh | af75c86 | 2010-04-27 11:49:27 +0000 | [diff] [blame] | 5179 | return SQLITE_OK; |
| 5180 | } |
| 5181 | |
drh | 400df2e | 2010-04-30 16:48:19 +0000 | [diff] [blame] | 5182 | /* |
| 5183 | ** Symbolic names for LOCK states used for debugging. |
| 5184 | */ |
| 5185 | #ifdef SQLITE_DEBUG |
| 5186 | static const char *azLkName[] = { |
| 5187 | "UNLOCK", |
| 5188 | "READ", |
| 5189 | "READ_FULL", |
| 5190 | "WRITE", |
| 5191 | "PENDING", |
| 5192 | "CHECKPOINT", |
| 5193 | "RECOVER" |
| 5194 | }; |
| 5195 | #endif |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5196 | |
| 5197 | |
drh | af75c86 | 2010-04-27 11:49:27 +0000 | [diff] [blame] | 5198 | /* |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5199 | ** Change the lock state for a shared-memory segment. |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5200 | */ |
| 5201 | static int unixShmLock( |
| 5202 | sqlite3_shm *pSharedMem, /* Pointer from unixShmOpen() */ |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5203 | int desiredLock, /* One of SQLITE_SHM_xxxxx locking states */ |
| 5204 | int *pGotLock /* The lock you actually got */ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5205 | ){ |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5206 | unixShm *p = (unixShm*)pSharedMem; |
| 5207 | unixShmFile *pFile = p->pFile; |
| 5208 | int rc = SQLITE_PROTOCOL; |
| 5209 | |
| 5210 | /* Note that SQLITE_SHM_READ_FULL and SQLITE_SHM_PENDING are never |
| 5211 | ** directly requested; they are side effects from requesting |
| 5212 | ** SQLITE_SHM_READ and SQLITE_SHM_CHECKPOINT, respectively. |
| 5213 | */ |
| 5214 | assert( desiredLock==SQLITE_SHM_QUERY |
| 5215 | || desiredLock==SQLITE_SHM_UNLOCK |
| 5216 | || desiredLock==SQLITE_SHM_READ |
| 5217 | || desiredLock==SQLITE_SHM_WRITE |
| 5218 | || desiredLock==SQLITE_SHM_CHECKPOINT |
| 5219 | || desiredLock==SQLITE_SHM_RECOVER ); |
| 5220 | |
| 5221 | /* Return directly if this is just a lock state query, or if |
| 5222 | ** the connection is already in the desired locking state. |
| 5223 | */ |
| 5224 | if( desiredLock==SQLITE_SHM_QUERY |
| 5225 | || desiredLock==p->lockState |
| 5226 | || (desiredLock==SQLITE_SHM_READ && p->lockState==SQLITE_SHM_READ_FULL) |
| 5227 | ){ |
drh | 400df2e | 2010-04-30 16:48:19 +0000 | [diff] [blame] | 5228 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d request %s and got %s\n", |
| 5229 | p->id, getpid(), azLkName[desiredLock], azLkName[p->lockState])); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 5230 | if( pGotLock ) *pGotLock = p->lockState; |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5231 | return SQLITE_OK; |
| 5232 | } |
| 5233 | |
drh | 400df2e | 2010-04-30 16:48:19 +0000 | [diff] [blame] | 5234 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d request %s->%s\n", |
| 5235 | p->id, getpid(), azLkName[p->lockState], azLkName[desiredLock])); |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5236 | sqlite3_mutex_enter(pFile->mutex); |
| 5237 | switch( desiredLock ){ |
| 5238 | case SQLITE_SHM_UNLOCK: { |
| 5239 | assert( p->lockState!=SQLITE_SHM_RECOVER ); |
| 5240 | unixShmUnlock(pFile, p, UNIX_SHM_A|UNIX_SHM_B|UNIX_SHM_C|UNIX_SHM_D); |
| 5241 | rc = SQLITE_OK; |
| 5242 | p->lockState = SQLITE_SHM_UNLOCK; |
| 5243 | break; |
| 5244 | } |
| 5245 | case SQLITE_SHM_READ: { |
| 5246 | if( p->lockState==SQLITE_SHM_UNLOCK ){ |
| 5247 | int nAttempt; |
| 5248 | rc = SQLITE_BUSY; |
| 5249 | assert( p->lockState==SQLITE_SHM_UNLOCK ); |
| 5250 | for(nAttempt=0; nAttempt<5 && rc==SQLITE_BUSY; nAttempt++){ |
| 5251 | rc = unixShmSharedLock(pFile, p, UNIX_SHM_A|UNIX_SHM_B); |
| 5252 | if( rc==SQLITE_BUSY ){ |
| 5253 | rc = unixShmSharedLock(pFile, p, UNIX_SHM_D); |
| 5254 | if( rc==SQLITE_OK ){ |
| 5255 | p->lockState = p->readLock = SQLITE_SHM_READ_FULL; |
| 5256 | } |
| 5257 | }else{ |
| 5258 | unixShmUnlock(pFile, p, UNIX_SHM_B); |
| 5259 | p->lockState = p->readLock = SQLITE_SHM_READ; |
| 5260 | } |
| 5261 | } |
| 5262 | }else if( p->lockState==SQLITE_SHM_WRITE ){ |
dan | 78daa5a | 2010-04-30 16:38:59 +0000 | [diff] [blame] | 5263 | rc = unixShmSharedLock(pFile, p, UNIX_SHM_A); |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5264 | unixShmUnlock(pFile, p, UNIX_SHM_C|UNIX_SHM_D); |
dan | 43a56b8 | 2010-04-30 16:41:53 +0000 | [diff] [blame] | 5265 | p->lockState = p->readLock = SQLITE_SHM_READ; |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5266 | }else{ |
| 5267 | assert( p->lockState==SQLITE_SHM_RECOVER ); |
| 5268 | unixShmUnlock(pFile, p, UNIX_SHM_MUTEX); |
| 5269 | sqlite3_mutex_leave(pFile->mutexRecov); |
| 5270 | p->lockState = p->readLock; |
| 5271 | rc = SQLITE_OK; |
| 5272 | } |
| 5273 | break; |
| 5274 | } |
| 5275 | case SQLITE_SHM_WRITE: { |
| 5276 | assert( p->lockState==SQLITE_SHM_READ |
| 5277 | || p->lockState==SQLITE_SHM_READ_FULL ); |
| 5278 | rc = unixShmExclusiveLock(pFile, p, UNIX_SHM_C|UNIX_SHM_D); |
| 5279 | if( rc==SQLITE_OK ){ |
| 5280 | p->lockState = SQLITE_SHM_WRITE; |
| 5281 | } |
| 5282 | break; |
| 5283 | } |
| 5284 | case SQLITE_SHM_CHECKPOINT: { |
| 5285 | assert( p->lockState==SQLITE_SHM_UNLOCK |
| 5286 | || p->lockState==SQLITE_SHM_PENDING |
| 5287 | || p->lockState==SQLITE_SHM_RECOVER ); |
| 5288 | if( p->lockState==SQLITE_SHM_RECOVER ){ |
| 5289 | unixShmUnlock(pFile, p, UNIX_SHM_MUTEX); |
| 5290 | sqlite3_mutex_leave(pFile->mutexRecov); |
| 5291 | p->lockState = SQLITE_SHM_CHECKPOINT; |
| 5292 | rc = SQLITE_OK; |
| 5293 | } |
| 5294 | if( p->lockState==SQLITE_SHM_UNLOCK ){ |
| 5295 | rc = unixShmExclusiveLock(pFile, p, UNIX_SHM_B|UNIX_SHM_C); |
| 5296 | if( rc==SQLITE_OK ){ |
| 5297 | p->lockState = SQLITE_SHM_PENDING; |
| 5298 | } |
| 5299 | } |
| 5300 | if( p->lockState==SQLITE_SHM_PENDING ){ |
| 5301 | rc = unixShmExclusiveLock(pFile, p, UNIX_SHM_A); |
| 5302 | if( rc==SQLITE_OK ){ |
| 5303 | p->lockState = SQLITE_SHM_CHECKPOINT; |
| 5304 | } |
| 5305 | } |
| 5306 | break; |
| 5307 | } |
| 5308 | default: { |
| 5309 | assert( desiredLock==SQLITE_SHM_RECOVER ); |
| 5310 | assert( p->lockState==SQLITE_SHM_READ |
| 5311 | || p->lockState==SQLITE_SHM_READ_FULL |
| 5312 | || p->lockState==SQLITE_SHM_CHECKPOINT ); |
| 5313 | sqlite3_mutex_leave(pFile->mutex); |
| 5314 | sqlite3_mutex_enter(pFile->mutexRecov); |
| 5315 | sqlite3_mutex_enter(pFile->mutex); |
| 5316 | rc = unixShmExclusiveLock(pFile, p, UNIX_SHM_MUTEX); |
| 5317 | if( rc==SQLITE_OK ){ |
| 5318 | p->lockState = SQLITE_SHM_RECOVER; |
| 5319 | } |
| 5320 | break; |
| 5321 | } |
| 5322 | } |
| 5323 | sqlite3_mutex_leave(pFile->mutex); |
drh | 400df2e | 2010-04-30 16:48:19 +0000 | [diff] [blame] | 5324 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %s\n", |
| 5325 | p->id, getpid(), azLkName[p->lockState])); |
drh | 7ed91f2 | 2010-04-29 22:34:07 +0000 | [diff] [blame] | 5326 | if( pGotLock ) *pGotLock = p->lockState; |
drh | 7b69440 | 2010-04-29 15:17:48 +0000 | [diff] [blame] | 5327 | return rc; |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5328 | } |
| 5329 | |
| 5330 | /* |
| 5331 | ** Delete a shared-memory segment from the system. |
| 5332 | */ |
| 5333 | static int unixShmDelete(sqlite3_vfs *pVfs, const char *zName){ |
| 5334 | return pVfs->xDelete(pVfs, zName, 0); |
| 5335 | } |
| 5336 | |
| 5337 | |
| 5338 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5339 | ************************ End of sqlite3_vfs methods *************************** |
| 5340 | ******************************************************************************/ |
| 5341 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5342 | /****************************************************************************** |
| 5343 | ************************** Begin Proxy Locking ******************************** |
| 5344 | ** |
| 5345 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 5346 | ** other locking methods on secondary lock files. Proxy locking is a |
| 5347 | ** meta-layer over top of the primitive locking implemented above. For |
| 5348 | ** this reason, the division that implements of proxy locking is deferred |
| 5349 | ** until late in the file (here) after all of the other I/O methods have |
| 5350 | ** been defined - so that the primitive locking methods are available |
| 5351 | ** as services to help with the implementation of proxy locking. |
| 5352 | ** |
| 5353 | **** |
| 5354 | ** |
| 5355 | ** The default locking schemes in SQLite use byte-range locks on the |
| 5356 | ** database file to coordinate safe, concurrent access by multiple readers |
| 5357 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 5358 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 5359 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 5360 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 5361 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 5362 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 5363 | ** address in the shared range is taken for a SHARED lock, the entire |
| 5364 | ** shared range is taken for an EXCLUSIVE lock): |
| 5365 | ** |
| 5366 | ** PENDING_BYTE 0x40000000 |
| 5367 | ** RESERVED_BYTE 0x40000001 |
| 5368 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 5369 | ** |
| 5370 | ** This works well on the local file system, but shows a nearly 100x |
| 5371 | ** slowdown in read performance on AFP because the AFP client disables |
| 5372 | ** the read cache when byte-range locks are present. Enabling the read |
| 5373 | ** cache exposes a cache coherency problem that is present on all OS X |
| 5374 | ** supported network file systems. NFS and AFP both observe the |
| 5375 | ** close-to-open semantics for ensuring cache coherency |
| 5376 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 5377 | ** address the requirements for concurrent database access by multiple |
| 5378 | ** readers and writers |
| 5379 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 5380 | ** |
| 5381 | ** To address the performance and cache coherency issues, proxy file locking |
| 5382 | ** changes the way database access is controlled by limiting access to a |
| 5383 | ** single host at a time and moving file locks off of the database file |
| 5384 | ** and onto a proxy file on the local file system. |
| 5385 | ** |
| 5386 | ** |
| 5387 | ** Using proxy locks |
| 5388 | ** ----------------- |
| 5389 | ** |
| 5390 | ** C APIs |
| 5391 | ** |
| 5392 | ** sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE, |
| 5393 | ** <proxy_path> | ":auto:"); |
| 5394 | ** sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>); |
| 5395 | ** |
| 5396 | ** |
| 5397 | ** SQL pragmas |
| 5398 | ** |
| 5399 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 5400 | ** PRAGMA [database.]lock_proxy_file |
| 5401 | ** |
| 5402 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 5403 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 5404 | ** a proxy path based on the user's temp dir |
| 5405 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 5406 | ** actual proxy file name is generated from the name and path of the |
| 5407 | ** database file. For example: |
| 5408 | ** |
| 5409 | ** For database path "/Users/me/foo.db" |
| 5410 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 5411 | ** |
| 5412 | ** Once a lock proxy is configured for a database connection, it can not |
| 5413 | ** be removed, however it may be switched to a different proxy path via |
| 5414 | ** the above APIs (assuming the conch file is not being held by another |
| 5415 | ** connection or process). |
| 5416 | ** |
| 5417 | ** |
| 5418 | ** How proxy locking works |
| 5419 | ** ----------------------- |
| 5420 | ** |
| 5421 | ** Proxy file locking relies primarily on two new supporting files: |
| 5422 | ** |
| 5423 | ** * conch file to limit access to the database file to a single host |
| 5424 | ** at a time |
| 5425 | ** |
| 5426 | ** * proxy file to act as a proxy for the advisory locks normally |
| 5427 | ** taken on the database |
| 5428 | ** |
| 5429 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 5430 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 5431 | ** contents and comparing the host's unique host ID (see below) and lock |
| 5432 | ** proxy path against the values stored in the conch. The conch file is |
| 5433 | ** stored in the same directory as the database file and the file name |
| 5434 | ** is patterned after the database file name as ".<databasename>-conch". |
| 5435 | ** If the conch file does not exist, or it's contents do not match the |
| 5436 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 5437 | ** lock and the conch file contents is updated with the host ID and proxy |
| 5438 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 5439 | ** is held by another process (with a shared lock), the exclusive lock |
| 5440 | ** will fail and SQLITE_BUSY is returned. |
| 5441 | ** |
| 5442 | ** The proxy file - a single-byte file used for all advisory file locks |
| 5443 | ** normally taken on the database file. This allows for safe sharing |
| 5444 | ** of the database file for multiple readers and writers on the same |
| 5445 | ** host (the conch ensures that they all use the same local lock file). |
| 5446 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5447 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 5448 | ** only taken when the first request to lock database file is made. |
| 5449 | ** This matches the semantics of the traditional locking behavior, where |
| 5450 | ** opening a connection to a database file does not take a lock on it. |
| 5451 | ** The shared lock and an open file descriptor are maintained until |
| 5452 | ** the connection to the database is closed. |
| 5453 | ** |
| 5454 | ** The proxy file and the lock file are never deleted so they only need |
| 5455 | ** to be created the first time they are used. |
| 5456 | ** |
| 5457 | ** Configuration options |
| 5458 | ** --------------------- |
| 5459 | ** |
| 5460 | ** SQLITE_PREFER_PROXY_LOCKING |
| 5461 | ** |
| 5462 | ** Database files accessed on non-local file systems are |
| 5463 | ** automatically configured for proxy locking, lock files are |
| 5464 | ** named automatically using the same logic as |
| 5465 | ** PRAGMA lock_proxy_file=":auto:" |
| 5466 | ** |
| 5467 | ** SQLITE_PROXY_DEBUG |
| 5468 | ** |
| 5469 | ** Enables the logging of error messages during host id file |
| 5470 | ** retrieval and creation |
| 5471 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5472 | ** LOCKPROXYDIR |
| 5473 | ** |
| 5474 | ** Overrides the default directory used for lock proxy files that |
| 5475 | ** are named automatically via the ":auto:" setting |
| 5476 | ** |
| 5477 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 5478 | ** |
| 5479 | ** Permissions to use when creating a directory for storing the |
| 5480 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 5481 | ** |
| 5482 | ** |
| 5483 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 5484 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 5485 | ** force proxy locking to be used for every database file opened, and 0 |
| 5486 | ** will force automatic proxy locking to be disabled for all database |
| 5487 | ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or |
| 5488 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 5489 | */ |
| 5490 | |
| 5491 | /* |
| 5492 | ** Proxy locking is only available on MacOSX |
| 5493 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5494 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5495 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5496 | /* |
| 5497 | ** The proxyLockingContext has the path and file structures for the remote |
| 5498 | ** and local proxy files in it |
| 5499 | */ |
| 5500 | typedef struct proxyLockingContext proxyLockingContext; |
| 5501 | struct proxyLockingContext { |
| 5502 | unixFile *conchFile; /* Open conch file */ |
| 5503 | char *conchFilePath; /* Name of the conch file */ |
| 5504 | unixFile *lockProxy; /* Open proxy lock file */ |
| 5505 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 5506 | char *dbPath; /* Name of the open file */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5507 | int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5508 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 5509 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 5510 | }; |
| 5511 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5512 | /* |
| 5513 | ** The proxy lock file path for the database at dbPath is written into lPath, |
| 5514 | ** which must point to valid, writable memory large enough for a maxLen length |
| 5515 | ** file path. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5516 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5517 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 5518 | int len; |
| 5519 | int dbLen; |
| 5520 | int i; |
| 5521 | |
| 5522 | #ifdef LOCKPROXYDIR |
| 5523 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 5524 | #else |
| 5525 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 5526 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5527 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
| 5528 | OSTRACE4("GETLOCKPATH failed %s errno=%d pid=%d\n", |
| 5529 | lPath, errno, getpid()); |
| 5530 | return SQLITE_IOERR_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5531 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5532 | len = strlcat(lPath, "sqliteplocks", maxLen); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5533 | } |
| 5534 | # else |
| 5535 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 5536 | # endif |
| 5537 | #endif |
| 5538 | |
| 5539 | if( lPath[len-1]!='/' ){ |
| 5540 | len = strlcat(lPath, "/", maxLen); |
| 5541 | } |
| 5542 | |
| 5543 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5544 | dbLen = (int)strlen(dbPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5545 | for( i=0; i<dbLen && (i+len+7)<maxLen; i++){ |
| 5546 | char c = dbPath[i]; |
| 5547 | lPath[i+len] = (c=='/')?'_':c; |
| 5548 | } |
| 5549 | lPath[i+len]='\0'; |
| 5550 | strlcat(lPath, ":auto:", maxLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5551 | OSTRACE3("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, getpid()); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5552 | return SQLITE_OK; |
| 5553 | } |
| 5554 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5555 | /* |
| 5556 | ** Creates the lock file and any missing directories in lockPath |
| 5557 | */ |
| 5558 | static int proxyCreateLockPath(const char *lockPath){ |
| 5559 | int i, len; |
| 5560 | char buf[MAXPATHLEN]; |
| 5561 | int start = 0; |
| 5562 | |
| 5563 | assert(lockPath!=NULL); |
| 5564 | /* try to create all the intermediate directories */ |
| 5565 | len = (int)strlen(lockPath); |
| 5566 | buf[0] = lockPath[0]; |
| 5567 | for( i=1; i<len; i++ ){ |
| 5568 | if( lockPath[i] == '/' && (i - start > 0) ){ |
| 5569 | /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 5570 | if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 5571 | || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 5572 | buf[i]='\0'; |
| 5573 | if( mkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
| 5574 | int err=errno; |
| 5575 | if( err!=EEXIST ) { |
| 5576 | OSTRACE5("CREATELOCKPATH FAILED creating %s, " |
| 5577 | "'%s' proxy lock path=%s pid=%d\n", |
| 5578 | buf, strerror(err), lockPath, getpid()); |
| 5579 | return err; |
| 5580 | } |
| 5581 | } |
| 5582 | } |
| 5583 | start=i+1; |
| 5584 | } |
| 5585 | buf[i] = lockPath[i]; |
| 5586 | } |
| 5587 | OSTRACE3("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, getpid()); |
| 5588 | return 0; |
| 5589 | } |
| 5590 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5591 | /* |
| 5592 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 5593 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 5594 | ** |
| 5595 | ** The caller is responsible not only for closing the file descriptor |
| 5596 | ** but also for freeing the memory associated with the file descriptor. |
| 5597 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5598 | static int proxyCreateUnixFile( |
| 5599 | const char *path, /* path for the new unixFile */ |
| 5600 | unixFile **ppFile, /* unixFile created and returned by ref */ |
| 5601 | int islockfile /* if non zero missing dirs will be created */ |
| 5602 | ) { |
| 5603 | int fd = -1; |
| 5604 | int dirfd = -1; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5605 | unixFile *pNew; |
| 5606 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5607 | int openFlags = O_RDWR | O_CREAT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5608 | sqlite3_vfs dummyVfs; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5609 | int terrno = 0; |
| 5610 | UnixUnusedFd *pUnused = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5611 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5612 | /* 1. first try to open/create the file |
| 5613 | ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 5614 | ** the parent directories and then try again. |
| 5615 | ** 3. if that fails, try to open the file read-only |
| 5616 | ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 5617 | */ |
| 5618 | pUnused = findReusableFd(path, openFlags); |
| 5619 | if( pUnused ){ |
| 5620 | fd = pUnused->fd; |
| 5621 | }else{ |
| 5622 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
| 5623 | if( !pUnused ){ |
| 5624 | return SQLITE_NOMEM; |
| 5625 | } |
| 5626 | } |
| 5627 | if( fd<0 ){ |
| 5628 | fd = open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5629 | terrno = errno; |
| 5630 | if( fd<0 && errno==ENOENT && islockfile ){ |
| 5631 | if( proxyCreateLockPath(path) == SQLITE_OK ){ |
| 5632 | fd = open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5633 | } |
| 5634 | } |
| 5635 | } |
| 5636 | if( fd<0 ){ |
| 5637 | openFlags = O_RDONLY; |
| 5638 | fd = open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5639 | terrno = errno; |
| 5640 | } |
| 5641 | if( fd<0 ){ |
| 5642 | if( islockfile ){ |
| 5643 | return SQLITE_BUSY; |
| 5644 | } |
| 5645 | switch (terrno) { |
| 5646 | case EACCES: |
| 5647 | return SQLITE_PERM; |
| 5648 | case EIO: |
| 5649 | return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 5650 | default: |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 5651 | return SQLITE_CANTOPEN_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5652 | } |
| 5653 | } |
| 5654 | |
| 5655 | pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew)); |
| 5656 | if( pNew==NULL ){ |
| 5657 | rc = SQLITE_NOMEM; |
| 5658 | goto end_create_proxy; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5659 | } |
| 5660 | memset(pNew, 0, sizeof(unixFile)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5661 | pNew->openFlags = openFlags; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5662 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5663 | pUnused->fd = fd; |
| 5664 | pUnused->flags = openFlags; |
| 5665 | pNew->pUnused = pUnused; |
| 5666 | |
| 5667 | rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0); |
| 5668 | if( rc==SQLITE_OK ){ |
| 5669 | *ppFile = pNew; |
| 5670 | return SQLITE_OK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5671 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5672 | end_create_proxy: |
| 5673 | close(fd); /* silently leak fd if error, we're already in error */ |
| 5674 | sqlite3_free(pNew); |
| 5675 | sqlite3_free(pUnused); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5676 | return rc; |
| 5677 | } |
| 5678 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5679 | #ifdef SQLITE_TEST |
| 5680 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 5681 | int sqlite3_hostid_num = 0; |
| 5682 | #endif |
| 5683 | |
| 5684 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 5685 | |
| 5686 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 5687 | ** bytes of writable memory. |
| 5688 | */ |
| 5689 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
| 5690 | struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
| 5691 | |
| 5692 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 5693 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
| 5694 | if( gethostuuid(pHostID, &timeout) ){ |
| 5695 | int err = errno; |
| 5696 | if( pError ){ |
| 5697 | *pError = err; |
| 5698 | } |
| 5699 | return SQLITE_IOERR; |
| 5700 | } |
| 5701 | #ifdef SQLITE_TEST |
| 5702 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 5703 | if( sqlite3_hostid_num != 0){ |
| 5704 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 5705 | } |
| 5706 | #endif |
| 5707 | |
| 5708 | return SQLITE_OK; |
| 5709 | } |
| 5710 | |
| 5711 | /* The conch file contains the header, host id and lock file path |
| 5712 | */ |
| 5713 | #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 5714 | #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 5715 | #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 5716 | #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 5717 | |
| 5718 | /* |
| 5719 | ** Takes an open conch file, copies the contents to a new path and then moves |
| 5720 | ** it back. The newly created file's file descriptor is assigned to the |
| 5721 | ** conch file structure and finally the original conch file descriptor is |
| 5722 | ** closed. Returns zero if successful. |
| 5723 | */ |
| 5724 | static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 5725 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5726 | unixFile *conchFile = pCtx->conchFile; |
| 5727 | char tPath[MAXPATHLEN]; |
| 5728 | char buf[PROXY_MAXCONCHLEN]; |
| 5729 | char *cPath = pCtx->conchFilePath; |
| 5730 | size_t readLen = 0; |
| 5731 | size_t pathLen = 0; |
| 5732 | char errmsg[64] = ""; |
| 5733 | int fd = -1; |
| 5734 | int rc = -1; |
| 5735 | |
| 5736 | /* create a new path by replace the trailing '-conch' with '-break' */ |
| 5737 | pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 5738 | if( pathLen>MAXPATHLEN || pathLen<6 || |
| 5739 | (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
| 5740 | sprintf(errmsg, "path error (len %d)", (int)pathLen); |
| 5741 | goto end_breaklock; |
| 5742 | } |
| 5743 | /* read the conch content */ |
| 5744 | readLen = pread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
| 5745 | if( readLen<PROXY_PATHINDEX ){ |
| 5746 | sprintf(errmsg, "read error (len %d)", (int)readLen); |
| 5747 | goto end_breaklock; |
| 5748 | } |
| 5749 | /* write it out to the temporary break file */ |
| 5750 | fd = open(tPath, (O_RDWR|O_CREAT|O_EXCL), SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5751 | if( fd<0 ){ |
| 5752 | sprintf(errmsg, "create failed (%d)", errno); |
| 5753 | goto end_breaklock; |
| 5754 | } |
| 5755 | if( pwrite(fd, buf, readLen, 0) != readLen ){ |
| 5756 | sprintf(errmsg, "write failed (%d)", errno); |
| 5757 | goto end_breaklock; |
| 5758 | } |
| 5759 | if( rename(tPath, cPath) ){ |
| 5760 | sprintf(errmsg, "rename failed (%d)", errno); |
| 5761 | goto end_breaklock; |
| 5762 | } |
| 5763 | rc = 0; |
| 5764 | fprintf(stderr, "broke stale lock on %s\n", cPath); |
| 5765 | close(conchFile->h); |
| 5766 | conchFile->h = fd; |
| 5767 | conchFile->openFlags = O_RDWR | O_CREAT; |
| 5768 | |
| 5769 | end_breaklock: |
| 5770 | if( rc ){ |
| 5771 | if( fd>=0 ){ |
| 5772 | unlink(tPath); |
| 5773 | close(fd); |
| 5774 | } |
| 5775 | fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 5776 | } |
| 5777 | return rc; |
| 5778 | } |
| 5779 | |
| 5780 | /* Take the requested lock on the conch file and break a stale lock if the |
| 5781 | ** host id matches. |
| 5782 | */ |
| 5783 | static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 5784 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5785 | unixFile *conchFile = pCtx->conchFile; |
| 5786 | int rc = SQLITE_OK; |
| 5787 | int nTries = 0; |
| 5788 | struct timespec conchModTime; |
| 5789 | |
| 5790 | do { |
| 5791 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 5792 | nTries ++; |
| 5793 | if( rc==SQLITE_BUSY ){ |
| 5794 | /* If the lock failed (busy): |
| 5795 | * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 5796 | * 2nd try: fail if the mod time changed or host id is different, wait |
| 5797 | * 10 sec and try again |
| 5798 | * 3rd try: break the lock unless the mod time has changed. |
| 5799 | */ |
| 5800 | struct stat buf; |
| 5801 | if( fstat(conchFile->h, &buf) ){ |
| 5802 | pFile->lastErrno = errno; |
| 5803 | return SQLITE_IOERR_LOCK; |
| 5804 | } |
| 5805 | |
| 5806 | if( nTries==1 ){ |
| 5807 | conchModTime = buf.st_mtimespec; |
| 5808 | usleep(500000); /* wait 0.5 sec and try the lock again*/ |
| 5809 | continue; |
| 5810 | } |
| 5811 | |
| 5812 | assert( nTries>1 ); |
| 5813 | if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 5814 | conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 5815 | return SQLITE_BUSY; |
| 5816 | } |
| 5817 | |
| 5818 | if( nTries==2 ){ |
| 5819 | char tBuf[PROXY_MAXCONCHLEN]; |
| 5820 | int len = pread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
| 5821 | if( len<0 ){ |
| 5822 | pFile->lastErrno = errno; |
| 5823 | return SQLITE_IOERR_LOCK; |
| 5824 | } |
| 5825 | if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 5826 | /* don't break the lock if the host id doesn't match */ |
| 5827 | if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 5828 | return SQLITE_BUSY; |
| 5829 | } |
| 5830 | }else{ |
| 5831 | /* don't break the lock on short read or a version mismatch */ |
| 5832 | return SQLITE_BUSY; |
| 5833 | } |
| 5834 | usleep(10000000); /* wait 10 sec and try the lock again */ |
| 5835 | continue; |
| 5836 | } |
| 5837 | |
| 5838 | assert( nTries==3 ); |
| 5839 | if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 5840 | rc = SQLITE_OK; |
| 5841 | if( lockType==EXCLUSIVE_LOCK ){ |
| 5842 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 5843 | } |
| 5844 | if( !rc ){ |
| 5845 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 5846 | } |
| 5847 | } |
| 5848 | } |
| 5849 | } while( rc==SQLITE_BUSY && nTries<3 ); |
| 5850 | |
| 5851 | return rc; |
| 5852 | } |
| 5853 | |
| 5854 | /* 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] | 5855 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 5856 | ** lockPath means that the lockPath in the conch file will be used if the |
| 5857 | ** host IDs match, or a new lock path will be generated automatically |
| 5858 | ** and written to the conch file. |
| 5859 | */ |
| 5860 | static int proxyTakeConch(unixFile *pFile){ |
| 5861 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5862 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5863 | if( pCtx->conchHeld!=0 ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5864 | return SQLITE_OK; |
| 5865 | }else{ |
| 5866 | unixFile *conchFile = pCtx->conchFile; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5867 | uuid_t myHostID; |
| 5868 | int pError = 0; |
| 5869 | char readBuf[PROXY_MAXCONCHLEN]; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5870 | char lockPath[MAXPATHLEN]; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5871 | char *tempLockPath = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5872 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5873 | int createConch = 0; |
| 5874 | int hostIdMatch = 0; |
| 5875 | int readLen = 0; |
| 5876 | int tryOldLockPath = 0; |
| 5877 | int forceNewLockPath = 0; |
| 5878 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5879 | OSTRACE4("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
| 5880 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid()); |
| 5881 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5882 | rc = proxyGetHostID(myHostID, &pError); |
| 5883 | if( (rc&0xff)==SQLITE_IOERR ){ |
| 5884 | pFile->lastErrno = pError; |
| 5885 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5886 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5887 | rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5888 | if( rc!=SQLITE_OK ){ |
| 5889 | goto end_takeconch; |
| 5890 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5891 | /* read the existing conch file */ |
| 5892 | readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 5893 | if( readLen<0 ){ |
| 5894 | /* I/O error: lastErrno set by seekAndRead */ |
| 5895 | pFile->lastErrno = conchFile->lastErrno; |
| 5896 | rc = SQLITE_IOERR_READ; |
| 5897 | goto end_takeconch; |
| 5898 | }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 5899 | readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 5900 | /* a short read or version format mismatch means we need to create a new |
| 5901 | ** conch file. |
| 5902 | */ |
| 5903 | createConch = 1; |
| 5904 | } |
| 5905 | /* if the host id matches and the lock path already exists in the conch |
| 5906 | ** we'll try to use the path there, if we can't open that path, we'll |
| 5907 | ** retry with a new auto-generated path |
| 5908 | */ |
| 5909 | do { /* in case we need to try again for an :auto: named lock file */ |
| 5910 | |
| 5911 | if( !createConch && !forceNewLockPath ){ |
| 5912 | hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 5913 | PROXY_HOSTIDLEN); |
| 5914 | /* if the conch has data compare the contents */ |
| 5915 | if( !pCtx->lockProxyPath ){ |
| 5916 | /* for auto-named local lock file, just check the host ID and we'll |
| 5917 | ** use the local lock file path that's already in there |
| 5918 | */ |
| 5919 | if( hostIdMatch ){ |
| 5920 | size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 5921 | |
| 5922 | if( pathLen>=MAXPATHLEN ){ |
| 5923 | pathLen=MAXPATHLEN-1; |
| 5924 | } |
| 5925 | memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 5926 | lockPath[pathLen] = 0; |
| 5927 | tempLockPath = lockPath; |
| 5928 | tryOldLockPath = 1; |
| 5929 | /* create a copy of the lock path if the conch is taken */ |
| 5930 | goto end_takeconch; |
| 5931 | } |
| 5932 | }else if( hostIdMatch |
| 5933 | && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 5934 | readLen-PROXY_PATHINDEX) |
| 5935 | ){ |
| 5936 | /* conch host and lock path match */ |
| 5937 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5938 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5939 | } |
| 5940 | |
| 5941 | /* if the conch isn't writable and doesn't match, we can't take it */ |
| 5942 | if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 5943 | rc = SQLITE_BUSY; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5944 | goto end_takeconch; |
| 5945 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5946 | |
| 5947 | /* 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] | 5948 | if( !pCtx->lockProxyPath ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5949 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 5950 | tempLockPath = lockPath; |
| 5951 | /* create a copy of the lock path _only_ if the conch is taken */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5952 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5953 | |
| 5954 | /* update conch with host and path (this will fail if other process |
| 5955 | ** has a shared lock already), if the host id matches, use the big |
| 5956 | ** stick. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5957 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5958 | futimes(conchFile->h, NULL); |
| 5959 | if( hostIdMatch && !createConch ){ |
| 5960 | if( conchFile->pLock && conchFile->pLock->cnt>1 ){ |
| 5961 | /* We are trying for an exclusive lock but another thread in this |
| 5962 | ** same process is still holding a shared lock. */ |
| 5963 | rc = SQLITE_BUSY; |
| 5964 | } else { |
| 5965 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5966 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5967 | }else{ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5968 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5969 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5970 | if( rc==SQLITE_OK ){ |
| 5971 | char writeBuffer[PROXY_MAXCONCHLEN]; |
| 5972 | int writeSize = 0; |
| 5973 | |
| 5974 | writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 5975 | memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 5976 | if( pCtx->lockProxyPath!=NULL ){ |
| 5977 | strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN); |
| 5978 | }else{ |
| 5979 | strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 5980 | } |
| 5981 | writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
| 5982 | ftruncate(conchFile->h, writeSize); |
| 5983 | rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
| 5984 | fsync(conchFile->h); |
| 5985 | /* If we created a new conch file (not just updated the contents of a |
| 5986 | ** valid conch file), try to match the permissions of the database |
| 5987 | */ |
| 5988 | if( rc==SQLITE_OK && createConch ){ |
| 5989 | struct stat buf; |
| 5990 | int err = fstat(pFile->h, &buf); |
| 5991 | if( err==0 ){ |
| 5992 | mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 5993 | S_IROTH|S_IWOTH); |
| 5994 | /* try to match the database file R/W permissions, ignore failure */ |
| 5995 | #ifndef SQLITE_PROXY_DEBUG |
| 5996 | fchmod(conchFile->h, cmode); |
| 5997 | #else |
| 5998 | if( fchmod(conchFile->h, cmode)!=0 ){ |
| 5999 | int code = errno; |
| 6000 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 6001 | cmode, code, strerror(code)); |
| 6002 | } else { |
| 6003 | fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 6004 | } |
| 6005 | }else{ |
| 6006 | int code = errno; |
| 6007 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 6008 | err, code, strerror(code)); |
| 6009 | #endif |
| 6010 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6011 | } |
| 6012 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6013 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 6014 | |
| 6015 | end_takeconch: |
| 6016 | OSTRACE2("TRANSPROXY: CLOSE %d\n", pFile->h); |
| 6017 | if( rc==SQLITE_OK && pFile->openFlags ){ |
| 6018 | if( pFile->h>=0 ){ |
| 6019 | #ifdef STRICT_CLOSE_ERROR |
| 6020 | if( close(pFile->h) ){ |
| 6021 | pFile->lastErrno = errno; |
| 6022 | return SQLITE_IOERR_CLOSE; |
| 6023 | } |
| 6024 | #else |
| 6025 | close(pFile->h); /* silently leak fd if fail */ |
| 6026 | #endif |
| 6027 | } |
| 6028 | pFile->h = -1; |
| 6029 | int fd = open(pCtx->dbPath, pFile->openFlags, |
| 6030 | SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 6031 | OSTRACE2("TRANSPROXY: OPEN %d\n", fd); |
| 6032 | if( fd>=0 ){ |
| 6033 | pFile->h = fd; |
| 6034 | }else{ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6035 | rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6036 | during locking */ |
| 6037 | } |
| 6038 | } |
| 6039 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 6040 | char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 6041 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 6042 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 6043 | /* we couldn't create the proxy lock file with the old lock file path |
| 6044 | ** so try again via auto-naming |
| 6045 | */ |
| 6046 | forceNewLockPath = 1; |
| 6047 | tryOldLockPath = 0; |
dan | 2b0ef47 | 2010-02-16 12:18:47 +0000 | [diff] [blame] | 6048 | continue; /* go back to the do {} while start point, try again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6049 | } |
| 6050 | } |
| 6051 | if( rc==SQLITE_OK ){ |
| 6052 | /* Need to make a copy of path if we extracted the value |
| 6053 | ** from the conch file or the path was allocated on the stack |
| 6054 | */ |
| 6055 | if( tempLockPath ){ |
| 6056 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 6057 | if( !pCtx->lockProxyPath ){ |
| 6058 | rc = SQLITE_NOMEM; |
| 6059 | } |
| 6060 | } |
| 6061 | } |
| 6062 | if( rc==SQLITE_OK ){ |
| 6063 | pCtx->conchHeld = 1; |
| 6064 | |
| 6065 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 6066 | afpLockingContext *afpCtx; |
| 6067 | afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 6068 | afpCtx->dbPath = pCtx->lockProxyPath; |
| 6069 | } |
| 6070 | } else { |
| 6071 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 6072 | } |
| 6073 | OSTRACE3("TAKECONCH %d %s\n", conchFile->h, rc==SQLITE_OK?"ok":"failed"); |
| 6074 | return rc; |
| 6075 | } while (1); /* in case we need to retry the :auto: lock file - we should never get here except via the 'continue' call. */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6076 | } |
| 6077 | } |
| 6078 | |
| 6079 | /* |
| 6080 | ** If pFile holds a lock on a conch file, then release that lock. |
| 6081 | */ |
| 6082 | static int proxyReleaseConch(unixFile *pFile){ |
| 6083 | int rc; /* Subroutine return code */ |
| 6084 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 6085 | unixFile *conchFile; /* Name of the conch file */ |
| 6086 | |
| 6087 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6088 | conchFile = pCtx->conchFile; |
| 6089 | OSTRACE4("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
| 6090 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
| 6091 | getpid()); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6092 | if( pCtx->conchHeld>0 ){ |
| 6093 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 6094 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6095 | pCtx->conchHeld = 0; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6096 | OSTRACE3("RELEASECONCH %d %s\n", conchFile->h, |
| 6097 | (rc==SQLITE_OK ? "ok" : "failed")); |
| 6098 | return rc; |
| 6099 | } |
| 6100 | |
| 6101 | /* |
| 6102 | ** Given the name of a database file, compute the name of its conch file. |
| 6103 | ** Store the conch filename in memory obtained from sqlite3_malloc(). |
| 6104 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 6105 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 6106 | ** |
| 6107 | ** The caller is responsible for ensuring that the allocated memory |
| 6108 | ** space is eventually freed. |
| 6109 | ** |
| 6110 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 6111 | */ |
| 6112 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 6113 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6114 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6115 | char *conchPath; /* buffer in which to construct conch name */ |
| 6116 | |
| 6117 | /* Allocate space for the conch filename and initialize the name to |
| 6118 | ** the name of the original database file. */ |
| 6119 | *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8); |
| 6120 | if( conchPath==0 ){ |
| 6121 | return SQLITE_NOMEM; |
| 6122 | } |
| 6123 | memcpy(conchPath, dbPath, len+1); |
| 6124 | |
| 6125 | /* now insert a "." before the last / character */ |
| 6126 | for( i=(len-1); i>=0; i-- ){ |
| 6127 | if( conchPath[i]=='/' ){ |
| 6128 | i++; |
| 6129 | break; |
| 6130 | } |
| 6131 | } |
| 6132 | conchPath[i]='.'; |
| 6133 | while ( i<len ){ |
| 6134 | conchPath[i+1]=dbPath[i]; |
| 6135 | i++; |
| 6136 | } |
| 6137 | |
| 6138 | /* append the "-conch" suffix to the file */ |
| 6139 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6140 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6141 | |
| 6142 | return SQLITE_OK; |
| 6143 | } |
| 6144 | |
| 6145 | |
| 6146 | /* Takes a fully configured proxy locking-style unix file and switches |
| 6147 | ** the local lock file path |
| 6148 | */ |
| 6149 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 6150 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 6151 | char *oldPath = pCtx->lockProxyPath; |
| 6152 | int rc = SQLITE_OK; |
| 6153 | |
| 6154 | if( pFile->locktype!=NO_LOCK ){ |
| 6155 | return SQLITE_BUSY; |
| 6156 | } |
| 6157 | |
| 6158 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 6159 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 6160 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 6161 | return SQLITE_OK; |
| 6162 | }else{ |
| 6163 | unixFile *lockProxy = pCtx->lockProxy; |
| 6164 | pCtx->lockProxy=NULL; |
| 6165 | pCtx->conchHeld = 0; |
| 6166 | if( lockProxy!=NULL ){ |
| 6167 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 6168 | if( rc ) return rc; |
| 6169 | sqlite3_free(lockProxy); |
| 6170 | } |
| 6171 | sqlite3_free(oldPath); |
| 6172 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 6173 | } |
| 6174 | |
| 6175 | return rc; |
| 6176 | } |
| 6177 | |
| 6178 | /* |
| 6179 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 6180 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 6181 | ** |
| 6182 | ** This routine find the filename associated with pFile and writes it |
| 6183 | ** int dbPath. |
| 6184 | */ |
| 6185 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6186 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6187 | if( pFile->pMethod == &afpIoMethods ){ |
| 6188 | /* afp style keeps a reference to the db path in the filePath field |
| 6189 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6190 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6191 | strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN); |
| 6192 | } else |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6193 | #endif |
| 6194 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 6195 | /* dot lock style uses the locking context to store the dot lock |
| 6196 | ** file path */ |
| 6197 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 6198 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 6199 | }else{ |
| 6200 | /* all other styles use the locking context to store the db file path */ |
| 6201 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6202 | strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6203 | } |
| 6204 | return SQLITE_OK; |
| 6205 | } |
| 6206 | |
| 6207 | /* |
| 6208 | ** Takes an already filled in unix file and alters it so all file locking |
| 6209 | ** will be performed on the local proxy lock file. The following fields |
| 6210 | ** are preserved in the locking context so that they can be restored and |
| 6211 | ** the unix structure properly cleaned up at close time: |
| 6212 | ** ->lockingContext |
| 6213 | ** ->pMethod |
| 6214 | */ |
| 6215 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 6216 | proxyLockingContext *pCtx; |
| 6217 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 6218 | char *lockPath=NULL; |
| 6219 | int rc = SQLITE_OK; |
| 6220 | |
| 6221 | if( pFile->locktype!=NO_LOCK ){ |
| 6222 | return SQLITE_BUSY; |
| 6223 | } |
| 6224 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 6225 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 6226 | lockPath=NULL; |
| 6227 | }else{ |
| 6228 | lockPath=(char *)path; |
| 6229 | } |
| 6230 | |
| 6231 | OSTRACE4("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
| 6232 | (lockPath ? lockPath : ":auto:"), getpid()); |
| 6233 | |
| 6234 | pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 6235 | if( pCtx==0 ){ |
| 6236 | return SQLITE_NOMEM; |
| 6237 | } |
| 6238 | memset(pCtx, 0, sizeof(*pCtx)); |
| 6239 | |
| 6240 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 6241 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6242 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 6243 | if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 6244 | /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 6245 | ** (c) the file system is read-only, then enable no-locking access. |
| 6246 | ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 6247 | ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 6248 | */ |
| 6249 | struct statfs fsInfo; |
| 6250 | struct stat conchInfo; |
| 6251 | int goLockless = 0; |
| 6252 | |
| 6253 | if( stat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
| 6254 | int err = errno; |
| 6255 | if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 6256 | goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 6257 | } |
| 6258 | } |
| 6259 | if( goLockless ){ |
| 6260 | pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 6261 | rc = SQLITE_OK; |
| 6262 | } |
| 6263 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6264 | } |
| 6265 | if( rc==SQLITE_OK && lockPath ){ |
| 6266 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 6267 | } |
| 6268 | |
| 6269 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6270 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 6271 | if( pCtx->dbPath==NULL ){ |
| 6272 | rc = SQLITE_NOMEM; |
| 6273 | } |
| 6274 | } |
| 6275 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6276 | /* all memory is allocated, proxys are created and assigned, |
| 6277 | ** switch the locking context and pMethod then return. |
| 6278 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6279 | pCtx->oldLockingContext = pFile->lockingContext; |
| 6280 | pFile->lockingContext = pCtx; |
| 6281 | pCtx->pOldMethod = pFile->pMethod; |
| 6282 | pFile->pMethod = &proxyIoMethods; |
| 6283 | }else{ |
| 6284 | if( pCtx->conchFile ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6285 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6286 | sqlite3_free(pCtx->conchFile); |
| 6287 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6288 | sqlite3_free(pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6289 | sqlite3_free(pCtx->conchFilePath); |
| 6290 | sqlite3_free(pCtx); |
| 6291 | } |
| 6292 | OSTRACE3("TRANSPROXY %d %s\n", pFile->h, |
| 6293 | (rc==SQLITE_OK ? "ok" : "failed")); |
| 6294 | return rc; |
| 6295 | } |
| 6296 | |
| 6297 | |
| 6298 | /* |
| 6299 | ** This routine handles sqlite3_file_control() calls that are specific |
| 6300 | ** to proxy locking. |
| 6301 | */ |
| 6302 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 6303 | switch( op ){ |
| 6304 | case SQLITE_GET_LOCKPROXYFILE: { |
| 6305 | unixFile *pFile = (unixFile*)id; |
| 6306 | if( pFile->pMethod == &proxyIoMethods ){ |
| 6307 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 6308 | proxyTakeConch(pFile); |
| 6309 | if( pCtx->lockProxyPath ){ |
| 6310 | *(const char **)pArg = pCtx->lockProxyPath; |
| 6311 | }else{ |
| 6312 | *(const char **)pArg = ":auto: (not held)"; |
| 6313 | } |
| 6314 | } else { |
| 6315 | *(const char **)pArg = NULL; |
| 6316 | } |
| 6317 | return SQLITE_OK; |
| 6318 | } |
| 6319 | case SQLITE_SET_LOCKPROXYFILE: { |
| 6320 | unixFile *pFile = (unixFile*)id; |
| 6321 | int rc = SQLITE_OK; |
| 6322 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 6323 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 6324 | if( isProxyStyle ){ |
| 6325 | /* turn off proxy locking - not supported */ |
| 6326 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 6327 | }else{ |
| 6328 | /* turn off proxy locking - already off - NOOP */ |
| 6329 | rc = SQLITE_OK; |
| 6330 | } |
| 6331 | }else{ |
| 6332 | const char *proxyPath = (const char *)pArg; |
| 6333 | if( isProxyStyle ){ |
| 6334 | proxyLockingContext *pCtx = |
| 6335 | (proxyLockingContext*)pFile->lockingContext; |
| 6336 | if( !strcmp(pArg, ":auto:") |
| 6337 | || (pCtx->lockProxyPath && |
| 6338 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 6339 | ){ |
| 6340 | rc = SQLITE_OK; |
| 6341 | }else{ |
| 6342 | rc = switchLockProxyPath(pFile, proxyPath); |
| 6343 | } |
| 6344 | }else{ |
| 6345 | /* turn on proxy file locking */ |
| 6346 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 6347 | } |
| 6348 | } |
| 6349 | return rc; |
| 6350 | } |
| 6351 | default: { |
| 6352 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 6353 | } |
| 6354 | } |
| 6355 | /*NOTREACHED*/ |
| 6356 | return SQLITE_ERROR; |
| 6357 | } |
| 6358 | |
| 6359 | /* |
| 6360 | ** Within this division (the proxying locking implementation) the procedures |
| 6361 | ** above this point are all utilities. The lock-related methods of the |
| 6362 | ** proxy-locking sqlite3_io_method object follow. |
| 6363 | */ |
| 6364 | |
| 6365 | |
| 6366 | /* |
| 6367 | ** This routine checks if there is a RESERVED lock held on the specified |
| 6368 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 6369 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 6370 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 6371 | */ |
| 6372 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 6373 | unixFile *pFile = (unixFile*)id; |
| 6374 | int rc = proxyTakeConch(pFile); |
| 6375 | if( rc==SQLITE_OK ){ |
| 6376 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6377 | if( pCtx->conchHeld>0 ){ |
| 6378 | unixFile *proxy = pCtx->lockProxy; |
| 6379 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 6380 | }else{ /* conchHeld < 0 is lockless */ |
| 6381 | pResOut=0; |
| 6382 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6383 | } |
| 6384 | return rc; |
| 6385 | } |
| 6386 | |
| 6387 | /* |
| 6388 | ** Lock the file with the lock specified by parameter locktype - one |
| 6389 | ** of the following: |
| 6390 | ** |
| 6391 | ** (1) SHARED_LOCK |
| 6392 | ** (2) RESERVED_LOCK |
| 6393 | ** (3) PENDING_LOCK |
| 6394 | ** (4) EXCLUSIVE_LOCK |
| 6395 | ** |
| 6396 | ** Sometimes when requesting one lock state, additional lock states |
| 6397 | ** are inserted in between. The locking might fail on one of the later |
| 6398 | ** transitions leaving the lock state different from what it started but |
| 6399 | ** still short of its goal. The following chart shows the allowed |
| 6400 | ** transitions and the inserted intermediate states: |
| 6401 | ** |
| 6402 | ** UNLOCKED -> SHARED |
| 6403 | ** SHARED -> RESERVED |
| 6404 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 6405 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 6406 | ** PENDING -> EXCLUSIVE |
| 6407 | ** |
| 6408 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 6409 | ** routine to lower a locking level. |
| 6410 | */ |
| 6411 | static int proxyLock(sqlite3_file *id, int locktype) { |
| 6412 | unixFile *pFile = (unixFile*)id; |
| 6413 | int rc = proxyTakeConch(pFile); |
| 6414 | if( rc==SQLITE_OK ){ |
| 6415 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6416 | if( pCtx->conchHeld>0 ){ |
| 6417 | unixFile *proxy = pCtx->lockProxy; |
| 6418 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, locktype); |
| 6419 | pFile->locktype = proxy->locktype; |
| 6420 | }else{ |
| 6421 | /* conchHeld < 0 is lockless */ |
| 6422 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6423 | } |
| 6424 | return rc; |
| 6425 | } |
| 6426 | |
| 6427 | |
| 6428 | /* |
| 6429 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
| 6430 | ** must be either NO_LOCK or SHARED_LOCK. |
| 6431 | ** |
| 6432 | ** If the locking level of the file descriptor is already at or below |
| 6433 | ** the requested locking level, this routine is a no-op. |
| 6434 | */ |
| 6435 | static int proxyUnlock(sqlite3_file *id, int locktype) { |
| 6436 | unixFile *pFile = (unixFile*)id; |
| 6437 | int rc = proxyTakeConch(pFile); |
| 6438 | if( rc==SQLITE_OK ){ |
| 6439 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6440 | if( pCtx->conchHeld>0 ){ |
| 6441 | unixFile *proxy = pCtx->lockProxy; |
| 6442 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, locktype); |
| 6443 | pFile->locktype = proxy->locktype; |
| 6444 | }else{ |
| 6445 | /* conchHeld < 0 is lockless */ |
| 6446 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6447 | } |
| 6448 | return rc; |
| 6449 | } |
| 6450 | |
| 6451 | /* |
| 6452 | ** Close a file that uses proxy locks. |
| 6453 | */ |
| 6454 | static int proxyClose(sqlite3_file *id) { |
| 6455 | if( id ){ |
| 6456 | unixFile *pFile = (unixFile*)id; |
| 6457 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6458 | unixFile *lockProxy = pCtx->lockProxy; |
| 6459 | unixFile *conchFile = pCtx->conchFile; |
| 6460 | int rc = SQLITE_OK; |
| 6461 | |
| 6462 | if( lockProxy ){ |
| 6463 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 6464 | if( rc ) return rc; |
| 6465 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 6466 | if( rc ) return rc; |
| 6467 | sqlite3_free(lockProxy); |
| 6468 | pCtx->lockProxy = 0; |
| 6469 | } |
| 6470 | if( conchFile ){ |
| 6471 | if( pCtx->conchHeld ){ |
| 6472 | rc = proxyReleaseConch(pFile); |
| 6473 | if( rc ) return rc; |
| 6474 | } |
| 6475 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 6476 | if( rc ) return rc; |
| 6477 | sqlite3_free(conchFile); |
| 6478 | } |
| 6479 | sqlite3_free(pCtx->lockProxyPath); |
| 6480 | sqlite3_free(pCtx->conchFilePath); |
| 6481 | sqlite3_free(pCtx->dbPath); |
| 6482 | /* restore the original locking context and pMethod then close it */ |
| 6483 | pFile->lockingContext = pCtx->oldLockingContext; |
| 6484 | pFile->pMethod = pCtx->pOldMethod; |
| 6485 | sqlite3_free(pCtx); |
| 6486 | return pFile->pMethod->xClose(id); |
| 6487 | } |
| 6488 | return SQLITE_OK; |
| 6489 | } |
| 6490 | |
| 6491 | |
| 6492 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6493 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6494 | /* |
| 6495 | ** The proxy locking style is intended for use with AFP filesystems. |
| 6496 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 6497 | ** restricted to MacOSX. |
| 6498 | ** |
| 6499 | ** |
| 6500 | ******************* End of the proxy lock implementation ********************** |
| 6501 | ******************************************************************************/ |
| 6502 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6503 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6504 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6505 | ** |
| 6506 | ** This routine registers all VFS implementations for unix-like operating |
| 6507 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 6508 | ** should be the only routines in this file that are visible from other |
| 6509 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6510 | ** |
| 6511 | ** This routine is called once during SQLite initialization and by a |
| 6512 | ** single thread. The memory allocation and mutex subsystems have not |
| 6513 | ** necessarily been initialized when this routine is called, and so they |
| 6514 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6515 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6516 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6517 | /* |
| 6518 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6519 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 6520 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 6521 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 6522 | ** and so we have to go through the intermediate pointer to avoid problems |
| 6523 | ** when compiling with -pedantic-errors on GCC.) |
| 6524 | ** |
| 6525 | ** 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] | 6526 | ** finder-function. The finder-function returns a pointer to the |
| 6527 | ** sqlite_io_methods object that implements the desired locking |
| 6528 | ** behaviors. See the division above that contains the IOMETHODS |
| 6529 | ** macro for addition information on finder-functions. |
| 6530 | ** |
| 6531 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 6532 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 6533 | ** more than that; it looks at the filesystem type that hosts the |
| 6534 | ** database file and tries to choose an locking method appropriate for |
| 6535 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6536 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6537 | #define UNIXVFS(VFSNAME, FINDER) { \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6538 | 2, /* iVersion */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6539 | sizeof(unixFile), /* szOsFile */ \ |
| 6540 | MAX_PATHNAME, /* mxPathname */ \ |
| 6541 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6542 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6543 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6544 | unixOpen, /* xOpen */ \ |
| 6545 | unixDelete, /* xDelete */ \ |
| 6546 | unixAccess, /* xAccess */ \ |
| 6547 | unixFullPathname, /* xFullPathname */ \ |
| 6548 | unixDlOpen, /* xDlOpen */ \ |
| 6549 | unixDlError, /* xDlError */ \ |
| 6550 | unixDlSym, /* xDlSym */ \ |
| 6551 | unixDlClose, /* xDlClose */ \ |
| 6552 | unixRandomness, /* xRandomness */ \ |
| 6553 | unixSleep, /* xSleep */ \ |
| 6554 | unixCurrentTime, /* xCurrentTime */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6555 | unixGetLastError, /* xGetLastError */ \ |
| 6556 | unixShmOpen, /* xShmOpen */ \ |
| 6557 | unixShmSize, /* xShmSize */ \ |
drh | 5530b76 | 2010-04-30 14:39:50 +0000 | [diff] [blame] | 6558 | unixShmGet, /* xShmGet */ \ |
drh | af75c86 | 2010-04-27 11:49:27 +0000 | [diff] [blame] | 6559 | unixShmRelease, /* xShmRelease */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6560 | 0, /* xShmPush */ \ |
| 6561 | 0, /* xShmPull */ \ |
| 6562 | unixShmLock, /* xShmLock */ \ |
| 6563 | unixShmClose, /* xShmClose */ \ |
| 6564 | unixShmDelete, /* xShmDelete */ \ |
| 6565 | 0, /* xRename */ \ |
| 6566 | 0, /* xCurrentTimeInt64 */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6567 | } |
| 6568 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6569 | /* |
| 6570 | ** All default VFSes for unix are contained in the following array. |
| 6571 | ** |
| 6572 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 6573 | ** by the SQLite core when the VFS is registered. So the following |
| 6574 | ** array cannot be const. |
| 6575 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6576 | static sqlite3_vfs aVfs[] = { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6577 | #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__)) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6578 | UNIXVFS("unix", autolockIoFinder ), |
| 6579 | #else |
| 6580 | UNIXVFS("unix", posixIoFinder ), |
| 6581 | #endif |
| 6582 | UNIXVFS("unix-none", nolockIoFinder ), |
| 6583 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6584 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6585 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6586 | #endif |
| 6587 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6588 | UNIXVFS("unix-posix", posixIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6589 | #if !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6590 | UNIXVFS("unix-flock", flockIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6591 | #endif |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6592 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6593 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6594 | UNIXVFS("unix-afp", afpIoFinder ), |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6595 | UNIXVFS("unix-nfs", nfsIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6596 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6597 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6598 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6599 | unsigned int i; /* Loop counter */ |
| 6600 | |
| 6601 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6602 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6603 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6604 | } |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6605 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6606 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6607 | |
| 6608 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6609 | ** Shutdown the operating system interface. |
| 6610 | ** |
| 6611 | ** Some operating systems might need to do some cleanup in this routine, |
| 6612 | ** to release dynamically allocated objects. But not on unix. |
| 6613 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6614 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6615 | int sqlite3_os_end(void){ |
| 6616 | return SQLITE_OK; |
| 6617 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 6618 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 6619 | #endif /* SQLITE_OS_UNIX */ |