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 | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 178 | /* Forward references */ |
| 179 | typedef struct unixShm unixShm; /* Connection shared memory */ |
| 180 | typedef struct unixShmNode unixShmNode; /* Shared memory instance */ |
| 181 | typedef struct unixInodeInfo unixInodeInfo; /* An i-node */ |
| 182 | typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 183 | |
| 184 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 185 | ** Sometimes, after a file handle is closed by SQLite, the file descriptor |
| 186 | ** cannot be closed immediately. In these cases, instances of the following |
| 187 | ** structure are used to store the file descriptor while waiting for an |
| 188 | ** opportunity to either close or reuse it. |
| 189 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 190 | struct UnixUnusedFd { |
| 191 | int fd; /* File descriptor to close */ |
| 192 | int flags; /* Flags this file descriptor was opened with */ |
| 193 | UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ |
| 194 | }; |
| 195 | |
| 196 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 197 | ** The unixFile structure is subclass of sqlite3_file specific to the unix |
| 198 | ** VFS implementations. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 199 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 200 | typedef struct unixFile unixFile; |
| 201 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 202 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 203 | unixInodeInfo *pInode; /* Info about locks on this inode */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 204 | int h; /* The file descriptor */ |
| 205 | int dirfd; /* File descriptor for the directory */ |
| 206 | unsigned char eFileLock; /* The type of lock held on this fd */ |
| 207 | int lastErrno; /* The unix errno from last I/O error */ |
| 208 | void *lockingContext; /* Locking style specific state */ |
| 209 | UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ |
| 210 | int fileFlags; /* Miscellanous flags */ |
| 211 | const char *zPath; /* Name of the file */ |
| 212 | unixShm *pShm; /* Shared memory segment information */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 213 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 214 | int openFlags; /* The flags specified at open() */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 215 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 216 | #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 217 | unsigned fsFlags; /* cached details from statfs() */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 218 | #endif |
| 219 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 220 | int isDelete; /* Delete on close if true */ |
| 221 | struct vxworksFileId *pId; /* Unique file ID */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 222 | #endif |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 223 | #ifndef NDEBUG |
| 224 | /* The next group of variables are used to track whether or not the |
| 225 | ** transaction counter in bytes 24-27 of database files are updated |
| 226 | ** whenever any part of the database changes. An assertion fault will |
| 227 | ** occur if a file is updated without also updating the transaction |
| 228 | ** counter. This test is made to avoid new problems similar to the |
| 229 | ** one described by ticket #3584. |
| 230 | */ |
| 231 | unsigned char transCntrChng; /* True if the transaction counter changed */ |
| 232 | unsigned char dbUpdate; /* True if any part of database file changed */ |
| 233 | unsigned char inNormalWrite; /* True if in a normal write operation */ |
| 234 | #endif |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 235 | #ifdef SQLITE_TEST |
| 236 | /* In test mode, increase the size of this structure a bit so that |
| 237 | ** it is larger than the struct CrashFile defined in test6.c. |
| 238 | */ |
| 239 | char aPadding[32]; |
| 240 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 241 | }; |
| 242 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 243 | /* |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 244 | ** The following macros define bits in unixFile.fileFlags |
| 245 | */ |
| 246 | #define SQLITE_WHOLE_FILE_LOCKING 0x0001 /* Use whole-file locking */ |
| 247 | |
| 248 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 249 | ** Include code that is common to all os_*.c files |
| 250 | */ |
| 251 | #include "os_common.h" |
| 252 | |
| 253 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 254 | ** Define various macros that are missing from some systems. |
| 255 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 256 | #ifndef O_LARGEFILE |
| 257 | # define O_LARGEFILE 0 |
| 258 | #endif |
| 259 | #ifdef SQLITE_DISABLE_LFS |
| 260 | # undef O_LARGEFILE |
| 261 | # define O_LARGEFILE 0 |
| 262 | #endif |
| 263 | #ifndef O_NOFOLLOW |
| 264 | # define O_NOFOLLOW 0 |
| 265 | #endif |
| 266 | #ifndef O_BINARY |
| 267 | # define O_BINARY 0 |
| 268 | #endif |
| 269 | |
| 270 | /* |
| 271 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 272 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 273 | ** that always succeeds. This means that locking does not occur under |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 274 | ** DJGPP. But it is DOS - what did you expect? |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 275 | */ |
| 276 | #ifdef __DJGPP__ |
| 277 | # define fcntl(A,B,C) 0 |
| 278 | #endif |
| 279 | |
| 280 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 281 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 282 | ** testing and debugging only. |
| 283 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 284 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 285 | #define threadid pthread_self() |
| 286 | #else |
| 287 | #define threadid 0 |
| 288 | #endif |
| 289 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 290 | |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 291 | /* |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 292 | ** Helper functions to obtain and relinquish the global mutex. The |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 293 | ** global mutex is used to protect the unixInodeInfo and |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 294 | ** vxworksFileId objects used by this file, all of which may be |
| 295 | ** shared by multiple threads. |
| 296 | ** |
| 297 | ** Function unixMutexHeld() is used to assert() that the global mutex |
| 298 | ** is held when required. This function is only used as part of assert() |
| 299 | ** statements. e.g. |
| 300 | ** |
| 301 | ** unixEnterMutex() |
| 302 | ** assert( unixMutexHeld() ); |
| 303 | ** unixEnterLeave() |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 304 | */ |
| 305 | static void unixEnterMutex(void){ |
| 306 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 307 | } |
| 308 | static void unixLeaveMutex(void){ |
| 309 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 310 | } |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 311 | #ifdef SQLITE_DEBUG |
| 312 | static int unixMutexHeld(void) { |
| 313 | return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 314 | } |
| 315 | #endif |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 316 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 317 | |
| 318 | #ifdef SQLITE_DEBUG |
| 319 | /* |
| 320 | ** Helper function for printing out trace information from debugging |
| 321 | ** binaries. This returns the string represetation of the supplied |
| 322 | ** integer lock-type. |
| 323 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 324 | static const char *azFileLock(int eFileLock){ |
| 325 | switch( eFileLock ){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 326 | case NO_LOCK: return "NONE"; |
| 327 | case SHARED_LOCK: return "SHARED"; |
| 328 | case RESERVED_LOCK: return "RESERVED"; |
| 329 | case PENDING_LOCK: return "PENDING"; |
| 330 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 331 | } |
| 332 | return "ERROR"; |
| 333 | } |
| 334 | #endif |
| 335 | |
| 336 | #ifdef SQLITE_LOCK_TRACE |
| 337 | /* |
| 338 | ** Print out information about all locking operations. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 339 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 340 | ** This routine is used for troubleshooting locks on multithreaded |
| 341 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 342 | ** command-line option on the compiler. This code is normally |
| 343 | ** turned off. |
| 344 | */ |
| 345 | static int lockTrace(int fd, int op, struct flock *p){ |
| 346 | char *zOpName, *zType; |
| 347 | int s; |
| 348 | int savedErrno; |
| 349 | if( op==F_GETLK ){ |
| 350 | zOpName = "GETLK"; |
| 351 | }else if( op==F_SETLK ){ |
| 352 | zOpName = "SETLK"; |
| 353 | }else{ |
| 354 | s = fcntl(fd, op, p); |
| 355 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 356 | return s; |
| 357 | } |
| 358 | if( p->l_type==F_RDLCK ){ |
| 359 | zType = "RDLCK"; |
| 360 | }else if( p->l_type==F_WRLCK ){ |
| 361 | zType = "WRLCK"; |
| 362 | }else if( p->l_type==F_UNLCK ){ |
| 363 | zType = "UNLCK"; |
| 364 | }else{ |
| 365 | assert( 0 ); |
| 366 | } |
| 367 | assert( p->l_whence==SEEK_SET ); |
| 368 | s = fcntl(fd, op, p); |
| 369 | savedErrno = errno; |
| 370 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 371 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 372 | (int)p->l_pid, s); |
| 373 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 374 | struct flock l2; |
| 375 | l2 = *p; |
| 376 | fcntl(fd, F_GETLK, &l2); |
| 377 | if( l2.l_type==F_RDLCK ){ |
| 378 | zType = "RDLCK"; |
| 379 | }else if( l2.l_type==F_WRLCK ){ |
| 380 | zType = "WRLCK"; |
| 381 | }else if( l2.l_type==F_UNLCK ){ |
| 382 | zType = "UNLCK"; |
| 383 | }else{ |
| 384 | assert( 0 ); |
| 385 | } |
| 386 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 387 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 388 | } |
| 389 | errno = savedErrno; |
| 390 | return s; |
| 391 | } |
| 392 | #define fcntl lockTrace |
| 393 | #endif /* SQLITE_LOCK_TRACE */ |
| 394 | |
| 395 | |
| 396 | |
| 397 | /* |
| 398 | ** This routine translates a standard POSIX errno code into something |
| 399 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 400 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 401 | ** and a variety of "please close the file descriptor NOW" errors into |
| 402 | ** SQLITE_IOERR |
| 403 | ** |
| 404 | ** Errors during initialization of locks, or file system support for locks, |
| 405 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 406 | */ |
| 407 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
| 408 | switch (posixError) { |
| 409 | case 0: |
| 410 | return SQLITE_OK; |
| 411 | |
| 412 | case EAGAIN: |
| 413 | case ETIMEDOUT: |
| 414 | case EBUSY: |
| 415 | case EINTR: |
| 416 | case ENOLCK: |
| 417 | /* random NFS retry error, unless during file system support |
| 418 | * introspection, in which it actually means what it says */ |
| 419 | return SQLITE_BUSY; |
| 420 | |
| 421 | case EACCES: |
| 422 | /* EACCES is like EAGAIN during locking operations, but not any other time*/ |
| 423 | if( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
| 424 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 425 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 426 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){ |
| 427 | return SQLITE_BUSY; |
| 428 | } |
| 429 | /* else fall through */ |
| 430 | case EPERM: |
| 431 | return SQLITE_PERM; |
| 432 | |
| 433 | case EDEADLK: |
| 434 | return SQLITE_IOERR_BLOCKED; |
| 435 | |
| 436 | #if EOPNOTSUPP!=ENOTSUP |
| 437 | case EOPNOTSUPP: |
| 438 | /* something went terribly awry, unless during file system support |
| 439 | * introspection, in which it actually means what it says */ |
| 440 | #endif |
| 441 | #ifdef ENOTSUP |
| 442 | case ENOTSUP: |
| 443 | /* invalid fd, unless during file system support introspection, in which |
| 444 | * it actually means what it says */ |
| 445 | #endif |
| 446 | case EIO: |
| 447 | case EBADF: |
| 448 | case EINVAL: |
| 449 | case ENOTCONN: |
| 450 | case ENODEV: |
| 451 | case ENXIO: |
| 452 | case ENOENT: |
| 453 | case ESTALE: |
| 454 | case ENOSYS: |
| 455 | /* these should force the client to close the file and reconnect */ |
| 456 | |
| 457 | default: |
| 458 | return sqliteIOErr; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | |
| 463 | |
| 464 | /****************************************************************************** |
| 465 | ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 466 | ** |
| 467 | ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 468 | ** the device number and the inode number. But this does not work on VxWorks. |
| 469 | ** On VxWorks, a unique file id must be based on the canonical filename. |
| 470 | ** |
| 471 | ** A pointer to an instance of the following structure can be used as a |
| 472 | ** unique file ID in VxWorks. Each instance of this structure contains |
| 473 | ** a copy of the canonical filename. There is also a reference count. |
| 474 | ** The structure is reclaimed when the number of pointers to it drops to |
| 475 | ** zero. |
| 476 | ** |
| 477 | ** There are never very many files open at one time and lookups are not |
| 478 | ** a performance-critical path, so it is sufficient to put these |
| 479 | ** structures on a linked list. |
| 480 | */ |
| 481 | struct vxworksFileId { |
| 482 | struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 483 | int nRef; /* Number of references to this one */ |
| 484 | int nName; /* Length of the zCanonicalName[] string */ |
| 485 | char *zCanonicalName; /* Canonical filename */ |
| 486 | }; |
| 487 | |
| 488 | #if OS_VXWORKS |
| 489 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 490 | ** All unique filenames are held on a linked list headed by this |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 491 | ** variable: |
| 492 | */ |
| 493 | static struct vxworksFileId *vxworksFileList = 0; |
| 494 | |
| 495 | /* |
| 496 | ** Simplify a filename into its canonical form |
| 497 | ** by making the following changes: |
| 498 | ** |
| 499 | ** * removing any trailing and duplicate / |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 500 | ** * convert /./ into just / |
| 501 | ** * convert /A/../ where A is any simple name into just / |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 502 | ** |
| 503 | ** Changes are made in-place. Return the new name length. |
| 504 | ** |
| 505 | ** The original filename is in z[0..n-1]. Return the number of |
| 506 | ** characters in the simplified name. |
| 507 | */ |
| 508 | static int vxworksSimplifyName(char *z, int n){ |
| 509 | int i, j; |
| 510 | while( n>1 && z[n-1]=='/' ){ n--; } |
| 511 | for(i=j=0; i<n; i++){ |
| 512 | if( z[i]=='/' ){ |
| 513 | if( z[i+1]=='/' ) continue; |
| 514 | if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 515 | i += 1; |
| 516 | continue; |
| 517 | } |
| 518 | if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 519 | while( j>0 && z[j-1]!='/' ){ j--; } |
| 520 | if( j>0 ){ j--; } |
| 521 | i += 2; |
| 522 | continue; |
| 523 | } |
| 524 | } |
| 525 | z[j++] = z[i]; |
| 526 | } |
| 527 | z[j] = 0; |
| 528 | return j; |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | ** Find a unique file ID for the given absolute pathname. Return |
| 533 | ** a pointer to the vxworksFileId object. This pointer is the unique |
| 534 | ** file ID. |
| 535 | ** |
| 536 | ** The nRef field of the vxworksFileId object is incremented before |
| 537 | ** the object is returned. A new vxworksFileId object is created |
| 538 | ** and added to the global list if necessary. |
| 539 | ** |
| 540 | ** If a memory allocation error occurs, return NULL. |
| 541 | */ |
| 542 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 543 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 544 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 545 | int n; /* Length of zAbsoluteName string */ |
| 546 | |
| 547 | assert( zAbsoluteName[0]=='/' ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 548 | n = (int)strlen(zAbsoluteName); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 549 | pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) ); |
| 550 | if( pNew==0 ) return 0; |
| 551 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 552 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 553 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 554 | |
| 555 | /* Search for an existing entry that matching the canonical name. |
| 556 | ** If found, increment the reference count and return a pointer to |
| 557 | ** the existing file ID. |
| 558 | */ |
| 559 | unixEnterMutex(); |
| 560 | for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 561 | if( pCandidate->nName==n |
| 562 | && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 563 | ){ |
| 564 | sqlite3_free(pNew); |
| 565 | pCandidate->nRef++; |
| 566 | unixLeaveMutex(); |
| 567 | return pCandidate; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | /* No match was found. We will make a new file ID */ |
| 572 | pNew->nRef = 1; |
| 573 | pNew->nName = n; |
| 574 | pNew->pNext = vxworksFileList; |
| 575 | vxworksFileList = pNew; |
| 576 | unixLeaveMutex(); |
| 577 | return pNew; |
| 578 | } |
| 579 | |
| 580 | /* |
| 581 | ** Decrement the reference count on a vxworksFileId object. Free |
| 582 | ** the object when the reference count reaches zero. |
| 583 | */ |
| 584 | static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 585 | unixEnterMutex(); |
| 586 | assert( pId->nRef>0 ); |
| 587 | pId->nRef--; |
| 588 | if( pId->nRef==0 ){ |
| 589 | struct vxworksFileId **pp; |
| 590 | for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 591 | assert( *pp==pId ); |
| 592 | *pp = pId->pNext; |
| 593 | sqlite3_free(pId); |
| 594 | } |
| 595 | unixLeaveMutex(); |
| 596 | } |
| 597 | #endif /* OS_VXWORKS */ |
| 598 | /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 599 | ******************************************************************************/ |
| 600 | |
| 601 | |
| 602 | /****************************************************************************** |
| 603 | *************************** Posix Advisory Locking **************************** |
| 604 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 605 | ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 606 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 607 | ** sets or clears a lock, that operation overrides any prior locks set |
| 608 | ** by the same process. It does not explicitly say so, but this implies |
| 609 | ** that it overrides locks set by the same process using a different |
| 610 | ** file descriptor. Consider this test case: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 611 | ** |
| 612 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 613 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 614 | ** |
| 615 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 616 | ** one is a hard or symbolic link to the other) then if you set |
| 617 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 618 | ** on fd2, it works. I would have expected the second lock to |
| 619 | ** fail since there was already a lock on the file due to fd1. |
| 620 | ** But not so. Since both locks came from the same process, the |
| 621 | ** second overrides the first, even though they were on different |
| 622 | ** file descriptors opened on different file names. |
| 623 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 624 | ** This means that we cannot use POSIX locks to synchronize file access |
| 625 | ** among competing threads of the same process. POSIX locks will work fine |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 626 | ** to synchronize access for threads in separate processes, but not |
| 627 | ** threads within the same process. |
| 628 | ** |
| 629 | ** To work around the problem, SQLite has to manage file locks internally |
| 630 | ** on its own. Whenever a new database is opened, we have to find the |
| 631 | ** specific inode of the database file (the inode is determined by the |
| 632 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 633 | ** and check for locks already existing on that inode. When locks are |
| 634 | ** created or removed, we have to look at our own internal record of the |
| 635 | ** locks to see if another thread has previously set a lock on that same |
| 636 | ** inode. |
| 637 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 638 | ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 639 | ** For VxWorks, we have to use the alternative unique ID system based on |
| 640 | ** canonical filename and implemented in the previous division.) |
| 641 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 642 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 643 | ** descriptor. It is now a structure that holds the integer file |
| 644 | ** descriptor and a pointer to a structure that describes the internal |
| 645 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 646 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 647 | ** point to the same locking structure. The locking structure keeps |
| 648 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 649 | ** field that tells us its internal lock status. cnt==0 means the |
| 650 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 651 | ** cnt>0 means there are cnt shared locks on the file. |
| 652 | ** |
| 653 | ** Any attempt to lock or unlock a file first checks the locking |
| 654 | ** structure. The fcntl() system call is only invoked to set a |
| 655 | ** POSIX lock if the internal lock structure transitions between |
| 656 | ** a locked and an unlocked state. |
| 657 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 658 | ** But wait: there are yet more problems with POSIX advisory locks. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 659 | ** |
| 660 | ** If you close a file descriptor that points to a file that has locks, |
| 661 | ** all locks on that file that are owned by the current process are |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 662 | ** released. To work around this problem, each unixInodeInfo object |
| 663 | ** maintains a count of the number of pending locks on tha inode. |
| 664 | ** When an attempt is made to close an unixFile, if there are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 665 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 666 | ** to close() the file descriptor is deferred until all of the locks clear. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 667 | ** The unixInodeInfo structure keeps a list of file descriptors that need to |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 668 | ** be closed and that list is walked (and cleared) when the last lock |
| 669 | ** clears. |
| 670 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 671 | ** Yet another problem: LinuxThreads do not play well with posix locks. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 672 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 673 | ** Many older versions of linux use the LinuxThreads library which is |
| 674 | ** not posix compliant. Under LinuxThreads, a lock created by thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 675 | ** A cannot be modified or overridden by a different thread B. |
| 676 | ** Only thread A can modify the lock. Locking behavior is correct |
| 677 | ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 678 | ** on linux - with NPTL a lock created by thread A can override locks |
| 679 | ** in thread B. But there is no way to know at compile-time which |
| 680 | ** threading library is being used. So there is no way to know at |
| 681 | ** compile-time whether or not thread A can override locks on thread B. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 682 | ** One has to do a run-time check to discover the behavior of the |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 683 | ** current process. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 684 | ** |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 685 | ** SQLite used to support LinuxThreads. But support for LinuxThreads |
| 686 | ** was dropped beginning with version 3.7.0. SQLite will still work with |
| 687 | ** LinuxThreads provided that (1) there is no more than one connection |
| 688 | ** per database file in the same process and (2) database connections |
| 689 | ** do not move across threads. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 690 | */ |
| 691 | |
| 692 | /* |
| 693 | ** An instance of the following structure serves as the key used |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 694 | ** to locate a particular unixInodeInfo object. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 695 | */ |
| 696 | struct unixFileId { |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 697 | dev_t dev; /* Device number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 698 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 699 | struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 700 | #else |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 701 | ino_t ino; /* Inode number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 702 | #endif |
| 703 | }; |
| 704 | |
| 705 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 706 | ** An instance of the following structure is allocated for each open |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 707 | ** inode. Or, on LinuxThreads, there is one of these structures for |
| 708 | ** each inode opened by each thread. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 709 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 710 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 711 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 712 | ** object keeps a count of the number of unixFile pointing to it. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 713 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 714 | struct unixInodeInfo { |
| 715 | struct unixFileId fileId; /* The lookup key */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 716 | int nShared; /* Number of SHARED locks held */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 717 | int eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 718 | int nRef; /* Number of pointers to this structure */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 719 | unixShmNode *pShmNode; /* Shared memory associated with this inode */ |
| 720 | int nLock; /* Number of outstanding file locks */ |
| 721 | UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
| 722 | unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ |
| 723 | unixInodeInfo *pPrev; /* .... doubly linked */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 724 | #if defined(SQLITE_ENABLE_LOCKING_STYLE) |
| 725 | unsigned long long sharedByte; /* for AFP simulated shared lock */ |
| 726 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 727 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 728 | sem_t *pSem; /* Named POSIX semaphore */ |
| 729 | char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 730 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 731 | }; |
| 732 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 733 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 734 | ** A lists of all unixInodeInfo objects. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 735 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 736 | static unixInodeInfo *inodeList = 0; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 737 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 738 | /* |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 739 | ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. |
| 740 | ** If all such file descriptors are closed without error, the list is |
| 741 | ** cleared and SQLITE_OK returned. |
| 742 | ** |
| 743 | ** Otherwise, if an error occurs, then successfully closed file descriptor |
| 744 | ** entries are removed from the list, and SQLITE_IOERR_CLOSE returned. |
| 745 | ** not deleted and SQLITE_IOERR_CLOSE returned. |
| 746 | */ |
| 747 | static int closePendingFds(unixFile *pFile){ |
| 748 | int rc = SQLITE_OK; |
| 749 | unixInodeInfo *pInode = pFile->pInode; |
| 750 | UnixUnusedFd *pError = 0; |
| 751 | UnixUnusedFd *p; |
| 752 | UnixUnusedFd *pNext; |
| 753 | for(p=pInode->pUnused; p; p=pNext){ |
| 754 | pNext = p->pNext; |
| 755 | if( close(p->fd) ){ |
| 756 | pFile->lastErrno = errno; |
| 757 | rc = SQLITE_IOERR_CLOSE; |
| 758 | p->pNext = pError; |
| 759 | pError = p; |
| 760 | }else{ |
| 761 | sqlite3_free(p); |
| 762 | } |
| 763 | } |
| 764 | pInode->pUnused = pError; |
| 765 | return rc; |
| 766 | } |
| 767 | |
| 768 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 769 | ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 770 | ** |
| 771 | ** The mutex entered using the unixEnterMutex() function must be held |
| 772 | ** when this function is called. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 773 | */ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 774 | static void releaseInodeInfo(unixFile *pFile){ |
| 775 | unixInodeInfo *pInode = pFile->pInode; |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 776 | assert( unixMutexHeld() ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 777 | if( pInode ){ |
| 778 | pInode->nRef--; |
| 779 | if( pInode->nRef==0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 780 | assert( pInode->pShmNode==0 ); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 781 | closePendingFds(pFile); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 782 | if( pInode->pPrev ){ |
| 783 | assert( pInode->pPrev->pNext==pInode ); |
| 784 | pInode->pPrev->pNext = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 785 | }else{ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 786 | assert( inodeList==pInode ); |
| 787 | inodeList = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 788 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 789 | if( pInode->pNext ){ |
| 790 | assert( pInode->pNext->pPrev==pInode ); |
| 791 | pInode->pNext->pPrev = pInode->pPrev; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 792 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 793 | sqlite3_free(pInode); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 794 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
| 798 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 799 | ** Given a file descriptor, locate the unixInodeInfo object that |
| 800 | ** describes that file descriptor. Create a new one if necessary. The |
| 801 | ** return value might be uninitialized if an error occurs. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 802 | ** |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 803 | ** The mutex entered using the unixEnterMutex() function must be held |
| 804 | ** when this function is called. |
| 805 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 806 | ** Return an appropriate error code. |
| 807 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 808 | static int findInodeInfo( |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 809 | unixFile *pFile, /* Unix file with file desc used in the key */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 810 | unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 811 | ){ |
| 812 | int rc; /* System call return code */ |
| 813 | int fd; /* The file descriptor for pFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 814 | struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ |
| 815 | struct stat statbuf; /* Low-level file information */ |
| 816 | unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 817 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 818 | assert( unixMutexHeld() ); |
| 819 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 820 | /* Get low-level information about the file that we can used to |
| 821 | ** create a unique name for the file. |
| 822 | */ |
| 823 | fd = pFile->h; |
| 824 | rc = fstat(fd, &statbuf); |
| 825 | if( rc!=0 ){ |
| 826 | pFile->lastErrno = errno; |
| 827 | #ifdef EOVERFLOW |
| 828 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 829 | #endif |
| 830 | return SQLITE_IOERR; |
| 831 | } |
| 832 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 833 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 834 | /* On OS X on an msdos filesystem, the inode number is reported |
| 835 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 836 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 837 | ** we always increase the file size to 1 by writing a single byte |
| 838 | ** prior to accessing the inode number. The one byte written is |
| 839 | ** an ASCII 'S' character which also happens to be the first byte |
| 840 | ** in the header of every SQLite database. In this way, if there |
| 841 | ** is a race condition such that another thread has already populated |
| 842 | ** the first page of the database, no damage is done. |
| 843 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 844 | if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 845 | rc = write(fd, "S", 1); |
| 846 | if( rc!=1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 847 | pFile->lastErrno = errno; |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 848 | return SQLITE_IOERR; |
| 849 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 850 | rc = fstat(fd, &statbuf); |
| 851 | if( rc!=0 ){ |
| 852 | pFile->lastErrno = errno; |
| 853 | return SQLITE_IOERR; |
| 854 | } |
| 855 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 856 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 857 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 858 | memset(&fileId, 0, sizeof(fileId)); |
| 859 | fileId.dev = statbuf.st_dev; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 860 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 861 | fileId.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 862 | #else |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 863 | fileId.ino = statbuf.st_ino; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 864 | #endif |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 865 | pInode = inodeList; |
| 866 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 867 | pInode = pInode->pNext; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 868 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 869 | if( pInode==0 ){ |
| 870 | pInode = sqlite3_malloc( sizeof(*pInode) ); |
| 871 | if( pInode==0 ){ |
| 872 | return SQLITE_NOMEM; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 873 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 874 | memset(pInode, 0, sizeof(*pInode)); |
| 875 | memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
| 876 | pInode->nRef = 1; |
| 877 | pInode->pNext = inodeList; |
| 878 | pInode->pPrev = 0; |
| 879 | if( inodeList ) inodeList->pPrev = pInode; |
| 880 | inodeList = pInode; |
| 881 | }else{ |
| 882 | pInode->nRef++; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 883 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 884 | *ppInode = pInode; |
| 885 | return SQLITE_OK; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 886 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 887 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 888 | |
| 889 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 890 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 891 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 892 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 893 | ** 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] | 894 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 895 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 896 | int rc = SQLITE_OK; |
| 897 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 898 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 899 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 900 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 901 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 902 | assert( pFile ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 903 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 904 | |
| 905 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 906 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 907 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 908 | } |
| 909 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 910 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 911 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 912 | #ifndef __DJGPP__ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 913 | if( !reserved ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 914 | struct flock lock; |
| 915 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 916 | lock.l_start = RESERVED_BYTE; |
| 917 | lock.l_len = 1; |
| 918 | lock.l_type = F_WRLCK; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 919 | if (-1 == fcntl(pFile->h, F_GETLK, &lock)) { |
| 920 | int tErrno = errno; |
| 921 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 922 | pFile->lastErrno = tErrno; |
| 923 | } else if( lock.l_type!=F_UNLCK ){ |
| 924 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 927 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 928 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 929 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 930 | OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 931 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 932 | *pResOut = reserved; |
| 933 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 937 | ** Lock the file with the lock specified by parameter eFileLock - one |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 938 | ** of the following: |
| 939 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 940 | ** (1) SHARED_LOCK |
| 941 | ** (2) RESERVED_LOCK |
| 942 | ** (3) PENDING_LOCK |
| 943 | ** (4) EXCLUSIVE_LOCK |
| 944 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 945 | ** Sometimes when requesting one lock state, additional lock states |
| 946 | ** are inserted in between. The locking might fail on one of the later |
| 947 | ** transitions leaving the lock state different from what it started but |
| 948 | ** still short of its goal. The following chart shows the allowed |
| 949 | ** transitions and the inserted intermediate states: |
| 950 | ** |
| 951 | ** UNLOCKED -> SHARED |
| 952 | ** SHARED -> RESERVED |
| 953 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 954 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 955 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 956 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 957 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 958 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 959 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 960 | static int unixLock(sqlite3_file *id, int eFileLock){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 961 | /* The following describes the implementation of the various locks and |
| 962 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 963 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 964 | ** confusion with SQLite lock names). The algorithms are complicated |
| 965 | ** slightly in order to be compatible with windows systems simultaneously |
| 966 | ** accessing the same database file, in case that is ever required. |
| 967 | ** |
| 968 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 969 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 970 | ** range', a range of 510 bytes at a well known offset. |
| 971 | ** |
| 972 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 973 | ** byte'. If this is successful, a random byte from the 'shared byte |
| 974 | ** range' is read-locked and the lock on the 'pending byte' released. |
| 975 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 976 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 977 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 978 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 979 | ** |
| 980 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 981 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 982 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 983 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 984 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 985 | ** This property is used by the algorithm for rolling back a journal file |
| 986 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 987 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 988 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 989 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 990 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 991 | ** within this range, this ensures that no other locks are held on the |
| 992 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 993 | ** |
| 994 | ** The reason a single byte cannot be used instead of the 'shared byte |
| 995 | ** range' is that some versions of windows do not support read-locks. By |
| 996 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 997 | ** even if the locking primitive used is always a write-lock. |
| 998 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 999 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1000 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1001 | unixInodeInfo *pInode = pFile->pInode; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1002 | struct flock lock; |
drh | 3f02218 | 2009-09-09 16:10:50 +0000 | [diff] [blame] | 1003 | int s = 0; |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 1004 | int tErrno = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1005 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1006 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1007 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 1008 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1009 | azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1010 | |
| 1011 | /* 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] | 1012 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1013 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1014 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1015 | if( pFile->eFileLock>=eFileLock ){ |
| 1016 | OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
| 1017 | azFileLock(eFileLock))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1018 | return SQLITE_OK; |
| 1019 | } |
| 1020 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1021 | /* Make sure the locking sequence is correct. |
| 1022 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 1023 | ** (2) SQLite never explicitly requests a pendig lock. |
| 1024 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1025 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1026 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 1027 | assert( eFileLock!=PENDING_LOCK ); |
| 1028 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1029 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1030 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1031 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1032 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1033 | pInode = pFile->pInode; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1034 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1035 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1036 | ** handle that precludes the requested lock, return BUSY. |
| 1037 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1038 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 1039 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1040 | ){ |
| 1041 | rc = SQLITE_BUSY; |
| 1042 | goto end_lock; |
| 1043 | } |
| 1044 | |
| 1045 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1046 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1047 | ** return SQLITE_OK. |
| 1048 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1049 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1050 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1051 | assert( eFileLock==SHARED_LOCK ); |
| 1052 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1053 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1054 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1055 | pInode->nShared++; |
| 1056 | pInode->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1057 | goto end_lock; |
| 1058 | } |
| 1059 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1060 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1061 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1062 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1063 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1064 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1065 | lock.l_len = 1L; |
| 1066 | lock.l_whence = SEEK_SET; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1067 | if( eFileLock==SHARED_LOCK |
| 1068 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1069 | ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1070 | lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1071 | lock.l_start = PENDING_BYTE; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1072 | s = fcntl(pFile->h, F_SETLK, &lock); |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1073 | if( s==(-1) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1074 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1075 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1076 | if( IS_LOCK_ERROR(rc) ){ |
| 1077 | pFile->lastErrno = tErrno; |
| 1078 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1079 | goto end_lock; |
| 1080 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | /* If control gets to this point, then actually go ahead and make |
| 1085 | ** operating system calls for the specified lock. |
| 1086 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1087 | if( eFileLock==SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1088 | assert( pInode->nShared==0 ); |
| 1089 | assert( pInode->eFileLock==0 ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1090 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1091 | /* Now get the read-lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1092 | lock.l_start = SHARED_FIRST; |
| 1093 | lock.l_len = SHARED_SIZE; |
| 1094 | if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){ |
| 1095 | tErrno = errno; |
| 1096 | } |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1097 | /* Drop the temporary PENDING lock */ |
| 1098 | lock.l_start = PENDING_BYTE; |
| 1099 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1100 | lock.l_type = F_UNLCK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1101 | if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1102 | if( s != -1 ){ |
| 1103 | /* This could happen with a network mount */ |
| 1104 | tErrno = errno; |
| 1105 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1106 | if( IS_LOCK_ERROR(rc) ){ |
| 1107 | pFile->lastErrno = tErrno; |
| 1108 | } |
| 1109 | goto end_lock; |
| 1110 | } |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1111 | } |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1112 | if( s==(-1) ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1113 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1114 | if( IS_LOCK_ERROR(rc) ){ |
| 1115 | pFile->lastErrno = tErrno; |
| 1116 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1117 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1118 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1119 | pInode->nLock++; |
| 1120 | pInode->nShared = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1121 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1122 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1123 | /* We are trying for an exclusive lock but another thread in this |
| 1124 | ** same process is still holding a shared lock. */ |
| 1125 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1126 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1127 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1128 | ** assumed that there is a SHARED or greater lock on the file |
| 1129 | ** already. |
| 1130 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1131 | assert( 0!=pFile->eFileLock ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1132 | lock.l_type = F_WRLCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1133 | switch( eFileLock ){ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1134 | case RESERVED_LOCK: |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1135 | lock.l_start = RESERVED_BYTE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1136 | break; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1137 | case EXCLUSIVE_LOCK: |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1138 | lock.l_start = SHARED_FIRST; |
| 1139 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1140 | break; |
| 1141 | default: |
| 1142 | assert(0); |
| 1143 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1144 | s = fcntl(pFile->h, F_SETLK, &lock); |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1145 | if( s==(-1) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1146 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1147 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1148 | if( IS_LOCK_ERROR(rc) ){ |
| 1149 | pFile->lastErrno = tErrno; |
| 1150 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1151 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1152 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1153 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1154 | |
| 1155 | #ifndef NDEBUG |
| 1156 | /* Set up the transaction-counter change checking flags when |
| 1157 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1158 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1159 | ** write operation (not a hot journal rollback). |
| 1160 | */ |
| 1161 | if( rc==SQLITE_OK |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1162 | && pFile->eFileLock<=SHARED_LOCK |
| 1163 | && eFileLock==RESERVED_LOCK |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1164 | ){ |
| 1165 | pFile->transCntrChng = 0; |
| 1166 | pFile->dbUpdate = 0; |
| 1167 | pFile->inNormalWrite = 1; |
| 1168 | } |
| 1169 | #endif |
| 1170 | |
| 1171 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1172 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1173 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1174 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1175 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 1176 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1177 | pInode->eFileLock = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1178 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1179 | |
| 1180 | end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1181 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1182 | OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), |
| 1183 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1184 | return rc; |
| 1185 | } |
| 1186 | |
| 1187 | /* |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1188 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1189 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1190 | */ |
| 1191 | static void setPendingFd(unixFile *pFile){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1192 | unixInodeInfo *pInode = pFile->pInode; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1193 | UnixUnusedFd *p = pFile->pUnused; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1194 | p->pNext = pInode->pUnused; |
| 1195 | pInode->pUnused = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1196 | pFile->h = -1; |
| 1197 | pFile->pUnused = 0; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1201 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1202 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1203 | ** |
| 1204 | ** If the locking level of the file descriptor is already at or below |
| 1205 | ** the requested locking level, this routine is a no-op. |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1206 | ** |
| 1207 | ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 1208 | ** the byte range is divided into 2 parts and the first part is unlocked then |
| 1209 | ** set to a read lock, then the other part is simply unlocked. This works |
| 1210 | ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 1211 | ** remove the write lock on a region when a read lock is set. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1212 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1213 | static int _posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1214 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1215 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1216 | struct flock lock; |
| 1217 | int rc = SQLITE_OK; |
| 1218 | int h; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1219 | int tErrno; /* Error code from system call errors */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1220 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1221 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1222 | OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1223 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1224 | getpid())); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1225 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1226 | assert( eFileLock<=SHARED_LOCK ); |
| 1227 | if( pFile->eFileLock<=eFileLock ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1228 | return SQLITE_OK; |
| 1229 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1230 | unixEnterMutex(); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1231 | h = pFile->h; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1232 | pInode = pFile->pInode; |
| 1233 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1234 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1235 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1236 | SimulateIOErrorBenign(1); |
| 1237 | SimulateIOError( h=(-1) ) |
| 1238 | SimulateIOErrorBenign(0); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1239 | |
| 1240 | #ifndef NDEBUG |
| 1241 | /* When reducing a lock such that other processes can start |
| 1242 | ** reading the database file again, make sure that the |
| 1243 | ** transaction counter was updated if any part of the database |
| 1244 | ** file changed. If the transaction counter is not updated, |
| 1245 | ** other connections to the same file might not realize that |
| 1246 | ** the file has changed and hence might not know to flush their |
| 1247 | ** cache. The use of a stale cache can lead to database corruption. |
| 1248 | */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1249 | #if 0 |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1250 | assert( pFile->inNormalWrite==0 |
| 1251 | || pFile->dbUpdate==0 |
| 1252 | || pFile->transCntrChng==1 ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1253 | #endif |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1254 | pFile->inNormalWrite = 0; |
| 1255 | #endif |
| 1256 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1257 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 1258 | ** before establishing the readlock - to avoid a race condition we downgrade |
| 1259 | ** the lock in 2 blocks, so that part of the range will be covered by a |
| 1260 | ** write lock until the rest is covered by a read lock: |
| 1261 | ** 1: [WWWWW] |
| 1262 | ** 2: [....W] |
| 1263 | ** 3: [RRRRW] |
| 1264 | ** 4: [RRRR.] |
| 1265 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1266 | if( eFileLock==SHARED_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1267 | if( handleNFSUnlock ){ |
| 1268 | off_t divSize = SHARED_SIZE - 1; |
| 1269 | |
| 1270 | lock.l_type = F_UNLCK; |
| 1271 | lock.l_whence = SEEK_SET; |
| 1272 | lock.l_start = SHARED_FIRST; |
| 1273 | lock.l_len = divSize; |
| 1274 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1275 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1276 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1277 | if( IS_LOCK_ERROR(rc) ){ |
| 1278 | pFile->lastErrno = tErrno; |
| 1279 | } |
| 1280 | goto end_unlock; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1281 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1282 | lock.l_type = F_RDLCK; |
| 1283 | lock.l_whence = SEEK_SET; |
| 1284 | lock.l_start = SHARED_FIRST; |
| 1285 | lock.l_len = divSize; |
| 1286 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1287 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1288 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1289 | if( IS_LOCK_ERROR(rc) ){ |
| 1290 | pFile->lastErrno = tErrno; |
| 1291 | } |
| 1292 | goto end_unlock; |
| 1293 | } |
| 1294 | lock.l_type = F_UNLCK; |
| 1295 | lock.l_whence = SEEK_SET; |
| 1296 | lock.l_start = SHARED_FIRST+divSize; |
| 1297 | lock.l_len = SHARED_SIZE-divSize; |
| 1298 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1299 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1300 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1301 | if( IS_LOCK_ERROR(rc) ){ |
| 1302 | pFile->lastErrno = tErrno; |
| 1303 | } |
| 1304 | goto end_unlock; |
| 1305 | } |
| 1306 | }else{ |
| 1307 | lock.l_type = F_RDLCK; |
| 1308 | lock.l_whence = SEEK_SET; |
| 1309 | lock.l_start = SHARED_FIRST; |
| 1310 | lock.l_len = SHARED_SIZE; |
| 1311 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1312 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1313 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1314 | if( IS_LOCK_ERROR(rc) ){ |
| 1315 | pFile->lastErrno = tErrno; |
| 1316 | } |
| 1317 | goto end_unlock; |
| 1318 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1319 | } |
| 1320 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1321 | lock.l_type = F_UNLCK; |
| 1322 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1323 | lock.l_start = PENDING_BYTE; |
| 1324 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1325 | if( fcntl(h, F_SETLK, &lock)!=(-1) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1326 | pInode->eFileLock = SHARED_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1327 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1328 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1329 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1330 | if( IS_LOCK_ERROR(rc) ){ |
| 1331 | pFile->lastErrno = tErrno; |
| 1332 | } |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 1333 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1334 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1335 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1336 | if( eFileLock==NO_LOCK ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1337 | /* Decrement the shared lock counter. Release the lock using an |
| 1338 | ** OS call only when all threads in this same process have released |
| 1339 | ** the lock. |
| 1340 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1341 | pInode->nShared--; |
| 1342 | if( pInode->nShared==0 ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1343 | lock.l_type = F_UNLCK; |
| 1344 | lock.l_whence = SEEK_SET; |
| 1345 | lock.l_start = lock.l_len = 0L; |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1346 | SimulateIOErrorBenign(1); |
| 1347 | SimulateIOError( h=(-1) ) |
| 1348 | SimulateIOErrorBenign(0); |
| 1349 | if( fcntl(h, F_SETLK, &lock)!=(-1) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1350 | pInode->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1351 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1352 | tErrno = errno; |
danielk1977 | 5ad6a88 | 2008-09-15 04:20:31 +0000 | [diff] [blame] | 1353 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1354 | if( IS_LOCK_ERROR(rc) ){ |
| 1355 | pFile->lastErrno = tErrno; |
| 1356 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1357 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1358 | pFile->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1359 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1362 | /* Decrement the count of locks against this same file. When the |
| 1363 | ** count reaches zero, close any other file descriptors whose close |
| 1364 | ** was deferred because of outstanding locks. |
| 1365 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1366 | pInode->nLock--; |
| 1367 | assert( pInode->nLock>=0 ); |
| 1368 | if( pInode->nLock==0 ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1369 | int rc2 = closePendingFds(pFile); |
| 1370 | if( rc==SQLITE_OK ){ |
| 1371 | rc = rc2; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1372 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1373 | } |
| 1374 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1375 | |
| 1376 | end_unlock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1377 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1378 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1379 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1383 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1384 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1385 | ** |
| 1386 | ** If the locking level of the file descriptor is already at or below |
| 1387 | ** the requested locking level, this routine is a no-op. |
| 1388 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1389 | static int unixUnlock(sqlite3_file *id, int eFileLock){ |
| 1390 | return _posixUnlock(id, eFileLock, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1394 | ** This function performs the parts of the "close file" operation |
| 1395 | ** common to all locking schemes. It closes the directory and file |
| 1396 | ** handles, if they are valid, and sets all fields of the unixFile |
| 1397 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1398 | ** |
| 1399 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 1400 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 1401 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1402 | */ |
| 1403 | static int closeUnixFile(sqlite3_file *id){ |
| 1404 | unixFile *pFile = (unixFile*)id; |
| 1405 | if( pFile ){ |
| 1406 | if( pFile->dirfd>=0 ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1407 | int err = close(pFile->dirfd); |
| 1408 | if( err ){ |
| 1409 | pFile->lastErrno = errno; |
| 1410 | return SQLITE_IOERR_DIR_CLOSE; |
| 1411 | }else{ |
| 1412 | pFile->dirfd=-1; |
| 1413 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1414 | } |
| 1415 | if( pFile->h>=0 ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1416 | int err = close(pFile->h); |
| 1417 | if( err ){ |
| 1418 | pFile->lastErrno = errno; |
| 1419 | return SQLITE_IOERR_CLOSE; |
| 1420 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1421 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1422 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1423 | if( pFile->pId ){ |
| 1424 | if( pFile->isDelete ){ |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1425 | unlink(pFile->pId->zCanonicalName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1426 | } |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1427 | vxworksReleaseFileId(pFile->pId); |
| 1428 | pFile->pId = 0; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1429 | } |
| 1430 | #endif |
drh | ff59a11 | 2010-05-14 20:15:51 +0000 | [diff] [blame] | 1431 | OSTRACE(("CLOSE %-3d\n", pFile->h)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1432 | OpenCounter(-1); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1433 | sqlite3_free(pFile->pUnused); |
drh | ff59a11 | 2010-05-14 20:15:51 +0000 | [diff] [blame] | 1434 | memset(pFile, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1435 | } |
| 1436 | return SQLITE_OK; |
| 1437 | } |
| 1438 | |
| 1439 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1440 | ** Close a file. |
| 1441 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1442 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1443 | int rc = SQLITE_OK; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1444 | if( id ){ |
| 1445 | unixFile *pFile = (unixFile *)id; |
| 1446 | unixUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1447 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1448 | if( pFile->pInode && pFile->pInode->nLock ){ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1449 | /* If there are outstanding locks, do not actually close the file just |
| 1450 | ** yet because that would clear those locks. Instead, add the file |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1451 | ** descriptor to pInode->pUnused list. It will be automatically closed |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1452 | ** when the last lock is cleared. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1453 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1454 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1455 | } |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1456 | releaseInodeInfo(pFile); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1457 | rc = closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1458 | unixLeaveMutex(); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1459 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1460 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1463 | /************** End of the posix advisory lock implementation ***************** |
| 1464 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1465 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1466 | /****************************************************************************** |
| 1467 | ****************************** No-op Locking ********************************** |
| 1468 | ** |
| 1469 | ** Of the various locking implementations available, this is by far the |
| 1470 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 1471 | ** file for reading or writing. |
| 1472 | ** |
| 1473 | ** This locking mode is appropriate for use on read-only databases |
| 1474 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 1475 | ** also be used if the application employs some external mechanism to |
| 1476 | ** prevent simultaneous access of the same database by two or more |
| 1477 | ** database connections. But there is a serious risk of database |
| 1478 | ** corruption if this locking mode is used in situations where multiple |
| 1479 | ** database connections are accessing the same database file at the same |
| 1480 | ** time and one or more of those connections are writing. |
| 1481 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1482 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1483 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 1484 | UNUSED_PARAMETER(NotUsed); |
| 1485 | *pResOut = 0; |
| 1486 | return SQLITE_OK; |
| 1487 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1488 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1489 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1490 | return SQLITE_OK; |
| 1491 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1492 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1493 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1494 | return SQLITE_OK; |
| 1495 | } |
| 1496 | |
| 1497 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1498 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1499 | */ |
| 1500 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1501 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | /******************* End of the no-op lock implementation ********************* |
| 1505 | ******************************************************************************/ |
| 1506 | |
| 1507 | /****************************************************************************** |
| 1508 | ************************* Begin dot-file Locking ****************************** |
| 1509 | ** |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1510 | ** The dotfile locking implementation uses the existance of separate lock |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1511 | ** files in order to control access to the database. This works on just |
| 1512 | ** about every filesystem imaginable. But there are serious downsides: |
| 1513 | ** |
| 1514 | ** (1) There is zero concurrency. A single reader blocks all other |
| 1515 | ** connections from reading or writing the database. |
| 1516 | ** |
| 1517 | ** (2) An application crash or power loss can leave stale lock files |
| 1518 | ** sitting around that need to be cleared manually. |
| 1519 | ** |
| 1520 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 1521 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1522 | ** |
| 1523 | ** Dotfile locking works by creating a file in the same directory as the |
| 1524 | ** database and with the same name but with a ".lock" extension added. |
| 1525 | ** The existance of a lock file implies an EXCLUSIVE lock. All other lock |
| 1526 | ** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1527 | */ |
| 1528 | |
| 1529 | /* |
| 1530 | ** The file suffix added to the data base filename in order to create the |
| 1531 | ** lock file. |
| 1532 | */ |
| 1533 | #define DOTLOCK_SUFFIX ".lock" |
| 1534 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1535 | /* |
| 1536 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1537 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1538 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1539 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1540 | ** |
| 1541 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 1542 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 1543 | ** is held on the file and false if the file is unlocked. |
| 1544 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1545 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 1546 | int rc = SQLITE_OK; |
| 1547 | int reserved = 0; |
| 1548 | unixFile *pFile = (unixFile*)id; |
| 1549 | |
| 1550 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1551 | |
| 1552 | assert( pFile ); |
| 1553 | |
| 1554 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1555 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1556 | /* Either this connection or some other connection in the same process |
| 1557 | ** holds a lock on the file. No need to check further. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1558 | reserved = 1; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1559 | }else{ |
| 1560 | /* The lock is held if and only if the lockfile exists */ |
| 1561 | const char *zLockFile = (const char*)pFile->lockingContext; |
| 1562 | reserved = access(zLockFile, 0)==0; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1563 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1564 | OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1565 | *pResOut = reserved; |
| 1566 | return rc; |
| 1567 | } |
| 1568 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1569 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1570 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1571 | ** of the following: |
| 1572 | ** |
| 1573 | ** (1) SHARED_LOCK |
| 1574 | ** (2) RESERVED_LOCK |
| 1575 | ** (3) PENDING_LOCK |
| 1576 | ** (4) EXCLUSIVE_LOCK |
| 1577 | ** |
| 1578 | ** Sometimes when requesting one lock state, additional lock states |
| 1579 | ** are inserted in between. The locking might fail on one of the later |
| 1580 | ** transitions leaving the lock state different from what it started but |
| 1581 | ** still short of its goal. The following chart shows the allowed |
| 1582 | ** transitions and the inserted intermediate states: |
| 1583 | ** |
| 1584 | ** UNLOCKED -> SHARED |
| 1585 | ** SHARED -> RESERVED |
| 1586 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1587 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1588 | ** PENDING -> EXCLUSIVE |
| 1589 | ** |
| 1590 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1591 | ** routine to lower a locking level. |
| 1592 | ** |
| 1593 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 1594 | ** But we track the other locking levels internally. |
| 1595 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1596 | static int dotlockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1597 | unixFile *pFile = (unixFile*)id; |
| 1598 | int fd; |
| 1599 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1600 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1601 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1602 | |
| 1603 | /* If we have any lock, then the lock file already exists. All we have |
| 1604 | ** to do is adjust our internal record of the lock level. |
| 1605 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1606 | if( pFile->eFileLock > NO_LOCK ){ |
| 1607 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1608 | #if !OS_VXWORKS |
| 1609 | /* Always update the timestamp on the old file */ |
| 1610 | utimes(zLockFile, NULL); |
| 1611 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1612 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | /* grab an exclusive lock */ |
| 1616 | fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600); |
| 1617 | if( fd<0 ){ |
| 1618 | /* failed to open/create the file, someone else may have stolen the lock */ |
| 1619 | int tErrno = errno; |
| 1620 | if( EEXIST == tErrno ){ |
| 1621 | rc = SQLITE_BUSY; |
| 1622 | } else { |
| 1623 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1624 | if( IS_LOCK_ERROR(rc) ){ |
| 1625 | pFile->lastErrno = tErrno; |
| 1626 | } |
| 1627 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1628 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1629 | } |
| 1630 | if( close(fd) ){ |
| 1631 | pFile->lastErrno = errno; |
| 1632 | rc = SQLITE_IOERR_CLOSE; |
| 1633 | } |
| 1634 | |
| 1635 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1636 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1637 | return rc; |
| 1638 | } |
| 1639 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1640 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1641 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1642 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1643 | ** |
| 1644 | ** If the locking level of the file descriptor is already at or below |
| 1645 | ** the requested locking level, this routine is a no-op. |
| 1646 | ** |
| 1647 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 1648 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1649 | static int dotlockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1650 | unixFile *pFile = (unixFile*)id; |
| 1651 | char *zLockFile = (char *)pFile->lockingContext; |
| 1652 | |
| 1653 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1654 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
| 1655 | pFile->eFileLock, getpid())); |
| 1656 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1657 | |
| 1658 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1659 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1660 | return SQLITE_OK; |
| 1661 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1662 | |
| 1663 | /* To downgrade to shared, simply update our internal notion of the |
| 1664 | ** lock state. No need to mess with the file on disk. |
| 1665 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1666 | if( eFileLock==SHARED_LOCK ){ |
| 1667 | pFile->eFileLock = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1668 | return SQLITE_OK; |
| 1669 | } |
| 1670 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1671 | /* To fully unlock the database, delete the lock file */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1672 | assert( eFileLock==NO_LOCK ); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1673 | if( unlink(zLockFile) ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 1674 | int rc = 0; |
| 1675 | int tErrno = errno; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1676 | if( ENOENT != tErrno ){ |
| 1677 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1678 | } |
| 1679 | if( IS_LOCK_ERROR(rc) ){ |
| 1680 | pFile->lastErrno = tErrno; |
| 1681 | } |
| 1682 | return rc; |
| 1683 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1684 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1685 | return SQLITE_OK; |
| 1686 | } |
| 1687 | |
| 1688 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1689 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1690 | */ |
| 1691 | static int dotlockClose(sqlite3_file *id) { |
| 1692 | int rc; |
| 1693 | if( id ){ |
| 1694 | unixFile *pFile = (unixFile*)id; |
| 1695 | dotlockUnlock(id, NO_LOCK); |
| 1696 | sqlite3_free(pFile->lockingContext); |
| 1697 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1698 | rc = closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1699 | return rc; |
| 1700 | } |
| 1701 | /****************** End of the dot-file lock implementation ******************* |
| 1702 | ******************************************************************************/ |
| 1703 | |
| 1704 | /****************************************************************************** |
| 1705 | ************************** Begin flock Locking ******************************** |
| 1706 | ** |
| 1707 | ** Use the flock() system call to do file locking. |
| 1708 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1709 | ** flock() locking is like dot-file locking in that the various |
| 1710 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 1711 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 1712 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 1713 | ** still works when you do this, but concurrency is reduced since |
| 1714 | ** only a single process can be reading the database at a time. |
| 1715 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1716 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if |
| 1717 | ** compiling for VXWORKS. |
| 1718 | */ |
| 1719 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1720 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1721 | /* |
| 1722 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1723 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1724 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1725 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1726 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1727 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 1728 | int rc = SQLITE_OK; |
| 1729 | int reserved = 0; |
| 1730 | unixFile *pFile = (unixFile*)id; |
| 1731 | |
| 1732 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1733 | |
| 1734 | assert( pFile ); |
| 1735 | |
| 1736 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1737 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1738 | reserved = 1; |
| 1739 | } |
| 1740 | |
| 1741 | /* Otherwise see if some other process holds it. */ |
| 1742 | if( !reserved ){ |
| 1743 | /* attempt to get the lock */ |
| 1744 | int lrc = flock(pFile->h, LOCK_EX | LOCK_NB); |
| 1745 | if( !lrc ){ |
| 1746 | /* got the lock, unlock it */ |
| 1747 | lrc = flock(pFile->h, LOCK_UN); |
| 1748 | if ( lrc ) { |
| 1749 | int tErrno = errno; |
| 1750 | /* unlock failed with an error */ |
| 1751 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1752 | if( IS_LOCK_ERROR(lrc) ){ |
| 1753 | pFile->lastErrno = tErrno; |
| 1754 | rc = lrc; |
| 1755 | } |
| 1756 | } |
| 1757 | } else { |
| 1758 | int tErrno = errno; |
| 1759 | reserved = 1; |
| 1760 | /* someone else might have it reserved */ |
| 1761 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1762 | if( IS_LOCK_ERROR(lrc) ){ |
| 1763 | pFile->lastErrno = tErrno; |
| 1764 | rc = lrc; |
| 1765 | } |
| 1766 | } |
| 1767 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1768 | OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1769 | |
| 1770 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 1771 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 1772 | rc = SQLITE_OK; |
| 1773 | reserved=1; |
| 1774 | } |
| 1775 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 1776 | *pResOut = reserved; |
| 1777 | return rc; |
| 1778 | } |
| 1779 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1780 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1781 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1782 | ** of the following: |
| 1783 | ** |
| 1784 | ** (1) SHARED_LOCK |
| 1785 | ** (2) RESERVED_LOCK |
| 1786 | ** (3) PENDING_LOCK |
| 1787 | ** (4) EXCLUSIVE_LOCK |
| 1788 | ** |
| 1789 | ** Sometimes when requesting one lock state, additional lock states |
| 1790 | ** are inserted in between. The locking might fail on one of the later |
| 1791 | ** transitions leaving the lock state different from what it started but |
| 1792 | ** still short of its goal. The following chart shows the allowed |
| 1793 | ** transitions and the inserted intermediate states: |
| 1794 | ** |
| 1795 | ** UNLOCKED -> SHARED |
| 1796 | ** SHARED -> RESERVED |
| 1797 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1798 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1799 | ** PENDING -> EXCLUSIVE |
| 1800 | ** |
| 1801 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 1802 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 1803 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 1804 | ** access the file. |
| 1805 | ** |
| 1806 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1807 | ** routine to lower a locking level. |
| 1808 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1809 | static int flockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1810 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1811 | unixFile *pFile = (unixFile*)id; |
| 1812 | |
| 1813 | assert( pFile ); |
| 1814 | |
| 1815 | /* if we already have a lock, it is exclusive. |
| 1816 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1817 | if (pFile->eFileLock > NO_LOCK) { |
| 1818 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1819 | return SQLITE_OK; |
| 1820 | } |
| 1821 | |
| 1822 | /* grab an exclusive lock */ |
| 1823 | |
| 1824 | if (flock(pFile->h, LOCK_EX | LOCK_NB)) { |
| 1825 | int tErrno = errno; |
| 1826 | /* didn't get, must be busy */ |
| 1827 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1828 | if( IS_LOCK_ERROR(rc) ){ |
| 1829 | pFile->lastErrno = tErrno; |
| 1830 | } |
| 1831 | } else { |
| 1832 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1833 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1834 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1835 | OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), |
| 1836 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1837 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 1838 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 1839 | rc = SQLITE_BUSY; |
| 1840 | } |
| 1841 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 1842 | return rc; |
| 1843 | } |
| 1844 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1845 | |
| 1846 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1847 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1848 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1849 | ** |
| 1850 | ** If the locking level of the file descriptor is already at or below |
| 1851 | ** the requested locking level, this routine is a no-op. |
| 1852 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1853 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1854 | unixFile *pFile = (unixFile*)id; |
| 1855 | |
| 1856 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1857 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
| 1858 | pFile->eFileLock, getpid())); |
| 1859 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1860 | |
| 1861 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1862 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1863 | return SQLITE_OK; |
| 1864 | } |
| 1865 | |
| 1866 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1867 | if (eFileLock==SHARED_LOCK) { |
| 1868 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1869 | return SQLITE_OK; |
| 1870 | } |
| 1871 | |
| 1872 | /* no, really, unlock. */ |
| 1873 | int rc = flock(pFile->h, LOCK_UN); |
| 1874 | if (rc) { |
| 1875 | int r, tErrno = errno; |
| 1876 | r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1877 | if( IS_LOCK_ERROR(r) ){ |
| 1878 | pFile->lastErrno = tErrno; |
| 1879 | } |
| 1880 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 1881 | if( (r & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 1882 | r = SQLITE_BUSY; |
| 1883 | } |
| 1884 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 1885 | |
| 1886 | return r; |
| 1887 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1888 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1889 | return SQLITE_OK; |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | /* |
| 1894 | ** Close a file. |
| 1895 | */ |
| 1896 | static int flockClose(sqlite3_file *id) { |
| 1897 | if( id ){ |
| 1898 | flockUnlock(id, NO_LOCK); |
| 1899 | } |
| 1900 | return closeUnixFile(id); |
| 1901 | } |
| 1902 | |
| 1903 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 1904 | |
| 1905 | /******************* End of the flock lock implementation ********************* |
| 1906 | ******************************************************************************/ |
| 1907 | |
| 1908 | /****************************************************************************** |
| 1909 | ************************ Begin Named Semaphore Locking ************************ |
| 1910 | ** |
| 1911 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1912 | ** |
| 1913 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 1914 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 1915 | ** the database file at a time. This reduces potential concurrency, but |
| 1916 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1917 | */ |
| 1918 | #if OS_VXWORKS |
| 1919 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1920 | /* |
| 1921 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1922 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1923 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1924 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1925 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1926 | static int semCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 1927 | int rc = SQLITE_OK; |
| 1928 | int reserved = 0; |
| 1929 | unixFile *pFile = (unixFile*)id; |
| 1930 | |
| 1931 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1932 | |
| 1933 | assert( pFile ); |
| 1934 | |
| 1935 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1936 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1937 | reserved = 1; |
| 1938 | } |
| 1939 | |
| 1940 | /* Otherwise see if some other process holds it. */ |
| 1941 | if( !reserved ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1942 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1943 | struct stat statBuf; |
| 1944 | |
| 1945 | if( sem_trywait(pSem)==-1 ){ |
| 1946 | int tErrno = errno; |
| 1947 | if( EAGAIN != tErrno ){ |
| 1948 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 1949 | pFile->lastErrno = tErrno; |
| 1950 | } else { |
| 1951 | /* someone else has the lock when we are in NO_LOCK */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1952 | reserved = (pFile->eFileLock < SHARED_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1953 | } |
| 1954 | }else{ |
| 1955 | /* we could have it if we want it */ |
| 1956 | sem_post(pSem); |
| 1957 | } |
| 1958 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1959 | OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1960 | |
| 1961 | *pResOut = reserved; |
| 1962 | return rc; |
| 1963 | } |
| 1964 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1965 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1966 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1967 | ** of the following: |
| 1968 | ** |
| 1969 | ** (1) SHARED_LOCK |
| 1970 | ** (2) RESERVED_LOCK |
| 1971 | ** (3) PENDING_LOCK |
| 1972 | ** (4) EXCLUSIVE_LOCK |
| 1973 | ** |
| 1974 | ** Sometimes when requesting one lock state, additional lock states |
| 1975 | ** are inserted in between. The locking might fail on one of the later |
| 1976 | ** transitions leaving the lock state different from what it started but |
| 1977 | ** still short of its goal. The following chart shows the allowed |
| 1978 | ** transitions and the inserted intermediate states: |
| 1979 | ** |
| 1980 | ** UNLOCKED -> SHARED |
| 1981 | ** SHARED -> RESERVED |
| 1982 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1983 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1984 | ** PENDING -> EXCLUSIVE |
| 1985 | ** |
| 1986 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 1987 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 1988 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 1989 | ** access the file. |
| 1990 | ** |
| 1991 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1992 | ** routine to lower a locking level. |
| 1993 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1994 | static int semLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1995 | unixFile *pFile = (unixFile*)id; |
| 1996 | int fd; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1997 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1998 | int rc = SQLITE_OK; |
| 1999 | |
| 2000 | /* if we already have a lock, it is exclusive. |
| 2001 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2002 | if (pFile->eFileLock > NO_LOCK) { |
| 2003 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2004 | rc = SQLITE_OK; |
| 2005 | goto sem_end_lock; |
| 2006 | } |
| 2007 | |
| 2008 | /* lock semaphore now but bail out when already locked. */ |
| 2009 | if( sem_trywait(pSem)==-1 ){ |
| 2010 | rc = SQLITE_BUSY; |
| 2011 | goto sem_end_lock; |
| 2012 | } |
| 2013 | |
| 2014 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2015 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2016 | |
| 2017 | sem_end_lock: |
| 2018 | return rc; |
| 2019 | } |
| 2020 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2021 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2022 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2023 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2024 | ** |
| 2025 | ** If the locking level of the file descriptor is already at or below |
| 2026 | ** the requested locking level, this routine is a no-op. |
| 2027 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2028 | static int semUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2029 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2030 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2031 | |
| 2032 | assert( pFile ); |
| 2033 | assert( pSem ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2034 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
| 2035 | pFile->eFileLock, getpid())); |
| 2036 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2037 | |
| 2038 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2039 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2040 | return SQLITE_OK; |
| 2041 | } |
| 2042 | |
| 2043 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2044 | if (eFileLock==SHARED_LOCK) { |
| 2045 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2046 | return SQLITE_OK; |
| 2047 | } |
| 2048 | |
| 2049 | /* no, really unlock. */ |
| 2050 | if ( sem_post(pSem)==-1 ) { |
| 2051 | int rc, tErrno = errno; |
| 2052 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2053 | if( IS_LOCK_ERROR(rc) ){ |
| 2054 | pFile->lastErrno = tErrno; |
| 2055 | } |
| 2056 | return rc; |
| 2057 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2058 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2059 | return SQLITE_OK; |
| 2060 | } |
| 2061 | |
| 2062 | /* |
| 2063 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2064 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2065 | static int semClose(sqlite3_file *id) { |
| 2066 | if( id ){ |
| 2067 | unixFile *pFile = (unixFile*)id; |
| 2068 | semUnlock(id, NO_LOCK); |
| 2069 | assert( pFile ); |
| 2070 | unixEnterMutex(); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2071 | releaseInodeInfo(pFile); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2072 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2073 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2074 | } |
| 2075 | return SQLITE_OK; |
| 2076 | } |
| 2077 | |
| 2078 | #endif /* OS_VXWORKS */ |
| 2079 | /* |
| 2080 | ** Named semaphore locking is only available on VxWorks. |
| 2081 | ** |
| 2082 | *************** End of the named semaphore lock implementation **************** |
| 2083 | ******************************************************************************/ |
| 2084 | |
| 2085 | |
| 2086 | /****************************************************************************** |
| 2087 | *************************** Begin AFP Locking ********************************* |
| 2088 | ** |
| 2089 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2090 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2091 | ** |
| 2092 | ** Third-party implementations of AFP are available. But this code here |
| 2093 | ** only works on OSX. |
| 2094 | */ |
| 2095 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2096 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2097 | /* |
| 2098 | ** The afpLockingContext structure contains all afp lock specific state |
| 2099 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2100 | typedef struct afpLockingContext afpLockingContext; |
| 2101 | struct afpLockingContext { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2102 | int reserved; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2103 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2104 | }; |
| 2105 | |
| 2106 | struct ByteRangeLockPB2 |
| 2107 | { |
| 2108 | unsigned long long offset; /* offset to first byte to lock */ |
| 2109 | unsigned long long length; /* nbr of bytes to lock */ |
| 2110 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2111 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2112 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2113 | int fd; /* file desc to assoc this lock with */ |
| 2114 | }; |
| 2115 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2116 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2117 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2118 | /* |
| 2119 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2120 | ** AFP filesystem. |
| 2121 | ** |
| 2122 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2123 | */ |
| 2124 | static int afpSetLock( |
| 2125 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2126 | unixFile *pFile, /* Open file descriptor on path */ |
| 2127 | unsigned long long offset, /* First byte to be locked */ |
| 2128 | unsigned long long length, /* Number of bytes to lock */ |
| 2129 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2130 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2131 | struct ByteRangeLockPB2 pb; |
| 2132 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2133 | |
| 2134 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2135 | pb.startEndFlag = 0; |
| 2136 | pb.offset = offset; |
| 2137 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2138 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2139 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2140 | OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2141 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2142 | offset, length)); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2143 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2144 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2145 | int rc; |
| 2146 | int tErrno = errno; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2147 | OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2148 | path, tErrno, strerror(tErrno))); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2149 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2150 | rc = SQLITE_BUSY; |
| 2151 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2152 | rc = sqliteErrorFromPosixError(tErrno, |
| 2153 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2154 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2155 | if( IS_LOCK_ERROR(rc) ){ |
| 2156 | pFile->lastErrno = tErrno; |
| 2157 | } |
| 2158 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2159 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2160 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2161 | } |
| 2162 | } |
| 2163 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2164 | /* |
| 2165 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2166 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2167 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2168 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2169 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2170 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2171 | int rc = SQLITE_OK; |
| 2172 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2173 | unixFile *pFile = (unixFile*)id; |
| 2174 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2175 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2176 | |
| 2177 | assert( pFile ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2178 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2179 | if( context->reserved ){ |
| 2180 | *pResOut = 1; |
| 2181 | return SQLITE_OK; |
| 2182 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2183 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2184 | |
| 2185 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2186 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2187 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | /* Otherwise see if some other process holds it. |
| 2191 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2192 | if( !reserved ){ |
| 2193 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2194 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2195 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2196 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2197 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2198 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2199 | } else { |
| 2200 | /* if we failed to get the lock then someone else must have it */ |
| 2201 | reserved = 1; |
| 2202 | } |
| 2203 | if( IS_LOCK_ERROR(lrc) ){ |
| 2204 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2205 | } |
| 2206 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2207 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2208 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2209 | OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2210 | |
| 2211 | *pResOut = reserved; |
| 2212 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2215 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2216 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2217 | ** of the following: |
| 2218 | ** |
| 2219 | ** (1) SHARED_LOCK |
| 2220 | ** (2) RESERVED_LOCK |
| 2221 | ** (3) PENDING_LOCK |
| 2222 | ** (4) EXCLUSIVE_LOCK |
| 2223 | ** |
| 2224 | ** Sometimes when requesting one lock state, additional lock states |
| 2225 | ** are inserted in between. The locking might fail on one of the later |
| 2226 | ** transitions leaving the lock state different from what it started but |
| 2227 | ** still short of its goal. The following chart shows the allowed |
| 2228 | ** transitions and the inserted intermediate states: |
| 2229 | ** |
| 2230 | ** UNLOCKED -> SHARED |
| 2231 | ** SHARED -> RESERVED |
| 2232 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2233 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2234 | ** PENDING -> EXCLUSIVE |
| 2235 | ** |
| 2236 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2237 | ** routine to lower a locking level. |
| 2238 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2239 | static int afpLock(sqlite3_file *id, int eFileLock){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2240 | int rc = SQLITE_OK; |
| 2241 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2242 | unixInodeInfo *pInode = pFile->pInode; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2243 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2244 | |
| 2245 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2246 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 2247 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2248 | azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2249 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2250 | /* 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] | 2251 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2252 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2253 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2254 | if( pFile->eFileLock>=eFileLock ){ |
| 2255 | OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
| 2256 | azFileLock(eFileLock))); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2257 | return SQLITE_OK; |
| 2258 | } |
| 2259 | |
| 2260 | /* Make sure the locking sequence is correct |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2261 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 2262 | ** (2) SQLite never explicitly requests a pendig lock. |
| 2263 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2264 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2265 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 2266 | assert( eFileLock!=PENDING_LOCK ); |
| 2267 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2268 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2269 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2270 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2271 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2272 | pInode = pFile->pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2273 | |
| 2274 | /* If some thread using this PID has a lock via a different unixFile* |
| 2275 | ** handle that precludes the requested lock, return BUSY. |
| 2276 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2277 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 2278 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2279 | ){ |
| 2280 | rc = SQLITE_BUSY; |
| 2281 | goto afp_end_lock; |
| 2282 | } |
| 2283 | |
| 2284 | /* If a SHARED lock is requested, and some thread using this PID already |
| 2285 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 2286 | ** return SQLITE_OK. |
| 2287 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2288 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2289 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2290 | assert( eFileLock==SHARED_LOCK ); |
| 2291 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2292 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2293 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2294 | pInode->nShared++; |
| 2295 | pInode->nLock++; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2296 | goto afp_end_lock; |
| 2297 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2298 | |
| 2299 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2300 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2301 | ** be released. |
| 2302 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2303 | if( eFileLock==SHARED_LOCK |
| 2304 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2305 | ){ |
| 2306 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2307 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2308 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2309 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2310 | goto afp_end_lock; |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2315 | ** operating system calls for the specified lock. |
| 2316 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2317 | if( eFileLock==SHARED_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2318 | int lrc1, lrc2, lrc1Errno; |
| 2319 | long lk, mask; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2320 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2321 | assert( pInode->nShared==0 ); |
| 2322 | assert( pInode->eFileLock==0 ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2323 | |
| 2324 | mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2325 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2326 | /* note that the quality of the randomness doesn't matter that much */ |
| 2327 | lk = random(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2328 | pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2329 | lrc1 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2330 | SHARED_FIRST+pInode->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2331 | if( IS_LOCK_ERROR(lrc1) ){ |
| 2332 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2333 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2334 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2335 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2336 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2337 | if( IS_LOCK_ERROR(lrc1) ) { |
| 2338 | pFile->lastErrno = lrc1Errno; |
| 2339 | rc = lrc1; |
| 2340 | goto afp_end_lock; |
| 2341 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 2342 | rc = lrc2; |
| 2343 | goto afp_end_lock; |
| 2344 | } else if( lrc1 != SQLITE_OK ) { |
| 2345 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2346 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2347 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2348 | pInode->nLock++; |
| 2349 | pInode->nShared = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2350 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2351 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2352 | /* We are trying for an exclusive lock but another thread in this |
| 2353 | ** same process is still holding a shared lock. */ |
| 2354 | rc = SQLITE_BUSY; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2355 | }else{ |
| 2356 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 2357 | ** assumed that there is a SHARED or greater lock on the file |
| 2358 | ** already. |
| 2359 | */ |
| 2360 | int failed = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2361 | assert( 0!=pFile->eFileLock ); |
| 2362 | if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2363 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2364 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2365 | if( !failed ){ |
| 2366 | context->reserved = 1; |
| 2367 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2368 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2369 | if (!failed && eFileLock == EXCLUSIVE_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2370 | /* Acquire an EXCLUSIVE lock */ |
| 2371 | |
| 2372 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2373 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2374 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2375 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2376 | pInode->sharedByte, 1, 0)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2377 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2378 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2379 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2380 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2381 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2382 | SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2383 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 2384 | ** a critical I/O error |
| 2385 | */ |
| 2386 | rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : |
| 2387 | SQLITE_IOERR_LOCK; |
| 2388 | goto afp_end_lock; |
| 2389 | } |
| 2390 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2391 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2392 | } |
| 2393 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2394 | if( failed ){ |
| 2395 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2396 | } |
| 2397 | } |
| 2398 | |
| 2399 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2400 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2401 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2402 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 2403 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2404 | pInode->eFileLock = PENDING_LOCK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | afp_end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2408 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2409 | OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), |
| 2410 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2411 | return rc; |
| 2412 | } |
| 2413 | |
| 2414 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2415 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2416 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2417 | ** |
| 2418 | ** If the locking level of the file descriptor is already at or below |
| 2419 | ** the requested locking level, this routine is a no-op. |
| 2420 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2421 | static int afpUnlock(sqlite3_file *id, int eFileLock) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2422 | int rc = SQLITE_OK; |
| 2423 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2424 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2425 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 2426 | int skipShared = 0; |
| 2427 | #ifdef SQLITE_TEST |
| 2428 | int h = pFile->h; |
| 2429 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2430 | |
| 2431 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2432 | OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2433 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2434 | getpid())); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2435 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2436 | assert( eFileLock<=SHARED_LOCK ); |
| 2437 | if( pFile->eFileLock<=eFileLock ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2438 | return SQLITE_OK; |
| 2439 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2440 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2441 | pInode = pFile->pInode; |
| 2442 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2443 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2444 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2445 | SimulateIOErrorBenign(1); |
| 2446 | SimulateIOError( h=(-1) ) |
| 2447 | SimulateIOErrorBenign(0); |
| 2448 | |
| 2449 | #ifndef NDEBUG |
| 2450 | /* When reducing a lock such that other processes can start |
| 2451 | ** reading the database file again, make sure that the |
| 2452 | ** transaction counter was updated if any part of the database |
| 2453 | ** file changed. If the transaction counter is not updated, |
| 2454 | ** other connections to the same file might not realize that |
| 2455 | ** the file has changed and hence might not know to flush their |
| 2456 | ** cache. The use of a stale cache can lead to database corruption. |
| 2457 | */ |
| 2458 | assert( pFile->inNormalWrite==0 |
| 2459 | || pFile->dbUpdate==0 |
| 2460 | || pFile->transCntrChng==1 ); |
| 2461 | pFile->inNormalWrite = 0; |
| 2462 | #endif |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2463 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2464 | if( pFile->eFileLock==EXCLUSIVE_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2465 | rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2466 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2467 | /* only re-establish the shared lock if necessary */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2468 | int sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2469 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 2470 | } else { |
| 2471 | skipShared = 1; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2472 | } |
| 2473 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2474 | if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2475 | rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2476 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2477 | if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2478 | rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 2479 | if( !rc ){ |
| 2480 | context->reserved = 0; |
| 2481 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2482 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2483 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ |
| 2484 | pInode->eFileLock = SHARED_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2485 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2486 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2487 | if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2488 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2489 | /* Decrement the shared lock counter. Release the lock using an |
| 2490 | ** OS call only when all threads in this same process have released |
| 2491 | ** the lock. |
| 2492 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2493 | unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 2494 | pInode->nShared--; |
| 2495 | if( pInode->nShared==0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2496 | SimulateIOErrorBenign(1); |
| 2497 | SimulateIOError( h=(-1) ) |
| 2498 | SimulateIOErrorBenign(0); |
| 2499 | if( !skipShared ){ |
| 2500 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 2501 | } |
| 2502 | if( !rc ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2503 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2504 | pFile->eFileLock = NO_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2505 | } |
| 2506 | } |
| 2507 | if( rc==SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2508 | pInode->nLock--; |
| 2509 | assert( pInode->nLock>=0 ); |
| 2510 | if( pInode->nLock==0 ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2511 | rc = closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2512 | } |
| 2513 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2514 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2515 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2516 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2517 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2518 | return rc; |
| 2519 | } |
| 2520 | |
| 2521 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2522 | ** Close a file & cleanup AFP specific locking context |
| 2523 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2524 | static int afpClose(sqlite3_file *id) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2525 | int rc = SQLITE_OK; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2526 | if( id ){ |
| 2527 | unixFile *pFile = (unixFile*)id; |
| 2528 | afpUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2529 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2530 | if( pFile->pInode && pFile->pInode->nLock ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2531 | /* If there are outstanding locks, do not actually close the file just |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2532 | ** yet because that would clear those locks. Instead, add the file |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2533 | ** descriptor to pInode->aPending. It will be automatically closed when |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2534 | ** the last lock is cleared. |
| 2535 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2536 | setPendingFd(pFile); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2537 | } |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2538 | releaseInodeInfo(pFile); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2539 | sqlite3_free(pFile->lockingContext); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2540 | rc = closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2541 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2542 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2543 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2544 | } |
| 2545 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2546 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2547 | /* |
| 2548 | ** The code above is the AFP lock implementation. The code is specific |
| 2549 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2550 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 2551 | ** VFS is not available. |
| 2552 | ** |
| 2553 | ********************* End of the AFP lock implementation ********************** |
| 2554 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2555 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2556 | /****************************************************************************** |
| 2557 | *************************** Begin NFS Locking ********************************/ |
| 2558 | |
| 2559 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 2560 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2561 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2562 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2563 | ** |
| 2564 | ** If the locking level of the file descriptor is already at or below |
| 2565 | ** the requested locking level, this routine is a no-op. |
| 2566 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2567 | static int nfsUnlock(sqlite3_file *id, int eFileLock){ |
| 2568 | return _posixUnlock(id, eFileLock, 1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
| 2571 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 2572 | /* |
| 2573 | ** The code above is the NFS lock implementation. The code is specific |
| 2574 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2575 | ** is available. |
| 2576 | ** |
| 2577 | ********************* End of the NFS lock implementation ********************** |
| 2578 | ******************************************************************************/ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2579 | |
| 2580 | /****************************************************************************** |
| 2581 | **************** Non-locking sqlite3_file methods ***************************** |
| 2582 | ** |
| 2583 | ** The next division contains implementations for all methods of the |
| 2584 | ** sqlite3_file object other than the locking methods. The locking |
| 2585 | ** methods were defined in divisions above (one locking method per |
| 2586 | ** division). Those methods that are common to all locking modes |
| 2587 | ** are gather together into this division. |
| 2588 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2589 | |
| 2590 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2591 | ** Seek to the offset passed as the second argument, then read cnt |
| 2592 | ** bytes into pBuf. Return the number of bytes actually read. |
| 2593 | ** |
| 2594 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 2595 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 2596 | ** one system to another. Since SQLite does not define USE_PREAD |
| 2597 | ** any any form by default, we will not attempt to define _XOPEN_SOURCE. |
| 2598 | ** See tickets #2741 and #2681. |
| 2599 | ** |
| 2600 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 2601 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2602 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2603 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 2604 | int got; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2605 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2606 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2607 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2608 | TIMER_START; |
| 2609 | #if defined(USE_PREAD) |
| 2610 | got = pread(id->h, pBuf, cnt, offset); |
| 2611 | SimulateIOError( got = -1 ); |
| 2612 | #elif defined(USE_PREAD64) |
| 2613 | got = pread64(id->h, pBuf, cnt, offset); |
| 2614 | SimulateIOError( got = -1 ); |
| 2615 | #else |
| 2616 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 2617 | SimulateIOError( newOffset-- ); |
| 2618 | if( newOffset!=offset ){ |
| 2619 | if( newOffset == -1 ){ |
| 2620 | ((unixFile*)id)->lastErrno = errno; |
| 2621 | }else{ |
| 2622 | ((unixFile*)id)->lastErrno = 0; |
| 2623 | } |
| 2624 | return -1; |
| 2625 | } |
| 2626 | got = read(id->h, pBuf, cnt); |
| 2627 | #endif |
| 2628 | TIMER_END; |
| 2629 | if( got<0 ){ |
| 2630 | ((unixFile*)id)->lastErrno = errno; |
| 2631 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2632 | OSTRACE(("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2633 | return got; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2637 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 2638 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 2639 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2640 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2641 | static int unixRead( |
| 2642 | sqlite3_file *id, |
| 2643 | void *pBuf, |
| 2644 | int amt, |
| 2645 | sqlite3_int64 offset |
| 2646 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2647 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2648 | int got; |
| 2649 | assert( id ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2650 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2651 | /* If this is a database file (not a journal, master-journal or temp |
| 2652 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2653 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 2654 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2655 | || offset>=PENDING_BYTE+512 |
| 2656 | || offset+amt<=PENDING_BYTE |
| 2657 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2658 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2659 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2660 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2661 | if( got==amt ){ |
| 2662 | return SQLITE_OK; |
| 2663 | }else if( got<0 ){ |
| 2664 | /* lastErrno set by seekAndRead */ |
| 2665 | return SQLITE_IOERR_READ; |
| 2666 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2667 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2668 | /* Unread parts of the buffer must be zero-filled */ |
| 2669 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 2670 | return SQLITE_IOERR_SHORT_READ; |
| 2671 | } |
| 2672 | } |
| 2673 | |
| 2674 | /* |
| 2675 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 2676 | ** Return the number of bytes actually read. Update the offset. |
| 2677 | ** |
| 2678 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 2679 | ** is set before returning. |
| 2680 | */ |
| 2681 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
| 2682 | int got; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2683 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2684 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2685 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2686 | TIMER_START; |
| 2687 | #if defined(USE_PREAD) |
| 2688 | got = pwrite(id->h, pBuf, cnt, offset); |
| 2689 | #elif defined(USE_PREAD64) |
| 2690 | got = pwrite64(id->h, pBuf, cnt, offset); |
| 2691 | #else |
| 2692 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 2693 | if( newOffset!=offset ){ |
| 2694 | if( newOffset == -1 ){ |
| 2695 | ((unixFile*)id)->lastErrno = errno; |
| 2696 | }else{ |
| 2697 | ((unixFile*)id)->lastErrno = 0; |
| 2698 | } |
| 2699 | return -1; |
| 2700 | } |
| 2701 | got = write(id->h, pBuf, cnt); |
| 2702 | #endif |
| 2703 | TIMER_END; |
| 2704 | if( got<0 ){ |
| 2705 | ((unixFile*)id)->lastErrno = errno; |
| 2706 | } |
| 2707 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2708 | OSTRACE(("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2709 | return got; |
| 2710 | } |
| 2711 | |
| 2712 | |
| 2713 | /* |
| 2714 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 2715 | ** or some other error code on failure. |
| 2716 | */ |
| 2717 | static int unixWrite( |
| 2718 | sqlite3_file *id, |
| 2719 | const void *pBuf, |
| 2720 | int amt, |
| 2721 | sqlite3_int64 offset |
| 2722 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2723 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2724 | int wrote = 0; |
| 2725 | assert( id ); |
| 2726 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2727 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2728 | /* If this is a database file (not a journal, master-journal or temp |
| 2729 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2730 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 2731 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2732 | || offset>=PENDING_BYTE+512 |
| 2733 | || offset+amt<=PENDING_BYTE |
| 2734 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2735 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2736 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2737 | #ifndef NDEBUG |
| 2738 | /* If we are doing a normal write to a database file (as opposed to |
| 2739 | ** doing a hot-journal rollback or a write to some file other than a |
| 2740 | ** normal database file) then record the fact that the database |
| 2741 | ** has changed. If the transaction counter is modified, record that |
| 2742 | ** fact too. |
| 2743 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2744 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2745 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 2746 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 2747 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2748 | char oldCntr[4]; |
| 2749 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 2750 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2751 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 2752 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2753 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 2754 | } |
| 2755 | } |
| 2756 | } |
| 2757 | #endif |
| 2758 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2759 | while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2760 | amt -= wrote; |
| 2761 | offset += wrote; |
| 2762 | pBuf = &((char*)pBuf)[wrote]; |
| 2763 | } |
| 2764 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 2765 | SimulateDiskfullError(( wrote=0, amt=1 )); |
| 2766 | if( amt>0 ){ |
| 2767 | if( wrote<0 ){ |
| 2768 | /* lastErrno set by seekAndWrite */ |
| 2769 | return SQLITE_IOERR_WRITE; |
| 2770 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2771 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2772 | return SQLITE_FULL; |
| 2773 | } |
| 2774 | } |
| 2775 | return SQLITE_OK; |
| 2776 | } |
| 2777 | |
| 2778 | #ifdef SQLITE_TEST |
| 2779 | /* |
| 2780 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2781 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2782 | */ |
| 2783 | int sqlite3_sync_count = 0; |
| 2784 | int sqlite3_fullsync_count = 0; |
| 2785 | #endif |
| 2786 | |
| 2787 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 2788 | ** We do not trust systems to provide a working fdatasync(). Some do. |
| 2789 | ** Others do no. To be safe, we will stick with the (slower) fsync(). |
| 2790 | ** If you know that your system does support fdatasync() correctly, |
| 2791 | ** then simply compile with -Dfdatasync=fdatasync |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2792 | */ |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 2793 | #if !defined(fdatasync) && !defined(__linux__) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2794 | # define fdatasync fsync |
| 2795 | #endif |
| 2796 | |
| 2797 | /* |
| 2798 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 2799 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 2800 | ** only available on Mac OS X. But that could change. |
| 2801 | */ |
| 2802 | #ifdef F_FULLFSYNC |
| 2803 | # define HAVE_FULLFSYNC 1 |
| 2804 | #else |
| 2805 | # define HAVE_FULLFSYNC 0 |
| 2806 | #endif |
| 2807 | |
| 2808 | |
| 2809 | /* |
| 2810 | ** The fsync() system call does not work as advertised on many |
| 2811 | ** unix systems. The following procedure is an attempt to make |
| 2812 | ** it work better. |
| 2813 | ** |
| 2814 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 2815 | ** for testing when we want to run through the test suite quickly. |
| 2816 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 2817 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 2818 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2819 | ** |
| 2820 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 2821 | ** The idea behind dataOnly is that it should only write the file content |
| 2822 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 2823 | ** unchanged since the file size is part of the inode. However, |
| 2824 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 2825 | ** file size has changed. The only real difference between fdatasync() |
| 2826 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 2827 | ** inode if the mtime or owner or other inode attributes have changed. |
| 2828 | ** We only care about the file size, not the other file attributes, so |
| 2829 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 2830 | ** So, we always use fdatasync() if it is available, regardless of |
| 2831 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2832 | */ |
| 2833 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 2834 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2835 | |
| 2836 | /* The following "ifdef/elif/else/" block has the same structure as |
| 2837 | ** the one below. It is replicated here solely to avoid cluttering |
| 2838 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 2839 | */ |
| 2840 | #ifdef SQLITE_NO_SYNC |
| 2841 | UNUSED_PARAMETER(fd); |
| 2842 | UNUSED_PARAMETER(fullSync); |
| 2843 | UNUSED_PARAMETER(dataOnly); |
| 2844 | #elif HAVE_FULLFSYNC |
| 2845 | UNUSED_PARAMETER(dataOnly); |
| 2846 | #else |
| 2847 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2848 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2849 | #endif |
| 2850 | |
| 2851 | /* Record the number of times that we do a normal fsync() and |
| 2852 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 2853 | ** gets called with the correct arguments. |
| 2854 | */ |
| 2855 | #ifdef SQLITE_TEST |
| 2856 | if( fullSync ) sqlite3_fullsync_count++; |
| 2857 | sqlite3_sync_count++; |
| 2858 | #endif |
| 2859 | |
| 2860 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 2861 | ** no-op |
| 2862 | */ |
| 2863 | #ifdef SQLITE_NO_SYNC |
| 2864 | rc = SQLITE_OK; |
| 2865 | #elif HAVE_FULLFSYNC |
| 2866 | if( fullSync ){ |
| 2867 | rc = fcntl(fd, F_FULLFSYNC, 0); |
| 2868 | }else{ |
| 2869 | rc = 1; |
| 2870 | } |
| 2871 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2872 | ** It shouldn't be possible for fullfsync to fail on the local |
| 2873 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 2874 | ** isn't supported for this file system. So, attempt an fsync |
| 2875 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 2876 | ** It'd be better to detect fullfsync support once and avoid |
| 2877 | ** the fcntl call every time sync is called. |
| 2878 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2879 | if( rc ) rc = fsync(fd); |
| 2880 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2881 | #elif defined(__APPLE__) |
| 2882 | /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 2883 | ** so currently we default to the macro that redefines fdatasync to fsync |
| 2884 | */ |
| 2885 | rc = fsync(fd); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2886 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2887 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 2888 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2889 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2890 | rc = fsync(fd); |
| 2891 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2892 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2893 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 2894 | |
| 2895 | if( OS_VXWORKS && rc!= -1 ){ |
| 2896 | rc = 0; |
| 2897 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 2898 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2901 | /* |
| 2902 | ** Make sure all writes to a particular file are committed to disk. |
| 2903 | ** |
| 2904 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 2905 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 2906 | ** file data is synced. |
| 2907 | ** |
| 2908 | ** Under Unix, also make sure that the directory entry for the file |
| 2909 | ** has been created by fsync-ing the directory that contains the file. |
| 2910 | ** If we do not do this and we encounter a power failure, the directory |
| 2911 | ** entry for the journal might not exist after we reboot. The next |
| 2912 | ** SQLite to access the file will not know that the journal exists (because |
| 2913 | ** the directory entry for the journal was never created) and the transaction |
| 2914 | ** will not roll back - possibly leading to database corruption. |
| 2915 | */ |
| 2916 | static int unixSync(sqlite3_file *id, int flags){ |
| 2917 | int rc; |
| 2918 | unixFile *pFile = (unixFile*)id; |
| 2919 | |
| 2920 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 2921 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 2922 | |
| 2923 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 2924 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 2925 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 2926 | ); |
| 2927 | |
| 2928 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 2929 | ** line is to test that doing so does not cause any problems. |
| 2930 | */ |
| 2931 | SimulateDiskfullError( return SQLITE_FULL ); |
| 2932 | |
| 2933 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2934 | OSTRACE(("SYNC %-3d\n", pFile->h)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2935 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 2936 | SimulateIOError( rc=1 ); |
| 2937 | if( rc ){ |
| 2938 | pFile->lastErrno = errno; |
| 2939 | return SQLITE_IOERR_FSYNC; |
| 2940 | } |
| 2941 | if( pFile->dirfd>=0 ){ |
| 2942 | int err; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2943 | OSTRACE(("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd, |
| 2944 | HAVE_FULLFSYNC, isFullsync)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2945 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 2946 | /* The directory sync is only attempted if full_fsync is |
| 2947 | ** turned off or unavailable. If a full_fsync occurred above, |
| 2948 | ** then the directory sync is superfluous. |
| 2949 | */ |
| 2950 | if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){ |
| 2951 | /* |
| 2952 | ** We have received multiple reports of fsync() returning |
| 2953 | ** errors when applied to directories on certain file systems. |
| 2954 | ** A failed directory sync is not a big deal. So it seems |
| 2955 | ** better to ignore the error. Ticket #1657 |
| 2956 | */ |
| 2957 | /* pFile->lastErrno = errno; */ |
| 2958 | /* return SQLITE_IOERR; */ |
| 2959 | } |
| 2960 | #endif |
| 2961 | err = close(pFile->dirfd); /* Only need to sync once, so close the */ |
| 2962 | if( err==0 ){ /* directory when we are done */ |
| 2963 | pFile->dirfd = -1; |
| 2964 | }else{ |
| 2965 | pFile->lastErrno = errno; |
| 2966 | rc = SQLITE_IOERR_DIR_CLOSE; |
| 2967 | } |
| 2968 | } |
| 2969 | return rc; |
| 2970 | } |
| 2971 | |
| 2972 | /* |
| 2973 | ** Truncate an open file to a specified size |
| 2974 | */ |
| 2975 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
| 2976 | int rc; |
| 2977 | assert( id ); |
| 2978 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
| 2979 | rc = ftruncate(((unixFile*)id)->h, (off_t)nByte); |
| 2980 | if( rc ){ |
| 2981 | ((unixFile*)id)->lastErrno = errno; |
| 2982 | return SQLITE_IOERR_TRUNCATE; |
| 2983 | }else{ |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 2984 | #ifndef NDEBUG |
| 2985 | /* If we are doing a normal write to a database file (as opposed to |
| 2986 | ** doing a hot-journal rollback or a write to some file other than a |
| 2987 | ** normal database file) and we truncate the file to zero length, |
| 2988 | ** that effectively updates the change counter. This might happen |
| 2989 | ** when restoring a database using the backup API from a zero-length |
| 2990 | ** source. |
| 2991 | */ |
| 2992 | if( ((unixFile*)id)->inNormalWrite && nByte==0 ){ |
| 2993 | ((unixFile*)id)->transCntrChng = 1; |
| 2994 | } |
| 2995 | #endif |
| 2996 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2997 | return SQLITE_OK; |
| 2998 | } |
| 2999 | } |
| 3000 | |
| 3001 | /* |
| 3002 | ** Determine the current size of a file in bytes |
| 3003 | */ |
| 3004 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 3005 | int rc; |
| 3006 | struct stat buf; |
| 3007 | assert( id ); |
| 3008 | rc = fstat(((unixFile*)id)->h, &buf); |
| 3009 | SimulateIOError( rc=1 ); |
| 3010 | if( rc!=0 ){ |
| 3011 | ((unixFile*)id)->lastErrno = errno; |
| 3012 | return SQLITE_IOERR_FSTAT; |
| 3013 | } |
| 3014 | *pSize = buf.st_size; |
| 3015 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3016 | /* When opening a zero-size database, the findInodeInfo() procedure |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3017 | ** writes a single byte into that file in order to work around a bug |
| 3018 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 3019 | ** layers, we need to report this file size as zero even though it is |
| 3020 | ** really 1. Ticket #3260. |
| 3021 | */ |
| 3022 | if( *pSize==1 ) *pSize = 0; |
| 3023 | |
| 3024 | |
| 3025 | return SQLITE_OK; |
| 3026 | } |
| 3027 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3028 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3029 | /* |
| 3030 | ** Handler for proxy-locking file-control verbs. Defined below in the |
| 3031 | ** proxying locking division. |
| 3032 | */ |
| 3033 | static int proxyFileControl(sqlite3_file*,int,void*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3034 | #endif |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3035 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3036 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3037 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3038 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3039 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3040 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3041 | switch( op ){ |
| 3042 | case SQLITE_FCNTL_LOCKSTATE: { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3043 | *(int*)pArg = ((unixFile*)id)->eFileLock; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3044 | return SQLITE_OK; |
| 3045 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3046 | case SQLITE_LAST_ERRNO: { |
| 3047 | *(int*)pArg = ((unixFile*)id)->lastErrno; |
| 3048 | return SQLITE_OK; |
| 3049 | } |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3050 | case SQLITE_FCNTL_SIZE_HINT: { |
drh | c527669 | 2010-05-21 18:24:06 +0000 | [diff] [blame] | 3051 | #if 0 /* No performance advantage seen on Linux */ |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3052 | sqlite3_int64 szFile = *(sqlite3_int64*)pArg; |
| 3053 | unixFile *pFile = (unixFile*)id; |
| 3054 | ftruncate(pFile->h, szFile); |
drh | c527669 | 2010-05-21 18:24:06 +0000 | [diff] [blame] | 3055 | #endif |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3056 | return SQLITE_OK; |
| 3057 | } |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3058 | #ifndef NDEBUG |
| 3059 | /* The pager calls this method to signal that it has done |
| 3060 | ** a rollback and that the database is therefore unchanged and |
| 3061 | ** it hence it is OK for the transaction change counter to be |
| 3062 | ** unchanged. |
| 3063 | */ |
| 3064 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 3065 | ((unixFile*)id)->dbUpdate = 0; |
| 3066 | return SQLITE_OK; |
| 3067 | } |
| 3068 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3069 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3070 | case SQLITE_SET_LOCKPROXYFILE: |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3071 | case SQLITE_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3072 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3073 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3074 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3075 | } |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3076 | return SQLITE_ERROR; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | /* |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3080 | ** Return the sector size in bytes of the underlying block device for |
| 3081 | ** the specified file. This is almost always 512 bytes, but may be |
| 3082 | ** larger for some devices. |
| 3083 | ** |
| 3084 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 3085 | ** if two files are created in the same file-system directory (i.e. |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3086 | ** a database and its journal file) that the sector size will be the |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3087 | ** same for both. |
| 3088 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3089 | static int unixSectorSize(sqlite3_file *NotUsed){ |
| 3090 | UNUSED_PARAMETER(NotUsed); |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 3091 | return SQLITE_DEFAULT_SECTOR_SIZE; |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3094 | /* |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3095 | ** Return the device characteristics for the file. This is always 0 for unix. |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3096 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3097 | static int unixDeviceCharacteristics(sqlite3_file *NotUsed){ |
| 3098 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3099 | return 0; |
| 3100 | } |
| 3101 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3102 | #ifndef SQLITE_OMIT_WAL |
| 3103 | |
| 3104 | |
| 3105 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3106 | ** Object used to represent an shared memory buffer. |
| 3107 | ** |
| 3108 | ** When multiple threads all reference the same wal-index, each thread |
| 3109 | ** has its own unixShm object, but they all point to a single instance |
| 3110 | ** of this unixShmNode object. In other words, each wal-index is opened |
| 3111 | ** only once per process. |
| 3112 | ** |
| 3113 | ** Each unixShmNode object is connected to a single unixInodeInfo object. |
| 3114 | ** We could coalesce this object into unixInodeInfo, but that would mean |
| 3115 | ** every open file that does not use shared memory (in other words, most |
| 3116 | ** open files) would have to carry around this extra information. So |
| 3117 | ** the unixInodeInfo object contains a pointer to this unixShmNode object |
| 3118 | ** and the unixShmNode object is created only when needed. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3119 | ** |
| 3120 | ** unixMutexHeld() must be true when creating or destroying |
| 3121 | ** this object or while reading or writing the following fields: |
| 3122 | ** |
| 3123 | ** nRef |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3124 | ** |
| 3125 | ** The following fields are read-only after the object is created: |
| 3126 | ** |
| 3127 | ** fid |
| 3128 | ** zFilename |
| 3129 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3130 | ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3131 | ** unixMutexHeld() is true when reading or writing any other field |
| 3132 | ** in this structure. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3133 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3134 | struct unixShmNode { |
| 3135 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3136 | sqlite3_mutex *mutex; /* Mutex to access this object */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3137 | char *zFilename; /* Name of the mmapped file */ |
| 3138 | int h; /* Open file descriptor */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3139 | int szRegion; /* Size of shared-memory regions */ |
| 3140 | int nRegion; /* Size of array apRegion */ |
| 3141 | char **apRegion; /* Array of mapped shared-memory regions */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3142 | int nRef; /* Number of unixShm objects pointing to this */ |
| 3143 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3144 | #ifdef SQLITE_DEBUG |
| 3145 | u8 exclMask; /* Mask of exclusive locks held */ |
| 3146 | u8 sharedMask; /* Mask of shared locks held */ |
| 3147 | u8 nextShmId; /* Next available unixShm.id value */ |
| 3148 | #endif |
| 3149 | }; |
| 3150 | |
| 3151 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3152 | ** Structure used internally by this VFS to record the state of an |
| 3153 | ** open shared memory connection. |
| 3154 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3155 | ** The following fields are initialized when this object is created and |
| 3156 | ** are read-only thereafter: |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3157 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3158 | ** unixShm.pFile |
| 3159 | ** unixShm.id |
| 3160 | ** |
| 3161 | ** All other fields are read/write. The unixShm.pFile->mutex must be held |
| 3162 | ** while accessing any read/write fields. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3163 | */ |
| 3164 | struct unixShm { |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3165 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 3166 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3167 | u8 hasMutex; /* True if holding the unixShmNode mutex */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3168 | u16 sharedMask; /* Mask of shared locks held */ |
| 3169 | u16 exclMask; /* Mask of exclusive locks held */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3170 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3171 | u8 id; /* Id of this connection within its unixShmNode */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3172 | #endif |
| 3173 | }; |
| 3174 | |
| 3175 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3176 | ** Constants used for locking |
| 3177 | */ |
drh | bd9676c | 2010-06-23 17:58:38 +0000 | [diff] [blame] | 3178 | #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
drh | 4222441 | 2010-05-31 14:28:25 +0000 | [diff] [blame] | 3179 | #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3180 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3181 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3182 | ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3183 | ** |
| 3184 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 3185 | ** otherwise. |
| 3186 | */ |
| 3187 | static int unixShmSystemLock( |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3188 | unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */ |
| 3189 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3190 | int ofst, /* First byte of the locking range */ |
| 3191 | int n /* Number of bytes to lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3192 | ){ |
| 3193 | struct flock f; /* The posix advisory locking structure */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3194 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3195 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3196 | /* Access to the unixShmNode object is serialized by the caller */ |
| 3197 | assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3198 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3199 | /* Shared locks never span more than one byte */ |
| 3200 | assert( n==1 || lockType!=F_RDLCK ); |
| 3201 | |
| 3202 | /* Locks are within range */ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3203 | assert( n>=1 && n<SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3204 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3205 | /* Initialize the locking parameters */ |
| 3206 | memset(&f, 0, sizeof(f)); |
| 3207 | f.l_type = lockType; |
| 3208 | f.l_whence = SEEK_SET; |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3209 | f.l_start = ofst; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3210 | f.l_len = n; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3211 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3212 | rc = fcntl(pShmNode->h, F_SETLK, &f); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3213 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 3214 | |
| 3215 | /* Update the global lock state and do debug tracing */ |
| 3216 | #ifdef SQLITE_DEBUG |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3217 | { u16 mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3218 | OSTRACE(("SHM-LOCK ")); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3219 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3220 | if( rc==SQLITE_OK ){ |
| 3221 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3222 | OSTRACE(("unlock %d ok", ofst)); |
| 3223 | pShmNode->exclMask &= ~mask; |
| 3224 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3225 | }else if( lockType==F_RDLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3226 | OSTRACE(("read-lock %d ok", ofst)); |
| 3227 | pShmNode->exclMask &= ~mask; |
| 3228 | pShmNode->sharedMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3229 | }else{ |
| 3230 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3231 | OSTRACE(("write-lock %d ok", ofst)); |
| 3232 | pShmNode->exclMask |= mask; |
| 3233 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3234 | } |
| 3235 | }else{ |
| 3236 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3237 | OSTRACE(("unlock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3238 | }else if( lockType==F_RDLCK ){ |
| 3239 | OSTRACE(("read-lock failed")); |
| 3240 | }else{ |
| 3241 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3242 | OSTRACE(("write-lock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3243 | } |
| 3244 | } |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 3245 | OSTRACE((" - afterwards %03x,%03x\n", |
| 3246 | pShmNode->sharedMask, pShmNode->exclMask)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3247 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3248 | #endif |
| 3249 | |
| 3250 | return rc; |
| 3251 | } |
| 3252 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3253 | |
| 3254 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3255 | ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3256 | ** |
| 3257 | ** This is not a VFS shared-memory method; it is a utility function called |
| 3258 | ** by VFS shared-memory methods. |
| 3259 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3260 | static void unixShmPurge(unixFile *pFd){ |
| 3261 | unixShmNode *p = pFd->pInode->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3262 | assert( unixMutexHeld() ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3263 | if( p && p->nRef==0 ){ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3264 | int i; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3265 | assert( p->pInode==pFd->pInode ); |
| 3266 | if( p->mutex ) sqlite3_mutex_free(p->mutex); |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3267 | for(i=0; i<p->nRegion; i++){ |
| 3268 | munmap(p->apRegion[i], p->szRegion); |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3269 | } |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3270 | sqlite3_free(p->apRegion); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3271 | if( p->h>=0 ) close(p->h); |
| 3272 | p->pInode->pShmNode = 0; |
| 3273 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3274 | } |
| 3275 | } |
| 3276 | |
| 3277 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3278 | ** Open a shared-memory area associated with open database file pDbFd. |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3279 | ** This particular implementation uses mmapped files. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3280 | ** |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3281 | ** The file used to implement shared-memory is in the same directory |
| 3282 | ** as the open database file and has the same name as the open database |
| 3283 | ** file with the "-shm" suffix added. For example, if the database file |
| 3284 | ** is "/home/user1/config.db" then the file that is created and mmapped |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3285 | ** for shared memory will be called "/home/user1/config.db-shm". |
| 3286 | ** |
| 3287 | ** Another approach to is to use files in /dev/shm or /dev/tmp or an |
| 3288 | ** some other tmpfs mount. But if a file in a different directory |
| 3289 | ** from the database file is used, then differing access permissions |
| 3290 | ** or a chroot() might cause two different processes on the same |
| 3291 | ** database to end up using different files for shared memory - |
| 3292 | ** meaning that their memory would not really be shared - resulting |
| 3293 | ** in database corruption. Nevertheless, this tmpfs file usage |
| 3294 | ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" |
| 3295 | ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time |
| 3296 | ** option results in an incompatible build of SQLite; builds of SQLite |
| 3297 | ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the |
| 3298 | ** same database file at the same time, database corruption will likely |
| 3299 | ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered |
| 3300 | ** "unsupported" and may go away in a future SQLite release. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3301 | ** |
| 3302 | ** When opening a new shared-memory file, if no other instances of that |
| 3303 | ** file are currently open, in this process or in other processes, then |
| 3304 | ** the file must be truncated to zero length or have its header cleared. |
| 3305 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3306 | static int unixOpenSharedMemory(unixFile *pDbFd){ |
| 3307 | struct unixShm *p = 0; /* The connection to be opened */ |
| 3308 | struct unixShmNode *pShmNode; /* The underlying mmapped file */ |
| 3309 | int rc; /* Result code */ |
| 3310 | unixInodeInfo *pInode; /* The inode of fd */ |
| 3311 | char *zShmFilename; /* Name of the file used for SHM */ |
| 3312 | int nShmFilename; /* Size of the SHM filename in bytes */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3313 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3314 | /* Allocate space for the new unixShm object. */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3315 | p = sqlite3_malloc( sizeof(*p) ); |
| 3316 | if( p==0 ) return SQLITE_NOMEM; |
| 3317 | memset(p, 0, sizeof(*p)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3318 | assert( pDbFd->pShm==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3319 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3320 | /* Check to see if a unixShmNode object already exists. Reuse an existing |
| 3321 | ** one if present. Create a new one if necessary. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3322 | */ |
| 3323 | unixEnterMutex(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 3324 | pInode = pDbFd->pInode; |
| 3325 | pShmNode = pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3326 | if( pShmNode==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 3327 | struct stat sStat; /* fstat() info for database file */ |
| 3328 | |
| 3329 | /* Call fstat() to figure out the permissions on the database file. If |
| 3330 | ** a new *-shm file is created, an attempt will be made to create it |
| 3331 | ** with the same permissions. The actual permissions the file is created |
| 3332 | ** with are subject to the current umask setting. |
| 3333 | */ |
| 3334 | if( fstat(pDbFd->h, &sStat) ){ |
| 3335 | rc = SQLITE_IOERR_FSTAT; |
| 3336 | goto shm_open_err; |
| 3337 | } |
| 3338 | |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3339 | #ifdef SQLITE_SHM_DIRECTORY |
| 3340 | nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 30; |
| 3341 | #else |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3342 | nShmFilename = 5 + (int)strlen(pDbFd->zPath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3343 | #endif |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3344 | pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3345 | if( pShmNode==0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3346 | rc = SQLITE_NOMEM; |
| 3347 | goto shm_open_err; |
| 3348 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3349 | memset(pShmNode, 0, sizeof(*pShmNode)); |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3350 | zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1]; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3351 | #ifdef SQLITE_SHM_DIRECTORY |
| 3352 | sqlite3_snprintf(nShmFilename, zShmFilename, |
| 3353 | SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", |
| 3354 | (u32)sStat.st_ino, (u32)sStat.st_dev); |
| 3355 | #else |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3356 | sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3357 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3358 | pShmNode->h = -1; |
| 3359 | pDbFd->pInode->pShmNode = pShmNode; |
| 3360 | pShmNode->pInode = pDbFd->pInode; |
| 3361 | pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 3362 | if( pShmNode->mutex==0 ){ |
| 3363 | rc = SQLITE_NOMEM; |
| 3364 | goto shm_open_err; |
| 3365 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3366 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 3367 | pShmNode->h = open(zShmFilename, O_RDWR|O_CREAT, (sStat.st_mode & 0777)); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3368 | if( pShmNode->h<0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3369 | rc = SQLITE_CANTOPEN_BKPT; |
| 3370 | goto shm_open_err; |
| 3371 | } |
| 3372 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3373 | /* Check to see if another process is holding the dead-man switch. |
| 3374 | ** If not, truncate the file to zero length. |
| 3375 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3376 | rc = SQLITE_OK; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3377 | if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3378 | if( ftruncate(pShmNode->h, 0) ){ |
drh | aab4c02 | 2010-06-02 14:45:51 +0000 | [diff] [blame] | 3379 | rc = SQLITE_IOERR_SHMOPEN; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3380 | } |
| 3381 | } |
| 3382 | if( rc==SQLITE_OK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3383 | rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3384 | } |
| 3385 | if( rc ) goto shm_open_err; |
| 3386 | } |
| 3387 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3388 | /* Make the new connection a child of the unixShmNode */ |
| 3389 | p->pShmNode = pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3390 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3391 | p->id = pShmNode->nextShmId++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3392 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3393 | pShmNode->nRef++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3394 | pDbFd->pShm = p; |
| 3395 | unixLeaveMutex(); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame^] | 3396 | |
| 3397 | /* The reference count on pShmNode has already been incremented under |
| 3398 | ** the cover of the unixEnterMutex() mutex and the pointer from the |
| 3399 | ** new (struct unixShm) object to the pShmNode has been set. All that is |
| 3400 | ** left to do is to link the new object into the linked list starting |
| 3401 | ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex |
| 3402 | ** mutex. |
| 3403 | */ |
| 3404 | sqlite3_mutex_enter(pShmNode->mutex); |
| 3405 | p->pNext = pShmNode->pFirst; |
| 3406 | pShmNode->pFirst = p; |
| 3407 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3408 | return SQLITE_OK; |
| 3409 | |
| 3410 | /* Jump here on any error */ |
| 3411 | shm_open_err: |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3412 | unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3413 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3414 | unixLeaveMutex(); |
| 3415 | return rc; |
| 3416 | } |
| 3417 | |
| 3418 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3419 | ** This function is called to obtain a pointer to region iRegion of the |
| 3420 | ** shared-memory associated with the database file fd. Shared-memory regions |
| 3421 | ** are numbered starting from zero. Each shared-memory region is szRegion |
| 3422 | ** bytes in size. |
| 3423 | ** |
| 3424 | ** If an error occurs, an error code is returned and *pp is set to NULL. |
| 3425 | ** |
| 3426 | ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory |
| 3427 | ** region has not been allocated (by any client, including one running in a |
| 3428 | ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 3429 | ** bExtend is non-zero and the requested shared-memory region has not yet |
| 3430 | ** been allocated, it is allocated by this function. |
| 3431 | ** |
| 3432 | ** If the shared-memory region has already been allocated or is allocated by |
| 3433 | ** this call as described above, then it is mapped into this processes |
| 3434 | ** address space (if it is not already), *pp is set to point to the mapped |
| 3435 | ** memory and SQLITE_OK returned. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3436 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3437 | static int unixShmMap( |
| 3438 | sqlite3_file *fd, /* Handle open on database file */ |
| 3439 | int iRegion, /* Region to retrieve */ |
| 3440 | int szRegion, /* Size of regions */ |
| 3441 | int bExtend, /* True to extend file if necessary */ |
| 3442 | void volatile **pp /* OUT: Mapped memory */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3443 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3444 | unixFile *pDbFd = (unixFile*)fd; |
| 3445 | unixShm *p; |
| 3446 | unixShmNode *pShmNode; |
| 3447 | int rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3448 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3449 | /* If the shared-memory file has not yet been opened, open it now. */ |
| 3450 | if( pDbFd->pShm==0 ){ |
| 3451 | rc = unixOpenSharedMemory(pDbFd); |
| 3452 | if( rc!=SQLITE_OK ) return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3453 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3454 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3455 | p = pDbFd->pShm; |
| 3456 | pShmNode = p->pShmNode; |
| 3457 | sqlite3_mutex_enter(pShmNode->mutex); |
| 3458 | assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
| 3459 | |
| 3460 | if( pShmNode->nRegion<=iRegion ){ |
| 3461 | char **apNew; /* New apRegion[] array */ |
| 3462 | int nByte = (iRegion+1)*szRegion; /* Minimum required file size */ |
| 3463 | struct stat sStat; /* Used by fstat() */ |
| 3464 | |
| 3465 | pShmNode->szRegion = szRegion; |
| 3466 | |
| 3467 | /* The requested region is not mapped into this processes address space. |
| 3468 | ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 3469 | ** large enough to contain the requested region). |
| 3470 | */ |
| 3471 | if( fstat(pShmNode->h, &sStat) ){ |
| 3472 | rc = SQLITE_IOERR_SHMSIZE; |
| 3473 | goto shmpage_out; |
| 3474 | } |
| 3475 | |
| 3476 | if( sStat.st_size<nByte ){ |
| 3477 | /* The requested memory region does not exist. If bExtend is set to |
| 3478 | ** false, exit early. *pp will be set to NULL and SQLITE_OK returned. |
| 3479 | ** |
| 3480 | ** Alternatively, if bExtend is true, use ftruncate() to allocate |
| 3481 | ** the requested memory region. |
| 3482 | */ |
| 3483 | if( !bExtend ) goto shmpage_out; |
| 3484 | if( ftruncate(pShmNode->h, nByte) ){ |
| 3485 | rc = SQLITE_IOERR_SHMSIZE; |
| 3486 | goto shmpage_out; |
| 3487 | } |
| 3488 | } |
| 3489 | |
| 3490 | /* Map the requested memory region into this processes address space. */ |
| 3491 | apNew = (char **)sqlite3_realloc( |
| 3492 | pShmNode->apRegion, (iRegion+1)*sizeof(char *) |
| 3493 | ); |
| 3494 | if( !apNew ){ |
| 3495 | rc = SQLITE_IOERR_NOMEM; |
| 3496 | goto shmpage_out; |
| 3497 | } |
| 3498 | pShmNode->apRegion = apNew; |
| 3499 | while(pShmNode->nRegion<=iRegion){ |
| 3500 | void *pMem = mmap(0, szRegion, PROT_READ|PROT_WRITE, |
| 3501 | MAP_SHARED, pShmNode->h, iRegion*szRegion |
| 3502 | ); |
| 3503 | if( pMem==MAP_FAILED ){ |
| 3504 | rc = SQLITE_IOERR; |
| 3505 | goto shmpage_out; |
| 3506 | } |
| 3507 | pShmNode->apRegion[pShmNode->nRegion] = pMem; |
| 3508 | pShmNode->nRegion++; |
| 3509 | } |
| 3510 | } |
| 3511 | |
| 3512 | shmpage_out: |
| 3513 | if( pShmNode->nRegion>iRegion ){ |
| 3514 | *pp = pShmNode->apRegion[iRegion]; |
| 3515 | }else{ |
| 3516 | *pp = 0; |
| 3517 | } |
| 3518 | sqlite3_mutex_leave(pShmNode->mutex); |
| 3519 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3520 | } |
| 3521 | |
| 3522 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3523 | ** Change the lock state for a shared-memory segment. |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 3524 | ** |
| 3525 | ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little |
| 3526 | ** different here than in posix. In xShmLock(), one can go from unlocked |
| 3527 | ** to shared and back or from unlocked to exclusive and back. But one may |
| 3528 | ** not go from shared to exclusive or from exclusive to shared. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3529 | */ |
| 3530 | static int unixShmLock( |
| 3531 | sqlite3_file *fd, /* Database file holding the shared memory */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3532 | int ofst, /* First lock to acquire or release */ |
| 3533 | int n, /* Number of locks to acquire or release */ |
| 3534 | int flags /* What to do with the lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3535 | ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3536 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 3537 | unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 3538 | unixShm *pX; /* For looping over all siblings */ |
| 3539 | unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 3540 | int rc = SQLITE_OK; /* Result code */ |
| 3541 | u16 mask; /* Mask of locks to take or release */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3542 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3543 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 3544 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3545 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3546 | assert( n>=1 ); |
| 3547 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 3548 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 3549 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 3550 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 3551 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3552 | |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3553 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3554 | assert( n>1 || mask==(1<<ofst) ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3555 | sqlite3_mutex_enter(pShmNode->mutex); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3556 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 3557 | u16 allMask = 0; /* Mask of locks held by siblings */ |
| 3558 | |
| 3559 | /* See if any siblings hold this same lock */ |
| 3560 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 3561 | if( pX==p ) continue; |
| 3562 | assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
| 3563 | allMask |= pX->sharedMask; |
| 3564 | } |
| 3565 | |
| 3566 | /* Unlock the system-level locks */ |
| 3567 | if( (mask & allMask)==0 ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3568 | rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3569 | }else{ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3570 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3571 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3572 | |
| 3573 | /* Undo the local locks */ |
| 3574 | if( rc==SQLITE_OK ){ |
| 3575 | p->exclMask &= ~mask; |
| 3576 | p->sharedMask &= ~mask; |
| 3577 | } |
| 3578 | }else if( flags & SQLITE_SHM_SHARED ){ |
| 3579 | u16 allShared = 0; /* Union of locks held by connections other than "p" */ |
| 3580 | |
| 3581 | /* Find out which shared locks are already held by sibling connections. |
| 3582 | ** If any sibling already holds an exclusive lock, go ahead and return |
| 3583 | ** SQLITE_BUSY. |
| 3584 | */ |
| 3585 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3586 | if( (pX->exclMask & mask)!=0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3587 | rc = SQLITE_BUSY; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3588 | break; |
| 3589 | } |
| 3590 | allShared |= pX->sharedMask; |
| 3591 | } |
| 3592 | |
| 3593 | /* Get shared locks at the system level, if necessary */ |
| 3594 | if( rc==SQLITE_OK ){ |
| 3595 | if( (allShared & mask)==0 ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3596 | rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3597 | }else{ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3598 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3599 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3600 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3601 | |
| 3602 | /* Get the local shared locks */ |
| 3603 | if( rc==SQLITE_OK ){ |
| 3604 | p->sharedMask |= mask; |
| 3605 | } |
| 3606 | }else{ |
| 3607 | /* Make sure no sibling connections hold locks that will block this |
| 3608 | ** lock. If any do, return SQLITE_BUSY right away. |
| 3609 | */ |
| 3610 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3611 | if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ |
| 3612 | rc = SQLITE_BUSY; |
| 3613 | break; |
| 3614 | } |
| 3615 | } |
| 3616 | |
| 3617 | /* Get the exclusive locks at the system level. Then if successful |
| 3618 | ** also mark the local connection as being locked. |
| 3619 | */ |
| 3620 | if( rc==SQLITE_OK ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3621 | rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3622 | if( rc==SQLITE_OK ){ |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 3623 | assert( (p->sharedMask & mask)==0 ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3624 | p->exclMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3625 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3626 | } |
| 3627 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3628 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 3629 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
| 3630 | p->id, getpid(), p->sharedMask, p->exclMask)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3631 | return rc; |
| 3632 | } |
| 3633 | |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3634 | /* |
| 3635 | ** Implement a memory barrier or memory fence on shared memory. |
| 3636 | ** |
| 3637 | ** All loads and stores begun before the barrier must complete before |
| 3638 | ** any load or store begun after the barrier. |
| 3639 | */ |
| 3640 | static void unixShmBarrier( |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3641 | sqlite3_file *fd /* Database file holding the shared memory */ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3642 | ){ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 3643 | UNUSED_PARAMETER(fd); |
drh | b29ad85 | 2010-06-01 00:03:57 +0000 | [diff] [blame] | 3644 | unixEnterMutex(); |
| 3645 | unixLeaveMutex(); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3646 | } |
| 3647 | |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3648 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3649 | ** Close a connection to shared-memory. Delete the underlying |
| 3650 | ** storage if deleteFlag is true. |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 3651 | ** |
| 3652 | ** If there is no shared memory associated with the connection then this |
| 3653 | ** routine is a harmless no-op. |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3654 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3655 | static int unixShmUnmap( |
| 3656 | sqlite3_file *fd, /* The underlying database file */ |
| 3657 | int deleteFlag /* Delete shared-memory if true */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3658 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3659 | unixShm *p; /* The connection to be closed */ |
| 3660 | unixShmNode *pShmNode; /* The underlying shared-memory file */ |
| 3661 | unixShm **pp; /* For looping over sibling connections */ |
| 3662 | unixFile *pDbFd; /* The underlying database file */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3663 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3664 | pDbFd = (unixFile*)fd; |
| 3665 | p = pDbFd->pShm; |
| 3666 | if( p==0 ) return SQLITE_OK; |
| 3667 | pShmNode = p->pShmNode; |
| 3668 | |
| 3669 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 3670 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 3671 | |
| 3672 | /* Remove connection p from the set of connections associated |
| 3673 | ** with pShmNode */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3674 | sqlite3_mutex_enter(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3675 | for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 3676 | *pp = p->pNext; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3677 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3678 | /* Free the connection p */ |
| 3679 | sqlite3_free(p); |
| 3680 | pDbFd->pShm = 0; |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3681 | sqlite3_mutex_leave(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3682 | |
| 3683 | /* If pShmNode->nRef has reached 0, then close the underlying |
| 3684 | ** shared-memory file, too */ |
| 3685 | unixEnterMutex(); |
| 3686 | assert( pShmNode->nRef>0 ); |
| 3687 | pShmNode->nRef--; |
| 3688 | if( pShmNode->nRef==0 ){ |
| 3689 | if( deleteFlag ) unlink(pShmNode->zFilename); |
| 3690 | unixShmPurge(pDbFd); |
| 3691 | } |
| 3692 | unixLeaveMutex(); |
| 3693 | |
| 3694 | return SQLITE_OK; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3695 | } |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3696 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3697 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3698 | #else |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 3699 | # define unixShmMap 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3700 | # define unixShmLock 0 |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3701 | # define unixShmBarrier 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3702 | # define unixShmUnmap 0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3703 | #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 3704 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3705 | /* |
| 3706 | ** Here ends the implementation of all sqlite3_file methods. |
| 3707 | ** |
| 3708 | ********************** End sqlite3_file Methods ******************************* |
| 3709 | ******************************************************************************/ |
| 3710 | |
| 3711 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3712 | ** This division contains definitions of sqlite3_io_methods objects that |
| 3713 | ** implement various file locking strategies. It also contains definitions |
| 3714 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 3715 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 3716 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 3717 | ** the correct finder-function for that VFS. |
| 3718 | ** |
| 3719 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 3720 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 3721 | ** looks at the filesystem type and tries to guess the best locking |
| 3722 | ** strategy from that. |
| 3723 | ** |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3724 | ** For finder-funtion F, two objects are created: |
| 3725 | ** |
| 3726 | ** (1) The real finder-function named "FImpt()". |
| 3727 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3728 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3729 | ** |
| 3730 | ** |
| 3731 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 3732 | ** objects. We have to do this instead of letting pAppData point |
| 3733 | ** directly at the finder-function since C90 rules prevent a void* |
| 3734 | ** from be cast into a function pointer. |
| 3735 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3736 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3737 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3738 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3739 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 3740 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 3741 | ** |
| 3742 | ** * An I/O method finder function called FINDER that returns a pointer |
| 3743 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3744 | */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3745 | #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3746 | static const sqlite3_io_methods METHOD = { \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3747 | VERSION, /* iVersion */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3748 | CLOSE, /* xClose */ \ |
| 3749 | unixRead, /* xRead */ \ |
| 3750 | unixWrite, /* xWrite */ \ |
| 3751 | unixTruncate, /* xTruncate */ \ |
| 3752 | unixSync, /* xSync */ \ |
| 3753 | unixFileSize, /* xFileSize */ \ |
| 3754 | LOCK, /* xLock */ \ |
| 3755 | UNLOCK, /* xUnlock */ \ |
| 3756 | CKLOCK, /* xCheckReservedLock */ \ |
| 3757 | unixFileControl, /* xFileControl */ \ |
| 3758 | unixSectorSize, /* xSectorSize */ \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3759 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 3760 | unixShmMap, /* xShmMap */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3761 | unixShmLock, /* xShmLock */ \ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3762 | unixShmBarrier, /* xShmBarrier */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3763 | unixShmUnmap /* xShmUnmap */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3764 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3765 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 3766 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3767 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3768 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3769 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3770 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3771 | |
| 3772 | /* |
| 3773 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 3774 | ** locking strategies. Functions that return pointers to these methods |
| 3775 | ** are also created. |
| 3776 | */ |
| 3777 | IOMETHODS( |
| 3778 | posixIoFinder, /* Finder function name */ |
| 3779 | posixIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3780 | 2, /* shared memory is enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3781 | unixClose, /* xClose method */ |
| 3782 | unixLock, /* xLock method */ |
| 3783 | unixUnlock, /* xUnlock method */ |
| 3784 | unixCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3785 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3786 | IOMETHODS( |
| 3787 | nolockIoFinder, /* Finder function name */ |
| 3788 | nolockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3789 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3790 | nolockClose, /* xClose method */ |
| 3791 | nolockLock, /* xLock method */ |
| 3792 | nolockUnlock, /* xUnlock method */ |
| 3793 | nolockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3794 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3795 | IOMETHODS( |
| 3796 | dotlockIoFinder, /* Finder function name */ |
| 3797 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3798 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3799 | dotlockClose, /* xClose method */ |
| 3800 | dotlockLock, /* xLock method */ |
| 3801 | dotlockUnlock, /* xUnlock method */ |
| 3802 | dotlockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3803 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3804 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3805 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3806 | IOMETHODS( |
| 3807 | flockIoFinder, /* Finder function name */ |
| 3808 | flockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3809 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3810 | flockClose, /* xClose method */ |
| 3811 | flockLock, /* xLock method */ |
| 3812 | flockUnlock, /* xUnlock method */ |
| 3813 | flockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3814 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3815 | #endif |
| 3816 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3817 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3818 | IOMETHODS( |
| 3819 | semIoFinder, /* Finder function name */ |
| 3820 | semIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3821 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3822 | semClose, /* xClose method */ |
| 3823 | semLock, /* xLock method */ |
| 3824 | semUnlock, /* xUnlock method */ |
| 3825 | semCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3826 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3827 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3828 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3829 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3830 | IOMETHODS( |
| 3831 | afpIoFinder, /* Finder function name */ |
| 3832 | afpIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3833 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3834 | afpClose, /* xClose method */ |
| 3835 | afpLock, /* xLock method */ |
| 3836 | afpUnlock, /* xUnlock method */ |
| 3837 | afpCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3838 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3839 | #endif |
| 3840 | |
| 3841 | /* |
| 3842 | ** The proxy locking method is a "super-method" in the sense that it |
| 3843 | ** opens secondary file descriptors for the conch and lock files and |
| 3844 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 3845 | ** secondary files. For this reason, the division that implements |
| 3846 | ** proxy locking is located much further down in the file. But we need |
| 3847 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 3848 | ** for proxy locking here. So we forward declare the I/O methods. |
| 3849 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3850 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3851 | static int proxyClose(sqlite3_file*); |
| 3852 | static int proxyLock(sqlite3_file*, int); |
| 3853 | static int proxyUnlock(sqlite3_file*, int); |
| 3854 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3855 | IOMETHODS( |
| 3856 | proxyIoFinder, /* Finder function name */ |
| 3857 | proxyIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3858 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3859 | proxyClose, /* xClose method */ |
| 3860 | proxyLock, /* xLock method */ |
| 3861 | proxyUnlock, /* xUnlock method */ |
| 3862 | proxyCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3863 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3864 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3865 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3866 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 3867 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 3868 | IOMETHODS( |
| 3869 | nfsIoFinder, /* Finder function name */ |
| 3870 | nfsIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3871 | 1, /* shared memory is disabled */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3872 | unixClose, /* xClose method */ |
| 3873 | unixLock, /* xLock method */ |
| 3874 | nfsUnlock, /* xUnlock method */ |
| 3875 | unixCheckReservedLock /* xCheckReservedLock method */ |
| 3876 | ) |
| 3877 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3878 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3879 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3880 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3881 | ** This "finder" function attempts to determine the best locking strategy |
| 3882 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3883 | ** object that implements that strategy. |
| 3884 | ** |
| 3885 | ** This is for MacOSX only. |
| 3886 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3887 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3888 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3889 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3890 | ){ |
| 3891 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3892 | const char *zFilesystem; /* Filesystem type name */ |
| 3893 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3894 | } aMap[] = { |
| 3895 | { "hfs", &posixIoMethods }, |
| 3896 | { "ufs", &posixIoMethods }, |
| 3897 | { "afpfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3898 | { "smbfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3899 | { "webdav", &nolockIoMethods }, |
| 3900 | { 0, 0 } |
| 3901 | }; |
| 3902 | int i; |
| 3903 | struct statfs fsInfo; |
| 3904 | struct flock lockInfo; |
| 3905 | |
| 3906 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3907 | /* If filePath==NULL that means we are dealing with a transient file |
| 3908 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3909 | return &nolockIoMethods; |
| 3910 | } |
| 3911 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 3912 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 3913 | return &nolockIoMethods; |
| 3914 | } |
| 3915 | for(i=0; aMap[i].zFilesystem; i++){ |
| 3916 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 3917 | return aMap[i].pMethods; |
| 3918 | } |
| 3919 | } |
| 3920 | } |
| 3921 | |
| 3922 | /* Default case. Handles, amongst others, "nfs". |
| 3923 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 3924 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3925 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3926 | lockInfo.l_len = 1; |
| 3927 | lockInfo.l_start = 0; |
| 3928 | lockInfo.l_whence = SEEK_SET; |
| 3929 | lockInfo.l_type = F_RDLCK; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3930 | if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3931 | if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 3932 | return &nfsIoMethods; |
| 3933 | } else { |
| 3934 | return &posixIoMethods; |
| 3935 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3936 | }else{ |
| 3937 | return &dotlockIoMethods; |
| 3938 | } |
| 3939 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3940 | static const sqlite3_io_methods |
| 3941 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3942 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3943 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3944 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3945 | #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE |
| 3946 | /* |
| 3947 | ** This "finder" function attempts to determine the best locking strategy |
| 3948 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
| 3949 | ** object that implements that strategy. |
| 3950 | ** |
| 3951 | ** This is for VXWorks only. |
| 3952 | */ |
| 3953 | static const sqlite3_io_methods *autolockIoFinderImpl( |
| 3954 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3955 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3956 | ){ |
| 3957 | struct flock lockInfo; |
| 3958 | |
| 3959 | if( !filePath ){ |
| 3960 | /* If filePath==NULL that means we are dealing with a transient file |
| 3961 | ** that does not need to be locked. */ |
| 3962 | return &nolockIoMethods; |
| 3963 | } |
| 3964 | |
| 3965 | /* Test if fcntl() is supported and use POSIX style locks. |
| 3966 | ** Otherwise fall back to the named semaphore method. |
| 3967 | */ |
| 3968 | lockInfo.l_len = 1; |
| 3969 | lockInfo.l_start = 0; |
| 3970 | lockInfo.l_whence = SEEK_SET; |
| 3971 | lockInfo.l_type = F_RDLCK; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3972 | if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3973 | return &posixIoMethods; |
| 3974 | }else{ |
| 3975 | return &semIoMethods; |
| 3976 | } |
| 3977 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3978 | static const sqlite3_io_methods |
| 3979 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3980 | |
| 3981 | #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */ |
| 3982 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3983 | /* |
| 3984 | ** An abstract type for a pointer to a IO method finder function: |
| 3985 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3986 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3987 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3988 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3989 | /**************************************************************************** |
| 3990 | **************************** sqlite3_vfs methods **************************** |
| 3991 | ** |
| 3992 | ** This division contains the implementation of methods on the |
| 3993 | ** sqlite3_vfs object. |
| 3994 | */ |
| 3995 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3996 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3997 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3998 | */ |
| 3999 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4000 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4001 | int h, /* Open file descriptor of file being opened */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4002 | int dirfd, /* Directory file descriptor */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4003 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4004 | const char *zFilename, /* Name of the file being opened */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4005 | int noLock, /* Omit locking if true */ |
| 4006 | int isDelete /* Delete on close if true */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4007 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4008 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4009 | unixFile *pNew = (unixFile *)pId; |
| 4010 | int rc = SQLITE_OK; |
| 4011 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4012 | assert( pNew->pInode==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4013 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4014 | /* Parameter isDelete is only used on vxworks. Express this explicitly |
| 4015 | ** here to prevent compiler warnings about unused parameters. |
danielk1977 | a03396a | 2008-11-19 14:35:46 +0000 | [diff] [blame] | 4016 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4017 | UNUSED_PARAMETER(isDelete); |
danielk1977 | a03396a | 2008-11-19 14:35:46 +0000 | [diff] [blame] | 4018 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4019 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4020 | pNew->h = h; |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4021 | pNew->dirfd = dirfd; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4022 | pNew->fileFlags = 0; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4023 | assert( zFilename==0 || zFilename[0]=='/' ); /* Never a relative pathname */ |
| 4024 | pNew->zPath = zFilename; |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 4025 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4026 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 4027 | pNew->pId = vxworksFindFileId(zFilename); |
| 4028 | if( pNew->pId==0 ){ |
| 4029 | noLock = 1; |
| 4030 | rc = SQLITE_NOMEM; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4031 | } |
| 4032 | #endif |
| 4033 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4034 | if( noLock ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4035 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4036 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4037 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4038 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4039 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 4040 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 4041 | ** zFilename remains valid until file is closed, to support */ |
| 4042 | pNew->lockingContext = (void*)zFilename; |
| 4043 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4044 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4045 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4046 | if( pLockingStyle == &posixIoMethods |
| 4047 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 4048 | || pLockingStyle == &nfsIoMethods |
| 4049 | #endif |
| 4050 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4051 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4052 | rc = findInodeInfo(pNew, &pNew->pInode); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4053 | if( rc!=SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4054 | /* If an error occured in findInodeInfo(), close the file descriptor |
| 4055 | ** immediately, before releasing the mutex. findInodeInfo() may fail |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4056 | ** in two scenarios: |
| 4057 | ** |
| 4058 | ** (a) A call to fstat() failed. |
| 4059 | ** (b) A malloc failed. |
| 4060 | ** |
| 4061 | ** Scenario (b) may only occur if the process is holding no other |
| 4062 | ** file descriptors open on the same file. If there were other file |
| 4063 | ** descriptors on this file, then no malloc would be required by |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4064 | ** findInodeInfo(). If this is the case, it is quite safe to close |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4065 | ** handle h - as it is guaranteed that no posix locks will be released |
| 4066 | ** by doing so. |
| 4067 | ** |
| 4068 | ** If scenario (a) caused the error then things are not so safe. The |
| 4069 | ** implicit assumption here is that if fstat() fails, things are in |
| 4070 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 4071 | */ |
| 4072 | close(h); |
| 4073 | h = -1; |
| 4074 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4075 | unixLeaveMutex(); |
| 4076 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4077 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4078 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 4079 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4080 | /* AFP locking uses the file path so it needs to be included in |
| 4081 | ** the afpLockingContext. |
| 4082 | */ |
| 4083 | afpLockingContext *pCtx; |
| 4084 | pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 4085 | if( pCtx==0 ){ |
| 4086 | rc = SQLITE_NOMEM; |
| 4087 | }else{ |
| 4088 | /* NB: zFilename exists and remains valid until the file is closed |
| 4089 | ** according to requirement F11141. So we do not need to make a |
| 4090 | ** copy of the filename. */ |
| 4091 | pCtx->dbPath = zFilename; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4092 | pCtx->reserved = 0; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4093 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4094 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4095 | rc = findInodeInfo(pNew, &pNew->pInode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4096 | if( rc!=SQLITE_OK ){ |
| 4097 | sqlite3_free(pNew->lockingContext); |
| 4098 | close(h); |
| 4099 | h = -1; |
| 4100 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4101 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4102 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4103 | } |
| 4104 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4105 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4106 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 4107 | /* Dotfile locking uses the file path so it needs to be included in |
| 4108 | ** the dotlockLockingContext |
| 4109 | */ |
| 4110 | char *zLockFile; |
| 4111 | int nFilename; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4112 | nFilename = (int)strlen(zFilename) + 6; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4113 | zLockFile = (char *)sqlite3_malloc(nFilename); |
| 4114 | if( zLockFile==0 ){ |
| 4115 | rc = SQLITE_NOMEM; |
| 4116 | }else{ |
| 4117 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4118 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4119 | pNew->lockingContext = zLockFile; |
| 4120 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4121 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4122 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4123 | else if( pLockingStyle == &semIoMethods ){ |
| 4124 | /* Named semaphore locking uses the file path so it needs to be |
| 4125 | ** included in the semLockingContext |
| 4126 | */ |
| 4127 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4128 | rc = findInodeInfo(pNew, &pNew->pInode); |
| 4129 | if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 4130 | char *zSemName = pNew->pInode->aSemName; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4131 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 4132 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4133 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 4134 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4135 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4136 | pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 4137 | if( pNew->pInode->pSem == SEM_FAILED ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4138 | rc = SQLITE_NOMEM; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4139 | pNew->pInode->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4140 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4141 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4142 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4143 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4144 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 4145 | |
| 4146 | pNew->lastErrno = 0; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4147 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4148 | if( rc!=SQLITE_OK ){ |
drh | 309e655 | 2010-02-05 18:00:26 +0000 | [diff] [blame] | 4149 | if( h>=0 ) close(h); |
| 4150 | h = -1; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4151 | unlink(zFilename); |
| 4152 | isDelete = 0; |
| 4153 | } |
| 4154 | pNew->isDelete = isDelete; |
| 4155 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4156 | if( rc!=SQLITE_OK ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4157 | if( dirfd>=0 ) close(dirfd); /* silent leak if fail, already in error */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4158 | if( h>=0 ) close(h); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4159 | }else{ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4160 | pNew->pMethod = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4161 | OpenCounter(+1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4162 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4163 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 4164 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 4165 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4166 | /* |
| 4167 | ** Open a file descriptor to the directory containing file zFilename. |
| 4168 | ** If successful, *pFd is set to the opened file descriptor and |
| 4169 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 4170 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 4171 | ** value. |
| 4172 | ** |
| 4173 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 4174 | ** the file descriptor *pFd using close(). |
| 4175 | */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4176 | static int openDirectory(const char *zFilename, int *pFd){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4177 | int ii; |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 4178 | int fd = -1; |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 4179 | char zDirname[MAX_PATHNAME+1]; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4180 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4181 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
drh | 617634e | 2009-01-08 14:36:20 +0000 | [diff] [blame] | 4182 | for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4183 | if( ii>0 ){ |
| 4184 | zDirname[ii] = '\0'; |
| 4185 | fd = open(zDirname, O_RDONLY|O_BINARY, 0); |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 4186 | if( fd>=0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4187 | #ifdef FD_CLOEXEC |
| 4188 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 4189 | #endif |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4190 | OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4191 | } |
| 4192 | } |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4193 | *pFd = fd; |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 4194 | return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN_BKPT); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4197 | /* |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4198 | ** Return the name of a directory in which to put temporary files. |
| 4199 | ** If no suitable temporary file directory can be found, return NULL. |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4200 | */ |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4201 | static const char *unixTempFileDir(void){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4202 | static const char *azDirs[] = { |
| 4203 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4204 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4205 | "/var/tmp", |
| 4206 | "/usr/tmp", |
| 4207 | "/tmp", |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4208 | 0 /* List terminator */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4209 | }; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4210 | unsigned int i; |
| 4211 | struct stat buf; |
| 4212 | const char *zDir = 0; |
| 4213 | |
| 4214 | azDirs[0] = sqlite3_temp_directory; |
| 4215 | if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); |
drh | 19515c8 | 2010-06-19 23:53:11 +0000 | [diff] [blame] | 4216 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){ |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4217 | if( zDir==0 ) continue; |
| 4218 | if( stat(zDir, &buf) ) continue; |
| 4219 | if( !S_ISDIR(buf.st_mode) ) continue; |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4220 | if( access(zDir, 07) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4221 | break; |
| 4222 | } |
| 4223 | return zDir; |
| 4224 | } |
| 4225 | |
| 4226 | /* |
| 4227 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 4228 | ** by the calling process and must be big enough to hold at least |
| 4229 | ** pVfs->mxPathname bytes. |
| 4230 | */ |
| 4231 | static int unixGetTempname(int nBuf, char *zBuf){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4232 | static const unsigned char zChars[] = |
| 4233 | "abcdefghijklmnopqrstuvwxyz" |
| 4234 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 4235 | "0123456789"; |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 4236 | unsigned int i, j; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4237 | const char *zDir; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4238 | |
| 4239 | /* It's odd to simulate an io-error here, but really this is just |
| 4240 | ** using the io-error infrastructure to test that SQLite handles this |
| 4241 | ** function failing. |
| 4242 | */ |
| 4243 | SimulateIOError( return SQLITE_IOERR ); |
| 4244 | |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4245 | zDir = unixTempFileDir(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4246 | if( zDir==0 ) zDir = "."; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4247 | |
| 4248 | /* Check that the output buffer is large enough for the temporary file |
| 4249 | ** name. If it is not, return SQLITE_ERROR. |
| 4250 | */ |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 4251 | if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4252 | return SQLITE_ERROR; |
| 4253 | } |
| 4254 | |
| 4255 | do{ |
| 4256 | sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4257 | j = (int)strlen(zBuf); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4258 | sqlite3_randomness(15, &zBuf[j]); |
| 4259 | for(i=0; i<15; i++, j++){ |
| 4260 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 4261 | } |
| 4262 | zBuf[j] = 0; |
| 4263 | }while( access(zBuf,0)==0 ); |
| 4264 | return SQLITE_OK; |
| 4265 | } |
| 4266 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4267 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 4268 | /* |
| 4269 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 4270 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 4271 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 4272 | */ |
| 4273 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 4274 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 4275 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4276 | /* |
| 4277 | ** Search for an unused file descriptor that was opened on the database |
| 4278 | ** file (not a journal or master-journal file) identified by pathname |
| 4279 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 4280 | ** argument to this function. |
| 4281 | ** |
| 4282 | ** Such a file descriptor may exist if a database connection was closed |
| 4283 | ** but the associated file descriptor could not be closed because some |
| 4284 | ** other file descriptor open on the same file is holding a file-lock. |
| 4285 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 4286 | ** describing "Posix Advisory Locking" at the start of this file for |
| 4287 | ** further details. Also, ticket #4018. |
| 4288 | ** |
| 4289 | ** If a suitable file descriptor is found, then it is returned. If no |
| 4290 | ** such file descriptor is located, -1 is returned. |
| 4291 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4292 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 4293 | UnixUnusedFd *pUnused = 0; |
| 4294 | |
| 4295 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 4296 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 4297 | ** but because no way to test it is currently available. It is better |
| 4298 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 4299 | ** feature. */ |
| 4300 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4301 | struct stat sStat; /* Results of stat() call */ |
| 4302 | |
| 4303 | /* A stat() call may fail for various reasons. If this happens, it is |
| 4304 | ** almost certain that an open() call on the same path will also fail. |
| 4305 | ** For this reason, if an error occurs in the stat() call here, it is |
| 4306 | ** ignored and -1 is returned. The caller will try to open a new file |
| 4307 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 4308 | ** |
| 4309 | ** Even if a subsequent open() call does succeed, the consequences of |
| 4310 | ** not searching for a resusable file descriptor are not dire. */ |
| 4311 | if( 0==stat(zPath, &sStat) ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4312 | unixInodeInfo *pInode; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4313 | |
| 4314 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4315 | pInode = inodeList; |
| 4316 | while( pInode && (pInode->fileId.dev!=sStat.st_dev |
| 4317 | || pInode->fileId.ino!=sStat.st_ino) ){ |
| 4318 | pInode = pInode->pNext; |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 4319 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4320 | if( pInode ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4321 | UnixUnusedFd **pp; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4322 | for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4323 | pUnused = *pp; |
| 4324 | if( pUnused ){ |
| 4325 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4326 | } |
| 4327 | } |
| 4328 | unixLeaveMutex(); |
| 4329 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4330 | #endif /* if !OS_VXWORKS */ |
| 4331 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4332 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4333 | |
| 4334 | /* |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4335 | ** This function is called by unixOpen() to determine the unix permissions |
drh | f65bc91 | 2010-07-14 20:51:34 +0000 | [diff] [blame] | 4336 | ** to create new files with. If no error occurs, then SQLITE_OK is returned |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4337 | ** and a value suitable for passing as the third argument to open(2) is |
| 4338 | ** written to *pMode. If an IO error occurs, an SQLite error code is |
| 4339 | ** returned and the value of *pMode is not modified. |
| 4340 | ** |
| 4341 | ** If the file being opened is a temporary file, it is always created with |
| 4342 | ** the octal permissions 0600 (read/writable by owner only). If the file |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4343 | ** is a database or master journal file, it is created with the permissions |
| 4344 | ** mask SQLITE_DEFAULT_FILE_PERMISSIONS. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4345 | ** |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4346 | ** Finally, if the file being opened is a WAL or regular journal file, then |
| 4347 | ** this function queries the file-system for the permissions on the |
| 4348 | ** corresponding database file and sets *pMode to this value. Whenever |
| 4349 | ** possible, WAL and journal files are created using the same permissions |
| 4350 | ** as the associated database file. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4351 | */ |
| 4352 | static int findCreateFileMode( |
| 4353 | const char *zPath, /* Path of file (possibly) being created */ |
| 4354 | int flags, /* Flags passed as 4th argument to xOpen() */ |
| 4355 | mode_t *pMode /* OUT: Permissions to open file with */ |
| 4356 | ){ |
| 4357 | int rc = SQLITE_OK; /* Return Code */ |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4358 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4359 | char zDb[MAX_PATHNAME+1]; /* Database file path */ |
| 4360 | int nDb; /* Number of valid bytes in zDb */ |
| 4361 | struct stat sStat; /* Output of stat() on database file */ |
| 4362 | |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4363 | nDb = sqlite3Strlen30(zPath) - ((flags & SQLITE_OPEN_WAL) ? 4 : 8); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4364 | memcpy(zDb, zPath, nDb); |
| 4365 | zDb[nDb] = '\0'; |
| 4366 | if( 0==stat(zDb, &sStat) ){ |
| 4367 | *pMode = sStat.st_mode & 0777; |
| 4368 | }else{ |
| 4369 | rc = SQLITE_IOERR_FSTAT; |
| 4370 | } |
| 4371 | }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 4372 | *pMode = 0600; |
| 4373 | }else{ |
| 4374 | *pMode = SQLITE_DEFAULT_FILE_PERMISSIONS; |
| 4375 | } |
| 4376 | return rc; |
| 4377 | } |
| 4378 | |
| 4379 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4380 | ** Open the file zPath. |
| 4381 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4382 | ** Previously, the SQLite OS layer used three functions in place of this |
| 4383 | ** one: |
| 4384 | ** |
| 4385 | ** sqlite3OsOpenReadWrite(); |
| 4386 | ** sqlite3OsOpenReadOnly(); |
| 4387 | ** sqlite3OsOpenExclusive(); |
| 4388 | ** |
| 4389 | ** These calls correspond to the following combinations of flags: |
| 4390 | ** |
| 4391 | ** ReadWrite() -> (READWRITE | CREATE) |
| 4392 | ** ReadOnly() -> (READONLY) |
| 4393 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 4394 | ** |
| 4395 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 4396 | ** true, the file was configured to be automatically deleted when the |
| 4397 | ** file handle closed. To achieve the same effect using this new |
| 4398 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 4399 | ** OpenExclusive(). |
| 4400 | */ |
| 4401 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4402 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 4403 | const char *zPath, /* Pathname of file to be opened */ |
| 4404 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 4405 | int flags, /* Input flags to control the opening */ |
| 4406 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4407 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4408 | unixFile *p = (unixFile *)pFile; |
| 4409 | int fd = -1; /* File descriptor returned by open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4410 | int dirfd = -1; /* Directory file descriptor */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4411 | int openFlags = 0; /* Flags to pass to open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4412 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4413 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4414 | int rc = SQLITE_OK; /* Function Return Code */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4415 | |
| 4416 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 4417 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 4418 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 4419 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 4420 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4421 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4422 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 4423 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4424 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4425 | /* If creating a master or main-file journal, this function will open |
| 4426 | ** a file-descriptor on the directory too. The first time unixSync() |
| 4427 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 4428 | */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4429 | int isOpenDirectory = (isCreate && ( |
| 4430 | eType==SQLITE_OPEN_MASTER_JOURNAL |
| 4431 | || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 4432 | || eType==SQLITE_OPEN_WAL |
| 4433 | )); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4434 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4435 | /* If argument zPath is a NULL pointer, this function is required to open |
| 4436 | ** a temporary file. Use this buffer to store the file name in. |
| 4437 | */ |
| 4438 | char zTmpname[MAX_PATHNAME+1]; |
| 4439 | const char *zName = zPath; |
| 4440 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4441 | /* Check the following statements are true: |
| 4442 | ** |
| 4443 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 4444 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 4445 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4446 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4447 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4448 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4449 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4450 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4451 | assert(isDelete==0 || isCreate); |
| 4452 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4453 | /* The main DB, main journal, WAL file and master journal are never |
| 4454 | ** automatically deleted. Nor are they ever temporary files. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4455 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 4456 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 4457 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4458 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4459 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4460 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 4461 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 4462 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 4463 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4464 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4465 | ); |
| 4466 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4467 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4468 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4469 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4470 | UnixUnusedFd *pUnused; |
| 4471 | pUnused = findReusableFd(zName, flags); |
| 4472 | if( pUnused ){ |
| 4473 | fd = pUnused->fd; |
| 4474 | }else{ |
dan | 6aa657f | 2009-08-24 18:57:58 +0000 | [diff] [blame] | 4475 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4476 | if( !pUnused ){ |
| 4477 | return SQLITE_NOMEM; |
| 4478 | } |
| 4479 | } |
| 4480 | p->pUnused = pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4481 | }else if( !zName ){ |
| 4482 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4483 | assert(isDelete && !isOpenDirectory); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4484 | rc = unixGetTempname(MAX_PATHNAME+1, zTmpname); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4485 | if( rc!=SQLITE_OK ){ |
| 4486 | return rc; |
| 4487 | } |
| 4488 | zName = zTmpname; |
| 4489 | } |
| 4490 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4491 | /* Determine the value of the flags parameter passed to POSIX function |
| 4492 | ** open(). These must be calculated even if open() is not called, as |
| 4493 | ** they may be stored as part of the file handle and used by the |
| 4494 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4495 | if( isReadonly ) openFlags |= O_RDONLY; |
| 4496 | if( isReadWrite ) openFlags |= O_RDWR; |
| 4497 | if( isCreate ) openFlags |= O_CREAT; |
| 4498 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 4499 | openFlags |= (O_LARGEFILE|O_BINARY); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4500 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4501 | if( fd<0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4502 | mode_t openMode; /* Permissions to create file with */ |
| 4503 | rc = findCreateFileMode(zName, flags, &openMode); |
| 4504 | if( rc!=SQLITE_OK ){ |
| 4505 | assert( !p->pUnused ); |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4506 | assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4507 | return rc; |
| 4508 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4509 | fd = open(zName, openFlags, openMode); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4510 | OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4511 | if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ |
| 4512 | /* Failed to open the file for read/write access. Try read-only. */ |
| 4513 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4514 | openFlags &= ~(O_RDWR|O_CREAT); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4515 | flags |= SQLITE_OPEN_READONLY; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4516 | openFlags |= O_RDONLY; |
| 4517 | fd = open(zName, openFlags, openMode); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4518 | } |
| 4519 | if( fd<0 ){ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 4520 | rc = SQLITE_CANTOPEN_BKPT; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4521 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4522 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4523 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4524 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4525 | if( pOutFlags ){ |
| 4526 | *pOutFlags = flags; |
| 4527 | } |
| 4528 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4529 | if( p->pUnused ){ |
| 4530 | p->pUnused->fd = fd; |
| 4531 | p->pUnused->flags = flags; |
| 4532 | } |
| 4533 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4534 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4535 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4536 | zPath = zName; |
| 4537 | #else |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4538 | unlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4539 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4540 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 4541 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4542 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4543 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 4544 | } |
| 4545 | #endif |
| 4546 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4547 | if( isOpenDirectory ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4548 | rc = openDirectory(zPath, &dirfd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4549 | if( rc!=SQLITE_OK ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4550 | /* It is safe to close fd at this point, because it is guaranteed not |
| 4551 | ** 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] | 4552 | ** it would not be safe to close as this would release any locks held |
| 4553 | ** on the file by this process. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4554 | assert( eType!=SQLITE_OPEN_MAIN_DB ); |
| 4555 | close(fd); /* silently leak if fail, already in error */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4556 | goto open_finished; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4557 | } |
| 4558 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4559 | |
| 4560 | #ifdef FD_CLOEXEC |
| 4561 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 4562 | #endif |
| 4563 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4564 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4565 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4566 | |
| 4567 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 4568 | struct statfs fsInfo; |
| 4569 | if( fstatfs(fd, &fsInfo) == -1 ){ |
| 4570 | ((unixFile*)pFile)->lastErrno = errno; |
| 4571 | if( dirfd>=0 ) close(dirfd); /* silently leak if fail, in error */ |
| 4572 | close(fd); /* silently leak if fail, in error */ |
| 4573 | return SQLITE_IOERR_ACCESS; |
| 4574 | } |
| 4575 | if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 4576 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 4577 | } |
| 4578 | #endif |
| 4579 | |
| 4580 | #if SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4581 | #if SQLITE_PREFER_PROXY_LOCKING |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4582 | isAutoProxy = 1; |
| 4583 | #endif |
| 4584 | if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4585 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 4586 | int useProxy = 0; |
| 4587 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4588 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 4589 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4590 | if( envforce!=NULL ){ |
| 4591 | useProxy = atoi(envforce)>0; |
| 4592 | }else{ |
| 4593 | struct statfs fsInfo; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4594 | if( statfs(zPath, &fsInfo) == -1 ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4595 | /* In theory, the close(fd) call is sub-optimal. If the file opened |
| 4596 | ** with fd is a database file, and there are other connections open |
| 4597 | ** on that file that are currently holding advisory locks on it, |
| 4598 | ** then the call to close() will cancel those locks. In practice, |
| 4599 | ** we're assuming that statfs() doesn't fail very often. At least |
| 4600 | ** not while other file descriptors opened by the same process on |
| 4601 | ** the same file are working. */ |
| 4602 | p->lastErrno = errno; |
| 4603 | if( dirfd>=0 ){ |
| 4604 | close(dirfd); /* silently leak if fail, in error */ |
| 4605 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4606 | close(fd); /* silently leak if fail, in error */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4607 | rc = SQLITE_IOERR_ACCESS; |
| 4608 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4609 | } |
| 4610 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 4611 | } |
| 4612 | if( useProxy ){ |
| 4613 | rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete); |
| 4614 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4615 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4616 | if( rc!=SQLITE_OK ){ |
| 4617 | /* Use unixClose to clean up the resources added in fillInUnixFile |
| 4618 | ** and clear all the structure's references. Specifically, |
| 4619 | ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 4620 | */ |
| 4621 | unixClose(pFile); |
| 4622 | return rc; |
| 4623 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4624 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4625 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4626 | } |
| 4627 | } |
| 4628 | #endif |
| 4629 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4630 | rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete); |
| 4631 | open_finished: |
| 4632 | if( rc!=SQLITE_OK ){ |
| 4633 | sqlite3_free(p->pUnused); |
| 4634 | } |
| 4635 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4636 | } |
| 4637 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4638 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4639 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4640 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 4641 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4642 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4643 | static int unixDelete( |
| 4644 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 4645 | const char *zPath, /* Name of file to be deleted */ |
| 4646 | int dirSync /* If true, fsync() directory after deleting file */ |
| 4647 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4648 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4649 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4650 | SimulateIOError(return SQLITE_IOERR_DELETE); |
drh | 5d4feff | 2010-07-14 01:45:22 +0000 | [diff] [blame] | 4651 | if( unlink(zPath)==(-1) && errno!=ENOENT ){ |
| 4652 | return SQLITE_IOERR_DELETE; |
| 4653 | } |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 4654 | #ifndef SQLITE_DISABLE_DIRSYNC |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4655 | if( dirSync ){ |
| 4656 | int fd; |
| 4657 | rc = openDirectory(zPath, &fd); |
| 4658 | if( rc==SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4659 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4660 | if( fsync(fd)==-1 ) |
| 4661 | #else |
| 4662 | if( fsync(fd) ) |
| 4663 | #endif |
| 4664 | { |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4665 | rc = SQLITE_IOERR_DIR_FSYNC; |
| 4666 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4667 | if( close(fd)&&!rc ){ |
| 4668 | rc = SQLITE_IOERR_DIR_CLOSE; |
| 4669 | } |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4670 | } |
| 4671 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 4672 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4673 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4674 | } |
| 4675 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4676 | /* |
| 4677 | ** Test the existance of or access permissions of file zPath. The |
| 4678 | ** test performed depends on the value of flags: |
| 4679 | ** |
| 4680 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 4681 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 4682 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 4683 | ** |
| 4684 | ** Otherwise return 0. |
| 4685 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4686 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4687 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 4688 | const char *zPath, /* Path of the file to examine */ |
| 4689 | int flags, /* What do we want to learn about the zPath file? */ |
| 4690 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4691 | ){ |
rse | 25c0d1a | 2007-09-20 08:38:14 +0000 | [diff] [blame] | 4692 | int amode = 0; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4693 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4694 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4695 | switch( flags ){ |
| 4696 | case SQLITE_ACCESS_EXISTS: |
| 4697 | amode = F_OK; |
| 4698 | break; |
| 4699 | case SQLITE_ACCESS_READWRITE: |
| 4700 | amode = W_OK|R_OK; |
| 4701 | break; |
drh | 50d3f90 | 2007-08-27 21:10:36 +0000 | [diff] [blame] | 4702 | case SQLITE_ACCESS_READ: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4703 | amode = R_OK; |
| 4704 | break; |
| 4705 | |
| 4706 | default: |
| 4707 | assert(!"Invalid flags argument"); |
| 4708 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4709 | *pResOut = (access(zPath, amode)==0); |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 4710 | if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){ |
| 4711 | struct stat buf; |
| 4712 | if( 0==stat(zPath, &buf) && buf.st_size==0 ){ |
| 4713 | *pResOut = 0; |
| 4714 | } |
| 4715 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4716 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4717 | } |
| 4718 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4719 | |
| 4720 | /* |
| 4721 | ** Turn a relative pathname into a full pathname. The relative path |
| 4722 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 4723 | ** zPath. |
| 4724 | ** |
| 4725 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 4726 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 4727 | ** this buffer before returning. |
| 4728 | */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 4729 | static int unixFullPathname( |
| 4730 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 4731 | const char *zPath, /* Possibly relative input path */ |
| 4732 | int nOut, /* Size of output buffer in bytes */ |
| 4733 | char *zOut /* Output buffer */ |
| 4734 | ){ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 4735 | |
| 4736 | /* It's odd to simulate an io-error here, but really this is just |
| 4737 | ** using the io-error infrastructure to test that SQLite handles this |
| 4738 | ** function failing. This function could fail if, for example, the |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4739 | ** current working directory has been unlinked. |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 4740 | */ |
| 4741 | SimulateIOError( return SQLITE_ERROR ); |
| 4742 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4743 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 4744 | UNUSED_PARAMETER(pVfs); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4745 | |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4746 | zOut[nOut-1] = '\0'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4747 | if( zPath[0]=='/' ){ |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4748 | sqlite3_snprintf(nOut, zOut, "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4749 | }else{ |
| 4750 | int nCwd; |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4751 | if( getcwd(zOut, nOut-1)==0 ){ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 4752 | return SQLITE_CANTOPEN_BKPT; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4753 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4754 | nCwd = (int)strlen(zOut); |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4755 | sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4756 | } |
| 4757 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4758 | } |
| 4759 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 4760 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4761 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 4762 | /* |
| 4763 | ** Interfaces for opening a shared library, finding entry points |
| 4764 | ** within the shared library, and closing the shared library. |
| 4765 | */ |
| 4766 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4767 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 4768 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4769 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 4770 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 4771 | |
| 4772 | /* |
| 4773 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 4774 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 4775 | ** message is available, it is written to zBufOut. If no error message |
| 4776 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 4777 | ** error message. |
| 4778 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4779 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4780 | char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4781 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4782 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4783 | zErr = dlerror(); |
| 4784 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4785 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4786 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4787 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4788 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4789 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 4790 | /* |
| 4791 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 4792 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 4793 | ** returns a void* which is really a pointer to a function. So how do we |
| 4794 | ** use dlsym() with -pedantic-errors? |
| 4795 | ** |
| 4796 | ** Variable x below is defined to be a pointer to a function taking |
| 4797 | ** parameters void* and const char* and returning a pointer to a function. |
| 4798 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 4799 | ** (That assignment requires a cast.) Then we call the function that |
| 4800 | ** x points to. |
| 4801 | ** |
| 4802 | ** This work-around is unlikely to work correctly on any system where |
| 4803 | ** you really cannot cast a function pointer into void*. But then, on the |
| 4804 | ** other hand, dlsym() will not work on such a system either, so we have |
| 4805 | ** not really lost anything. |
| 4806 | */ |
| 4807 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4808 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4809 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 4810 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4811 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4812 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 4813 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4814 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4815 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4816 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 4817 | #define unixDlOpen 0 |
| 4818 | #define unixDlError 0 |
| 4819 | #define unixDlSym 0 |
| 4820 | #define unixDlClose 0 |
| 4821 | #endif |
| 4822 | |
| 4823 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4824 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4825 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4826 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 4827 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 4828 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4829 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4830 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 4831 | ** errors. The reports issued by valgrind are incorrect - we would |
| 4832 | ** prefer that the randomness be increased by making use of the |
| 4833 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 4834 | ** some users. Rather than argue, it seems easier just to initialize |
| 4835 | ** the whole array and silence valgrind, even if that means less randomness |
| 4836 | ** in the random seed. |
| 4837 | ** |
| 4838 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 4839 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4840 | ** tests repeatable. |
| 4841 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4842 | memset(zBuf, 0, nBuf); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4843 | #if !defined(SQLITE_TEST) |
| 4844 | { |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 4845 | int pid, fd; |
| 4846 | fd = open("/dev/urandom", O_RDONLY); |
| 4847 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 4848 | time_t t; |
| 4849 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4850 | memcpy(zBuf, &t, sizeof(t)); |
| 4851 | pid = getpid(); |
| 4852 | memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid)); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 4853 | assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf ); |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 4854 | nBuf = sizeof(t) + sizeof(pid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 4855 | }else{ |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 4856 | nBuf = read(fd, zBuf, nBuf); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 4857 | close(fd); |
| 4858 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4859 | } |
| 4860 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 4861 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4862 | } |
| 4863 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4864 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4865 | /* |
| 4866 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4867 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 4868 | ** The return value is the number of microseconds of sleep actually |
| 4869 | ** requested from the underlying operating system, a number which |
| 4870 | ** might be greater than or equal to the argument, but not less |
| 4871 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4872 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4873 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4874 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4875 | struct timespec sp; |
| 4876 | |
| 4877 | sp.tv_sec = microseconds / 1000000; |
| 4878 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 4879 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 4880 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4881 | return microseconds; |
| 4882 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4883 | usleep(microseconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 4884 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4885 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4886 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4887 | int seconds = (microseconds+999999)/1000000; |
| 4888 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 4889 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 4890 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 4891 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
| 4894 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4895 | ** The following variable, if set to a non-zero value, is interpreted as |
| 4896 | ** the number of seconds since 1970 and is used to set the result of |
| 4897 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4898 | */ |
| 4899 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4900 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4901 | #endif |
| 4902 | |
| 4903 | /* |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 4904 | ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 4905 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 4906 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 4907 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 4908 | ** proleptic Gregorian calendar. |
| 4909 | ** |
| 4910 | ** On success, return 0. Return 1 if the time and date cannot be found. |
| 4911 | */ |
| 4912 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 4913 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
| 4914 | #if defined(NO_GETTOD) |
| 4915 | time_t t; |
| 4916 | time(&t); |
| 4917 | *piNow = ((sqlite3_int64)i)*1000 + unixEpoch; |
| 4918 | #elif OS_VXWORKS |
| 4919 | struct timespec sNow; |
| 4920 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 4921 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 4922 | #else |
| 4923 | struct timeval sNow; |
| 4924 | gettimeofday(&sNow, 0); |
| 4925 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
| 4926 | #endif |
| 4927 | |
| 4928 | #ifdef SQLITE_TEST |
| 4929 | if( sqlite3_current_time ){ |
| 4930 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 4931 | } |
| 4932 | #endif |
| 4933 | UNUSED_PARAMETER(NotUsed); |
| 4934 | return 0; |
| 4935 | } |
| 4936 | |
| 4937 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4938 | ** Find the current time (in Universal Coordinated Time). Write the |
| 4939 | ** current time and date as a Julian Day number into *prNow and |
| 4940 | ** return 0. Return 1 if the time and date cannot be found. |
| 4941 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4942 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 4943 | sqlite3_int64 i; |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 4944 | UNUSED_PARAMETER(NotUsed); |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 4945 | unixCurrentTimeInt64(0, &i); |
drh | 0dcb0a7 | 2010-05-03 18:22:52 +0000 | [diff] [blame] | 4946 | *prNow = i/86400000.0; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4947 | return 0; |
| 4948 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4949 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4950 | /* |
| 4951 | ** We added the xGetLastError() method with the intention of providing |
| 4952 | ** better low-level error messages when operating-system problems come up |
| 4953 | ** during SQLite operation. But so far, none of that has been implemented |
| 4954 | ** in the core. So this routine is never called. For now, it is merely |
| 4955 | ** a place-holder. |
| 4956 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4957 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 4958 | UNUSED_PARAMETER(NotUsed); |
| 4959 | UNUSED_PARAMETER(NotUsed2); |
| 4960 | UNUSED_PARAMETER(NotUsed3); |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 4961 | return 0; |
| 4962 | } |
| 4963 | |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 4964 | |
| 4965 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4966 | ************************ End of sqlite3_vfs methods *************************** |
| 4967 | ******************************************************************************/ |
| 4968 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4969 | /****************************************************************************** |
| 4970 | ************************** Begin Proxy Locking ******************************** |
| 4971 | ** |
| 4972 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 4973 | ** other locking methods on secondary lock files. Proxy locking is a |
| 4974 | ** meta-layer over top of the primitive locking implemented above. For |
| 4975 | ** this reason, the division that implements of proxy locking is deferred |
| 4976 | ** until late in the file (here) after all of the other I/O methods have |
| 4977 | ** been defined - so that the primitive locking methods are available |
| 4978 | ** as services to help with the implementation of proxy locking. |
| 4979 | ** |
| 4980 | **** |
| 4981 | ** |
| 4982 | ** The default locking schemes in SQLite use byte-range locks on the |
| 4983 | ** database file to coordinate safe, concurrent access by multiple readers |
| 4984 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 4985 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 4986 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 4987 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 4988 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 4989 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 4990 | ** address in the shared range is taken for a SHARED lock, the entire |
| 4991 | ** shared range is taken for an EXCLUSIVE lock): |
| 4992 | ** |
| 4993 | ** PENDING_BYTE 0x40000000 |
| 4994 | ** RESERVED_BYTE 0x40000001 |
| 4995 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 4996 | ** |
| 4997 | ** This works well on the local file system, but shows a nearly 100x |
| 4998 | ** slowdown in read performance on AFP because the AFP client disables |
| 4999 | ** the read cache when byte-range locks are present. Enabling the read |
| 5000 | ** cache exposes a cache coherency problem that is present on all OS X |
| 5001 | ** supported network file systems. NFS and AFP both observe the |
| 5002 | ** close-to-open semantics for ensuring cache coherency |
| 5003 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 5004 | ** address the requirements for concurrent database access by multiple |
| 5005 | ** readers and writers |
| 5006 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 5007 | ** |
| 5008 | ** To address the performance and cache coherency issues, proxy file locking |
| 5009 | ** changes the way database access is controlled by limiting access to a |
| 5010 | ** single host at a time and moving file locks off of the database file |
| 5011 | ** and onto a proxy file on the local file system. |
| 5012 | ** |
| 5013 | ** |
| 5014 | ** Using proxy locks |
| 5015 | ** ----------------- |
| 5016 | ** |
| 5017 | ** C APIs |
| 5018 | ** |
| 5019 | ** sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE, |
| 5020 | ** <proxy_path> | ":auto:"); |
| 5021 | ** sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>); |
| 5022 | ** |
| 5023 | ** |
| 5024 | ** SQL pragmas |
| 5025 | ** |
| 5026 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 5027 | ** PRAGMA [database.]lock_proxy_file |
| 5028 | ** |
| 5029 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 5030 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 5031 | ** a proxy path based on the user's temp dir |
| 5032 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 5033 | ** actual proxy file name is generated from the name and path of the |
| 5034 | ** database file. For example: |
| 5035 | ** |
| 5036 | ** For database path "/Users/me/foo.db" |
| 5037 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 5038 | ** |
| 5039 | ** Once a lock proxy is configured for a database connection, it can not |
| 5040 | ** be removed, however it may be switched to a different proxy path via |
| 5041 | ** the above APIs (assuming the conch file is not being held by another |
| 5042 | ** connection or process). |
| 5043 | ** |
| 5044 | ** |
| 5045 | ** How proxy locking works |
| 5046 | ** ----------------------- |
| 5047 | ** |
| 5048 | ** Proxy file locking relies primarily on two new supporting files: |
| 5049 | ** |
| 5050 | ** * conch file to limit access to the database file to a single host |
| 5051 | ** at a time |
| 5052 | ** |
| 5053 | ** * proxy file to act as a proxy for the advisory locks normally |
| 5054 | ** taken on the database |
| 5055 | ** |
| 5056 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 5057 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 5058 | ** contents and comparing the host's unique host ID (see below) and lock |
| 5059 | ** proxy path against the values stored in the conch. The conch file is |
| 5060 | ** stored in the same directory as the database file and the file name |
| 5061 | ** is patterned after the database file name as ".<databasename>-conch". |
| 5062 | ** If the conch file does not exist, or it's contents do not match the |
| 5063 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 5064 | ** lock and the conch file contents is updated with the host ID and proxy |
| 5065 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 5066 | ** is held by another process (with a shared lock), the exclusive lock |
| 5067 | ** will fail and SQLITE_BUSY is returned. |
| 5068 | ** |
| 5069 | ** The proxy file - a single-byte file used for all advisory file locks |
| 5070 | ** normally taken on the database file. This allows for safe sharing |
| 5071 | ** of the database file for multiple readers and writers on the same |
| 5072 | ** host (the conch ensures that they all use the same local lock file). |
| 5073 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5074 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 5075 | ** only taken when the first request to lock database file is made. |
| 5076 | ** This matches the semantics of the traditional locking behavior, where |
| 5077 | ** opening a connection to a database file does not take a lock on it. |
| 5078 | ** The shared lock and an open file descriptor are maintained until |
| 5079 | ** the connection to the database is closed. |
| 5080 | ** |
| 5081 | ** The proxy file and the lock file are never deleted so they only need |
| 5082 | ** to be created the first time they are used. |
| 5083 | ** |
| 5084 | ** Configuration options |
| 5085 | ** --------------------- |
| 5086 | ** |
| 5087 | ** SQLITE_PREFER_PROXY_LOCKING |
| 5088 | ** |
| 5089 | ** Database files accessed on non-local file systems are |
| 5090 | ** automatically configured for proxy locking, lock files are |
| 5091 | ** named automatically using the same logic as |
| 5092 | ** PRAGMA lock_proxy_file=":auto:" |
| 5093 | ** |
| 5094 | ** SQLITE_PROXY_DEBUG |
| 5095 | ** |
| 5096 | ** Enables the logging of error messages during host id file |
| 5097 | ** retrieval and creation |
| 5098 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5099 | ** LOCKPROXYDIR |
| 5100 | ** |
| 5101 | ** Overrides the default directory used for lock proxy files that |
| 5102 | ** are named automatically via the ":auto:" setting |
| 5103 | ** |
| 5104 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 5105 | ** |
| 5106 | ** Permissions to use when creating a directory for storing the |
| 5107 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 5108 | ** |
| 5109 | ** |
| 5110 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 5111 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 5112 | ** force proxy locking to be used for every database file opened, and 0 |
| 5113 | ** will force automatic proxy locking to be disabled for all database |
| 5114 | ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or |
| 5115 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 5116 | */ |
| 5117 | |
| 5118 | /* |
| 5119 | ** Proxy locking is only available on MacOSX |
| 5120 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5121 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5122 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5123 | /* |
| 5124 | ** The proxyLockingContext has the path and file structures for the remote |
| 5125 | ** and local proxy files in it |
| 5126 | */ |
| 5127 | typedef struct proxyLockingContext proxyLockingContext; |
| 5128 | struct proxyLockingContext { |
| 5129 | unixFile *conchFile; /* Open conch file */ |
| 5130 | char *conchFilePath; /* Name of the conch file */ |
| 5131 | unixFile *lockProxy; /* Open proxy lock file */ |
| 5132 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 5133 | char *dbPath; /* Name of the open file */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5134 | int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5135 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 5136 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 5137 | }; |
| 5138 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5139 | /* |
| 5140 | ** The proxy lock file path for the database at dbPath is written into lPath, |
| 5141 | ** which must point to valid, writable memory large enough for a maxLen length |
| 5142 | ** file path. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5143 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5144 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 5145 | int len; |
| 5146 | int dbLen; |
| 5147 | int i; |
| 5148 | |
| 5149 | #ifdef LOCKPROXYDIR |
| 5150 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 5151 | #else |
| 5152 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 5153 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5154 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5155 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
| 5156 | lPath, errno, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5157 | return SQLITE_IOERR_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5158 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5159 | len = strlcat(lPath, "sqliteplocks", maxLen); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5160 | } |
| 5161 | # else |
| 5162 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 5163 | # endif |
| 5164 | #endif |
| 5165 | |
| 5166 | if( lPath[len-1]!='/' ){ |
| 5167 | len = strlcat(lPath, "/", maxLen); |
| 5168 | } |
| 5169 | |
| 5170 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5171 | dbLen = (int)strlen(dbPath); |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5172 | for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5173 | char c = dbPath[i]; |
| 5174 | lPath[i+len] = (c=='/')?'_':c; |
| 5175 | } |
| 5176 | lPath[i+len]='\0'; |
| 5177 | strlcat(lPath, ":auto:", maxLen); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5178 | OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5179 | return SQLITE_OK; |
| 5180 | } |
| 5181 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5182 | /* |
| 5183 | ** Creates the lock file and any missing directories in lockPath |
| 5184 | */ |
| 5185 | static int proxyCreateLockPath(const char *lockPath){ |
| 5186 | int i, len; |
| 5187 | char buf[MAXPATHLEN]; |
| 5188 | int start = 0; |
| 5189 | |
| 5190 | assert(lockPath!=NULL); |
| 5191 | /* try to create all the intermediate directories */ |
| 5192 | len = (int)strlen(lockPath); |
| 5193 | buf[0] = lockPath[0]; |
| 5194 | for( i=1; i<len; i++ ){ |
| 5195 | if( lockPath[i] == '/' && (i - start > 0) ){ |
| 5196 | /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 5197 | if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 5198 | || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 5199 | buf[i]='\0'; |
| 5200 | if( mkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
| 5201 | int err=errno; |
| 5202 | if( err!=EEXIST ) { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5203 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5204 | "'%s' proxy lock path=%s pid=%d\n", |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5205 | buf, strerror(err), lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5206 | return err; |
| 5207 | } |
| 5208 | } |
| 5209 | } |
| 5210 | start=i+1; |
| 5211 | } |
| 5212 | buf[i] = lockPath[i]; |
| 5213 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5214 | OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5215 | return 0; |
| 5216 | } |
| 5217 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5218 | /* |
| 5219 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 5220 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 5221 | ** |
| 5222 | ** The caller is responsible not only for closing the file descriptor |
| 5223 | ** but also for freeing the memory associated with the file descriptor. |
| 5224 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5225 | static int proxyCreateUnixFile( |
| 5226 | const char *path, /* path for the new unixFile */ |
| 5227 | unixFile **ppFile, /* unixFile created and returned by ref */ |
| 5228 | int islockfile /* if non zero missing dirs will be created */ |
| 5229 | ) { |
| 5230 | int fd = -1; |
| 5231 | int dirfd = -1; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5232 | unixFile *pNew; |
| 5233 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5234 | int openFlags = O_RDWR | O_CREAT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5235 | sqlite3_vfs dummyVfs; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5236 | int terrno = 0; |
| 5237 | UnixUnusedFd *pUnused = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5238 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5239 | /* 1. first try to open/create the file |
| 5240 | ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 5241 | ** the parent directories and then try again. |
| 5242 | ** 3. if that fails, try to open the file read-only |
| 5243 | ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 5244 | */ |
| 5245 | pUnused = findReusableFd(path, openFlags); |
| 5246 | if( pUnused ){ |
| 5247 | fd = pUnused->fd; |
| 5248 | }else{ |
| 5249 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
| 5250 | if( !pUnused ){ |
| 5251 | return SQLITE_NOMEM; |
| 5252 | } |
| 5253 | } |
| 5254 | if( fd<0 ){ |
| 5255 | fd = open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5256 | terrno = errno; |
| 5257 | if( fd<0 && errno==ENOENT && islockfile ){ |
| 5258 | if( proxyCreateLockPath(path) == SQLITE_OK ){ |
| 5259 | fd = open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5260 | } |
| 5261 | } |
| 5262 | } |
| 5263 | if( fd<0 ){ |
| 5264 | openFlags = O_RDONLY; |
| 5265 | fd = open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5266 | terrno = errno; |
| 5267 | } |
| 5268 | if( fd<0 ){ |
| 5269 | if( islockfile ){ |
| 5270 | return SQLITE_BUSY; |
| 5271 | } |
| 5272 | switch (terrno) { |
| 5273 | case EACCES: |
| 5274 | return SQLITE_PERM; |
| 5275 | case EIO: |
| 5276 | return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 5277 | default: |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 5278 | return SQLITE_CANTOPEN_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5279 | } |
| 5280 | } |
| 5281 | |
| 5282 | pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew)); |
| 5283 | if( pNew==NULL ){ |
| 5284 | rc = SQLITE_NOMEM; |
| 5285 | goto end_create_proxy; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5286 | } |
| 5287 | memset(pNew, 0, sizeof(unixFile)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5288 | pNew->openFlags = openFlags; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5289 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5290 | pUnused->fd = fd; |
| 5291 | pUnused->flags = openFlags; |
| 5292 | pNew->pUnused = pUnused; |
| 5293 | |
| 5294 | rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0); |
| 5295 | if( rc==SQLITE_OK ){ |
| 5296 | *ppFile = pNew; |
| 5297 | return SQLITE_OK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5298 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5299 | end_create_proxy: |
| 5300 | close(fd); /* silently leak fd if error, we're already in error */ |
| 5301 | sqlite3_free(pNew); |
| 5302 | sqlite3_free(pUnused); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5303 | return rc; |
| 5304 | } |
| 5305 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5306 | #ifdef SQLITE_TEST |
| 5307 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 5308 | int sqlite3_hostid_num = 0; |
| 5309 | #endif |
| 5310 | |
| 5311 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 5312 | |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5313 | /* Not always defined in the headers as it ought to be */ |
| 5314 | extern int gethostuuid(uuid_t id, const struct timespec *wait); |
| 5315 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5316 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 5317 | ** bytes of writable memory. |
| 5318 | */ |
| 5319 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
| 5320 | struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
| 5321 | |
| 5322 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 5323 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
| 5324 | if( gethostuuid(pHostID, &timeout) ){ |
| 5325 | int err = errno; |
| 5326 | if( pError ){ |
| 5327 | *pError = err; |
| 5328 | } |
| 5329 | return SQLITE_IOERR; |
| 5330 | } |
| 5331 | #ifdef SQLITE_TEST |
| 5332 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 5333 | if( sqlite3_hostid_num != 0){ |
| 5334 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 5335 | } |
| 5336 | #endif |
| 5337 | |
| 5338 | return SQLITE_OK; |
| 5339 | } |
| 5340 | |
| 5341 | /* The conch file contains the header, host id and lock file path |
| 5342 | */ |
| 5343 | #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 5344 | #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 5345 | #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 5346 | #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 5347 | |
| 5348 | /* |
| 5349 | ** Takes an open conch file, copies the contents to a new path and then moves |
| 5350 | ** it back. The newly created file's file descriptor is assigned to the |
| 5351 | ** conch file structure and finally the original conch file descriptor is |
| 5352 | ** closed. Returns zero if successful. |
| 5353 | */ |
| 5354 | static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 5355 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5356 | unixFile *conchFile = pCtx->conchFile; |
| 5357 | char tPath[MAXPATHLEN]; |
| 5358 | char buf[PROXY_MAXCONCHLEN]; |
| 5359 | char *cPath = pCtx->conchFilePath; |
| 5360 | size_t readLen = 0; |
| 5361 | size_t pathLen = 0; |
| 5362 | char errmsg[64] = ""; |
| 5363 | int fd = -1; |
| 5364 | int rc = -1; |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5365 | UNUSED_PARAMETER(myHostID); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5366 | |
| 5367 | /* create a new path by replace the trailing '-conch' with '-break' */ |
| 5368 | pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 5369 | if( pathLen>MAXPATHLEN || pathLen<6 || |
| 5370 | (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
| 5371 | sprintf(errmsg, "path error (len %d)", (int)pathLen); |
| 5372 | goto end_breaklock; |
| 5373 | } |
| 5374 | /* read the conch content */ |
| 5375 | readLen = pread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
| 5376 | if( readLen<PROXY_PATHINDEX ){ |
| 5377 | sprintf(errmsg, "read error (len %d)", (int)readLen); |
| 5378 | goto end_breaklock; |
| 5379 | } |
| 5380 | /* write it out to the temporary break file */ |
| 5381 | fd = open(tPath, (O_RDWR|O_CREAT|O_EXCL), SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5382 | if( fd<0 ){ |
| 5383 | sprintf(errmsg, "create failed (%d)", errno); |
| 5384 | goto end_breaklock; |
| 5385 | } |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5386 | if( pwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5387 | sprintf(errmsg, "write failed (%d)", errno); |
| 5388 | goto end_breaklock; |
| 5389 | } |
| 5390 | if( rename(tPath, cPath) ){ |
| 5391 | sprintf(errmsg, "rename failed (%d)", errno); |
| 5392 | goto end_breaklock; |
| 5393 | } |
| 5394 | rc = 0; |
| 5395 | fprintf(stderr, "broke stale lock on %s\n", cPath); |
| 5396 | close(conchFile->h); |
| 5397 | conchFile->h = fd; |
| 5398 | conchFile->openFlags = O_RDWR | O_CREAT; |
| 5399 | |
| 5400 | end_breaklock: |
| 5401 | if( rc ){ |
| 5402 | if( fd>=0 ){ |
| 5403 | unlink(tPath); |
| 5404 | close(fd); |
| 5405 | } |
| 5406 | fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 5407 | } |
| 5408 | return rc; |
| 5409 | } |
| 5410 | |
| 5411 | /* Take the requested lock on the conch file and break a stale lock if the |
| 5412 | ** host id matches. |
| 5413 | */ |
| 5414 | static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 5415 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5416 | unixFile *conchFile = pCtx->conchFile; |
| 5417 | int rc = SQLITE_OK; |
| 5418 | int nTries = 0; |
| 5419 | struct timespec conchModTime; |
| 5420 | |
| 5421 | do { |
| 5422 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 5423 | nTries ++; |
| 5424 | if( rc==SQLITE_BUSY ){ |
| 5425 | /* If the lock failed (busy): |
| 5426 | * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 5427 | * 2nd try: fail if the mod time changed or host id is different, wait |
| 5428 | * 10 sec and try again |
| 5429 | * 3rd try: break the lock unless the mod time has changed. |
| 5430 | */ |
| 5431 | struct stat buf; |
| 5432 | if( fstat(conchFile->h, &buf) ){ |
| 5433 | pFile->lastErrno = errno; |
| 5434 | return SQLITE_IOERR_LOCK; |
| 5435 | } |
| 5436 | |
| 5437 | if( nTries==1 ){ |
| 5438 | conchModTime = buf.st_mtimespec; |
| 5439 | usleep(500000); /* wait 0.5 sec and try the lock again*/ |
| 5440 | continue; |
| 5441 | } |
| 5442 | |
| 5443 | assert( nTries>1 ); |
| 5444 | if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 5445 | conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 5446 | return SQLITE_BUSY; |
| 5447 | } |
| 5448 | |
| 5449 | if( nTries==2 ){ |
| 5450 | char tBuf[PROXY_MAXCONCHLEN]; |
| 5451 | int len = pread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
| 5452 | if( len<0 ){ |
| 5453 | pFile->lastErrno = errno; |
| 5454 | return SQLITE_IOERR_LOCK; |
| 5455 | } |
| 5456 | if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 5457 | /* don't break the lock if the host id doesn't match */ |
| 5458 | if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 5459 | return SQLITE_BUSY; |
| 5460 | } |
| 5461 | }else{ |
| 5462 | /* don't break the lock on short read or a version mismatch */ |
| 5463 | return SQLITE_BUSY; |
| 5464 | } |
| 5465 | usleep(10000000); /* wait 10 sec and try the lock again */ |
| 5466 | continue; |
| 5467 | } |
| 5468 | |
| 5469 | assert( nTries==3 ); |
| 5470 | if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 5471 | rc = SQLITE_OK; |
| 5472 | if( lockType==EXCLUSIVE_LOCK ){ |
| 5473 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 5474 | } |
| 5475 | if( !rc ){ |
| 5476 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 5477 | } |
| 5478 | } |
| 5479 | } |
| 5480 | } while( rc==SQLITE_BUSY && nTries<3 ); |
| 5481 | |
| 5482 | return rc; |
| 5483 | } |
| 5484 | |
| 5485 | /* 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] | 5486 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 5487 | ** lockPath means that the lockPath in the conch file will be used if the |
| 5488 | ** host IDs match, or a new lock path will be generated automatically |
| 5489 | ** and written to the conch file. |
| 5490 | */ |
| 5491 | static int proxyTakeConch(unixFile *pFile){ |
| 5492 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5493 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5494 | if( pCtx->conchHeld!=0 ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5495 | return SQLITE_OK; |
| 5496 | }else{ |
| 5497 | unixFile *conchFile = pCtx->conchFile; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5498 | uuid_t myHostID; |
| 5499 | int pError = 0; |
| 5500 | char readBuf[PROXY_MAXCONCHLEN]; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5501 | char lockPath[MAXPATHLEN]; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5502 | char *tempLockPath = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5503 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5504 | int createConch = 0; |
| 5505 | int hostIdMatch = 0; |
| 5506 | int readLen = 0; |
| 5507 | int tryOldLockPath = 0; |
| 5508 | int forceNewLockPath = 0; |
| 5509 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5510 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
| 5511 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5512 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5513 | rc = proxyGetHostID(myHostID, &pError); |
| 5514 | if( (rc&0xff)==SQLITE_IOERR ){ |
| 5515 | pFile->lastErrno = pError; |
| 5516 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5517 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5518 | rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5519 | if( rc!=SQLITE_OK ){ |
| 5520 | goto end_takeconch; |
| 5521 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5522 | /* read the existing conch file */ |
| 5523 | readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 5524 | if( readLen<0 ){ |
| 5525 | /* I/O error: lastErrno set by seekAndRead */ |
| 5526 | pFile->lastErrno = conchFile->lastErrno; |
| 5527 | rc = SQLITE_IOERR_READ; |
| 5528 | goto end_takeconch; |
| 5529 | }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 5530 | readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 5531 | /* a short read or version format mismatch means we need to create a new |
| 5532 | ** conch file. |
| 5533 | */ |
| 5534 | createConch = 1; |
| 5535 | } |
| 5536 | /* if the host id matches and the lock path already exists in the conch |
| 5537 | ** we'll try to use the path there, if we can't open that path, we'll |
| 5538 | ** retry with a new auto-generated path |
| 5539 | */ |
| 5540 | do { /* in case we need to try again for an :auto: named lock file */ |
| 5541 | |
| 5542 | if( !createConch && !forceNewLockPath ){ |
| 5543 | hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 5544 | PROXY_HOSTIDLEN); |
| 5545 | /* if the conch has data compare the contents */ |
| 5546 | if( !pCtx->lockProxyPath ){ |
| 5547 | /* for auto-named local lock file, just check the host ID and we'll |
| 5548 | ** use the local lock file path that's already in there |
| 5549 | */ |
| 5550 | if( hostIdMatch ){ |
| 5551 | size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 5552 | |
| 5553 | if( pathLen>=MAXPATHLEN ){ |
| 5554 | pathLen=MAXPATHLEN-1; |
| 5555 | } |
| 5556 | memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 5557 | lockPath[pathLen] = 0; |
| 5558 | tempLockPath = lockPath; |
| 5559 | tryOldLockPath = 1; |
| 5560 | /* create a copy of the lock path if the conch is taken */ |
| 5561 | goto end_takeconch; |
| 5562 | } |
| 5563 | }else if( hostIdMatch |
| 5564 | && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 5565 | readLen-PROXY_PATHINDEX) |
| 5566 | ){ |
| 5567 | /* conch host and lock path match */ |
| 5568 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5569 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
| 5572 | /* if the conch isn't writable and doesn't match, we can't take it */ |
| 5573 | if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 5574 | rc = SQLITE_BUSY; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5575 | goto end_takeconch; |
| 5576 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5577 | |
| 5578 | /* 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] | 5579 | if( !pCtx->lockProxyPath ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5580 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 5581 | tempLockPath = lockPath; |
| 5582 | /* create a copy of the lock path _only_ if the conch is taken */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5583 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5584 | |
| 5585 | /* update conch with host and path (this will fail if other process |
| 5586 | ** has a shared lock already), if the host id matches, use the big |
| 5587 | ** stick. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5588 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5589 | futimes(conchFile->h, NULL); |
| 5590 | if( hostIdMatch && !createConch ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5591 | if( conchFile->pInode && conchFile->pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5592 | /* We are trying for an exclusive lock but another thread in this |
| 5593 | ** same process is still holding a shared lock. */ |
| 5594 | rc = SQLITE_BUSY; |
| 5595 | } else { |
| 5596 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5597 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5598 | }else{ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5599 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5600 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5601 | if( rc==SQLITE_OK ){ |
| 5602 | char writeBuffer[PROXY_MAXCONCHLEN]; |
| 5603 | int writeSize = 0; |
| 5604 | |
| 5605 | writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 5606 | memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 5607 | if( pCtx->lockProxyPath!=NULL ){ |
| 5608 | strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN); |
| 5609 | }else{ |
| 5610 | strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 5611 | } |
| 5612 | writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
| 5613 | ftruncate(conchFile->h, writeSize); |
| 5614 | rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
| 5615 | fsync(conchFile->h); |
| 5616 | /* If we created a new conch file (not just updated the contents of a |
| 5617 | ** valid conch file), try to match the permissions of the database |
| 5618 | */ |
| 5619 | if( rc==SQLITE_OK && createConch ){ |
| 5620 | struct stat buf; |
| 5621 | int err = fstat(pFile->h, &buf); |
| 5622 | if( err==0 ){ |
| 5623 | mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 5624 | S_IROTH|S_IWOTH); |
| 5625 | /* try to match the database file R/W permissions, ignore failure */ |
| 5626 | #ifndef SQLITE_PROXY_DEBUG |
| 5627 | fchmod(conchFile->h, cmode); |
| 5628 | #else |
| 5629 | if( fchmod(conchFile->h, cmode)!=0 ){ |
| 5630 | int code = errno; |
| 5631 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 5632 | cmode, code, strerror(code)); |
| 5633 | } else { |
| 5634 | fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 5635 | } |
| 5636 | }else{ |
| 5637 | int code = errno; |
| 5638 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 5639 | err, code, strerror(code)); |
| 5640 | #endif |
| 5641 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5642 | } |
| 5643 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5644 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 5645 | |
| 5646 | end_takeconch: |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5647 | OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5648 | if( rc==SQLITE_OK && pFile->openFlags ){ |
| 5649 | if( pFile->h>=0 ){ |
| 5650 | #ifdef STRICT_CLOSE_ERROR |
| 5651 | if( close(pFile->h) ){ |
| 5652 | pFile->lastErrno = errno; |
| 5653 | return SQLITE_IOERR_CLOSE; |
| 5654 | } |
| 5655 | #else |
| 5656 | close(pFile->h); /* silently leak fd if fail */ |
| 5657 | #endif |
| 5658 | } |
| 5659 | pFile->h = -1; |
| 5660 | int fd = open(pCtx->dbPath, pFile->openFlags, |
| 5661 | SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5662 | OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5663 | if( fd>=0 ){ |
| 5664 | pFile->h = fd; |
| 5665 | }else{ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 5666 | rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5667 | during locking */ |
| 5668 | } |
| 5669 | } |
| 5670 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 5671 | char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 5672 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 5673 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 5674 | /* we couldn't create the proxy lock file with the old lock file path |
| 5675 | ** so try again via auto-naming |
| 5676 | */ |
| 5677 | forceNewLockPath = 1; |
| 5678 | tryOldLockPath = 0; |
dan | 2b0ef47 | 2010-02-16 12:18:47 +0000 | [diff] [blame] | 5679 | continue; /* go back to the do {} while start point, try again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5680 | } |
| 5681 | } |
| 5682 | if( rc==SQLITE_OK ){ |
| 5683 | /* Need to make a copy of path if we extracted the value |
| 5684 | ** from the conch file or the path was allocated on the stack |
| 5685 | */ |
| 5686 | if( tempLockPath ){ |
| 5687 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 5688 | if( !pCtx->lockProxyPath ){ |
| 5689 | rc = SQLITE_NOMEM; |
| 5690 | } |
| 5691 | } |
| 5692 | } |
| 5693 | if( rc==SQLITE_OK ){ |
| 5694 | pCtx->conchHeld = 1; |
| 5695 | |
| 5696 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 5697 | afpLockingContext *afpCtx; |
| 5698 | afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 5699 | afpCtx->dbPath = pCtx->lockProxyPath; |
| 5700 | } |
| 5701 | } else { |
| 5702 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 5703 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5704 | OSTRACE(("TAKECONCH %d %s\n", conchFile->h, |
| 5705 | rc==SQLITE_OK?"ok":"failed")); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5706 | return rc; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5707 | } while (1); /* in case we need to retry the :auto: lock file - |
| 5708 | ** we should never get here except via the 'continue' call. */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5709 | } |
| 5710 | } |
| 5711 | |
| 5712 | /* |
| 5713 | ** If pFile holds a lock on a conch file, then release that lock. |
| 5714 | */ |
| 5715 | static int proxyReleaseConch(unixFile *pFile){ |
drh | 1c5bb4d | 2010-05-10 17:29:28 +0000 | [diff] [blame] | 5716 | int rc = SQLITE_OK; /* Subroutine return code */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5717 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 5718 | unixFile *conchFile; /* Name of the conch file */ |
| 5719 | |
| 5720 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5721 | conchFile = pCtx->conchFile; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5722 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5723 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5724 | getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5725 | if( pCtx->conchHeld>0 ){ |
| 5726 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 5727 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5728 | pCtx->conchHeld = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5729 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| 5730 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5731 | return rc; |
| 5732 | } |
| 5733 | |
| 5734 | /* |
| 5735 | ** Given the name of a database file, compute the name of its conch file. |
| 5736 | ** Store the conch filename in memory obtained from sqlite3_malloc(). |
| 5737 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 5738 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 5739 | ** |
| 5740 | ** The caller is responsible for ensuring that the allocated memory |
| 5741 | ** space is eventually freed. |
| 5742 | ** |
| 5743 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 5744 | */ |
| 5745 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 5746 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5747 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5748 | char *conchPath; /* buffer in which to construct conch name */ |
| 5749 | |
| 5750 | /* Allocate space for the conch filename and initialize the name to |
| 5751 | ** the name of the original database file. */ |
| 5752 | *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8); |
| 5753 | if( conchPath==0 ){ |
| 5754 | return SQLITE_NOMEM; |
| 5755 | } |
| 5756 | memcpy(conchPath, dbPath, len+1); |
| 5757 | |
| 5758 | /* now insert a "." before the last / character */ |
| 5759 | for( i=(len-1); i>=0; i-- ){ |
| 5760 | if( conchPath[i]=='/' ){ |
| 5761 | i++; |
| 5762 | break; |
| 5763 | } |
| 5764 | } |
| 5765 | conchPath[i]='.'; |
| 5766 | while ( i<len ){ |
| 5767 | conchPath[i+1]=dbPath[i]; |
| 5768 | i++; |
| 5769 | } |
| 5770 | |
| 5771 | /* append the "-conch" suffix to the file */ |
| 5772 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5773 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5774 | |
| 5775 | return SQLITE_OK; |
| 5776 | } |
| 5777 | |
| 5778 | |
| 5779 | /* Takes a fully configured proxy locking-style unix file and switches |
| 5780 | ** the local lock file path |
| 5781 | */ |
| 5782 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 5783 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 5784 | char *oldPath = pCtx->lockProxyPath; |
| 5785 | int rc = SQLITE_OK; |
| 5786 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5787 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5788 | return SQLITE_BUSY; |
| 5789 | } |
| 5790 | |
| 5791 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 5792 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 5793 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 5794 | return SQLITE_OK; |
| 5795 | }else{ |
| 5796 | unixFile *lockProxy = pCtx->lockProxy; |
| 5797 | pCtx->lockProxy=NULL; |
| 5798 | pCtx->conchHeld = 0; |
| 5799 | if( lockProxy!=NULL ){ |
| 5800 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 5801 | if( rc ) return rc; |
| 5802 | sqlite3_free(lockProxy); |
| 5803 | } |
| 5804 | sqlite3_free(oldPath); |
| 5805 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 5806 | } |
| 5807 | |
| 5808 | return rc; |
| 5809 | } |
| 5810 | |
| 5811 | /* |
| 5812 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 5813 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 5814 | ** |
| 5815 | ** This routine find the filename associated with pFile and writes it |
| 5816 | ** int dbPath. |
| 5817 | */ |
| 5818 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5819 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5820 | if( pFile->pMethod == &afpIoMethods ){ |
| 5821 | /* afp style keeps a reference to the db path in the filePath field |
| 5822 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5823 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5824 | strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN); |
| 5825 | } else |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5826 | #endif |
| 5827 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 5828 | /* dot lock style uses the locking context to store the dot lock |
| 5829 | ** file path */ |
| 5830 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 5831 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 5832 | }else{ |
| 5833 | /* all other styles use the locking context to store the db file path */ |
| 5834 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5835 | strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5836 | } |
| 5837 | return SQLITE_OK; |
| 5838 | } |
| 5839 | |
| 5840 | /* |
| 5841 | ** Takes an already filled in unix file and alters it so all file locking |
| 5842 | ** will be performed on the local proxy lock file. The following fields |
| 5843 | ** are preserved in the locking context so that they can be restored and |
| 5844 | ** the unix structure properly cleaned up at close time: |
| 5845 | ** ->lockingContext |
| 5846 | ** ->pMethod |
| 5847 | */ |
| 5848 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 5849 | proxyLockingContext *pCtx; |
| 5850 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 5851 | char *lockPath=NULL; |
| 5852 | int rc = SQLITE_OK; |
| 5853 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5854 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5855 | return SQLITE_BUSY; |
| 5856 | } |
| 5857 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 5858 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 5859 | lockPath=NULL; |
| 5860 | }else{ |
| 5861 | lockPath=(char *)path; |
| 5862 | } |
| 5863 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5864 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
| 5865 | (lockPath ? lockPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5866 | |
| 5867 | pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 5868 | if( pCtx==0 ){ |
| 5869 | return SQLITE_NOMEM; |
| 5870 | } |
| 5871 | memset(pCtx, 0, sizeof(*pCtx)); |
| 5872 | |
| 5873 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 5874 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5875 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 5876 | if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 5877 | /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 5878 | ** (c) the file system is read-only, then enable no-locking access. |
| 5879 | ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 5880 | ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 5881 | */ |
| 5882 | struct statfs fsInfo; |
| 5883 | struct stat conchInfo; |
| 5884 | int goLockless = 0; |
| 5885 | |
| 5886 | if( stat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
| 5887 | int err = errno; |
| 5888 | if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 5889 | goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 5890 | } |
| 5891 | } |
| 5892 | if( goLockless ){ |
| 5893 | pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 5894 | rc = SQLITE_OK; |
| 5895 | } |
| 5896 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5897 | } |
| 5898 | if( rc==SQLITE_OK && lockPath ){ |
| 5899 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 5900 | } |
| 5901 | |
| 5902 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5903 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 5904 | if( pCtx->dbPath==NULL ){ |
| 5905 | rc = SQLITE_NOMEM; |
| 5906 | } |
| 5907 | } |
| 5908 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5909 | /* all memory is allocated, proxys are created and assigned, |
| 5910 | ** switch the locking context and pMethod then return. |
| 5911 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5912 | pCtx->oldLockingContext = pFile->lockingContext; |
| 5913 | pFile->lockingContext = pCtx; |
| 5914 | pCtx->pOldMethod = pFile->pMethod; |
| 5915 | pFile->pMethod = &proxyIoMethods; |
| 5916 | }else{ |
| 5917 | if( pCtx->conchFile ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5918 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5919 | sqlite3_free(pCtx->conchFile); |
| 5920 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5921 | sqlite3_free(pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5922 | sqlite3_free(pCtx->conchFilePath); |
| 5923 | sqlite3_free(pCtx); |
| 5924 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5925 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 5926 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5927 | return rc; |
| 5928 | } |
| 5929 | |
| 5930 | |
| 5931 | /* |
| 5932 | ** This routine handles sqlite3_file_control() calls that are specific |
| 5933 | ** to proxy locking. |
| 5934 | */ |
| 5935 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 5936 | switch( op ){ |
| 5937 | case SQLITE_GET_LOCKPROXYFILE: { |
| 5938 | unixFile *pFile = (unixFile*)id; |
| 5939 | if( pFile->pMethod == &proxyIoMethods ){ |
| 5940 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 5941 | proxyTakeConch(pFile); |
| 5942 | if( pCtx->lockProxyPath ){ |
| 5943 | *(const char **)pArg = pCtx->lockProxyPath; |
| 5944 | }else{ |
| 5945 | *(const char **)pArg = ":auto: (not held)"; |
| 5946 | } |
| 5947 | } else { |
| 5948 | *(const char **)pArg = NULL; |
| 5949 | } |
| 5950 | return SQLITE_OK; |
| 5951 | } |
| 5952 | case SQLITE_SET_LOCKPROXYFILE: { |
| 5953 | unixFile *pFile = (unixFile*)id; |
| 5954 | int rc = SQLITE_OK; |
| 5955 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 5956 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 5957 | if( isProxyStyle ){ |
| 5958 | /* turn off proxy locking - not supported */ |
| 5959 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 5960 | }else{ |
| 5961 | /* turn off proxy locking - already off - NOOP */ |
| 5962 | rc = SQLITE_OK; |
| 5963 | } |
| 5964 | }else{ |
| 5965 | const char *proxyPath = (const char *)pArg; |
| 5966 | if( isProxyStyle ){ |
| 5967 | proxyLockingContext *pCtx = |
| 5968 | (proxyLockingContext*)pFile->lockingContext; |
| 5969 | if( !strcmp(pArg, ":auto:") |
| 5970 | || (pCtx->lockProxyPath && |
| 5971 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 5972 | ){ |
| 5973 | rc = SQLITE_OK; |
| 5974 | }else{ |
| 5975 | rc = switchLockProxyPath(pFile, proxyPath); |
| 5976 | } |
| 5977 | }else{ |
| 5978 | /* turn on proxy file locking */ |
| 5979 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 5980 | } |
| 5981 | } |
| 5982 | return rc; |
| 5983 | } |
| 5984 | default: { |
| 5985 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 5986 | } |
| 5987 | } |
| 5988 | /*NOTREACHED*/ |
| 5989 | return SQLITE_ERROR; |
| 5990 | } |
| 5991 | |
| 5992 | /* |
| 5993 | ** Within this division (the proxying locking implementation) the procedures |
| 5994 | ** above this point are all utilities. The lock-related methods of the |
| 5995 | ** proxy-locking sqlite3_io_method object follow. |
| 5996 | */ |
| 5997 | |
| 5998 | |
| 5999 | /* |
| 6000 | ** This routine checks if there is a RESERVED lock held on the specified |
| 6001 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 6002 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 6003 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 6004 | */ |
| 6005 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 6006 | unixFile *pFile = (unixFile*)id; |
| 6007 | int rc = proxyTakeConch(pFile); |
| 6008 | if( rc==SQLITE_OK ){ |
| 6009 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6010 | if( pCtx->conchHeld>0 ){ |
| 6011 | unixFile *proxy = pCtx->lockProxy; |
| 6012 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 6013 | }else{ /* conchHeld < 0 is lockless */ |
| 6014 | pResOut=0; |
| 6015 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6016 | } |
| 6017 | return rc; |
| 6018 | } |
| 6019 | |
| 6020 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6021 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6022 | ** of the following: |
| 6023 | ** |
| 6024 | ** (1) SHARED_LOCK |
| 6025 | ** (2) RESERVED_LOCK |
| 6026 | ** (3) PENDING_LOCK |
| 6027 | ** (4) EXCLUSIVE_LOCK |
| 6028 | ** |
| 6029 | ** Sometimes when requesting one lock state, additional lock states |
| 6030 | ** are inserted in between. The locking might fail on one of the later |
| 6031 | ** transitions leaving the lock state different from what it started but |
| 6032 | ** still short of its goal. The following chart shows the allowed |
| 6033 | ** transitions and the inserted intermediate states: |
| 6034 | ** |
| 6035 | ** UNLOCKED -> SHARED |
| 6036 | ** SHARED -> RESERVED |
| 6037 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 6038 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 6039 | ** PENDING -> EXCLUSIVE |
| 6040 | ** |
| 6041 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 6042 | ** routine to lower a locking level. |
| 6043 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6044 | static int proxyLock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6045 | unixFile *pFile = (unixFile*)id; |
| 6046 | int rc = proxyTakeConch(pFile); |
| 6047 | if( rc==SQLITE_OK ){ |
| 6048 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6049 | if( pCtx->conchHeld>0 ){ |
| 6050 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6051 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); |
| 6052 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6053 | }else{ |
| 6054 | /* conchHeld < 0 is lockless */ |
| 6055 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6056 | } |
| 6057 | return rc; |
| 6058 | } |
| 6059 | |
| 6060 | |
| 6061 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6062 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6063 | ** must be either NO_LOCK or SHARED_LOCK. |
| 6064 | ** |
| 6065 | ** If the locking level of the file descriptor is already at or below |
| 6066 | ** the requested locking level, this routine is a no-op. |
| 6067 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6068 | static int proxyUnlock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6069 | unixFile *pFile = (unixFile*)id; |
| 6070 | int rc = proxyTakeConch(pFile); |
| 6071 | if( rc==SQLITE_OK ){ |
| 6072 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6073 | if( pCtx->conchHeld>0 ){ |
| 6074 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6075 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); |
| 6076 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6077 | }else{ |
| 6078 | /* conchHeld < 0 is lockless */ |
| 6079 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6080 | } |
| 6081 | return rc; |
| 6082 | } |
| 6083 | |
| 6084 | /* |
| 6085 | ** Close a file that uses proxy locks. |
| 6086 | */ |
| 6087 | static int proxyClose(sqlite3_file *id) { |
| 6088 | if( id ){ |
| 6089 | unixFile *pFile = (unixFile*)id; |
| 6090 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6091 | unixFile *lockProxy = pCtx->lockProxy; |
| 6092 | unixFile *conchFile = pCtx->conchFile; |
| 6093 | int rc = SQLITE_OK; |
| 6094 | |
| 6095 | if( lockProxy ){ |
| 6096 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 6097 | if( rc ) return rc; |
| 6098 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 6099 | if( rc ) return rc; |
| 6100 | sqlite3_free(lockProxy); |
| 6101 | pCtx->lockProxy = 0; |
| 6102 | } |
| 6103 | if( conchFile ){ |
| 6104 | if( pCtx->conchHeld ){ |
| 6105 | rc = proxyReleaseConch(pFile); |
| 6106 | if( rc ) return rc; |
| 6107 | } |
| 6108 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 6109 | if( rc ) return rc; |
| 6110 | sqlite3_free(conchFile); |
| 6111 | } |
| 6112 | sqlite3_free(pCtx->lockProxyPath); |
| 6113 | sqlite3_free(pCtx->conchFilePath); |
| 6114 | sqlite3_free(pCtx->dbPath); |
| 6115 | /* restore the original locking context and pMethod then close it */ |
| 6116 | pFile->lockingContext = pCtx->oldLockingContext; |
| 6117 | pFile->pMethod = pCtx->pOldMethod; |
| 6118 | sqlite3_free(pCtx); |
| 6119 | return pFile->pMethod->xClose(id); |
| 6120 | } |
| 6121 | return SQLITE_OK; |
| 6122 | } |
| 6123 | |
| 6124 | |
| 6125 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6126 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6127 | /* |
| 6128 | ** The proxy locking style is intended for use with AFP filesystems. |
| 6129 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 6130 | ** restricted to MacOSX. |
| 6131 | ** |
| 6132 | ** |
| 6133 | ******************* End of the proxy lock implementation ********************** |
| 6134 | ******************************************************************************/ |
| 6135 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6136 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6137 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6138 | ** |
| 6139 | ** This routine registers all VFS implementations for unix-like operating |
| 6140 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 6141 | ** should be the only routines in this file that are visible from other |
| 6142 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6143 | ** |
| 6144 | ** This routine is called once during SQLite initialization and by a |
| 6145 | ** single thread. The memory allocation and mutex subsystems have not |
| 6146 | ** necessarily been initialized when this routine is called, and so they |
| 6147 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6148 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6149 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6150 | /* |
| 6151 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6152 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 6153 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 6154 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 6155 | ** and so we have to go through the intermediate pointer to avoid problems |
| 6156 | ** when compiling with -pedantic-errors on GCC.) |
| 6157 | ** |
| 6158 | ** 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] | 6159 | ** finder-function. The finder-function returns a pointer to the |
| 6160 | ** sqlite_io_methods object that implements the desired locking |
| 6161 | ** behaviors. See the division above that contains the IOMETHODS |
| 6162 | ** macro for addition information on finder-functions. |
| 6163 | ** |
| 6164 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 6165 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 6166 | ** more than that; it looks at the filesystem type that hosts the |
| 6167 | ** database file and tries to choose an locking method appropriate for |
| 6168 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6169 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6170 | #define UNIXVFS(VFSNAME, FINDER) { \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6171 | 2, /* iVersion */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6172 | sizeof(unixFile), /* szOsFile */ \ |
| 6173 | MAX_PATHNAME, /* mxPathname */ \ |
| 6174 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6175 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6176 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6177 | unixOpen, /* xOpen */ \ |
| 6178 | unixDelete, /* xDelete */ \ |
| 6179 | unixAccess, /* xAccess */ \ |
| 6180 | unixFullPathname, /* xFullPathname */ \ |
| 6181 | unixDlOpen, /* xDlOpen */ \ |
| 6182 | unixDlError, /* xDlError */ \ |
| 6183 | unixDlSym, /* xDlSym */ \ |
| 6184 | unixDlClose, /* xDlClose */ \ |
| 6185 | unixRandomness, /* xRandomness */ \ |
| 6186 | unixSleep, /* xSleep */ \ |
| 6187 | unixCurrentTime, /* xCurrentTime */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6188 | unixGetLastError, /* xGetLastError */ \ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6189 | unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6190 | } |
| 6191 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6192 | /* |
| 6193 | ** All default VFSes for unix are contained in the following array. |
| 6194 | ** |
| 6195 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 6196 | ** by the SQLite core when the VFS is registered. So the following |
| 6197 | ** array cannot be const. |
| 6198 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6199 | static sqlite3_vfs aVfs[] = { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6200 | #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__)) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6201 | UNIXVFS("unix", autolockIoFinder ), |
| 6202 | #else |
| 6203 | UNIXVFS("unix", posixIoFinder ), |
| 6204 | #endif |
| 6205 | UNIXVFS("unix-none", nolockIoFinder ), |
| 6206 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6207 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6208 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6209 | #endif |
| 6210 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6211 | UNIXVFS("unix-posix", posixIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6212 | #if !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6213 | UNIXVFS("unix-flock", flockIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6214 | #endif |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6215 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6216 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6217 | UNIXVFS("unix-afp", afpIoFinder ), |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6218 | UNIXVFS("unix-nfs", nfsIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6219 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6220 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6221 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6222 | unsigned int i; /* Loop counter */ |
| 6223 | |
| 6224 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6225 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6226 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6227 | } |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6228 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6229 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6230 | |
| 6231 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6232 | ** Shutdown the operating system interface. |
| 6233 | ** |
| 6234 | ** Some operating systems might need to do some cleanup in this routine, |
| 6235 | ** to release dynamically allocated objects. But not on unix. |
| 6236 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6237 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6238 | int sqlite3_os_end(void){ |
| 6239 | return SQLITE_OK; |
| 6240 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 6241 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 6242 | #endif /* SQLITE_OS_UNIX */ |