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 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 739 | ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 740 | ** |
| 741 | ** The mutex entered using the unixEnterMutex() function must be held |
| 742 | ** when this function is called. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 743 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 744 | static void releaseInodeInfo(unixInodeInfo *pInode){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 745 | assert( unixMutexHeld() ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 746 | if( pInode ){ |
| 747 | pInode->nRef--; |
| 748 | if( pInode->nRef==0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 749 | assert( pInode->pShmNode==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 750 | if( pInode->pPrev ){ |
| 751 | assert( pInode->pPrev->pNext==pInode ); |
| 752 | pInode->pPrev->pNext = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 753 | }else{ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 754 | assert( inodeList==pInode ); |
| 755 | inodeList = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 756 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 757 | if( pInode->pNext ){ |
| 758 | assert( pInode->pNext->pPrev==pInode ); |
| 759 | pInode->pNext->pPrev = pInode->pPrev; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 760 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 761 | sqlite3_free(pInode); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 762 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | |
| 766 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 767 | ** Given a file descriptor, locate the unixInodeInfo object that |
| 768 | ** describes that file descriptor. Create a new one if necessary. The |
| 769 | ** return value might be uninitialized if an error occurs. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 770 | ** |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 771 | ** The mutex entered using the unixEnterMutex() function must be held |
| 772 | ** when this function is called. |
| 773 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 774 | ** Return an appropriate error code. |
| 775 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 776 | static int findInodeInfo( |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 777 | unixFile *pFile, /* Unix file with file desc used in the key */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 778 | unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 779 | ){ |
| 780 | int rc; /* System call return code */ |
| 781 | int fd; /* The file descriptor for pFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 782 | struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ |
| 783 | struct stat statbuf; /* Low-level file information */ |
| 784 | unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 785 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 786 | assert( unixMutexHeld() ); |
| 787 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 788 | /* Get low-level information about the file that we can used to |
| 789 | ** create a unique name for the file. |
| 790 | */ |
| 791 | fd = pFile->h; |
| 792 | rc = fstat(fd, &statbuf); |
| 793 | if( rc!=0 ){ |
| 794 | pFile->lastErrno = errno; |
| 795 | #ifdef EOVERFLOW |
| 796 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 797 | #endif |
| 798 | return SQLITE_IOERR; |
| 799 | } |
| 800 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 801 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 802 | /* On OS X on an msdos filesystem, the inode number is reported |
| 803 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 804 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 805 | ** we always increase the file size to 1 by writing a single byte |
| 806 | ** prior to accessing the inode number. The one byte written is |
| 807 | ** an ASCII 'S' character which also happens to be the first byte |
| 808 | ** in the header of every SQLite database. In this way, if there |
| 809 | ** is a race condition such that another thread has already populated |
| 810 | ** the first page of the database, no damage is done. |
| 811 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 812 | if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 813 | rc = write(fd, "S", 1); |
| 814 | if( rc!=1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 815 | pFile->lastErrno = errno; |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 816 | return SQLITE_IOERR; |
| 817 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 818 | rc = fstat(fd, &statbuf); |
| 819 | if( rc!=0 ){ |
| 820 | pFile->lastErrno = errno; |
| 821 | return SQLITE_IOERR; |
| 822 | } |
| 823 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 824 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 825 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 826 | memset(&fileId, 0, sizeof(fileId)); |
| 827 | fileId.dev = statbuf.st_dev; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 828 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 829 | fileId.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 830 | #else |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 831 | fileId.ino = statbuf.st_ino; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 832 | #endif |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 833 | pInode = inodeList; |
| 834 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 835 | pInode = pInode->pNext; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 836 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 837 | if( pInode==0 ){ |
| 838 | pInode = sqlite3_malloc( sizeof(*pInode) ); |
| 839 | if( pInode==0 ){ |
| 840 | return SQLITE_NOMEM; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 841 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 842 | memset(pInode, 0, sizeof(*pInode)); |
| 843 | memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
| 844 | pInode->nRef = 1; |
| 845 | pInode->pNext = inodeList; |
| 846 | pInode->pPrev = 0; |
| 847 | if( inodeList ) inodeList->pPrev = pInode; |
| 848 | inodeList = pInode; |
| 849 | }else{ |
| 850 | pInode->nRef++; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 851 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 852 | *ppInode = pInode; |
| 853 | return SQLITE_OK; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 854 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 855 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 856 | |
| 857 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 858 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 859 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 860 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 861 | ** 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] | 862 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 863 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 864 | int rc = SQLITE_OK; |
| 865 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 866 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 867 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 868 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 869 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 870 | assert( pFile ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 871 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 872 | |
| 873 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 874 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 875 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 876 | } |
| 877 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 878 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 879 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 880 | #ifndef __DJGPP__ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 881 | if( !reserved ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 882 | struct flock lock; |
| 883 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 884 | lock.l_start = RESERVED_BYTE; |
| 885 | lock.l_len = 1; |
| 886 | lock.l_type = F_WRLCK; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 887 | if (-1 == fcntl(pFile->h, F_GETLK, &lock)) { |
| 888 | int tErrno = errno; |
| 889 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 890 | pFile->lastErrno = tErrno; |
| 891 | } else if( lock.l_type!=F_UNLCK ){ |
| 892 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 893 | } |
| 894 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 895 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 896 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 897 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 898 | OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 899 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 900 | *pResOut = reserved; |
| 901 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 905 | ** Lock the file with the lock specified by parameter eFileLock - one |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 906 | ** of the following: |
| 907 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 908 | ** (1) SHARED_LOCK |
| 909 | ** (2) RESERVED_LOCK |
| 910 | ** (3) PENDING_LOCK |
| 911 | ** (4) EXCLUSIVE_LOCK |
| 912 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 913 | ** Sometimes when requesting one lock state, additional lock states |
| 914 | ** are inserted in between. The locking might fail on one of the later |
| 915 | ** transitions leaving the lock state different from what it started but |
| 916 | ** still short of its goal. The following chart shows the allowed |
| 917 | ** transitions and the inserted intermediate states: |
| 918 | ** |
| 919 | ** UNLOCKED -> SHARED |
| 920 | ** SHARED -> RESERVED |
| 921 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 922 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 923 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 924 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 925 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 926 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 927 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 928 | static int unixLock(sqlite3_file *id, int eFileLock){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 929 | /* The following describes the implementation of the various locks and |
| 930 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 931 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 932 | ** confusion with SQLite lock names). The algorithms are complicated |
| 933 | ** slightly in order to be compatible with windows systems simultaneously |
| 934 | ** accessing the same database file, in case that is ever required. |
| 935 | ** |
| 936 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 937 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 938 | ** range', a range of 510 bytes at a well known offset. |
| 939 | ** |
| 940 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 941 | ** byte'. If this is successful, a random byte from the 'shared byte |
| 942 | ** range' is read-locked and the lock on the 'pending byte' released. |
| 943 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 944 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 945 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 946 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 947 | ** |
| 948 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 949 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 950 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 951 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 952 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 953 | ** This property is used by the algorithm for rolling back a journal file |
| 954 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 955 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 956 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 957 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 958 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 959 | ** within this range, this ensures that no other locks are held on the |
| 960 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 961 | ** |
| 962 | ** The reason a single byte cannot be used instead of the 'shared byte |
| 963 | ** range' is that some versions of windows do not support read-locks. By |
| 964 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 965 | ** even if the locking primitive used is always a write-lock. |
| 966 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 967 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 968 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 969 | unixInodeInfo *pInode = pFile->pInode; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 970 | struct flock lock; |
drh | 3f02218 | 2009-09-09 16:10:50 +0000 | [diff] [blame] | 971 | int s = 0; |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 972 | int tErrno = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 973 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 974 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 975 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 976 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 977 | azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 978 | |
| 979 | /* 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] | 980 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 981 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 982 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 983 | if( pFile->eFileLock>=eFileLock ){ |
| 984 | OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
| 985 | azFileLock(eFileLock))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 986 | return SQLITE_OK; |
| 987 | } |
| 988 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 989 | /* Make sure the locking sequence is correct. |
| 990 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 991 | ** (2) SQLite never explicitly requests a pendig lock. |
| 992 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 993 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 994 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 995 | assert( eFileLock!=PENDING_LOCK ); |
| 996 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 997 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 998 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 999 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1000 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1001 | pInode = pFile->pInode; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1002 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1003 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1004 | ** handle that precludes the requested lock, return BUSY. |
| 1005 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1006 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 1007 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1008 | ){ |
| 1009 | rc = SQLITE_BUSY; |
| 1010 | goto end_lock; |
| 1011 | } |
| 1012 | |
| 1013 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1014 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1015 | ** return SQLITE_OK. |
| 1016 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1017 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1018 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1019 | assert( eFileLock==SHARED_LOCK ); |
| 1020 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1021 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1022 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1023 | pInode->nShared++; |
| 1024 | pInode->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1025 | goto end_lock; |
| 1026 | } |
| 1027 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1028 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1029 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1030 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1031 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1032 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1033 | lock.l_len = 1L; |
| 1034 | lock.l_whence = SEEK_SET; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1035 | if( eFileLock==SHARED_LOCK |
| 1036 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1037 | ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1038 | lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1039 | lock.l_start = PENDING_BYTE; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1040 | s = fcntl(pFile->h, F_SETLK, &lock); |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1041 | if( s==(-1) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1042 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1043 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1044 | if( IS_LOCK_ERROR(rc) ){ |
| 1045 | pFile->lastErrno = tErrno; |
| 1046 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1047 | goto end_lock; |
| 1048 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | |
| 1052 | /* If control gets to this point, then actually go ahead and make |
| 1053 | ** operating system calls for the specified lock. |
| 1054 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1055 | if( eFileLock==SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1056 | assert( pInode->nShared==0 ); |
| 1057 | assert( pInode->eFileLock==0 ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1058 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1059 | /* Now get the read-lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1060 | lock.l_start = SHARED_FIRST; |
| 1061 | lock.l_len = SHARED_SIZE; |
| 1062 | if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){ |
| 1063 | tErrno = errno; |
| 1064 | } |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1065 | /* Drop the temporary PENDING lock */ |
| 1066 | lock.l_start = PENDING_BYTE; |
| 1067 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1068 | lock.l_type = F_UNLCK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1069 | if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1070 | if( s != -1 ){ |
| 1071 | /* This could happen with a network mount */ |
| 1072 | tErrno = errno; |
| 1073 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1074 | if( IS_LOCK_ERROR(rc) ){ |
| 1075 | pFile->lastErrno = tErrno; |
| 1076 | } |
| 1077 | goto end_lock; |
| 1078 | } |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1079 | } |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1080 | if( s==(-1) ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1081 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1082 | if( IS_LOCK_ERROR(rc) ){ |
| 1083 | pFile->lastErrno = tErrno; |
| 1084 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1085 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1086 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1087 | pInode->nLock++; |
| 1088 | pInode->nShared = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1089 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1090 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1091 | /* We are trying for an exclusive lock but another thread in this |
| 1092 | ** same process is still holding a shared lock. */ |
| 1093 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1094 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1095 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1096 | ** assumed that there is a SHARED or greater lock on the file |
| 1097 | ** already. |
| 1098 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1099 | assert( 0!=pFile->eFileLock ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1100 | lock.l_type = F_WRLCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1101 | switch( eFileLock ){ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1102 | case RESERVED_LOCK: |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1103 | lock.l_start = RESERVED_BYTE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1104 | break; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1105 | case EXCLUSIVE_LOCK: |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1106 | lock.l_start = SHARED_FIRST; |
| 1107 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1108 | break; |
| 1109 | default: |
| 1110 | assert(0); |
| 1111 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1112 | s = fcntl(pFile->h, F_SETLK, &lock); |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1113 | if( s==(-1) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1114 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1115 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1116 | if( IS_LOCK_ERROR(rc) ){ |
| 1117 | pFile->lastErrno = tErrno; |
| 1118 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1119 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1120 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1121 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1122 | |
| 1123 | #ifndef NDEBUG |
| 1124 | /* Set up the transaction-counter change checking flags when |
| 1125 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1126 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1127 | ** write operation (not a hot journal rollback). |
| 1128 | */ |
| 1129 | if( rc==SQLITE_OK |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1130 | && pFile->eFileLock<=SHARED_LOCK |
| 1131 | && eFileLock==RESERVED_LOCK |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1132 | ){ |
| 1133 | pFile->transCntrChng = 0; |
| 1134 | pFile->dbUpdate = 0; |
| 1135 | pFile->inNormalWrite = 1; |
| 1136 | } |
| 1137 | #endif |
| 1138 | |
| 1139 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1140 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1141 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1142 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1143 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 1144 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1145 | pInode->eFileLock = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1146 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1147 | |
| 1148 | end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1149 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1150 | OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), |
| 1151 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1152 | return rc; |
| 1153 | } |
| 1154 | |
| 1155 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1156 | ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1157 | ** If all such file descriptors are closed without error, the list is |
| 1158 | ** cleared and SQLITE_OK returned. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1159 | ** |
| 1160 | ** Otherwise, if an error occurs, then successfully closed file descriptor |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1161 | ** entries are removed from the list, and SQLITE_IOERR_CLOSE returned. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1162 | ** not deleted and SQLITE_IOERR_CLOSE returned. |
| 1163 | */ |
| 1164 | static int closePendingFds(unixFile *pFile){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1165 | int rc = SQLITE_OK; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1166 | unixInodeInfo *pInode = pFile->pInode; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1167 | UnixUnusedFd *pError = 0; |
| 1168 | UnixUnusedFd *p; |
| 1169 | UnixUnusedFd *pNext; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1170 | for(p=pInode->pUnused; p; p=pNext){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1171 | pNext = p->pNext; |
| 1172 | if( close(p->fd) ){ |
| 1173 | pFile->lastErrno = errno; |
| 1174 | rc = SQLITE_IOERR_CLOSE; |
| 1175 | p->pNext = pError; |
| 1176 | pError = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1177 | }else{ |
| 1178 | sqlite3_free(p); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1179 | } |
| 1180 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1181 | pInode->pUnused = pError; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1182 | return rc; |
| 1183 | } |
| 1184 | |
| 1185 | /* |
| 1186 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1187 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1188 | */ |
| 1189 | static void setPendingFd(unixFile *pFile){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1190 | unixInodeInfo *pInode = pFile->pInode; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1191 | UnixUnusedFd *p = pFile->pUnused; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1192 | p->pNext = pInode->pUnused; |
| 1193 | pInode->pUnused = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1194 | pFile->h = -1; |
| 1195 | pFile->pUnused = 0; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1199 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1200 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1201 | ** |
| 1202 | ** If the locking level of the file descriptor is already at or below |
| 1203 | ** the requested locking level, this routine is a no-op. |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1204 | ** |
| 1205 | ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 1206 | ** the byte range is divided into 2 parts and the first part is unlocked then |
| 1207 | ** set to a read lock, then the other part is simply unlocked. This works |
| 1208 | ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 1209 | ** remove the write lock on a region when a read lock is set. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1210 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1211 | static int _posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1212 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1213 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1214 | struct flock lock; |
| 1215 | int rc = SQLITE_OK; |
| 1216 | int h; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1217 | int tErrno; /* Error code from system call errors */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1218 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1219 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1220 | 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] | 1221 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1222 | getpid())); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1223 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1224 | assert( eFileLock<=SHARED_LOCK ); |
| 1225 | if( pFile->eFileLock<=eFileLock ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1226 | return SQLITE_OK; |
| 1227 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1228 | unixEnterMutex(); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1229 | h = pFile->h; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1230 | pInode = pFile->pInode; |
| 1231 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1232 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1233 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1234 | SimulateIOErrorBenign(1); |
| 1235 | SimulateIOError( h=(-1) ) |
| 1236 | SimulateIOErrorBenign(0); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1237 | |
| 1238 | #ifndef NDEBUG |
| 1239 | /* When reducing a lock such that other processes can start |
| 1240 | ** reading the database file again, make sure that the |
| 1241 | ** transaction counter was updated if any part of the database |
| 1242 | ** file changed. If the transaction counter is not updated, |
| 1243 | ** other connections to the same file might not realize that |
| 1244 | ** the file has changed and hence might not know to flush their |
| 1245 | ** cache. The use of a stale cache can lead to database corruption. |
| 1246 | */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1247 | #if 0 |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1248 | assert( pFile->inNormalWrite==0 |
| 1249 | || pFile->dbUpdate==0 |
| 1250 | || pFile->transCntrChng==1 ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1251 | #endif |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1252 | pFile->inNormalWrite = 0; |
| 1253 | #endif |
| 1254 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1255 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 1256 | ** before establishing the readlock - to avoid a race condition we downgrade |
| 1257 | ** the lock in 2 blocks, so that part of the range will be covered by a |
| 1258 | ** write lock until the rest is covered by a read lock: |
| 1259 | ** 1: [WWWWW] |
| 1260 | ** 2: [....W] |
| 1261 | ** 3: [RRRRW] |
| 1262 | ** 4: [RRRR.] |
| 1263 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1264 | if( eFileLock==SHARED_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1265 | if( handleNFSUnlock ){ |
| 1266 | off_t divSize = SHARED_SIZE - 1; |
| 1267 | |
| 1268 | lock.l_type = F_UNLCK; |
| 1269 | lock.l_whence = SEEK_SET; |
| 1270 | lock.l_start = SHARED_FIRST; |
| 1271 | lock.l_len = divSize; |
| 1272 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1273 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1274 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1275 | if( IS_LOCK_ERROR(rc) ){ |
| 1276 | pFile->lastErrno = tErrno; |
| 1277 | } |
| 1278 | goto end_unlock; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1279 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1280 | lock.l_type = F_RDLCK; |
| 1281 | lock.l_whence = SEEK_SET; |
| 1282 | lock.l_start = SHARED_FIRST; |
| 1283 | lock.l_len = divSize; |
| 1284 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1285 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1286 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1287 | if( IS_LOCK_ERROR(rc) ){ |
| 1288 | pFile->lastErrno = tErrno; |
| 1289 | } |
| 1290 | goto end_unlock; |
| 1291 | } |
| 1292 | lock.l_type = F_UNLCK; |
| 1293 | lock.l_whence = SEEK_SET; |
| 1294 | lock.l_start = SHARED_FIRST+divSize; |
| 1295 | lock.l_len = SHARED_SIZE-divSize; |
| 1296 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1297 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1298 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1299 | if( IS_LOCK_ERROR(rc) ){ |
| 1300 | pFile->lastErrno = tErrno; |
| 1301 | } |
| 1302 | goto end_unlock; |
| 1303 | } |
| 1304 | }else{ |
| 1305 | lock.l_type = F_RDLCK; |
| 1306 | lock.l_whence = SEEK_SET; |
| 1307 | lock.l_start = SHARED_FIRST; |
| 1308 | lock.l_len = SHARED_SIZE; |
| 1309 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1310 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1311 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1312 | if( IS_LOCK_ERROR(rc) ){ |
| 1313 | pFile->lastErrno = tErrno; |
| 1314 | } |
| 1315 | goto end_unlock; |
| 1316 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1317 | } |
| 1318 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1319 | lock.l_type = F_UNLCK; |
| 1320 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1321 | lock.l_start = PENDING_BYTE; |
| 1322 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1323 | if( fcntl(h, F_SETLK, &lock)!=(-1) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1324 | pInode->eFileLock = SHARED_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1325 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1326 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1327 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1328 | if( IS_LOCK_ERROR(rc) ){ |
| 1329 | pFile->lastErrno = tErrno; |
| 1330 | } |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 1331 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1332 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1333 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1334 | if( eFileLock==NO_LOCK ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1335 | /* Decrement the shared lock counter. Release the lock using an |
| 1336 | ** OS call only when all threads in this same process have released |
| 1337 | ** the lock. |
| 1338 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1339 | pInode->nShared--; |
| 1340 | if( pInode->nShared==0 ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1341 | lock.l_type = F_UNLCK; |
| 1342 | lock.l_whence = SEEK_SET; |
| 1343 | lock.l_start = lock.l_len = 0L; |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1344 | SimulateIOErrorBenign(1); |
| 1345 | SimulateIOError( h=(-1) ) |
| 1346 | SimulateIOErrorBenign(0); |
| 1347 | if( fcntl(h, F_SETLK, &lock)!=(-1) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1348 | pInode->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1349 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1350 | tErrno = errno; |
danielk1977 | 5ad6a88 | 2008-09-15 04:20:31 +0000 | [diff] [blame] | 1351 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1352 | if( IS_LOCK_ERROR(rc) ){ |
| 1353 | pFile->lastErrno = tErrno; |
| 1354 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1355 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1356 | pFile->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1357 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1360 | /* Decrement the count of locks against this same file. When the |
| 1361 | ** count reaches zero, close any other file descriptors whose close |
| 1362 | ** was deferred because of outstanding locks. |
| 1363 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1364 | pInode->nLock--; |
| 1365 | assert( pInode->nLock>=0 ); |
| 1366 | if( pInode->nLock==0 ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1367 | int rc2 = closePendingFds(pFile); |
| 1368 | if( rc==SQLITE_OK ){ |
| 1369 | rc = rc2; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1370 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1371 | } |
| 1372 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1373 | |
| 1374 | end_unlock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1375 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1376 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1377 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1381 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1382 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1383 | ** |
| 1384 | ** If the locking level of the file descriptor is already at or below |
| 1385 | ** the requested locking level, this routine is a no-op. |
| 1386 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1387 | static int unixUnlock(sqlite3_file *id, int eFileLock){ |
| 1388 | return _posixUnlock(id, eFileLock, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1392 | ** This function performs the parts of the "close file" operation |
| 1393 | ** common to all locking schemes. It closes the directory and file |
| 1394 | ** handles, if they are valid, and sets all fields of the unixFile |
| 1395 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1396 | ** |
| 1397 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 1398 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 1399 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1400 | */ |
| 1401 | static int closeUnixFile(sqlite3_file *id){ |
| 1402 | unixFile *pFile = (unixFile*)id; |
| 1403 | if( pFile ){ |
| 1404 | if( pFile->dirfd>=0 ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1405 | int err = close(pFile->dirfd); |
| 1406 | if( err ){ |
| 1407 | pFile->lastErrno = errno; |
| 1408 | return SQLITE_IOERR_DIR_CLOSE; |
| 1409 | }else{ |
| 1410 | pFile->dirfd=-1; |
| 1411 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1412 | } |
| 1413 | if( pFile->h>=0 ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1414 | int err = close(pFile->h); |
| 1415 | if( err ){ |
| 1416 | pFile->lastErrno = errno; |
| 1417 | return SQLITE_IOERR_CLOSE; |
| 1418 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1419 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1420 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1421 | if( pFile->pId ){ |
| 1422 | if( pFile->isDelete ){ |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1423 | unlink(pFile->pId->zCanonicalName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1424 | } |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1425 | vxworksReleaseFileId(pFile->pId); |
| 1426 | pFile->pId = 0; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1427 | } |
| 1428 | #endif |
drh | ff59a11 | 2010-05-14 20:15:51 +0000 | [diff] [blame] | 1429 | OSTRACE(("CLOSE %-3d\n", pFile->h)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1430 | OpenCounter(-1); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1431 | sqlite3_free(pFile->pUnused); |
drh | ff59a11 | 2010-05-14 20:15:51 +0000 | [diff] [blame] | 1432 | memset(pFile, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1433 | } |
| 1434 | return SQLITE_OK; |
| 1435 | } |
| 1436 | |
| 1437 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1438 | ** Close a file. |
| 1439 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1440 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1441 | int rc = SQLITE_OK; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1442 | if( id ){ |
| 1443 | unixFile *pFile = (unixFile *)id; |
| 1444 | unixUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1445 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1446 | if( pFile->pInode && pFile->pInode->nLock ){ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1447 | /* If there are outstanding locks, do not actually close the file just |
| 1448 | ** yet because that would clear those locks. Instead, add the file |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1449 | ** descriptor to pInode->pUnused list. It will be automatically closed |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1450 | ** when the last lock is cleared. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1451 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1452 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1453 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1454 | releaseInodeInfo(pFile->pInode); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1455 | rc = closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1456 | unixLeaveMutex(); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1457 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1458 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1461 | /************** End of the posix advisory lock implementation ***************** |
| 1462 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1463 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1464 | /****************************************************************************** |
| 1465 | ****************************** No-op Locking ********************************** |
| 1466 | ** |
| 1467 | ** Of the various locking implementations available, this is by far the |
| 1468 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 1469 | ** file for reading or writing. |
| 1470 | ** |
| 1471 | ** This locking mode is appropriate for use on read-only databases |
| 1472 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 1473 | ** also be used if the application employs some external mechanism to |
| 1474 | ** prevent simultaneous access of the same database by two or more |
| 1475 | ** database connections. But there is a serious risk of database |
| 1476 | ** corruption if this locking mode is used in situations where multiple |
| 1477 | ** database connections are accessing the same database file at the same |
| 1478 | ** time and one or more of those connections are writing. |
| 1479 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1480 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1481 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 1482 | UNUSED_PARAMETER(NotUsed); |
| 1483 | *pResOut = 0; |
| 1484 | return SQLITE_OK; |
| 1485 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1486 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1487 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1488 | return SQLITE_OK; |
| 1489 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1490 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1491 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1492 | return SQLITE_OK; |
| 1493 | } |
| 1494 | |
| 1495 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1496 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1497 | */ |
| 1498 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1499 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | /******************* End of the no-op lock implementation ********************* |
| 1503 | ******************************************************************************/ |
| 1504 | |
| 1505 | /****************************************************************************** |
| 1506 | ************************* Begin dot-file Locking ****************************** |
| 1507 | ** |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1508 | ** The dotfile locking implementation uses the existance of separate lock |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1509 | ** files in order to control access to the database. This works on just |
| 1510 | ** about every filesystem imaginable. But there are serious downsides: |
| 1511 | ** |
| 1512 | ** (1) There is zero concurrency. A single reader blocks all other |
| 1513 | ** connections from reading or writing the database. |
| 1514 | ** |
| 1515 | ** (2) An application crash or power loss can leave stale lock files |
| 1516 | ** sitting around that need to be cleared manually. |
| 1517 | ** |
| 1518 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 1519 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1520 | ** |
| 1521 | ** Dotfile locking works by creating a file in the same directory as the |
| 1522 | ** database and with the same name but with a ".lock" extension added. |
| 1523 | ** The existance of a lock file implies an EXCLUSIVE lock. All other lock |
| 1524 | ** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1525 | */ |
| 1526 | |
| 1527 | /* |
| 1528 | ** The file suffix added to the data base filename in order to create the |
| 1529 | ** lock file. |
| 1530 | */ |
| 1531 | #define DOTLOCK_SUFFIX ".lock" |
| 1532 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1533 | /* |
| 1534 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1535 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1536 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1537 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1538 | ** |
| 1539 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 1540 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 1541 | ** is held on the file and false if the file is unlocked. |
| 1542 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1543 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 1544 | int rc = SQLITE_OK; |
| 1545 | int reserved = 0; |
| 1546 | unixFile *pFile = (unixFile*)id; |
| 1547 | |
| 1548 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1549 | |
| 1550 | assert( pFile ); |
| 1551 | |
| 1552 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1553 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1554 | /* Either this connection or some other connection in the same process |
| 1555 | ** holds a lock on the file. No need to check further. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1556 | reserved = 1; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1557 | }else{ |
| 1558 | /* The lock is held if and only if the lockfile exists */ |
| 1559 | const char *zLockFile = (const char*)pFile->lockingContext; |
| 1560 | reserved = access(zLockFile, 0)==0; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1561 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1562 | OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1563 | *pResOut = reserved; |
| 1564 | return rc; |
| 1565 | } |
| 1566 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1567 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1568 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1569 | ** of the following: |
| 1570 | ** |
| 1571 | ** (1) SHARED_LOCK |
| 1572 | ** (2) RESERVED_LOCK |
| 1573 | ** (3) PENDING_LOCK |
| 1574 | ** (4) EXCLUSIVE_LOCK |
| 1575 | ** |
| 1576 | ** Sometimes when requesting one lock state, additional lock states |
| 1577 | ** are inserted in between. The locking might fail on one of the later |
| 1578 | ** transitions leaving the lock state different from what it started but |
| 1579 | ** still short of its goal. The following chart shows the allowed |
| 1580 | ** transitions and the inserted intermediate states: |
| 1581 | ** |
| 1582 | ** UNLOCKED -> SHARED |
| 1583 | ** SHARED -> RESERVED |
| 1584 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1585 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1586 | ** PENDING -> EXCLUSIVE |
| 1587 | ** |
| 1588 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1589 | ** routine to lower a locking level. |
| 1590 | ** |
| 1591 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 1592 | ** But we track the other locking levels internally. |
| 1593 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1594 | static int dotlockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1595 | unixFile *pFile = (unixFile*)id; |
| 1596 | int fd; |
| 1597 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1598 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1599 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1600 | |
| 1601 | /* If we have any lock, then the lock file already exists. All we have |
| 1602 | ** to do is adjust our internal record of the lock level. |
| 1603 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1604 | if( pFile->eFileLock > NO_LOCK ){ |
| 1605 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1606 | #if !OS_VXWORKS |
| 1607 | /* Always update the timestamp on the old file */ |
| 1608 | utimes(zLockFile, NULL); |
| 1609 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1610 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | /* grab an exclusive lock */ |
| 1614 | fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600); |
| 1615 | if( fd<0 ){ |
| 1616 | /* failed to open/create the file, someone else may have stolen the lock */ |
| 1617 | int tErrno = errno; |
| 1618 | if( EEXIST == tErrno ){ |
| 1619 | rc = SQLITE_BUSY; |
| 1620 | } else { |
| 1621 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1622 | if( IS_LOCK_ERROR(rc) ){ |
| 1623 | pFile->lastErrno = tErrno; |
| 1624 | } |
| 1625 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1626 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1627 | } |
| 1628 | if( close(fd) ){ |
| 1629 | pFile->lastErrno = errno; |
| 1630 | rc = SQLITE_IOERR_CLOSE; |
| 1631 | } |
| 1632 | |
| 1633 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1634 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1635 | return rc; |
| 1636 | } |
| 1637 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1638 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1639 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1640 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1641 | ** |
| 1642 | ** If the locking level of the file descriptor is already at or below |
| 1643 | ** the requested locking level, this routine is a no-op. |
| 1644 | ** |
| 1645 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 1646 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1647 | static int dotlockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1648 | unixFile *pFile = (unixFile*)id; |
| 1649 | char *zLockFile = (char *)pFile->lockingContext; |
| 1650 | |
| 1651 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1652 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
| 1653 | pFile->eFileLock, getpid())); |
| 1654 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1655 | |
| 1656 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1657 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1658 | return SQLITE_OK; |
| 1659 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1660 | |
| 1661 | /* To downgrade to shared, simply update our internal notion of the |
| 1662 | ** lock state. No need to mess with the file on disk. |
| 1663 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1664 | if( eFileLock==SHARED_LOCK ){ |
| 1665 | pFile->eFileLock = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1666 | return SQLITE_OK; |
| 1667 | } |
| 1668 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1669 | /* To fully unlock the database, delete the lock file */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1670 | assert( eFileLock==NO_LOCK ); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1671 | if( unlink(zLockFile) ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 1672 | int rc = 0; |
| 1673 | int tErrno = errno; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1674 | if( ENOENT != tErrno ){ |
| 1675 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1676 | } |
| 1677 | if( IS_LOCK_ERROR(rc) ){ |
| 1678 | pFile->lastErrno = tErrno; |
| 1679 | } |
| 1680 | return rc; |
| 1681 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1682 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1683 | return SQLITE_OK; |
| 1684 | } |
| 1685 | |
| 1686 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1687 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1688 | */ |
| 1689 | static int dotlockClose(sqlite3_file *id) { |
| 1690 | int rc; |
| 1691 | if( id ){ |
| 1692 | unixFile *pFile = (unixFile*)id; |
| 1693 | dotlockUnlock(id, NO_LOCK); |
| 1694 | sqlite3_free(pFile->lockingContext); |
| 1695 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1696 | rc = closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1697 | return rc; |
| 1698 | } |
| 1699 | /****************** End of the dot-file lock implementation ******************* |
| 1700 | ******************************************************************************/ |
| 1701 | |
| 1702 | /****************************************************************************** |
| 1703 | ************************** Begin flock Locking ******************************** |
| 1704 | ** |
| 1705 | ** Use the flock() system call to do file locking. |
| 1706 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1707 | ** flock() locking is like dot-file locking in that the various |
| 1708 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 1709 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 1710 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 1711 | ** still works when you do this, but concurrency is reduced since |
| 1712 | ** only a single process can be reading the database at a time. |
| 1713 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1714 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if |
| 1715 | ** compiling for VXWORKS. |
| 1716 | */ |
| 1717 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1718 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1719 | /* |
| 1720 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1721 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1722 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1723 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1724 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1725 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 1726 | int rc = SQLITE_OK; |
| 1727 | int reserved = 0; |
| 1728 | unixFile *pFile = (unixFile*)id; |
| 1729 | |
| 1730 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1731 | |
| 1732 | assert( pFile ); |
| 1733 | |
| 1734 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1735 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1736 | reserved = 1; |
| 1737 | } |
| 1738 | |
| 1739 | /* Otherwise see if some other process holds it. */ |
| 1740 | if( !reserved ){ |
| 1741 | /* attempt to get the lock */ |
| 1742 | int lrc = flock(pFile->h, LOCK_EX | LOCK_NB); |
| 1743 | if( !lrc ){ |
| 1744 | /* got the lock, unlock it */ |
| 1745 | lrc = flock(pFile->h, LOCK_UN); |
| 1746 | if ( lrc ) { |
| 1747 | int tErrno = errno; |
| 1748 | /* unlock failed with an error */ |
| 1749 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1750 | if( IS_LOCK_ERROR(lrc) ){ |
| 1751 | pFile->lastErrno = tErrno; |
| 1752 | rc = lrc; |
| 1753 | } |
| 1754 | } |
| 1755 | } else { |
| 1756 | int tErrno = errno; |
| 1757 | reserved = 1; |
| 1758 | /* someone else might have it reserved */ |
| 1759 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1760 | if( IS_LOCK_ERROR(lrc) ){ |
| 1761 | pFile->lastErrno = tErrno; |
| 1762 | rc = lrc; |
| 1763 | } |
| 1764 | } |
| 1765 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1766 | OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1767 | |
| 1768 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 1769 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 1770 | rc = SQLITE_OK; |
| 1771 | reserved=1; |
| 1772 | } |
| 1773 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 1774 | *pResOut = reserved; |
| 1775 | return rc; |
| 1776 | } |
| 1777 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1778 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1779 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1780 | ** of the following: |
| 1781 | ** |
| 1782 | ** (1) SHARED_LOCK |
| 1783 | ** (2) RESERVED_LOCK |
| 1784 | ** (3) PENDING_LOCK |
| 1785 | ** (4) EXCLUSIVE_LOCK |
| 1786 | ** |
| 1787 | ** Sometimes when requesting one lock state, additional lock states |
| 1788 | ** are inserted in between. The locking might fail on one of the later |
| 1789 | ** transitions leaving the lock state different from what it started but |
| 1790 | ** still short of its goal. The following chart shows the allowed |
| 1791 | ** transitions and the inserted intermediate states: |
| 1792 | ** |
| 1793 | ** UNLOCKED -> SHARED |
| 1794 | ** SHARED -> RESERVED |
| 1795 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1796 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1797 | ** PENDING -> EXCLUSIVE |
| 1798 | ** |
| 1799 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 1800 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 1801 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 1802 | ** access the file. |
| 1803 | ** |
| 1804 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1805 | ** routine to lower a locking level. |
| 1806 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1807 | static int flockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1808 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1809 | unixFile *pFile = (unixFile*)id; |
| 1810 | |
| 1811 | assert( pFile ); |
| 1812 | |
| 1813 | /* if we already have a lock, it is exclusive. |
| 1814 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1815 | if (pFile->eFileLock > NO_LOCK) { |
| 1816 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1817 | return SQLITE_OK; |
| 1818 | } |
| 1819 | |
| 1820 | /* grab an exclusive lock */ |
| 1821 | |
| 1822 | if (flock(pFile->h, LOCK_EX | LOCK_NB)) { |
| 1823 | int tErrno = errno; |
| 1824 | /* didn't get, must be busy */ |
| 1825 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1826 | if( IS_LOCK_ERROR(rc) ){ |
| 1827 | pFile->lastErrno = tErrno; |
| 1828 | } |
| 1829 | } else { |
| 1830 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1831 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1832 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1833 | OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), |
| 1834 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1835 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 1836 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 1837 | rc = SQLITE_BUSY; |
| 1838 | } |
| 1839 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 1840 | return rc; |
| 1841 | } |
| 1842 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1843 | |
| 1844 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1845 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1846 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1847 | ** |
| 1848 | ** If the locking level of the file descriptor is already at or below |
| 1849 | ** the requested locking level, this routine is a no-op. |
| 1850 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1851 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1852 | unixFile *pFile = (unixFile*)id; |
| 1853 | |
| 1854 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1855 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
| 1856 | pFile->eFileLock, getpid())); |
| 1857 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1858 | |
| 1859 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1860 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1861 | return SQLITE_OK; |
| 1862 | } |
| 1863 | |
| 1864 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1865 | if (eFileLock==SHARED_LOCK) { |
| 1866 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1867 | return SQLITE_OK; |
| 1868 | } |
| 1869 | |
| 1870 | /* no, really, unlock. */ |
| 1871 | int rc = flock(pFile->h, LOCK_UN); |
| 1872 | if (rc) { |
| 1873 | int r, tErrno = errno; |
| 1874 | r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1875 | if( IS_LOCK_ERROR(r) ){ |
| 1876 | pFile->lastErrno = tErrno; |
| 1877 | } |
| 1878 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 1879 | if( (r & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 1880 | r = SQLITE_BUSY; |
| 1881 | } |
| 1882 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 1883 | |
| 1884 | return r; |
| 1885 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1886 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1887 | return SQLITE_OK; |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | /* |
| 1892 | ** Close a file. |
| 1893 | */ |
| 1894 | static int flockClose(sqlite3_file *id) { |
| 1895 | if( id ){ |
| 1896 | flockUnlock(id, NO_LOCK); |
| 1897 | } |
| 1898 | return closeUnixFile(id); |
| 1899 | } |
| 1900 | |
| 1901 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 1902 | |
| 1903 | /******************* End of the flock lock implementation ********************* |
| 1904 | ******************************************************************************/ |
| 1905 | |
| 1906 | /****************************************************************************** |
| 1907 | ************************ Begin Named Semaphore Locking ************************ |
| 1908 | ** |
| 1909 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1910 | ** |
| 1911 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 1912 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 1913 | ** the database file at a time. This reduces potential concurrency, but |
| 1914 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1915 | */ |
| 1916 | #if OS_VXWORKS |
| 1917 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1918 | /* |
| 1919 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1920 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1921 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1922 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1923 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1924 | static int semCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 1925 | int rc = SQLITE_OK; |
| 1926 | int reserved = 0; |
| 1927 | unixFile *pFile = (unixFile*)id; |
| 1928 | |
| 1929 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1930 | |
| 1931 | assert( pFile ); |
| 1932 | |
| 1933 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1934 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1935 | reserved = 1; |
| 1936 | } |
| 1937 | |
| 1938 | /* Otherwise see if some other process holds it. */ |
| 1939 | if( !reserved ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1940 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1941 | struct stat statBuf; |
| 1942 | |
| 1943 | if( sem_trywait(pSem)==-1 ){ |
| 1944 | int tErrno = errno; |
| 1945 | if( EAGAIN != tErrno ){ |
| 1946 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 1947 | pFile->lastErrno = tErrno; |
| 1948 | } else { |
| 1949 | /* someone else has the lock when we are in NO_LOCK */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1950 | reserved = (pFile->eFileLock < SHARED_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1951 | } |
| 1952 | }else{ |
| 1953 | /* we could have it if we want it */ |
| 1954 | sem_post(pSem); |
| 1955 | } |
| 1956 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1957 | OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1958 | |
| 1959 | *pResOut = reserved; |
| 1960 | return rc; |
| 1961 | } |
| 1962 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1963 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1964 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1965 | ** of the following: |
| 1966 | ** |
| 1967 | ** (1) SHARED_LOCK |
| 1968 | ** (2) RESERVED_LOCK |
| 1969 | ** (3) PENDING_LOCK |
| 1970 | ** (4) EXCLUSIVE_LOCK |
| 1971 | ** |
| 1972 | ** Sometimes when requesting one lock state, additional lock states |
| 1973 | ** are inserted in between. The locking might fail on one of the later |
| 1974 | ** transitions leaving the lock state different from what it started but |
| 1975 | ** still short of its goal. The following chart shows the allowed |
| 1976 | ** transitions and the inserted intermediate states: |
| 1977 | ** |
| 1978 | ** UNLOCKED -> SHARED |
| 1979 | ** SHARED -> RESERVED |
| 1980 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1981 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1982 | ** PENDING -> EXCLUSIVE |
| 1983 | ** |
| 1984 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 1985 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 1986 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 1987 | ** access the file. |
| 1988 | ** |
| 1989 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1990 | ** routine to lower a locking level. |
| 1991 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1992 | static int semLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1993 | unixFile *pFile = (unixFile*)id; |
| 1994 | int fd; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1995 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1996 | int rc = SQLITE_OK; |
| 1997 | |
| 1998 | /* if we already have a lock, it is exclusive. |
| 1999 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2000 | if (pFile->eFileLock > NO_LOCK) { |
| 2001 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2002 | rc = SQLITE_OK; |
| 2003 | goto sem_end_lock; |
| 2004 | } |
| 2005 | |
| 2006 | /* lock semaphore now but bail out when already locked. */ |
| 2007 | if( sem_trywait(pSem)==-1 ){ |
| 2008 | rc = SQLITE_BUSY; |
| 2009 | goto sem_end_lock; |
| 2010 | } |
| 2011 | |
| 2012 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2013 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2014 | |
| 2015 | sem_end_lock: |
| 2016 | return rc; |
| 2017 | } |
| 2018 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2019 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2020 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2021 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2022 | ** |
| 2023 | ** If the locking level of the file descriptor is already at or below |
| 2024 | ** the requested locking level, this routine is a no-op. |
| 2025 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2026 | static int semUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2027 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2028 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2029 | |
| 2030 | assert( pFile ); |
| 2031 | assert( pSem ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2032 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
| 2033 | pFile->eFileLock, getpid())); |
| 2034 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2035 | |
| 2036 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2037 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2038 | return SQLITE_OK; |
| 2039 | } |
| 2040 | |
| 2041 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2042 | if (eFileLock==SHARED_LOCK) { |
| 2043 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2044 | return SQLITE_OK; |
| 2045 | } |
| 2046 | |
| 2047 | /* no, really unlock. */ |
| 2048 | if ( sem_post(pSem)==-1 ) { |
| 2049 | int rc, tErrno = errno; |
| 2050 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2051 | if( IS_LOCK_ERROR(rc) ){ |
| 2052 | pFile->lastErrno = tErrno; |
| 2053 | } |
| 2054 | return rc; |
| 2055 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2056 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2057 | return SQLITE_OK; |
| 2058 | } |
| 2059 | |
| 2060 | /* |
| 2061 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2062 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2063 | static int semClose(sqlite3_file *id) { |
| 2064 | if( id ){ |
| 2065 | unixFile *pFile = (unixFile*)id; |
| 2066 | semUnlock(id, NO_LOCK); |
| 2067 | assert( pFile ); |
| 2068 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2069 | releaseLockInfo(pFile->pInode); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2070 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2071 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2072 | } |
| 2073 | return SQLITE_OK; |
| 2074 | } |
| 2075 | |
| 2076 | #endif /* OS_VXWORKS */ |
| 2077 | /* |
| 2078 | ** Named semaphore locking is only available on VxWorks. |
| 2079 | ** |
| 2080 | *************** End of the named semaphore lock implementation **************** |
| 2081 | ******************************************************************************/ |
| 2082 | |
| 2083 | |
| 2084 | /****************************************************************************** |
| 2085 | *************************** Begin AFP Locking ********************************* |
| 2086 | ** |
| 2087 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2088 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2089 | ** |
| 2090 | ** Third-party implementations of AFP are available. But this code here |
| 2091 | ** only works on OSX. |
| 2092 | */ |
| 2093 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2094 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2095 | /* |
| 2096 | ** The afpLockingContext structure contains all afp lock specific state |
| 2097 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2098 | typedef struct afpLockingContext afpLockingContext; |
| 2099 | struct afpLockingContext { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2100 | int reserved; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2101 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2102 | }; |
| 2103 | |
| 2104 | struct ByteRangeLockPB2 |
| 2105 | { |
| 2106 | unsigned long long offset; /* offset to first byte to lock */ |
| 2107 | unsigned long long length; /* nbr of bytes to lock */ |
| 2108 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2109 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2110 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2111 | int fd; /* file desc to assoc this lock with */ |
| 2112 | }; |
| 2113 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2114 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2115 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2116 | /* |
| 2117 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2118 | ** AFP filesystem. |
| 2119 | ** |
| 2120 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2121 | */ |
| 2122 | static int afpSetLock( |
| 2123 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2124 | unixFile *pFile, /* Open file descriptor on path */ |
| 2125 | unsigned long long offset, /* First byte to be locked */ |
| 2126 | unsigned long long length, /* Number of bytes to lock */ |
| 2127 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2128 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2129 | struct ByteRangeLockPB2 pb; |
| 2130 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2131 | |
| 2132 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2133 | pb.startEndFlag = 0; |
| 2134 | pb.offset = offset; |
| 2135 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2136 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2137 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2138 | OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2139 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2140 | offset, length)); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2141 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2142 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2143 | int rc; |
| 2144 | int tErrno = errno; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2145 | OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2146 | path, tErrno, strerror(tErrno))); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2147 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2148 | rc = SQLITE_BUSY; |
| 2149 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2150 | rc = sqliteErrorFromPosixError(tErrno, |
| 2151 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2152 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2153 | if( IS_LOCK_ERROR(rc) ){ |
| 2154 | pFile->lastErrno = tErrno; |
| 2155 | } |
| 2156 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2157 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2158 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2159 | } |
| 2160 | } |
| 2161 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2162 | /* |
| 2163 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2164 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2165 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2166 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2167 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2168 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2169 | int rc = SQLITE_OK; |
| 2170 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2171 | unixFile *pFile = (unixFile*)id; |
| 2172 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2173 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2174 | |
| 2175 | assert( pFile ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2176 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2177 | if( context->reserved ){ |
| 2178 | *pResOut = 1; |
| 2179 | return SQLITE_OK; |
| 2180 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2181 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2182 | |
| 2183 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2184 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2185 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
| 2188 | /* Otherwise see if some other process holds it. |
| 2189 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2190 | if( !reserved ){ |
| 2191 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2192 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2193 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2194 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2195 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2196 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2197 | } else { |
| 2198 | /* if we failed to get the lock then someone else must have it */ |
| 2199 | reserved = 1; |
| 2200 | } |
| 2201 | if( IS_LOCK_ERROR(lrc) ){ |
| 2202 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2203 | } |
| 2204 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2205 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2206 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2207 | OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2208 | |
| 2209 | *pResOut = reserved; |
| 2210 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2213 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2214 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2215 | ** of the following: |
| 2216 | ** |
| 2217 | ** (1) SHARED_LOCK |
| 2218 | ** (2) RESERVED_LOCK |
| 2219 | ** (3) PENDING_LOCK |
| 2220 | ** (4) EXCLUSIVE_LOCK |
| 2221 | ** |
| 2222 | ** Sometimes when requesting one lock state, additional lock states |
| 2223 | ** are inserted in between. The locking might fail on one of the later |
| 2224 | ** transitions leaving the lock state different from what it started but |
| 2225 | ** still short of its goal. The following chart shows the allowed |
| 2226 | ** transitions and the inserted intermediate states: |
| 2227 | ** |
| 2228 | ** UNLOCKED -> SHARED |
| 2229 | ** SHARED -> RESERVED |
| 2230 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2231 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2232 | ** PENDING -> EXCLUSIVE |
| 2233 | ** |
| 2234 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2235 | ** routine to lower a locking level. |
| 2236 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2237 | static int afpLock(sqlite3_file *id, int eFileLock){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2238 | int rc = SQLITE_OK; |
| 2239 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2240 | unixInodeInfo *pInode = pFile->pInode; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2241 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2242 | |
| 2243 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2244 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 2245 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2246 | azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2247 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2248 | /* 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] | 2249 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2250 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2251 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2252 | if( pFile->eFileLock>=eFileLock ){ |
| 2253 | OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
| 2254 | azFileLock(eFileLock))); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2255 | return SQLITE_OK; |
| 2256 | } |
| 2257 | |
| 2258 | /* Make sure the locking sequence is correct |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2259 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 2260 | ** (2) SQLite never explicitly requests a pendig lock. |
| 2261 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2262 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2263 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 2264 | assert( eFileLock!=PENDING_LOCK ); |
| 2265 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2266 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2267 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2268 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2269 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2270 | pInode = pFile->pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2271 | |
| 2272 | /* If some thread using this PID has a lock via a different unixFile* |
| 2273 | ** handle that precludes the requested lock, return BUSY. |
| 2274 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2275 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 2276 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2277 | ){ |
| 2278 | rc = SQLITE_BUSY; |
| 2279 | goto afp_end_lock; |
| 2280 | } |
| 2281 | |
| 2282 | /* If a SHARED lock is requested, and some thread using this PID already |
| 2283 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 2284 | ** return SQLITE_OK. |
| 2285 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2286 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2287 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2288 | assert( eFileLock==SHARED_LOCK ); |
| 2289 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2290 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2291 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2292 | pInode->nShared++; |
| 2293 | pInode->nLock++; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2294 | goto afp_end_lock; |
| 2295 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2296 | |
| 2297 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2298 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2299 | ** be released. |
| 2300 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2301 | if( eFileLock==SHARED_LOCK |
| 2302 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2303 | ){ |
| 2304 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2305 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2306 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2307 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2308 | goto afp_end_lock; |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2313 | ** operating system calls for the specified lock. |
| 2314 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2315 | if( eFileLock==SHARED_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2316 | int lrc1, lrc2, lrc1Errno; |
| 2317 | long lk, mask; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2318 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2319 | assert( pInode->nShared==0 ); |
| 2320 | assert( pInode->eFileLock==0 ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2321 | |
| 2322 | mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2323 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2324 | /* note that the quality of the randomness doesn't matter that much */ |
| 2325 | lk = random(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2326 | pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2327 | lrc1 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2328 | SHARED_FIRST+pInode->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2329 | if( IS_LOCK_ERROR(lrc1) ){ |
| 2330 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2331 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2332 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2333 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2334 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2335 | if( IS_LOCK_ERROR(lrc1) ) { |
| 2336 | pFile->lastErrno = lrc1Errno; |
| 2337 | rc = lrc1; |
| 2338 | goto afp_end_lock; |
| 2339 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 2340 | rc = lrc2; |
| 2341 | goto afp_end_lock; |
| 2342 | } else if( lrc1 != SQLITE_OK ) { |
| 2343 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2344 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2345 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2346 | pInode->nLock++; |
| 2347 | pInode->nShared = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2348 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2349 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2350 | /* We are trying for an exclusive lock but another thread in this |
| 2351 | ** same process is still holding a shared lock. */ |
| 2352 | rc = SQLITE_BUSY; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2353 | }else{ |
| 2354 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 2355 | ** assumed that there is a SHARED or greater lock on the file |
| 2356 | ** already. |
| 2357 | */ |
| 2358 | int failed = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2359 | assert( 0!=pFile->eFileLock ); |
| 2360 | if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2361 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2362 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2363 | if( !failed ){ |
| 2364 | context->reserved = 1; |
| 2365 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2366 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2367 | if (!failed && eFileLock == EXCLUSIVE_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2368 | /* Acquire an EXCLUSIVE lock */ |
| 2369 | |
| 2370 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2371 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2372 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2373 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2374 | pInode->sharedByte, 1, 0)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2375 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2376 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2377 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2378 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2379 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2380 | SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2381 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 2382 | ** a critical I/O error |
| 2383 | */ |
| 2384 | rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : |
| 2385 | SQLITE_IOERR_LOCK; |
| 2386 | goto afp_end_lock; |
| 2387 | } |
| 2388 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2389 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2390 | } |
| 2391 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2392 | if( failed ){ |
| 2393 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2394 | } |
| 2395 | } |
| 2396 | |
| 2397 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2398 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2399 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2400 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 2401 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2402 | pInode->eFileLock = PENDING_LOCK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
| 2405 | afp_end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2406 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2407 | OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), |
| 2408 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2409 | return rc; |
| 2410 | } |
| 2411 | |
| 2412 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2413 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2414 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2415 | ** |
| 2416 | ** If the locking level of the file descriptor is already at or below |
| 2417 | ** the requested locking level, this routine is a no-op. |
| 2418 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2419 | static int afpUnlock(sqlite3_file *id, int eFileLock) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2420 | int rc = SQLITE_OK; |
| 2421 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2422 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2423 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 2424 | int skipShared = 0; |
| 2425 | #ifdef SQLITE_TEST |
| 2426 | int h = pFile->h; |
| 2427 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2428 | |
| 2429 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2430 | 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] | 2431 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2432 | getpid())); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2433 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2434 | assert( eFileLock<=SHARED_LOCK ); |
| 2435 | if( pFile->eFileLock<=eFileLock ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2436 | return SQLITE_OK; |
| 2437 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2438 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2439 | pInode = pFile->pInode; |
| 2440 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2441 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2442 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2443 | SimulateIOErrorBenign(1); |
| 2444 | SimulateIOError( h=(-1) ) |
| 2445 | SimulateIOErrorBenign(0); |
| 2446 | |
| 2447 | #ifndef NDEBUG |
| 2448 | /* When reducing a lock such that other processes can start |
| 2449 | ** reading the database file again, make sure that the |
| 2450 | ** transaction counter was updated if any part of the database |
| 2451 | ** file changed. If the transaction counter is not updated, |
| 2452 | ** other connections to the same file might not realize that |
| 2453 | ** the file has changed and hence might not know to flush their |
| 2454 | ** cache. The use of a stale cache can lead to database corruption. |
| 2455 | */ |
| 2456 | assert( pFile->inNormalWrite==0 |
| 2457 | || pFile->dbUpdate==0 |
| 2458 | || pFile->transCntrChng==1 ); |
| 2459 | pFile->inNormalWrite = 0; |
| 2460 | #endif |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2461 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2462 | if( pFile->eFileLock==EXCLUSIVE_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2463 | rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2464 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2465 | /* only re-establish the shared lock if necessary */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2466 | int sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2467 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 2468 | } else { |
| 2469 | skipShared = 1; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2470 | } |
| 2471 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2472 | if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2473 | rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2474 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2475 | if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2476 | rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 2477 | if( !rc ){ |
| 2478 | context->reserved = 0; |
| 2479 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2480 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2481 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ |
| 2482 | pInode->eFileLock = SHARED_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2483 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2484 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2485 | if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2486 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2487 | /* Decrement the shared lock counter. Release the lock using an |
| 2488 | ** OS call only when all threads in this same process have released |
| 2489 | ** the lock. |
| 2490 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2491 | unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 2492 | pInode->nShared--; |
| 2493 | if( pInode->nShared==0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2494 | SimulateIOErrorBenign(1); |
| 2495 | SimulateIOError( h=(-1) ) |
| 2496 | SimulateIOErrorBenign(0); |
| 2497 | if( !skipShared ){ |
| 2498 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 2499 | } |
| 2500 | if( !rc ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2501 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2502 | pFile->eFileLock = NO_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2503 | } |
| 2504 | } |
| 2505 | if( rc==SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2506 | pInode->nLock--; |
| 2507 | assert( pInode->nLock>=0 ); |
| 2508 | if( pInode->nLock==0 ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2509 | rc = closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2510 | } |
| 2511 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2512 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2513 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2514 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2515 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2516 | return rc; |
| 2517 | } |
| 2518 | |
| 2519 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2520 | ** Close a file & cleanup AFP specific locking context |
| 2521 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2522 | static int afpClose(sqlite3_file *id) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2523 | int rc = SQLITE_OK; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2524 | if( id ){ |
| 2525 | unixFile *pFile = (unixFile*)id; |
| 2526 | afpUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2527 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2528 | if( pFile->pInode && pFile->pInode->nLock ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2529 | /* If there are outstanding locks, do not actually close the file just |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2530 | ** yet because that would clear those locks. Instead, add the file |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2531 | ** descriptor to pInode->aPending. It will be automatically closed when |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2532 | ** the last lock is cleared. |
| 2533 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2534 | setPendingFd(pFile); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2535 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2536 | releaseLockInfo(pFile->pInode); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2537 | sqlite3_free(pFile->lockingContext); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2538 | rc = closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2539 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2540 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2541 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2542 | } |
| 2543 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2544 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2545 | /* |
| 2546 | ** The code above is the AFP lock implementation. The code is specific |
| 2547 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2548 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 2549 | ** VFS is not available. |
| 2550 | ** |
| 2551 | ********************* End of the AFP lock implementation ********************** |
| 2552 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2553 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2554 | /****************************************************************************** |
| 2555 | *************************** Begin NFS Locking ********************************/ |
| 2556 | |
| 2557 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 2558 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2559 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2560 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2561 | ** |
| 2562 | ** If the locking level of the file descriptor is already at or below |
| 2563 | ** the requested locking level, this routine is a no-op. |
| 2564 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2565 | static int nfsUnlock(sqlite3_file *id, int eFileLock){ |
| 2566 | return _posixUnlock(id, eFileLock, 1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2567 | } |
| 2568 | |
| 2569 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 2570 | /* |
| 2571 | ** The code above is the NFS lock implementation. The code is specific |
| 2572 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2573 | ** is available. |
| 2574 | ** |
| 2575 | ********************* End of the NFS lock implementation ********************** |
| 2576 | ******************************************************************************/ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2577 | |
| 2578 | /****************************************************************************** |
| 2579 | **************** Non-locking sqlite3_file methods ***************************** |
| 2580 | ** |
| 2581 | ** The next division contains implementations for all methods of the |
| 2582 | ** sqlite3_file object other than the locking methods. The locking |
| 2583 | ** methods were defined in divisions above (one locking method per |
| 2584 | ** division). Those methods that are common to all locking modes |
| 2585 | ** are gather together into this division. |
| 2586 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2587 | |
| 2588 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2589 | ** Seek to the offset passed as the second argument, then read cnt |
| 2590 | ** bytes into pBuf. Return the number of bytes actually read. |
| 2591 | ** |
| 2592 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 2593 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 2594 | ** one system to another. Since SQLite does not define USE_PREAD |
| 2595 | ** any any form by default, we will not attempt to define _XOPEN_SOURCE. |
| 2596 | ** See tickets #2741 and #2681. |
| 2597 | ** |
| 2598 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 2599 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2600 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2601 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 2602 | int got; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2603 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2604 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2605 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2606 | TIMER_START; |
| 2607 | #if defined(USE_PREAD) |
| 2608 | got = pread(id->h, pBuf, cnt, offset); |
| 2609 | SimulateIOError( got = -1 ); |
| 2610 | #elif defined(USE_PREAD64) |
| 2611 | got = pread64(id->h, pBuf, cnt, offset); |
| 2612 | SimulateIOError( got = -1 ); |
| 2613 | #else |
| 2614 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 2615 | SimulateIOError( newOffset-- ); |
| 2616 | if( newOffset!=offset ){ |
| 2617 | if( newOffset == -1 ){ |
| 2618 | ((unixFile*)id)->lastErrno = errno; |
| 2619 | }else{ |
| 2620 | ((unixFile*)id)->lastErrno = 0; |
| 2621 | } |
| 2622 | return -1; |
| 2623 | } |
| 2624 | got = read(id->h, pBuf, cnt); |
| 2625 | #endif |
| 2626 | TIMER_END; |
| 2627 | if( got<0 ){ |
| 2628 | ((unixFile*)id)->lastErrno = errno; |
| 2629 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2630 | OSTRACE(("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2631 | return got; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2635 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 2636 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 2637 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2638 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2639 | static int unixRead( |
| 2640 | sqlite3_file *id, |
| 2641 | void *pBuf, |
| 2642 | int amt, |
| 2643 | sqlite3_int64 offset |
| 2644 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2645 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2646 | int got; |
| 2647 | assert( id ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2648 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2649 | /* If this is a database file (not a journal, master-journal or temp |
| 2650 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2651 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 2652 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2653 | || offset>=PENDING_BYTE+512 |
| 2654 | || offset+amt<=PENDING_BYTE |
| 2655 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2656 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2657 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2658 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2659 | if( got==amt ){ |
| 2660 | return SQLITE_OK; |
| 2661 | }else if( got<0 ){ |
| 2662 | /* lastErrno set by seekAndRead */ |
| 2663 | return SQLITE_IOERR_READ; |
| 2664 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2665 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2666 | /* Unread parts of the buffer must be zero-filled */ |
| 2667 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 2668 | return SQLITE_IOERR_SHORT_READ; |
| 2669 | } |
| 2670 | } |
| 2671 | |
| 2672 | /* |
| 2673 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 2674 | ** Return the number of bytes actually read. Update the offset. |
| 2675 | ** |
| 2676 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 2677 | ** is set before returning. |
| 2678 | */ |
| 2679 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
| 2680 | int got; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2681 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2682 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2683 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2684 | TIMER_START; |
| 2685 | #if defined(USE_PREAD) |
| 2686 | got = pwrite(id->h, pBuf, cnt, offset); |
| 2687 | #elif defined(USE_PREAD64) |
| 2688 | got = pwrite64(id->h, pBuf, cnt, offset); |
| 2689 | #else |
| 2690 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 2691 | if( newOffset!=offset ){ |
| 2692 | if( newOffset == -1 ){ |
| 2693 | ((unixFile*)id)->lastErrno = errno; |
| 2694 | }else{ |
| 2695 | ((unixFile*)id)->lastErrno = 0; |
| 2696 | } |
| 2697 | return -1; |
| 2698 | } |
| 2699 | got = write(id->h, pBuf, cnt); |
| 2700 | #endif |
| 2701 | TIMER_END; |
| 2702 | if( got<0 ){ |
| 2703 | ((unixFile*)id)->lastErrno = errno; |
| 2704 | } |
| 2705 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2706 | OSTRACE(("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2707 | return got; |
| 2708 | } |
| 2709 | |
| 2710 | |
| 2711 | /* |
| 2712 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 2713 | ** or some other error code on failure. |
| 2714 | */ |
| 2715 | static int unixWrite( |
| 2716 | sqlite3_file *id, |
| 2717 | const void *pBuf, |
| 2718 | int amt, |
| 2719 | sqlite3_int64 offset |
| 2720 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2721 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2722 | int wrote = 0; |
| 2723 | assert( id ); |
| 2724 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2725 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2726 | /* If this is a database file (not a journal, master-journal or temp |
| 2727 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2728 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 2729 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2730 | || offset>=PENDING_BYTE+512 |
| 2731 | || offset+amt<=PENDING_BYTE |
| 2732 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2733 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2734 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2735 | #ifndef NDEBUG |
| 2736 | /* If we are doing a normal write to a database file (as opposed to |
| 2737 | ** doing a hot-journal rollback or a write to some file other than a |
| 2738 | ** normal database file) then record the fact that the database |
| 2739 | ** has changed. If the transaction counter is modified, record that |
| 2740 | ** fact too. |
| 2741 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2742 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2743 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 2744 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 2745 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2746 | char oldCntr[4]; |
| 2747 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 2748 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2749 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 2750 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2751 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 2752 | } |
| 2753 | } |
| 2754 | } |
| 2755 | #endif |
| 2756 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2757 | while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2758 | amt -= wrote; |
| 2759 | offset += wrote; |
| 2760 | pBuf = &((char*)pBuf)[wrote]; |
| 2761 | } |
| 2762 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 2763 | SimulateDiskfullError(( wrote=0, amt=1 )); |
| 2764 | if( amt>0 ){ |
| 2765 | if( wrote<0 ){ |
| 2766 | /* lastErrno set by seekAndWrite */ |
| 2767 | return SQLITE_IOERR_WRITE; |
| 2768 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2769 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2770 | return SQLITE_FULL; |
| 2771 | } |
| 2772 | } |
| 2773 | return SQLITE_OK; |
| 2774 | } |
| 2775 | |
| 2776 | #ifdef SQLITE_TEST |
| 2777 | /* |
| 2778 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2779 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2780 | */ |
| 2781 | int sqlite3_sync_count = 0; |
| 2782 | int sqlite3_fullsync_count = 0; |
| 2783 | #endif |
| 2784 | |
| 2785 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 2786 | ** We do not trust systems to provide a working fdatasync(). Some do. |
| 2787 | ** Others do no. To be safe, we will stick with the (slower) fsync(). |
| 2788 | ** If you know that your system does support fdatasync() correctly, |
| 2789 | ** then simply compile with -Dfdatasync=fdatasync |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2790 | */ |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 2791 | #if !defined(fdatasync) && !defined(__linux__) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2792 | # define fdatasync fsync |
| 2793 | #endif |
| 2794 | |
| 2795 | /* |
| 2796 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 2797 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 2798 | ** only available on Mac OS X. But that could change. |
| 2799 | */ |
| 2800 | #ifdef F_FULLFSYNC |
| 2801 | # define HAVE_FULLFSYNC 1 |
| 2802 | #else |
| 2803 | # define HAVE_FULLFSYNC 0 |
| 2804 | #endif |
| 2805 | |
| 2806 | |
| 2807 | /* |
| 2808 | ** The fsync() system call does not work as advertised on many |
| 2809 | ** unix systems. The following procedure is an attempt to make |
| 2810 | ** it work better. |
| 2811 | ** |
| 2812 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 2813 | ** for testing when we want to run through the test suite quickly. |
| 2814 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 2815 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 2816 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2817 | ** |
| 2818 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 2819 | ** The idea behind dataOnly is that it should only write the file content |
| 2820 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 2821 | ** unchanged since the file size is part of the inode. However, |
| 2822 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 2823 | ** file size has changed. The only real difference between fdatasync() |
| 2824 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 2825 | ** inode if the mtime or owner or other inode attributes have changed. |
| 2826 | ** We only care about the file size, not the other file attributes, so |
| 2827 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 2828 | ** So, we always use fdatasync() if it is available, regardless of |
| 2829 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2830 | */ |
| 2831 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 2832 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2833 | |
| 2834 | /* The following "ifdef/elif/else/" block has the same structure as |
| 2835 | ** the one below. It is replicated here solely to avoid cluttering |
| 2836 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 2837 | */ |
| 2838 | #ifdef SQLITE_NO_SYNC |
| 2839 | UNUSED_PARAMETER(fd); |
| 2840 | UNUSED_PARAMETER(fullSync); |
| 2841 | UNUSED_PARAMETER(dataOnly); |
| 2842 | #elif HAVE_FULLFSYNC |
| 2843 | UNUSED_PARAMETER(dataOnly); |
| 2844 | #else |
| 2845 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2846 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2847 | #endif |
| 2848 | |
| 2849 | /* Record the number of times that we do a normal fsync() and |
| 2850 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 2851 | ** gets called with the correct arguments. |
| 2852 | */ |
| 2853 | #ifdef SQLITE_TEST |
| 2854 | if( fullSync ) sqlite3_fullsync_count++; |
| 2855 | sqlite3_sync_count++; |
| 2856 | #endif |
| 2857 | |
| 2858 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 2859 | ** no-op |
| 2860 | */ |
| 2861 | #ifdef SQLITE_NO_SYNC |
| 2862 | rc = SQLITE_OK; |
| 2863 | #elif HAVE_FULLFSYNC |
| 2864 | if( fullSync ){ |
| 2865 | rc = fcntl(fd, F_FULLFSYNC, 0); |
| 2866 | }else{ |
| 2867 | rc = 1; |
| 2868 | } |
| 2869 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2870 | ** It shouldn't be possible for fullfsync to fail on the local |
| 2871 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 2872 | ** isn't supported for this file system. So, attempt an fsync |
| 2873 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 2874 | ** It'd be better to detect fullfsync support once and avoid |
| 2875 | ** the fcntl call every time sync is called. |
| 2876 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2877 | if( rc ) rc = fsync(fd); |
| 2878 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2879 | #elif defined(__APPLE__) |
| 2880 | /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 2881 | ** so currently we default to the macro that redefines fdatasync to fsync |
| 2882 | */ |
| 2883 | rc = fsync(fd); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2884 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2885 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 2886 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2887 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2888 | rc = fsync(fd); |
| 2889 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2890 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2891 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 2892 | |
| 2893 | if( OS_VXWORKS && rc!= -1 ){ |
| 2894 | rc = 0; |
| 2895 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 2896 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2899 | /* |
| 2900 | ** Make sure all writes to a particular file are committed to disk. |
| 2901 | ** |
| 2902 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 2903 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 2904 | ** file data is synced. |
| 2905 | ** |
| 2906 | ** Under Unix, also make sure that the directory entry for the file |
| 2907 | ** has been created by fsync-ing the directory that contains the file. |
| 2908 | ** If we do not do this and we encounter a power failure, the directory |
| 2909 | ** entry for the journal might not exist after we reboot. The next |
| 2910 | ** SQLite to access the file will not know that the journal exists (because |
| 2911 | ** the directory entry for the journal was never created) and the transaction |
| 2912 | ** will not roll back - possibly leading to database corruption. |
| 2913 | */ |
| 2914 | static int unixSync(sqlite3_file *id, int flags){ |
| 2915 | int rc; |
| 2916 | unixFile *pFile = (unixFile*)id; |
| 2917 | |
| 2918 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 2919 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 2920 | |
| 2921 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 2922 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 2923 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 2924 | ); |
| 2925 | |
| 2926 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 2927 | ** line is to test that doing so does not cause any problems. |
| 2928 | */ |
| 2929 | SimulateDiskfullError( return SQLITE_FULL ); |
| 2930 | |
| 2931 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2932 | OSTRACE(("SYNC %-3d\n", pFile->h)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2933 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 2934 | SimulateIOError( rc=1 ); |
| 2935 | if( rc ){ |
| 2936 | pFile->lastErrno = errno; |
| 2937 | return SQLITE_IOERR_FSYNC; |
| 2938 | } |
| 2939 | if( pFile->dirfd>=0 ){ |
| 2940 | int err; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2941 | OSTRACE(("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd, |
| 2942 | HAVE_FULLFSYNC, isFullsync)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2943 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 2944 | /* The directory sync is only attempted if full_fsync is |
| 2945 | ** turned off or unavailable. If a full_fsync occurred above, |
| 2946 | ** then the directory sync is superfluous. |
| 2947 | */ |
| 2948 | if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){ |
| 2949 | /* |
| 2950 | ** We have received multiple reports of fsync() returning |
| 2951 | ** errors when applied to directories on certain file systems. |
| 2952 | ** A failed directory sync is not a big deal. So it seems |
| 2953 | ** better to ignore the error. Ticket #1657 |
| 2954 | */ |
| 2955 | /* pFile->lastErrno = errno; */ |
| 2956 | /* return SQLITE_IOERR; */ |
| 2957 | } |
| 2958 | #endif |
| 2959 | err = close(pFile->dirfd); /* Only need to sync once, so close the */ |
| 2960 | if( err==0 ){ /* directory when we are done */ |
| 2961 | pFile->dirfd = -1; |
| 2962 | }else{ |
| 2963 | pFile->lastErrno = errno; |
| 2964 | rc = SQLITE_IOERR_DIR_CLOSE; |
| 2965 | } |
| 2966 | } |
| 2967 | return rc; |
| 2968 | } |
| 2969 | |
| 2970 | /* |
| 2971 | ** Truncate an open file to a specified size |
| 2972 | */ |
| 2973 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
| 2974 | int rc; |
| 2975 | assert( id ); |
| 2976 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
| 2977 | rc = ftruncate(((unixFile*)id)->h, (off_t)nByte); |
| 2978 | if( rc ){ |
| 2979 | ((unixFile*)id)->lastErrno = errno; |
| 2980 | return SQLITE_IOERR_TRUNCATE; |
| 2981 | }else{ |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 2982 | #ifndef NDEBUG |
| 2983 | /* If we are doing a normal write to a database file (as opposed to |
| 2984 | ** doing a hot-journal rollback or a write to some file other than a |
| 2985 | ** normal database file) and we truncate the file to zero length, |
| 2986 | ** that effectively updates the change counter. This might happen |
| 2987 | ** when restoring a database using the backup API from a zero-length |
| 2988 | ** source. |
| 2989 | */ |
| 2990 | if( ((unixFile*)id)->inNormalWrite && nByte==0 ){ |
| 2991 | ((unixFile*)id)->transCntrChng = 1; |
| 2992 | } |
| 2993 | #endif |
| 2994 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2995 | return SQLITE_OK; |
| 2996 | } |
| 2997 | } |
| 2998 | |
| 2999 | /* |
| 3000 | ** Determine the current size of a file in bytes |
| 3001 | */ |
| 3002 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 3003 | int rc; |
| 3004 | struct stat buf; |
| 3005 | assert( id ); |
| 3006 | rc = fstat(((unixFile*)id)->h, &buf); |
| 3007 | SimulateIOError( rc=1 ); |
| 3008 | if( rc!=0 ){ |
| 3009 | ((unixFile*)id)->lastErrno = errno; |
| 3010 | return SQLITE_IOERR_FSTAT; |
| 3011 | } |
| 3012 | *pSize = buf.st_size; |
| 3013 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3014 | /* When opening a zero-size database, the findInodeInfo() procedure |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3015 | ** writes a single byte into that file in order to work around a bug |
| 3016 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 3017 | ** layers, we need to report this file size as zero even though it is |
| 3018 | ** really 1. Ticket #3260. |
| 3019 | */ |
| 3020 | if( *pSize==1 ) *pSize = 0; |
| 3021 | |
| 3022 | |
| 3023 | return SQLITE_OK; |
| 3024 | } |
| 3025 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3026 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3027 | /* |
| 3028 | ** Handler for proxy-locking file-control verbs. Defined below in the |
| 3029 | ** proxying locking division. |
| 3030 | */ |
| 3031 | static int proxyFileControl(sqlite3_file*,int,void*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3032 | #endif |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3033 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3034 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3035 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3036 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3037 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3038 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3039 | switch( op ){ |
| 3040 | case SQLITE_FCNTL_LOCKSTATE: { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3041 | *(int*)pArg = ((unixFile*)id)->eFileLock; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3042 | return SQLITE_OK; |
| 3043 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3044 | case SQLITE_LAST_ERRNO: { |
| 3045 | *(int*)pArg = ((unixFile*)id)->lastErrno; |
| 3046 | return SQLITE_OK; |
| 3047 | } |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3048 | case SQLITE_FCNTL_SIZE_HINT: { |
drh | c527669 | 2010-05-21 18:24:06 +0000 | [diff] [blame] | 3049 | #if 0 /* No performance advantage seen on Linux */ |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3050 | sqlite3_int64 szFile = *(sqlite3_int64*)pArg; |
| 3051 | unixFile *pFile = (unixFile*)id; |
| 3052 | ftruncate(pFile->h, szFile); |
drh | c527669 | 2010-05-21 18:24:06 +0000 | [diff] [blame] | 3053 | #endif |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3054 | return SQLITE_OK; |
| 3055 | } |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3056 | #ifndef NDEBUG |
| 3057 | /* The pager calls this method to signal that it has done |
| 3058 | ** a rollback and that the database is therefore unchanged and |
| 3059 | ** it hence it is OK for the transaction change counter to be |
| 3060 | ** unchanged. |
| 3061 | */ |
| 3062 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 3063 | ((unixFile*)id)->dbUpdate = 0; |
| 3064 | return SQLITE_OK; |
| 3065 | } |
| 3066 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3067 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3068 | case SQLITE_SET_LOCKPROXYFILE: |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3069 | case SQLITE_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3070 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3071 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3072 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3073 | } |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3074 | return SQLITE_ERROR; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
| 3077 | /* |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3078 | ** Return the sector size in bytes of the underlying block device for |
| 3079 | ** the specified file. This is almost always 512 bytes, but may be |
| 3080 | ** larger for some devices. |
| 3081 | ** |
| 3082 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 3083 | ** if two files are created in the same file-system directory (i.e. |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3084 | ** a database and its journal file) that the sector size will be the |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3085 | ** same for both. |
| 3086 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3087 | static int unixSectorSize(sqlite3_file *NotUsed){ |
| 3088 | UNUSED_PARAMETER(NotUsed); |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 3089 | return SQLITE_DEFAULT_SECTOR_SIZE; |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3092 | /* |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3093 | ** Return the device characteristics for the file. This is always 0 for unix. |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3094 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3095 | static int unixDeviceCharacteristics(sqlite3_file *NotUsed){ |
| 3096 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3097 | return 0; |
| 3098 | } |
| 3099 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3100 | #ifndef SQLITE_OMIT_WAL |
| 3101 | |
| 3102 | |
| 3103 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3104 | ** Object used to represent an shared memory buffer. |
| 3105 | ** |
| 3106 | ** When multiple threads all reference the same wal-index, each thread |
| 3107 | ** has its own unixShm object, but they all point to a single instance |
| 3108 | ** of this unixShmNode object. In other words, each wal-index is opened |
| 3109 | ** only once per process. |
| 3110 | ** |
| 3111 | ** Each unixShmNode object is connected to a single unixInodeInfo object. |
| 3112 | ** We could coalesce this object into unixInodeInfo, but that would mean |
| 3113 | ** every open file that does not use shared memory (in other words, most |
| 3114 | ** open files) would have to carry around this extra information. So |
| 3115 | ** the unixInodeInfo object contains a pointer to this unixShmNode object |
| 3116 | ** and the unixShmNode object is created only when needed. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3117 | ** |
| 3118 | ** unixMutexHeld() must be true when creating or destroying |
| 3119 | ** this object or while reading or writing the following fields: |
| 3120 | ** |
| 3121 | ** nRef |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3122 | ** |
| 3123 | ** The following fields are read-only after the object is created: |
| 3124 | ** |
| 3125 | ** fid |
| 3126 | ** zFilename |
| 3127 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3128 | ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3129 | ** unixMutexHeld() is true when reading or writing any other field |
| 3130 | ** in this structure. |
| 3131 | ** |
| 3132 | ** To avoid deadlocks, mutex and mutexBuf are always released in the |
| 3133 | ** reverse order that they are acquired. mutexBuf is always acquired |
| 3134 | ** first and released last. This invariant is check by asserting |
| 3135 | ** sqlite3_mutex_notheld() on mutex whenever mutexBuf is acquired or |
| 3136 | ** released. |
| 3137 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3138 | struct unixShmNode { |
| 3139 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3140 | sqlite3_mutex *mutex; /* Mutex to access this object */ |
| 3141 | sqlite3_mutex *mutexBuf; /* Mutex to access zBuf[] */ |
| 3142 | char *zFilename; /* Name of the mmapped file */ |
| 3143 | int h; /* Open file descriptor */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3144 | int szMap; /* Size of the mapping into memory */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3145 | char *pMMapBuf; /* Where currently mmapped(). NULL if unmapped */ |
| 3146 | int nRef; /* Number of unixShm objects pointing to this */ |
| 3147 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3148 | #ifdef SQLITE_DEBUG |
| 3149 | u8 exclMask; /* Mask of exclusive locks held */ |
| 3150 | u8 sharedMask; /* Mask of shared locks held */ |
| 3151 | u8 nextShmId; /* Next available unixShm.id value */ |
| 3152 | #endif |
| 3153 | }; |
| 3154 | |
| 3155 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3156 | ** Structure used internally by this VFS to record the state of an |
| 3157 | ** open shared memory connection. |
| 3158 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3159 | ** The following fields are initialized when this object is created and |
| 3160 | ** are read-only thereafter: |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3161 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3162 | ** unixShm.pFile |
| 3163 | ** unixShm.id |
| 3164 | ** |
| 3165 | ** All other fields are read/write. The unixShm.pFile->mutex must be held |
| 3166 | ** while accessing any read/write fields. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3167 | */ |
| 3168 | struct unixShm { |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3169 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 3170 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3171 | u8 hasMutex; /* True if holding the unixShmNode mutex */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3172 | u8 hasMutexBuf; /* True if holding pFile->mutexBuf */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3173 | u16 sharedMask; /* Mask of shared locks held */ |
| 3174 | u16 exclMask; /* Mask of exclusive locks held */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3175 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3176 | u8 id; /* Id of this connection within its unixShmNode */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3177 | #endif |
| 3178 | }; |
| 3179 | |
| 3180 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3181 | ** Constants used for locking |
| 3182 | */ |
drh | 1b78eaf | 2010-05-25 13:40:03 +0000 | [diff] [blame] | 3183 | #define UNIX_SHM_BASE 80 /* Byte offset of the first lock byte */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3184 | #define UNIX_SHM_DMS 80 /* The deadman switch lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3185 | |
| 3186 | #ifdef SQLITE_DEBUG |
| 3187 | /* |
| 3188 | ** Return a pointer to a nul-terminated string in static memory that |
| 3189 | ** describes a locking mask. The string is of the form "MSABCD" with |
| 3190 | ** each character representing a lock. "M" for MUTEX, "S" for DMS, |
| 3191 | ** and "A" through "D" for the region locks. If a lock is held, the |
| 3192 | ** letter is shown. If the lock is not held, the letter is converted |
| 3193 | ** to ".". |
| 3194 | ** |
| 3195 | ** This routine is for debugging purposes only and does not appear |
| 3196 | ** in a production build. |
| 3197 | */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3198 | static const char *unixShmLockString(u16 maskShared, u16 maskExclusive){ |
| 3199 | static char zBuf[52]; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3200 | static int iBuf = 0; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3201 | int i; |
| 3202 | u16 mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3203 | char *z; |
| 3204 | |
| 3205 | z = &zBuf[iBuf]; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3206 | iBuf += 16; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3207 | if( iBuf>=sizeof(zBuf) ) iBuf = 0; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3208 | for(i=0, mask=1; i<SQLITE_SHM_NLOCK; i++, mask += mask){ |
| 3209 | if( mask & maskShared ){ |
| 3210 | z[i] = 's'; |
| 3211 | }else if( mask & maskExclusive ){ |
| 3212 | z[i] = 'E'; |
| 3213 | }else{ |
| 3214 | z[i] = '.'; |
| 3215 | } |
| 3216 | } |
| 3217 | z[i] = 0; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3218 | return z; |
| 3219 | } |
| 3220 | #endif /* SQLITE_DEBUG */ |
| 3221 | |
| 3222 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3223 | ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3224 | ** |
| 3225 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 3226 | ** otherwise. |
| 3227 | */ |
| 3228 | static int unixShmSystemLock( |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3229 | unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */ |
| 3230 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3231 | int ofst, /* First byte of the locking range */ |
| 3232 | int n /* Number of bytes to lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3233 | ){ |
| 3234 | struct flock f; /* The posix advisory locking structure */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3235 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3236 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3237 | /* Access to the unixShmNode object is serialized by the caller */ |
| 3238 | assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3239 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3240 | /* Shared locks never span more than one byte */ |
| 3241 | assert( n==1 || lockType!=F_RDLCK ); |
| 3242 | |
| 3243 | /* Locks are within range */ |
| 3244 | assert( n>=1 && ofst>=0 && ofst+n<SQLITE_SHM_NLOCK ); |
| 3245 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3246 | /* Initialize the locking parameters */ |
| 3247 | memset(&f, 0, sizeof(f)); |
| 3248 | f.l_type = lockType; |
| 3249 | f.l_whence = SEEK_SET; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3250 | f.l_start = ofst+UNIX_SHM_BASE; |
| 3251 | f.l_len = n; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3252 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3253 | rc = fcntl(pShmNode->h, F_SETLK, &f); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3254 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 3255 | |
| 3256 | /* Update the global lock state and do debug tracing */ |
| 3257 | #ifdef SQLITE_DEBUG |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3258 | { u16 mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3259 | OSTRACE(("SHM-LOCK ")); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3260 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3261 | if( rc==SQLITE_OK ){ |
| 3262 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3263 | OSTRACE(("unlock %d ok", ofst)); |
| 3264 | pShmNode->exclMask &= ~mask; |
| 3265 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3266 | }else if( lockType==F_RDLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3267 | OSTRACE(("read-lock %d ok", ofst)); |
| 3268 | pShmNode->exclMask &= ~mask; |
| 3269 | pShmNode->sharedMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3270 | }else{ |
| 3271 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3272 | OSTRACE(("write-lock %d ok", ofst)); |
| 3273 | pShmNode->exclMask |= mask; |
| 3274 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3275 | } |
| 3276 | }else{ |
| 3277 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3278 | OSTRACE(("unlock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3279 | }else if( lockType==F_RDLCK ){ |
| 3280 | OSTRACE(("read-lock failed")); |
| 3281 | }else{ |
| 3282 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3283 | OSTRACE(("write-lock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3284 | } |
| 3285 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3286 | OSTRACE((" - afterwards %s\n", |
| 3287 | unixShmLockString(pShmNode->sharedMask, pShmNode->exclMask))); |
| 3288 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3289 | #endif |
| 3290 | |
| 3291 | return rc; |
| 3292 | } |
| 3293 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3294 | |
| 3295 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3296 | ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3297 | ** |
| 3298 | ** This is not a VFS shared-memory method; it is a utility function called |
| 3299 | ** by VFS shared-memory methods. |
| 3300 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3301 | static void unixShmPurge(unixFile *pFd){ |
| 3302 | unixShmNode *p = pFd->pInode->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3303 | assert( unixMutexHeld() ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3304 | if( p && p->nRef==0 ){ |
| 3305 | assert( p->pInode==pFd->pInode ); |
| 3306 | if( p->mutex ) sqlite3_mutex_free(p->mutex); |
| 3307 | if( p->mutexBuf ) sqlite3_mutex_free(p->mutexBuf); |
| 3308 | if( p->h>=0 ) close(p->h); |
| 3309 | p->pInode->pShmNode = 0; |
| 3310 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3311 | } |
| 3312 | } |
| 3313 | |
| 3314 | /* |
| 3315 | ** Open a shared-memory area. This particular implementation uses |
| 3316 | ** mmapped files. |
| 3317 | ** |
| 3318 | ** zName is a filename used to identify the shared-memory area. The |
| 3319 | ** implementation does not (and perhaps should not) use this name |
| 3320 | ** directly, but rather use it as a template for finding an appropriate |
| 3321 | ** name for the shared-memory storage. In this implementation, the |
| 3322 | ** string "-index" is appended to zName and used as the name of the |
| 3323 | ** mmapped file. |
| 3324 | ** |
| 3325 | ** When opening a new shared-memory file, if no other instances of that |
| 3326 | ** file are currently open, in this process or in other processes, then |
| 3327 | ** the file must be truncated to zero length or have its header cleared. |
| 3328 | */ |
| 3329 | static int unixShmOpen( |
| 3330 | sqlite3_file *fd /* The file descriptor of the associated database */ |
| 3331 | ){ |
| 3332 | struct unixShm *p = 0; /* The connection to be opened */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3333 | struct unixShmNode *pShmNode = 0; /* The underlying mmapped file */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3334 | int rc; /* Result code */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3335 | struct unixFile *pDbFd; /* Underlying database file */ |
| 3336 | int nPath; /* Size of pDbFd->zPath in bytes */ |
| 3337 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3338 | /* Allocate space for the new sqlite3_shm object. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3339 | */ |
| 3340 | p = sqlite3_malloc( sizeof(*p) ); |
| 3341 | if( p==0 ) return SQLITE_NOMEM; |
| 3342 | memset(p, 0, sizeof(*p)); |
| 3343 | pDbFd = (struct unixFile*)fd; |
| 3344 | assert( pDbFd->pShm==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3345 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3346 | /* Check to see if a unixShmNode object already exists. Reuse an existing |
| 3347 | ** one if present. Create a new one if necessary. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3348 | */ |
| 3349 | unixEnterMutex(); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3350 | pShmNode = pDbFd->pInode->pShmNode; |
| 3351 | if( pShmNode==0 ){ |
| 3352 | nPath = strlen(pDbFd->zPath); |
| 3353 | pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nPath + 15 ); |
| 3354 | if( pShmNode==0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3355 | rc = SQLITE_NOMEM; |
| 3356 | goto shm_open_err; |
| 3357 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3358 | memset(pShmNode, 0, sizeof(*pShmNode)); |
| 3359 | pShmNode->zFilename = (char*)&pShmNode[1]; |
| 3360 | sqlite3_snprintf(nPath+15, pShmNode->zFilename, |
| 3361 | "%s-wal-index", pDbFd->zPath); |
| 3362 | pShmNode->h = -1; |
| 3363 | pDbFd->pInode->pShmNode = pShmNode; |
| 3364 | pShmNode->pInode = pDbFd->pInode; |
| 3365 | pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 3366 | if( pShmNode->mutex==0 ){ |
| 3367 | rc = SQLITE_NOMEM; |
| 3368 | goto shm_open_err; |
| 3369 | } |
| 3370 | pShmNode->mutexBuf = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 3371 | if( pShmNode->mutexBuf==0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3372 | rc = SQLITE_NOMEM; |
| 3373 | goto shm_open_err; |
| 3374 | } |
| 3375 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3376 | pShmNode->h = open(pShmNode->zFilename, O_RDWR|O_CREAT, 0664); |
| 3377 | if( pShmNode->h<0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3378 | rc = SQLITE_CANTOPEN_BKPT; |
| 3379 | goto shm_open_err; |
| 3380 | } |
| 3381 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3382 | /* Check to see if another process is holding the dead-man switch. |
| 3383 | ** If not, truncate the file to zero length. |
| 3384 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3385 | rc = SQLITE_OK; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3386 | if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3387 | if( ftruncate(pShmNode->h, 0) ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3388 | rc = SQLITE_IOERR; |
| 3389 | } |
| 3390 | } |
| 3391 | if( rc==SQLITE_OK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3392 | rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3393 | } |
| 3394 | if( rc ) goto shm_open_err; |
| 3395 | } |
| 3396 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3397 | /* Make the new connection a child of the unixShmNode */ |
| 3398 | p->pShmNode = pShmNode; |
| 3399 | p->pNext = pShmNode->pFirst; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3400 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3401 | p->id = pShmNode->nextShmId++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3402 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3403 | pShmNode->pFirst = p; |
| 3404 | pShmNode->nRef++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3405 | pDbFd->pShm = p; |
| 3406 | unixLeaveMutex(); |
| 3407 | return SQLITE_OK; |
| 3408 | |
| 3409 | /* Jump here on any error */ |
| 3410 | shm_open_err: |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3411 | unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3412 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3413 | unixLeaveMutex(); |
| 3414 | return rc; |
| 3415 | } |
| 3416 | |
| 3417 | /* |
| 3418 | ** Close a connection to shared-memory. Delete the underlying |
| 3419 | ** storage if deleteFlag is true. |
| 3420 | */ |
| 3421 | static int unixShmClose( |
| 3422 | sqlite3_file *fd, /* The underlying database file */ |
| 3423 | int deleteFlag /* Delete shared-memory if true */ |
| 3424 | ){ |
| 3425 | unixShm *p; /* The connection to be closed */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3426 | unixShmNode *pShmNode; /* The underlying shared-memory file */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3427 | unixShm **pp; /* For looping over sibling connections */ |
| 3428 | unixFile *pDbFd; /* The underlying database file */ |
| 3429 | |
| 3430 | pDbFd = (unixFile*)fd; |
| 3431 | p = pDbFd->pShm; |
| 3432 | if( p==0 ) return SQLITE_OK; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3433 | pShmNode = p->pShmNode; |
| 3434 | |
| 3435 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 3436 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3437 | |
| 3438 | /* Verify that the connection being closed holds no locks */ |
| 3439 | assert( p->exclMask==0 ); |
| 3440 | assert( p->sharedMask==0 ); |
| 3441 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3442 | /* Remove connection p from the set of connections associated |
| 3443 | ** with pShmNode */ |
| 3444 | sqlite3_mutex_enter(pShmNode->mutex); |
| 3445 | for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3446 | *pp = p->pNext; |
| 3447 | |
| 3448 | /* Free the connection p */ |
| 3449 | sqlite3_free(p); |
| 3450 | pDbFd->pShm = 0; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3451 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3452 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3453 | /* If pShmNode->nRef has reached 0, then close the underlying |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3454 | ** shared-memory file, too */ |
| 3455 | unixEnterMutex(); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3456 | assert( pShmNode->nRef>0 ); |
| 3457 | pShmNode->nRef--; |
| 3458 | if( pShmNode->nRef==0 ){ |
| 3459 | if( deleteFlag ) unlink(pShmNode->zFilename); |
| 3460 | unixShmPurge(pDbFd); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3461 | } |
| 3462 | unixLeaveMutex(); |
| 3463 | |
| 3464 | return SQLITE_OK; |
| 3465 | } |
| 3466 | |
| 3467 | /* |
drh | 026ac28 | 2010-05-26 15:06:38 +0000 | [diff] [blame] | 3468 | ** Changes the size of the underlying storage for a shared-memory segment. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3469 | ** |
drh | 026ac28 | 2010-05-26 15:06:38 +0000 | [diff] [blame] | 3470 | ** The reqSize parameter is the new requested size of the shared memory. |
| 3471 | ** This implementation is free to increase the shared memory size to |
| 3472 | ** any amount greater than or equal to reqSize. If the shared memory is |
| 3473 | ** already as big or bigger as reqSize, this routine is a no-op. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3474 | ** |
| 3475 | ** The reqSize parameter is the minimum size requested. The implementation |
| 3476 | ** is free to expand the storage to some larger amount if it chooses. |
| 3477 | */ |
| 3478 | static int unixShmSize( |
| 3479 | sqlite3_file *fd, /* The open database file holding SHM */ |
| 3480 | int reqSize, /* Requested size. -1 for query only */ |
| 3481 | int *pNewSize /* Write new size here */ |
| 3482 | ){ |
| 3483 | unixFile *pDbFd = (unixFile*)fd; |
| 3484 | unixShm *p = pDbFd->pShm; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3485 | unixShmNode *pShmNode = p->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3486 | int rc = SQLITE_OK; |
| 3487 | struct stat sStat; |
| 3488 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3489 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 3490 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 3491 | |
drh | a925fd2 | 2010-05-13 08:33:35 +0000 | [diff] [blame] | 3492 | while( 1 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3493 | if( fstat(pShmNode->h, &sStat)==0 ){ |
drh | a925fd2 | 2010-05-13 08:33:35 +0000 | [diff] [blame] | 3494 | *pNewSize = (int)sStat.st_size; |
drh | 026ac28 | 2010-05-26 15:06:38 +0000 | [diff] [blame] | 3495 | if( reqSize<=(int)sStat.st_size ) break; |
drh | a925fd2 | 2010-05-13 08:33:35 +0000 | [diff] [blame] | 3496 | }else{ |
| 3497 | *pNewSize = 0; |
| 3498 | rc = SQLITE_IOERR; |
| 3499 | break; |
| 3500 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3501 | rc = ftruncate(pShmNode->h, reqSize); |
drh | a925fd2 | 2010-05-13 08:33:35 +0000 | [diff] [blame] | 3502 | reqSize = -1; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3503 | } |
| 3504 | return rc; |
| 3505 | } |
| 3506 | |
| 3507 | |
| 3508 | /* |
drh | 026ac28 | 2010-05-26 15:06:38 +0000 | [diff] [blame] | 3509 | ** Map the shared storage into memory. |
| 3510 | ** |
| 3511 | ** If reqMapSize is positive, then an attempt is made to make the |
| 3512 | ** mapping at least reqMapSize bytes in size. However, the mapping |
| 3513 | ** will never be larger than the size of the underlying shared memory |
| 3514 | ** as set by prior calls to xShmSize(). |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3515 | ** |
| 3516 | ** *ppBuf is made to point to the memory which is a mapping of the |
| 3517 | ** underlying storage. A mutex is acquired to prevent other threads |
| 3518 | ** from running while *ppBuf is in use in order to prevent other threads |
| 3519 | ** remapping *ppBuf out from under this thread. The unixShmRelease() |
| 3520 | ** call will release the mutex. However, if the lock state is CHECKPOINT, |
| 3521 | ** the mutex is not acquired because CHECKPOINT will never remap the |
| 3522 | ** buffer. RECOVER might remap, though, so CHECKPOINT will acquire |
| 3523 | ** the mutex if and when it promotes to RECOVER. |
| 3524 | ** |
| 3525 | ** RECOVER needs to be atomic. The same mutex that prevents *ppBuf from |
| 3526 | ** being remapped also prevents more than one thread from being in |
| 3527 | ** RECOVER at a time. But, RECOVER sometimes wants to remap itself. |
| 3528 | ** To prevent RECOVER from losing its lock while remapping, the |
| 3529 | ** mutex is not released by unixShmRelease() when in RECOVER. |
| 3530 | ** |
drh | 026ac28 | 2010-05-26 15:06:38 +0000 | [diff] [blame] | 3531 | ** *pNewMapSize is set to the size of the mapping. Usually *pNewMapSize |
| 3532 | ** will be reqMapSize or larger, though it could be smaller if the |
| 3533 | ** underlying shared memory has never been enlarged to reqMapSize bytes |
| 3534 | ** by prior calls to xShmSize(). |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3535 | ** |
drh | 026ac28 | 2010-05-26 15:06:38 +0000 | [diff] [blame] | 3536 | ** *ppBuf might be NULL and zero if no space has |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3537 | ** yet been allocated to the underlying storage. |
| 3538 | */ |
| 3539 | static int unixShmGet( |
| 3540 | sqlite3_file *fd, /* Database file holding shared memory */ |
| 3541 | int reqMapSize, /* Requested size of mapping. -1 means don't care */ |
| 3542 | int *pNewMapSize, /* Write new size of mapping here */ |
drh | 5939f44 | 2010-05-18 13:27:12 +0000 | [diff] [blame] | 3543 | void volatile **ppBuf /* Write mapping buffer origin here */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3544 | ){ |
| 3545 | unixFile *pDbFd = (unixFile*)fd; |
| 3546 | unixShm *p = pDbFd->pShm; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3547 | unixShmNode *pShmNode = p->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3548 | int rc = SQLITE_OK; |
| 3549 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3550 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 3551 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 3552 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3553 | if( p->hasMutexBuf==0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3554 | assert( sqlite3_mutex_notheld(pShmNode->mutex) ); |
| 3555 | sqlite3_mutex_enter(pShmNode->mutexBuf); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3556 | p->hasMutexBuf = 1; |
| 3557 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3558 | sqlite3_mutex_enter(pShmNode->mutex); |
| 3559 | if( pShmNode->szMap==0 || reqMapSize>pShmNode->szMap ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3560 | int actualSize; |
drh | 026ac28 | 2010-05-26 15:06:38 +0000 | [diff] [blame] | 3561 | if( unixShmSize(fd, -1, &actualSize)!=SQLITE_OK ){ |
| 3562 | actualSize = 0; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3563 | } |
drh | 026ac28 | 2010-05-26 15:06:38 +0000 | [diff] [blame] | 3564 | reqMapSize = actualSize; |
| 3565 | if( pShmNode->pMMapBuf || reqMapSize<=0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3566 | munmap(pShmNode->pMMapBuf, pShmNode->szMap); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3567 | } |
drh | 026ac28 | 2010-05-26 15:06:38 +0000 | [diff] [blame] | 3568 | if( reqMapSize>0 ){ |
| 3569 | pShmNode->pMMapBuf = mmap(0, reqMapSize, PROT_READ|PROT_WRITE, MAP_SHARED, |
| 3570 | pShmNode->h, 0); |
| 3571 | pShmNode->szMap = pShmNode->pMMapBuf ? reqMapSize : 0; |
| 3572 | }else{ |
| 3573 | pShmNode->pMMapBuf = 0; |
| 3574 | pShmNode->szMap = 0; |
| 3575 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3576 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3577 | *pNewMapSize = pShmNode->szMap; |
| 3578 | *ppBuf = pShmNode->pMMapBuf; |
| 3579 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3580 | return rc; |
| 3581 | } |
| 3582 | |
| 3583 | /* |
| 3584 | ** Release the lock held on the shared memory segment to that other |
| 3585 | ** threads are free to resize it if necessary. |
| 3586 | ** |
| 3587 | ** If the lock is not currently held, this routine is a harmless no-op. |
| 3588 | ** |
| 3589 | ** If the shared-memory object is in lock state RECOVER, then we do not |
| 3590 | ** really want to release the lock, so in that case too, this routine |
| 3591 | ** is a no-op. |
| 3592 | */ |
| 3593 | static int unixShmRelease(sqlite3_file *fd){ |
| 3594 | unixFile *pDbFd = (unixFile*)fd; |
| 3595 | unixShm *p = pDbFd->pShm; |
| 3596 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3597 | if( p->hasMutexBuf ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3598 | assert( sqlite3_mutex_notheld(p->pShmNode->mutex) ); |
| 3599 | sqlite3_mutex_leave(p->pShmNode->mutexBuf); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3600 | p->hasMutexBuf = 0; |
| 3601 | } |
| 3602 | return SQLITE_OK; |
| 3603 | } |
| 3604 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3605 | |
| 3606 | /* |
| 3607 | ** Change the lock state for a shared-memory segment. |
| 3608 | */ |
| 3609 | static int unixShmLock( |
| 3610 | sqlite3_file *fd, /* Database file holding the shared memory */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3611 | int ofst, /* First lock to acquire or release */ |
| 3612 | int n, /* Number of locks to acquire or release */ |
| 3613 | int flags /* What to do with the lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3614 | ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3615 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 3616 | unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 3617 | unixShm *pX; /* For looping over all siblings */ |
| 3618 | unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 3619 | int rc = SQLITE_OK; /* Result code */ |
| 3620 | u16 mask; /* Mask of locks to take or release */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3621 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3622 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 3623 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3624 | assert( ofst>=0 && ofst+n<SQLITE_SHM_NLOCK ); |
| 3625 | assert( n>=1 ); |
| 3626 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 3627 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 3628 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 3629 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 3630 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3631 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3632 | mask = (1<<(ofst+n+1)) - (1<<(ofst+1)); |
| 3633 | assert( n>1 || mask==(1<<ofst) ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3634 | sqlite3_mutex_enter(pShmNode->mutex); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3635 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 3636 | u16 allMask = 0; /* Mask of locks held by siblings */ |
| 3637 | |
| 3638 | /* See if any siblings hold this same lock */ |
| 3639 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 3640 | if( pX==p ) continue; |
| 3641 | assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
| 3642 | allMask |= pX->sharedMask; |
| 3643 | } |
| 3644 | |
| 3645 | /* Unlock the system-level locks */ |
| 3646 | if( (mask & allMask)==0 ){ |
| 3647 | rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+1, n); |
| 3648 | }else{ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3649 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3650 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3651 | |
| 3652 | /* Undo the local locks */ |
| 3653 | if( rc==SQLITE_OK ){ |
| 3654 | p->exclMask &= ~mask; |
| 3655 | p->sharedMask &= ~mask; |
| 3656 | } |
| 3657 | }else if( flags & SQLITE_SHM_SHARED ){ |
| 3658 | u16 allShared = 0; /* Union of locks held by connections other than "p" */ |
| 3659 | |
| 3660 | /* Find out which shared locks are already held by sibling connections. |
| 3661 | ** If any sibling already holds an exclusive lock, go ahead and return |
| 3662 | ** SQLITE_BUSY. |
| 3663 | */ |
| 3664 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 3665 | if( pX==p ) continue; |
| 3666 | if( (pX->exclMask & mask)!=0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3667 | rc = SQLITE_BUSY; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3668 | break; |
| 3669 | } |
| 3670 | allShared |= pX->sharedMask; |
| 3671 | } |
| 3672 | |
| 3673 | /* Get shared locks at the system level, if necessary */ |
| 3674 | if( rc==SQLITE_OK ){ |
| 3675 | if( (allShared & mask)==0 ){ |
| 3676 | rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+1, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3677 | }else{ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3678 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3679 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3680 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3681 | |
| 3682 | /* Get the local shared locks */ |
| 3683 | if( rc==SQLITE_OK ){ |
| 3684 | p->sharedMask |= mask; |
| 3685 | } |
| 3686 | }else{ |
| 3687 | /* Make sure no sibling connections hold locks that will block this |
| 3688 | ** lock. If any do, return SQLITE_BUSY right away. |
| 3689 | */ |
| 3690 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 3691 | if( pX==p ) continue; |
| 3692 | if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ |
| 3693 | rc = SQLITE_BUSY; |
| 3694 | break; |
| 3695 | } |
| 3696 | } |
| 3697 | |
| 3698 | /* Get the exclusive locks at the system level. Then if successful |
| 3699 | ** also mark the local connection as being locked. |
| 3700 | */ |
| 3701 | if( rc==SQLITE_OK ){ |
| 3702 | rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+1, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3703 | if( rc==SQLITE_OK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3704 | p->sharedMask &= ~mask; |
| 3705 | p->exclMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3706 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3707 | } |
| 3708 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3709 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3710 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %s\n", |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame^] | 3711 | p->id, getpid(), unixShmLockString(p->sharedMask, p->exclMask))); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3712 | return rc; |
| 3713 | } |
| 3714 | |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3715 | /* |
| 3716 | ** Implement a memory barrier or memory fence on shared memory. |
| 3717 | ** |
| 3718 | ** All loads and stores begun before the barrier must complete before |
| 3719 | ** any load or store begun after the barrier. |
| 3720 | */ |
| 3721 | static void unixShmBarrier( |
| 3722 | sqlite3_file *fd /* Database file holding the shared memory */ |
| 3723 | ){ |
| 3724 | #ifdef __GNUC__ |
| 3725 | __sync_synchronize(); |
| 3726 | #else |
| 3727 | unixMutexEnter(); |
| 3728 | unixMutexLeave(); |
| 3729 | #endif |
| 3730 | } |
| 3731 | |
| 3732 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3733 | #else |
| 3734 | # define unixShmOpen 0 |
| 3735 | # define unixShmSize 0 |
| 3736 | # define unixShmGet 0 |
| 3737 | # define unixShmRelease 0 |
| 3738 | # define unixShmLock 0 |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3739 | # define unixShmBarrier 0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3740 | # define unixShmClose 0 |
| 3741 | #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 3742 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3743 | /* |
| 3744 | ** Here ends the implementation of all sqlite3_file methods. |
| 3745 | ** |
| 3746 | ********************** End sqlite3_file Methods ******************************* |
| 3747 | ******************************************************************************/ |
| 3748 | |
| 3749 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3750 | ** This division contains definitions of sqlite3_io_methods objects that |
| 3751 | ** implement various file locking strategies. It also contains definitions |
| 3752 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 3753 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 3754 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 3755 | ** the correct finder-function for that VFS. |
| 3756 | ** |
| 3757 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 3758 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 3759 | ** looks at the filesystem type and tries to guess the best locking |
| 3760 | ** strategy from that. |
| 3761 | ** |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3762 | ** For finder-funtion F, two objects are created: |
| 3763 | ** |
| 3764 | ** (1) The real finder-function named "FImpt()". |
| 3765 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3766 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3767 | ** |
| 3768 | ** |
| 3769 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 3770 | ** objects. We have to do this instead of letting pAppData point |
| 3771 | ** directly at the finder-function since C90 rules prevent a void* |
| 3772 | ** from be cast into a function pointer. |
| 3773 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3774 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3775 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3776 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3777 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 3778 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 3779 | ** |
| 3780 | ** * An I/O method finder function called FINDER that returns a pointer |
| 3781 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3782 | */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3783 | #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3784 | static const sqlite3_io_methods METHOD = { \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3785 | VERSION, /* iVersion */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3786 | CLOSE, /* xClose */ \ |
| 3787 | unixRead, /* xRead */ \ |
| 3788 | unixWrite, /* xWrite */ \ |
| 3789 | unixTruncate, /* xTruncate */ \ |
| 3790 | unixSync, /* xSync */ \ |
| 3791 | unixFileSize, /* xFileSize */ \ |
| 3792 | LOCK, /* xLock */ \ |
| 3793 | UNLOCK, /* xUnlock */ \ |
| 3794 | CKLOCK, /* xCheckReservedLock */ \ |
| 3795 | unixFileControl, /* xFileControl */ \ |
| 3796 | unixSectorSize, /* xSectorSize */ \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3797 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
| 3798 | unixShmOpen, /* xShmOpen */ \ |
| 3799 | unixShmSize, /* xShmSize */ \ |
| 3800 | unixShmGet, /* xShmGet */ \ |
| 3801 | unixShmRelease, /* xShmRelease */ \ |
| 3802 | unixShmLock, /* xShmLock */ \ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3803 | unixShmBarrier, /* xShmBarrier */ \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3804 | unixShmClose /* xShmClose */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3805 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3806 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 3807 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3808 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3809 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3810 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3811 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3812 | |
| 3813 | /* |
| 3814 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 3815 | ** locking strategies. Functions that return pointers to these methods |
| 3816 | ** are also created. |
| 3817 | */ |
| 3818 | IOMETHODS( |
| 3819 | posixIoFinder, /* Finder function name */ |
| 3820 | posixIoMethods, /* sqlite3_io_methods object name */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3821 | 2, /* ShmOpen is enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3822 | unixClose, /* xClose method */ |
| 3823 | unixLock, /* xLock method */ |
| 3824 | unixUnlock, /* xUnlock method */ |
| 3825 | unixCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3826 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3827 | IOMETHODS( |
| 3828 | nolockIoFinder, /* Finder function name */ |
| 3829 | nolockIoMethods, /* sqlite3_io_methods object name */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3830 | 1, /* ShmOpen is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3831 | nolockClose, /* xClose method */ |
| 3832 | nolockLock, /* xLock method */ |
| 3833 | nolockUnlock, /* xUnlock method */ |
| 3834 | nolockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3835 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3836 | IOMETHODS( |
| 3837 | dotlockIoFinder, /* Finder function name */ |
| 3838 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3839 | 1, /* ShmOpen is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3840 | dotlockClose, /* xClose method */ |
| 3841 | dotlockLock, /* xLock method */ |
| 3842 | dotlockUnlock, /* xUnlock method */ |
| 3843 | dotlockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3844 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3845 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3846 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3847 | IOMETHODS( |
| 3848 | flockIoFinder, /* Finder function name */ |
| 3849 | flockIoMethods, /* sqlite3_io_methods object name */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3850 | 1, /* ShmOpen is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3851 | flockClose, /* xClose method */ |
| 3852 | flockLock, /* xLock method */ |
| 3853 | flockUnlock, /* xUnlock method */ |
| 3854 | flockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3855 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3856 | #endif |
| 3857 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3858 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3859 | IOMETHODS( |
| 3860 | semIoFinder, /* Finder function name */ |
| 3861 | semIoMethods, /* sqlite3_io_methods object name */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3862 | 1, /* ShmOpen is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3863 | semClose, /* xClose method */ |
| 3864 | semLock, /* xLock method */ |
| 3865 | semUnlock, /* xUnlock method */ |
| 3866 | semCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3867 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3868 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3869 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3870 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3871 | IOMETHODS( |
| 3872 | afpIoFinder, /* Finder function name */ |
| 3873 | afpIoMethods, /* sqlite3_io_methods object name */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3874 | 1, /* ShmOpen is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3875 | afpClose, /* xClose method */ |
| 3876 | afpLock, /* xLock method */ |
| 3877 | afpUnlock, /* xUnlock method */ |
| 3878 | afpCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3879 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3880 | #endif |
| 3881 | |
| 3882 | /* |
| 3883 | ** The proxy locking method is a "super-method" in the sense that it |
| 3884 | ** opens secondary file descriptors for the conch and lock files and |
| 3885 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 3886 | ** secondary files. For this reason, the division that implements |
| 3887 | ** proxy locking is located much further down in the file. But we need |
| 3888 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 3889 | ** for proxy locking here. So we forward declare the I/O methods. |
| 3890 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3891 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3892 | static int proxyClose(sqlite3_file*); |
| 3893 | static int proxyLock(sqlite3_file*, int); |
| 3894 | static int proxyUnlock(sqlite3_file*, int); |
| 3895 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3896 | IOMETHODS( |
| 3897 | proxyIoFinder, /* Finder function name */ |
| 3898 | proxyIoMethods, /* sqlite3_io_methods object name */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3899 | 1, /* ShmOpen is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3900 | proxyClose, /* xClose method */ |
| 3901 | proxyLock, /* xLock method */ |
| 3902 | proxyUnlock, /* xUnlock method */ |
| 3903 | proxyCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3904 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3905 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3906 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3907 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 3908 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 3909 | IOMETHODS( |
| 3910 | nfsIoFinder, /* Finder function name */ |
| 3911 | nfsIoMethods, /* sqlite3_io_methods object name */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3912 | 1, /* ShmOpen is disabled */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3913 | unixClose, /* xClose method */ |
| 3914 | unixLock, /* xLock method */ |
| 3915 | nfsUnlock, /* xUnlock method */ |
| 3916 | unixCheckReservedLock /* xCheckReservedLock method */ |
| 3917 | ) |
| 3918 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3919 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3920 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3921 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3922 | ** This "finder" function attempts to determine the best locking strategy |
| 3923 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3924 | ** object that implements that strategy. |
| 3925 | ** |
| 3926 | ** This is for MacOSX only. |
| 3927 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3928 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3929 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3930 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3931 | ){ |
| 3932 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3933 | const char *zFilesystem; /* Filesystem type name */ |
| 3934 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3935 | } aMap[] = { |
| 3936 | { "hfs", &posixIoMethods }, |
| 3937 | { "ufs", &posixIoMethods }, |
| 3938 | { "afpfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3939 | { "smbfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3940 | { "webdav", &nolockIoMethods }, |
| 3941 | { 0, 0 } |
| 3942 | }; |
| 3943 | int i; |
| 3944 | struct statfs fsInfo; |
| 3945 | struct flock lockInfo; |
| 3946 | |
| 3947 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3948 | /* If filePath==NULL that means we are dealing with a transient file |
| 3949 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3950 | return &nolockIoMethods; |
| 3951 | } |
| 3952 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 3953 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 3954 | return &nolockIoMethods; |
| 3955 | } |
| 3956 | for(i=0; aMap[i].zFilesystem; i++){ |
| 3957 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 3958 | return aMap[i].pMethods; |
| 3959 | } |
| 3960 | } |
| 3961 | } |
| 3962 | |
| 3963 | /* Default case. Handles, amongst others, "nfs". |
| 3964 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 3965 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3966 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3967 | lockInfo.l_len = 1; |
| 3968 | lockInfo.l_start = 0; |
| 3969 | lockInfo.l_whence = SEEK_SET; |
| 3970 | lockInfo.l_type = F_RDLCK; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3971 | if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3972 | if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 3973 | return &nfsIoMethods; |
| 3974 | } else { |
| 3975 | return &posixIoMethods; |
| 3976 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3977 | }else{ |
| 3978 | return &dotlockIoMethods; |
| 3979 | } |
| 3980 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3981 | static const sqlite3_io_methods |
| 3982 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3983 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3984 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3985 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3986 | #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE |
| 3987 | /* |
| 3988 | ** This "finder" function attempts to determine the best locking strategy |
| 3989 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
| 3990 | ** object that implements that strategy. |
| 3991 | ** |
| 3992 | ** This is for VXWorks only. |
| 3993 | */ |
| 3994 | static const sqlite3_io_methods *autolockIoFinderImpl( |
| 3995 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3996 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3997 | ){ |
| 3998 | struct flock lockInfo; |
| 3999 | |
| 4000 | if( !filePath ){ |
| 4001 | /* If filePath==NULL that means we are dealing with a transient file |
| 4002 | ** that does not need to be locked. */ |
| 4003 | return &nolockIoMethods; |
| 4004 | } |
| 4005 | |
| 4006 | /* Test if fcntl() is supported and use POSIX style locks. |
| 4007 | ** Otherwise fall back to the named semaphore method. |
| 4008 | */ |
| 4009 | lockInfo.l_len = 1; |
| 4010 | lockInfo.l_start = 0; |
| 4011 | lockInfo.l_whence = SEEK_SET; |
| 4012 | lockInfo.l_type = F_RDLCK; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4013 | if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4014 | return &posixIoMethods; |
| 4015 | }else{ |
| 4016 | return &semIoMethods; |
| 4017 | } |
| 4018 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4019 | static const sqlite3_io_methods |
| 4020 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4021 | |
| 4022 | #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */ |
| 4023 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4024 | /* |
| 4025 | ** An abstract type for a pointer to a IO method finder function: |
| 4026 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4027 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4028 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4029 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4030 | /**************************************************************************** |
| 4031 | **************************** sqlite3_vfs methods **************************** |
| 4032 | ** |
| 4033 | ** This division contains the implementation of methods on the |
| 4034 | ** sqlite3_vfs object. |
| 4035 | */ |
| 4036 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4037 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4038 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4039 | */ |
| 4040 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4041 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4042 | int h, /* Open file descriptor of file being opened */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4043 | int dirfd, /* Directory file descriptor */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4044 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4045 | const char *zFilename, /* Name of the file being opened */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4046 | int noLock, /* Omit locking if true */ |
| 4047 | int isDelete /* Delete on close if true */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4048 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4049 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4050 | unixFile *pNew = (unixFile *)pId; |
| 4051 | int rc = SQLITE_OK; |
| 4052 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4053 | assert( pNew->pInode==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4054 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4055 | /* Parameter isDelete is only used on vxworks. Express this explicitly |
| 4056 | ** here to prevent compiler warnings about unused parameters. |
danielk1977 | a03396a | 2008-11-19 14:35:46 +0000 | [diff] [blame] | 4057 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4058 | UNUSED_PARAMETER(isDelete); |
danielk1977 | a03396a | 2008-11-19 14:35:46 +0000 | [diff] [blame] | 4059 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4060 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4061 | pNew->h = h; |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4062 | pNew->dirfd = dirfd; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4063 | pNew->fileFlags = 0; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4064 | assert( zFilename==0 || zFilename[0]=='/' ); /* Never a relative pathname */ |
| 4065 | pNew->zPath = zFilename; |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 4066 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4067 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 4068 | pNew->pId = vxworksFindFileId(zFilename); |
| 4069 | if( pNew->pId==0 ){ |
| 4070 | noLock = 1; |
| 4071 | rc = SQLITE_NOMEM; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4072 | } |
| 4073 | #endif |
| 4074 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4075 | if( noLock ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4076 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4077 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4078 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4079 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4080 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 4081 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 4082 | ** zFilename remains valid until file is closed, to support */ |
| 4083 | pNew->lockingContext = (void*)zFilename; |
| 4084 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4085 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4086 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4087 | if( pLockingStyle == &posixIoMethods |
| 4088 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 4089 | || pLockingStyle == &nfsIoMethods |
| 4090 | #endif |
| 4091 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4092 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4093 | rc = findInodeInfo(pNew, &pNew->pInode); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4094 | if( rc!=SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4095 | /* If an error occured in findInodeInfo(), close the file descriptor |
| 4096 | ** immediately, before releasing the mutex. findInodeInfo() may fail |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4097 | ** in two scenarios: |
| 4098 | ** |
| 4099 | ** (a) A call to fstat() failed. |
| 4100 | ** (b) A malloc failed. |
| 4101 | ** |
| 4102 | ** Scenario (b) may only occur if the process is holding no other |
| 4103 | ** file descriptors open on the same file. If there were other file |
| 4104 | ** descriptors on this file, then no malloc would be required by |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4105 | ** findInodeInfo(). If this is the case, it is quite safe to close |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4106 | ** handle h - as it is guaranteed that no posix locks will be released |
| 4107 | ** by doing so. |
| 4108 | ** |
| 4109 | ** If scenario (a) caused the error then things are not so safe. The |
| 4110 | ** implicit assumption here is that if fstat() fails, things are in |
| 4111 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 4112 | */ |
| 4113 | close(h); |
| 4114 | h = -1; |
| 4115 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4116 | unixLeaveMutex(); |
| 4117 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4118 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4119 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 4120 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4121 | /* AFP locking uses the file path so it needs to be included in |
| 4122 | ** the afpLockingContext. |
| 4123 | */ |
| 4124 | afpLockingContext *pCtx; |
| 4125 | pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 4126 | if( pCtx==0 ){ |
| 4127 | rc = SQLITE_NOMEM; |
| 4128 | }else{ |
| 4129 | /* NB: zFilename exists and remains valid until the file is closed |
| 4130 | ** according to requirement F11141. So we do not need to make a |
| 4131 | ** copy of the filename. */ |
| 4132 | pCtx->dbPath = zFilename; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4133 | pCtx->reserved = 0; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4134 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4135 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4136 | rc = findInodeInfo(pNew, &pNew->pInode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4137 | if( rc!=SQLITE_OK ){ |
| 4138 | sqlite3_free(pNew->lockingContext); |
| 4139 | close(h); |
| 4140 | h = -1; |
| 4141 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4142 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4143 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4144 | } |
| 4145 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4146 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4147 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 4148 | /* Dotfile locking uses the file path so it needs to be included in |
| 4149 | ** the dotlockLockingContext |
| 4150 | */ |
| 4151 | char *zLockFile; |
| 4152 | int nFilename; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4153 | nFilename = (int)strlen(zFilename) + 6; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4154 | zLockFile = (char *)sqlite3_malloc(nFilename); |
| 4155 | if( zLockFile==0 ){ |
| 4156 | rc = SQLITE_NOMEM; |
| 4157 | }else{ |
| 4158 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4159 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4160 | pNew->lockingContext = zLockFile; |
| 4161 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4162 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4163 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4164 | else if( pLockingStyle == &semIoMethods ){ |
| 4165 | /* Named semaphore locking uses the file path so it needs to be |
| 4166 | ** included in the semLockingContext |
| 4167 | */ |
| 4168 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4169 | rc = findInodeInfo(pNew, &pNew->pInode); |
| 4170 | if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 4171 | char *zSemName = pNew->pInode->aSemName; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4172 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 4173 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4174 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 4175 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4176 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4177 | pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 4178 | if( pNew->pInode->pSem == SEM_FAILED ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4179 | rc = SQLITE_NOMEM; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4180 | pNew->pInode->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4181 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4182 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4183 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4184 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4185 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 4186 | |
| 4187 | pNew->lastErrno = 0; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4188 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4189 | if( rc!=SQLITE_OK ){ |
drh | 309e655 | 2010-02-05 18:00:26 +0000 | [diff] [blame] | 4190 | if( h>=0 ) close(h); |
| 4191 | h = -1; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4192 | unlink(zFilename); |
| 4193 | isDelete = 0; |
| 4194 | } |
| 4195 | pNew->isDelete = isDelete; |
| 4196 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4197 | if( rc!=SQLITE_OK ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4198 | if( dirfd>=0 ) close(dirfd); /* silent leak if fail, already in error */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4199 | if( h>=0 ) close(h); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4200 | }else{ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4201 | pNew->pMethod = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4202 | OpenCounter(+1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4203 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4204 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 4205 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 4206 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4207 | /* |
| 4208 | ** Open a file descriptor to the directory containing file zFilename. |
| 4209 | ** If successful, *pFd is set to the opened file descriptor and |
| 4210 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 4211 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 4212 | ** value. |
| 4213 | ** |
| 4214 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 4215 | ** the file descriptor *pFd using close(). |
| 4216 | */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4217 | static int openDirectory(const char *zFilename, int *pFd){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4218 | int ii; |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 4219 | int fd = -1; |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 4220 | char zDirname[MAX_PATHNAME+1]; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4221 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4222 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
drh | 617634e | 2009-01-08 14:36:20 +0000 | [diff] [blame] | 4223 | for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4224 | if( ii>0 ){ |
| 4225 | zDirname[ii] = '\0'; |
| 4226 | fd = open(zDirname, O_RDONLY|O_BINARY, 0); |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 4227 | if( fd>=0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4228 | #ifdef FD_CLOEXEC |
| 4229 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 4230 | #endif |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4231 | OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4232 | } |
| 4233 | } |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4234 | *pFd = fd; |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 4235 | return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN_BKPT); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4238 | /* |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4239 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 4240 | ** by the calling process and must be big enough to hold at least |
| 4241 | ** pVfs->mxPathname bytes. |
| 4242 | */ |
| 4243 | static int getTempname(int nBuf, char *zBuf){ |
| 4244 | static const char *azDirs[] = { |
| 4245 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4246 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4247 | "/var/tmp", |
| 4248 | "/usr/tmp", |
| 4249 | "/tmp", |
| 4250 | ".", |
| 4251 | }; |
| 4252 | static const unsigned char zChars[] = |
| 4253 | "abcdefghijklmnopqrstuvwxyz" |
| 4254 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 4255 | "0123456789"; |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 4256 | unsigned int i, j; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4257 | struct stat buf; |
| 4258 | const char *zDir = "."; |
| 4259 | |
| 4260 | /* It's odd to simulate an io-error here, but really this is just |
| 4261 | ** using the io-error infrastructure to test that SQLite handles this |
| 4262 | ** function failing. |
| 4263 | */ |
| 4264 | SimulateIOError( return SQLITE_IOERR ); |
| 4265 | |
| 4266 | azDirs[0] = sqlite3_temp_directory; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4267 | if (NULL == azDirs[1]) { |
| 4268 | azDirs[1] = getenv("TMPDIR"); |
| 4269 | } |
| 4270 | |
| 4271 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4272 | if( azDirs[i]==0 ) continue; |
| 4273 | if( stat(azDirs[i], &buf) ) continue; |
| 4274 | if( !S_ISDIR(buf.st_mode) ) continue; |
| 4275 | if( access(azDirs[i], 07) ) continue; |
| 4276 | zDir = azDirs[i]; |
| 4277 | break; |
| 4278 | } |
| 4279 | |
| 4280 | /* Check that the output buffer is large enough for the temporary file |
| 4281 | ** name. If it is not, return SQLITE_ERROR. |
| 4282 | */ |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 4283 | if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4284 | return SQLITE_ERROR; |
| 4285 | } |
| 4286 | |
| 4287 | do{ |
| 4288 | sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4289 | j = (int)strlen(zBuf); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4290 | sqlite3_randomness(15, &zBuf[j]); |
| 4291 | for(i=0; i<15; i++, j++){ |
| 4292 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 4293 | } |
| 4294 | zBuf[j] = 0; |
| 4295 | }while( access(zBuf,0)==0 ); |
| 4296 | return SQLITE_OK; |
| 4297 | } |
| 4298 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4299 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 4300 | /* |
| 4301 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 4302 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 4303 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 4304 | */ |
| 4305 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 4306 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 4307 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4308 | /* |
| 4309 | ** Search for an unused file descriptor that was opened on the database |
| 4310 | ** file (not a journal or master-journal file) identified by pathname |
| 4311 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 4312 | ** argument to this function. |
| 4313 | ** |
| 4314 | ** Such a file descriptor may exist if a database connection was closed |
| 4315 | ** but the associated file descriptor could not be closed because some |
| 4316 | ** other file descriptor open on the same file is holding a file-lock. |
| 4317 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 4318 | ** describing "Posix Advisory Locking" at the start of this file for |
| 4319 | ** further details. Also, ticket #4018. |
| 4320 | ** |
| 4321 | ** If a suitable file descriptor is found, then it is returned. If no |
| 4322 | ** such file descriptor is located, -1 is returned. |
| 4323 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4324 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 4325 | UnixUnusedFd *pUnused = 0; |
| 4326 | |
| 4327 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 4328 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 4329 | ** but because no way to test it is currently available. It is better |
| 4330 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 4331 | ** feature. */ |
| 4332 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4333 | struct stat sStat; /* Results of stat() call */ |
| 4334 | |
| 4335 | /* A stat() call may fail for various reasons. If this happens, it is |
| 4336 | ** almost certain that an open() call on the same path will also fail. |
| 4337 | ** For this reason, if an error occurs in the stat() call here, it is |
| 4338 | ** ignored and -1 is returned. The caller will try to open a new file |
| 4339 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 4340 | ** |
| 4341 | ** Even if a subsequent open() call does succeed, the consequences of |
| 4342 | ** not searching for a resusable file descriptor are not dire. */ |
| 4343 | if( 0==stat(zPath, &sStat) ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4344 | unixInodeInfo *pInode; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4345 | |
| 4346 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4347 | pInode = inodeList; |
| 4348 | while( pInode && (pInode->fileId.dev!=sStat.st_dev |
| 4349 | || pInode->fileId.ino!=sStat.st_ino) ){ |
| 4350 | pInode = pInode->pNext; |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 4351 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4352 | if( pInode ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4353 | UnixUnusedFd **pp; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4354 | for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4355 | pUnused = *pp; |
| 4356 | if( pUnused ){ |
| 4357 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4358 | } |
| 4359 | } |
| 4360 | unixLeaveMutex(); |
| 4361 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4362 | #endif /* if !OS_VXWORKS */ |
| 4363 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4364 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4365 | |
| 4366 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4367 | ** Open the file zPath. |
| 4368 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4369 | ** Previously, the SQLite OS layer used three functions in place of this |
| 4370 | ** one: |
| 4371 | ** |
| 4372 | ** sqlite3OsOpenReadWrite(); |
| 4373 | ** sqlite3OsOpenReadOnly(); |
| 4374 | ** sqlite3OsOpenExclusive(); |
| 4375 | ** |
| 4376 | ** These calls correspond to the following combinations of flags: |
| 4377 | ** |
| 4378 | ** ReadWrite() -> (READWRITE | CREATE) |
| 4379 | ** ReadOnly() -> (READONLY) |
| 4380 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 4381 | ** |
| 4382 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 4383 | ** true, the file was configured to be automatically deleted when the |
| 4384 | ** file handle closed. To achieve the same effect using this new |
| 4385 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 4386 | ** OpenExclusive(). |
| 4387 | */ |
| 4388 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4389 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 4390 | const char *zPath, /* Pathname of file to be opened */ |
| 4391 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 4392 | int flags, /* Input flags to control the opening */ |
| 4393 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4394 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4395 | unixFile *p = (unixFile *)pFile; |
| 4396 | int fd = -1; /* File descriptor returned by open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4397 | int dirfd = -1; /* Directory file descriptor */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4398 | int openFlags = 0; /* Flags to pass to open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4399 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4400 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4401 | int rc = SQLITE_OK; /* Function Return Code */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4402 | |
| 4403 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 4404 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 4405 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 4406 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 4407 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4408 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4409 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 4410 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4411 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4412 | /* If creating a master or main-file journal, this function will open |
| 4413 | ** a file-descriptor on the directory too. The first time unixSync() |
| 4414 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 4415 | */ |
| 4416 | int isOpenDirectory = (isCreate && |
| 4417 | (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL) |
| 4418 | ); |
| 4419 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4420 | /* If argument zPath is a NULL pointer, this function is required to open |
| 4421 | ** a temporary file. Use this buffer to store the file name in. |
| 4422 | */ |
| 4423 | char zTmpname[MAX_PATHNAME+1]; |
| 4424 | const char *zName = zPath; |
| 4425 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4426 | /* Check the following statements are true: |
| 4427 | ** |
| 4428 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 4429 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 4430 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4431 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4432 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4433 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4434 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4435 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4436 | assert(isDelete==0 || isCreate); |
| 4437 | |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4438 | /* The main DB, main journal, and master journal are never automatically |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4439 | ** deleted. Nor are they ever temporary files. */ |
| 4440 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 4441 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 4442 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4443 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4444 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 4445 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 4446 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 4447 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4448 | || eType==SQLITE_OPEN_TRANSIENT_DB |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4449 | ); |
| 4450 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4451 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4452 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4453 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4454 | UnixUnusedFd *pUnused; |
| 4455 | pUnused = findReusableFd(zName, flags); |
| 4456 | if( pUnused ){ |
| 4457 | fd = pUnused->fd; |
| 4458 | }else{ |
dan | 6aa657f | 2009-08-24 18:57:58 +0000 | [diff] [blame] | 4459 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4460 | if( !pUnused ){ |
| 4461 | return SQLITE_NOMEM; |
| 4462 | } |
| 4463 | } |
| 4464 | p->pUnused = pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4465 | }else if( !zName ){ |
| 4466 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4467 | assert(isDelete && !isOpenDirectory); |
| 4468 | rc = getTempname(MAX_PATHNAME+1, zTmpname); |
| 4469 | if( rc!=SQLITE_OK ){ |
| 4470 | return rc; |
| 4471 | } |
| 4472 | zName = zTmpname; |
| 4473 | } |
| 4474 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4475 | /* Determine the value of the flags parameter passed to POSIX function |
| 4476 | ** open(). These must be calculated even if open() is not called, as |
| 4477 | ** they may be stored as part of the file handle and used by the |
| 4478 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4479 | if( isReadonly ) openFlags |= O_RDONLY; |
| 4480 | if( isReadWrite ) openFlags |= O_RDWR; |
| 4481 | if( isCreate ) openFlags |= O_CREAT; |
| 4482 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 4483 | openFlags |= (O_LARGEFILE|O_BINARY); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4484 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4485 | if( fd<0 ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4486 | mode_t openMode = (isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 4487 | fd = open(zName, openFlags, openMode); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4488 | OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4489 | if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ |
| 4490 | /* Failed to open the file for read/write access. Try read-only. */ |
| 4491 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4492 | openFlags &= ~(O_RDWR|O_CREAT); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4493 | flags |= SQLITE_OPEN_READONLY; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4494 | openFlags |= O_RDONLY; |
| 4495 | fd = open(zName, openFlags, openMode); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4496 | } |
| 4497 | if( fd<0 ){ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 4498 | rc = SQLITE_CANTOPEN_BKPT; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4499 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4500 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4501 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4502 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4503 | if( pOutFlags ){ |
| 4504 | *pOutFlags = flags; |
| 4505 | } |
| 4506 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4507 | if( p->pUnused ){ |
| 4508 | p->pUnused->fd = fd; |
| 4509 | p->pUnused->flags = flags; |
| 4510 | } |
| 4511 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4512 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4513 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4514 | zPath = zName; |
| 4515 | #else |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4516 | unlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4517 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4518 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 4519 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4520 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4521 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 4522 | } |
| 4523 | #endif |
| 4524 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4525 | if( isOpenDirectory ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4526 | rc = openDirectory(zPath, &dirfd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4527 | if( rc!=SQLITE_OK ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4528 | /* It is safe to close fd at this point, because it is guaranteed not |
| 4529 | ** 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] | 4530 | ** it would not be safe to close as this would release any locks held |
| 4531 | ** on the file by this process. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4532 | assert( eType!=SQLITE_OPEN_MAIN_DB ); |
| 4533 | close(fd); /* silently leak if fail, already in error */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4534 | goto open_finished; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4535 | } |
| 4536 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4537 | |
| 4538 | #ifdef FD_CLOEXEC |
| 4539 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 4540 | #endif |
| 4541 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4542 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4543 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4544 | |
| 4545 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 4546 | struct statfs fsInfo; |
| 4547 | if( fstatfs(fd, &fsInfo) == -1 ){ |
| 4548 | ((unixFile*)pFile)->lastErrno = errno; |
| 4549 | if( dirfd>=0 ) close(dirfd); /* silently leak if fail, in error */ |
| 4550 | close(fd); /* silently leak if fail, in error */ |
| 4551 | return SQLITE_IOERR_ACCESS; |
| 4552 | } |
| 4553 | if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 4554 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 4555 | } |
| 4556 | #endif |
| 4557 | |
| 4558 | #if SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4559 | #if SQLITE_PREFER_PROXY_LOCKING |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4560 | isAutoProxy = 1; |
| 4561 | #endif |
| 4562 | if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4563 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 4564 | int useProxy = 0; |
| 4565 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4566 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 4567 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4568 | if( envforce!=NULL ){ |
| 4569 | useProxy = atoi(envforce)>0; |
| 4570 | }else{ |
| 4571 | struct statfs fsInfo; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4572 | if( statfs(zPath, &fsInfo) == -1 ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4573 | /* In theory, the close(fd) call is sub-optimal. If the file opened |
| 4574 | ** with fd is a database file, and there are other connections open |
| 4575 | ** on that file that are currently holding advisory locks on it, |
| 4576 | ** then the call to close() will cancel those locks. In practice, |
| 4577 | ** we're assuming that statfs() doesn't fail very often. At least |
| 4578 | ** not while other file descriptors opened by the same process on |
| 4579 | ** the same file are working. */ |
| 4580 | p->lastErrno = errno; |
| 4581 | if( dirfd>=0 ){ |
| 4582 | close(dirfd); /* silently leak if fail, in error */ |
| 4583 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4584 | close(fd); /* silently leak if fail, in error */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4585 | rc = SQLITE_IOERR_ACCESS; |
| 4586 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4587 | } |
| 4588 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 4589 | } |
| 4590 | if( useProxy ){ |
| 4591 | rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete); |
| 4592 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4593 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4594 | if( rc!=SQLITE_OK ){ |
| 4595 | /* Use unixClose to clean up the resources added in fillInUnixFile |
| 4596 | ** and clear all the structure's references. Specifically, |
| 4597 | ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 4598 | */ |
| 4599 | unixClose(pFile); |
| 4600 | return rc; |
| 4601 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4602 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4603 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4604 | } |
| 4605 | } |
| 4606 | #endif |
| 4607 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4608 | rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete); |
| 4609 | open_finished: |
| 4610 | if( rc!=SQLITE_OK ){ |
| 4611 | sqlite3_free(p->pUnused); |
| 4612 | } |
| 4613 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4614 | } |
| 4615 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4616 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4617 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4618 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 4619 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4620 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4621 | static int unixDelete( |
| 4622 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 4623 | const char *zPath, /* Name of file to be deleted */ |
| 4624 | int dirSync /* If true, fsync() directory after deleting file */ |
| 4625 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4626 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4627 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4628 | SimulateIOError(return SQLITE_IOERR_DELETE); |
| 4629 | unlink(zPath); |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 4630 | #ifndef SQLITE_DISABLE_DIRSYNC |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4631 | if( dirSync ){ |
| 4632 | int fd; |
| 4633 | rc = openDirectory(zPath, &fd); |
| 4634 | if( rc==SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4635 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4636 | if( fsync(fd)==-1 ) |
| 4637 | #else |
| 4638 | if( fsync(fd) ) |
| 4639 | #endif |
| 4640 | { |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4641 | rc = SQLITE_IOERR_DIR_FSYNC; |
| 4642 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4643 | if( close(fd)&&!rc ){ |
| 4644 | rc = SQLITE_IOERR_DIR_CLOSE; |
| 4645 | } |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4646 | } |
| 4647 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 4648 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4649 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4650 | } |
| 4651 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4652 | /* |
| 4653 | ** Test the existance of or access permissions of file zPath. The |
| 4654 | ** test performed depends on the value of flags: |
| 4655 | ** |
| 4656 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 4657 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 4658 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 4659 | ** |
| 4660 | ** Otherwise return 0. |
| 4661 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4662 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4663 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 4664 | const char *zPath, /* Path of the file to examine */ |
| 4665 | int flags, /* What do we want to learn about the zPath file? */ |
| 4666 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4667 | ){ |
rse | 25c0d1a | 2007-09-20 08:38:14 +0000 | [diff] [blame] | 4668 | int amode = 0; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4669 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4670 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4671 | switch( flags ){ |
| 4672 | case SQLITE_ACCESS_EXISTS: |
| 4673 | amode = F_OK; |
| 4674 | break; |
| 4675 | case SQLITE_ACCESS_READWRITE: |
| 4676 | amode = W_OK|R_OK; |
| 4677 | break; |
drh | 50d3f90 | 2007-08-27 21:10:36 +0000 | [diff] [blame] | 4678 | case SQLITE_ACCESS_READ: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4679 | amode = R_OK; |
| 4680 | break; |
| 4681 | |
| 4682 | default: |
| 4683 | assert(!"Invalid flags argument"); |
| 4684 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4685 | *pResOut = (access(zPath, amode)==0); |
| 4686 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4687 | } |
| 4688 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4689 | |
| 4690 | /* |
| 4691 | ** Turn a relative pathname into a full pathname. The relative path |
| 4692 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 4693 | ** zPath. |
| 4694 | ** |
| 4695 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 4696 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 4697 | ** this buffer before returning. |
| 4698 | */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 4699 | static int unixFullPathname( |
| 4700 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 4701 | const char *zPath, /* Possibly relative input path */ |
| 4702 | int nOut, /* Size of output buffer in bytes */ |
| 4703 | char *zOut /* Output buffer */ |
| 4704 | ){ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 4705 | |
| 4706 | /* It's odd to simulate an io-error here, but really this is just |
| 4707 | ** using the io-error infrastructure to test that SQLite handles this |
| 4708 | ** function failing. This function could fail if, for example, the |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4709 | ** current working directory has been unlinked. |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 4710 | */ |
| 4711 | SimulateIOError( return SQLITE_ERROR ); |
| 4712 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4713 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 4714 | UNUSED_PARAMETER(pVfs); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4715 | |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4716 | zOut[nOut-1] = '\0'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4717 | if( zPath[0]=='/' ){ |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4718 | sqlite3_snprintf(nOut, zOut, "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4719 | }else{ |
| 4720 | int nCwd; |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4721 | if( getcwd(zOut, nOut-1)==0 ){ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 4722 | return SQLITE_CANTOPEN_BKPT; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4723 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4724 | nCwd = (int)strlen(zOut); |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4725 | sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4726 | } |
| 4727 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4728 | } |
| 4729 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 4730 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4731 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 4732 | /* |
| 4733 | ** Interfaces for opening a shared library, finding entry points |
| 4734 | ** within the shared library, and closing the shared library. |
| 4735 | */ |
| 4736 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4737 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 4738 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4739 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 4740 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 4741 | |
| 4742 | /* |
| 4743 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 4744 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 4745 | ** message is available, it is written to zBufOut. If no error message |
| 4746 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 4747 | ** error message. |
| 4748 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4749 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4750 | char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4751 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4752 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4753 | zErr = dlerror(); |
| 4754 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4755 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4756 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4757 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4758 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4759 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 4760 | /* |
| 4761 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 4762 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 4763 | ** returns a void* which is really a pointer to a function. So how do we |
| 4764 | ** use dlsym() with -pedantic-errors? |
| 4765 | ** |
| 4766 | ** Variable x below is defined to be a pointer to a function taking |
| 4767 | ** parameters void* and const char* and returning a pointer to a function. |
| 4768 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 4769 | ** (That assignment requires a cast.) Then we call the function that |
| 4770 | ** x points to. |
| 4771 | ** |
| 4772 | ** This work-around is unlikely to work correctly on any system where |
| 4773 | ** you really cannot cast a function pointer into void*. But then, on the |
| 4774 | ** other hand, dlsym() will not work on such a system either, so we have |
| 4775 | ** not really lost anything. |
| 4776 | */ |
| 4777 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4778 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4779 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 4780 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4781 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4782 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 4783 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4784 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4785 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4786 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 4787 | #define unixDlOpen 0 |
| 4788 | #define unixDlError 0 |
| 4789 | #define unixDlSym 0 |
| 4790 | #define unixDlClose 0 |
| 4791 | #endif |
| 4792 | |
| 4793 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4794 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4795 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4796 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 4797 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 4798 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4799 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4800 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 4801 | ** errors. The reports issued by valgrind are incorrect - we would |
| 4802 | ** prefer that the randomness be increased by making use of the |
| 4803 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 4804 | ** some users. Rather than argue, it seems easier just to initialize |
| 4805 | ** the whole array and silence valgrind, even if that means less randomness |
| 4806 | ** in the random seed. |
| 4807 | ** |
| 4808 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 4809 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4810 | ** tests repeatable. |
| 4811 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4812 | memset(zBuf, 0, nBuf); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4813 | #if !defined(SQLITE_TEST) |
| 4814 | { |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 4815 | int pid, fd; |
| 4816 | fd = open("/dev/urandom", O_RDONLY); |
| 4817 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 4818 | time_t t; |
| 4819 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4820 | memcpy(zBuf, &t, sizeof(t)); |
| 4821 | pid = getpid(); |
| 4822 | memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid)); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 4823 | assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf ); |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 4824 | nBuf = sizeof(t) + sizeof(pid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 4825 | }else{ |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 4826 | nBuf = read(fd, zBuf, nBuf); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 4827 | close(fd); |
| 4828 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4829 | } |
| 4830 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 4831 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4832 | } |
| 4833 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4834 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4835 | /* |
| 4836 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4837 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 4838 | ** The return value is the number of microseconds of sleep actually |
| 4839 | ** requested from the underlying operating system, a number which |
| 4840 | ** might be greater than or equal to the argument, but not less |
| 4841 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4842 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4843 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4844 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4845 | struct timespec sp; |
| 4846 | |
| 4847 | sp.tv_sec = microseconds / 1000000; |
| 4848 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 4849 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 4850 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4851 | return microseconds; |
| 4852 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4853 | usleep(microseconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 4854 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4855 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4856 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4857 | int seconds = (microseconds+999999)/1000000; |
| 4858 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 4859 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 4860 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 4861 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 4862 | } |
| 4863 | |
| 4864 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4865 | ** The following variable, if set to a non-zero value, is interpreted as |
| 4866 | ** the number of seconds since 1970 and is used to set the result of |
| 4867 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4868 | */ |
| 4869 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4870 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4871 | #endif |
| 4872 | |
| 4873 | /* |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 4874 | ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 4875 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 4876 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 4877 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 4878 | ** proleptic Gregorian calendar. |
| 4879 | ** |
| 4880 | ** On success, return 0. Return 1 if the time and date cannot be found. |
| 4881 | */ |
| 4882 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 4883 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
| 4884 | #if defined(NO_GETTOD) |
| 4885 | time_t t; |
| 4886 | time(&t); |
| 4887 | *piNow = ((sqlite3_int64)i)*1000 + unixEpoch; |
| 4888 | #elif OS_VXWORKS |
| 4889 | struct timespec sNow; |
| 4890 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 4891 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 4892 | #else |
| 4893 | struct timeval sNow; |
| 4894 | gettimeofday(&sNow, 0); |
| 4895 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
| 4896 | #endif |
| 4897 | |
| 4898 | #ifdef SQLITE_TEST |
| 4899 | if( sqlite3_current_time ){ |
| 4900 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 4901 | } |
| 4902 | #endif |
| 4903 | UNUSED_PARAMETER(NotUsed); |
| 4904 | return 0; |
| 4905 | } |
| 4906 | |
| 4907 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4908 | ** Find the current time (in Universal Coordinated Time). Write the |
| 4909 | ** current time and date as a Julian Day number into *prNow and |
| 4910 | ** return 0. Return 1 if the time and date cannot be found. |
| 4911 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4912 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 4913 | sqlite3_int64 i; |
| 4914 | unixCurrentTimeInt64(0, &i); |
drh | 0dcb0a7 | 2010-05-03 18:22:52 +0000 | [diff] [blame] | 4915 | *prNow = i/86400000.0; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4916 | return 0; |
| 4917 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4918 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4919 | /* |
| 4920 | ** We added the xGetLastError() method with the intention of providing |
| 4921 | ** better low-level error messages when operating-system problems come up |
| 4922 | ** during SQLite operation. But so far, none of that has been implemented |
| 4923 | ** in the core. So this routine is never called. For now, it is merely |
| 4924 | ** a place-holder. |
| 4925 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4926 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 4927 | UNUSED_PARAMETER(NotUsed); |
| 4928 | UNUSED_PARAMETER(NotUsed2); |
| 4929 | UNUSED_PARAMETER(NotUsed3); |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 4930 | return 0; |
| 4931 | } |
| 4932 | |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 4933 | |
| 4934 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4935 | ************************ End of sqlite3_vfs methods *************************** |
| 4936 | ******************************************************************************/ |
| 4937 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4938 | /****************************************************************************** |
| 4939 | ************************** Begin Proxy Locking ******************************** |
| 4940 | ** |
| 4941 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 4942 | ** other locking methods on secondary lock files. Proxy locking is a |
| 4943 | ** meta-layer over top of the primitive locking implemented above. For |
| 4944 | ** this reason, the division that implements of proxy locking is deferred |
| 4945 | ** until late in the file (here) after all of the other I/O methods have |
| 4946 | ** been defined - so that the primitive locking methods are available |
| 4947 | ** as services to help with the implementation of proxy locking. |
| 4948 | ** |
| 4949 | **** |
| 4950 | ** |
| 4951 | ** The default locking schemes in SQLite use byte-range locks on the |
| 4952 | ** database file to coordinate safe, concurrent access by multiple readers |
| 4953 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 4954 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 4955 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 4956 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 4957 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 4958 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 4959 | ** address in the shared range is taken for a SHARED lock, the entire |
| 4960 | ** shared range is taken for an EXCLUSIVE lock): |
| 4961 | ** |
| 4962 | ** PENDING_BYTE 0x40000000 |
| 4963 | ** RESERVED_BYTE 0x40000001 |
| 4964 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 4965 | ** |
| 4966 | ** This works well on the local file system, but shows a nearly 100x |
| 4967 | ** slowdown in read performance on AFP because the AFP client disables |
| 4968 | ** the read cache when byte-range locks are present. Enabling the read |
| 4969 | ** cache exposes a cache coherency problem that is present on all OS X |
| 4970 | ** supported network file systems. NFS and AFP both observe the |
| 4971 | ** close-to-open semantics for ensuring cache coherency |
| 4972 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 4973 | ** address the requirements for concurrent database access by multiple |
| 4974 | ** readers and writers |
| 4975 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 4976 | ** |
| 4977 | ** To address the performance and cache coherency issues, proxy file locking |
| 4978 | ** changes the way database access is controlled by limiting access to a |
| 4979 | ** single host at a time and moving file locks off of the database file |
| 4980 | ** and onto a proxy file on the local file system. |
| 4981 | ** |
| 4982 | ** |
| 4983 | ** Using proxy locks |
| 4984 | ** ----------------- |
| 4985 | ** |
| 4986 | ** C APIs |
| 4987 | ** |
| 4988 | ** sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE, |
| 4989 | ** <proxy_path> | ":auto:"); |
| 4990 | ** sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>); |
| 4991 | ** |
| 4992 | ** |
| 4993 | ** SQL pragmas |
| 4994 | ** |
| 4995 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 4996 | ** PRAGMA [database.]lock_proxy_file |
| 4997 | ** |
| 4998 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 4999 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 5000 | ** a proxy path based on the user's temp dir |
| 5001 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 5002 | ** actual proxy file name is generated from the name and path of the |
| 5003 | ** database file. For example: |
| 5004 | ** |
| 5005 | ** For database path "/Users/me/foo.db" |
| 5006 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 5007 | ** |
| 5008 | ** Once a lock proxy is configured for a database connection, it can not |
| 5009 | ** be removed, however it may be switched to a different proxy path via |
| 5010 | ** the above APIs (assuming the conch file is not being held by another |
| 5011 | ** connection or process). |
| 5012 | ** |
| 5013 | ** |
| 5014 | ** How proxy locking works |
| 5015 | ** ----------------------- |
| 5016 | ** |
| 5017 | ** Proxy file locking relies primarily on two new supporting files: |
| 5018 | ** |
| 5019 | ** * conch file to limit access to the database file to a single host |
| 5020 | ** at a time |
| 5021 | ** |
| 5022 | ** * proxy file to act as a proxy for the advisory locks normally |
| 5023 | ** taken on the database |
| 5024 | ** |
| 5025 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 5026 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 5027 | ** contents and comparing the host's unique host ID (see below) and lock |
| 5028 | ** proxy path against the values stored in the conch. The conch file is |
| 5029 | ** stored in the same directory as the database file and the file name |
| 5030 | ** is patterned after the database file name as ".<databasename>-conch". |
| 5031 | ** If the conch file does not exist, or it's contents do not match the |
| 5032 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 5033 | ** lock and the conch file contents is updated with the host ID and proxy |
| 5034 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 5035 | ** is held by another process (with a shared lock), the exclusive lock |
| 5036 | ** will fail and SQLITE_BUSY is returned. |
| 5037 | ** |
| 5038 | ** The proxy file - a single-byte file used for all advisory file locks |
| 5039 | ** normally taken on the database file. This allows for safe sharing |
| 5040 | ** of the database file for multiple readers and writers on the same |
| 5041 | ** host (the conch ensures that they all use the same local lock file). |
| 5042 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5043 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 5044 | ** only taken when the first request to lock database file is made. |
| 5045 | ** This matches the semantics of the traditional locking behavior, where |
| 5046 | ** opening a connection to a database file does not take a lock on it. |
| 5047 | ** The shared lock and an open file descriptor are maintained until |
| 5048 | ** the connection to the database is closed. |
| 5049 | ** |
| 5050 | ** The proxy file and the lock file are never deleted so they only need |
| 5051 | ** to be created the first time they are used. |
| 5052 | ** |
| 5053 | ** Configuration options |
| 5054 | ** --------------------- |
| 5055 | ** |
| 5056 | ** SQLITE_PREFER_PROXY_LOCKING |
| 5057 | ** |
| 5058 | ** Database files accessed on non-local file systems are |
| 5059 | ** automatically configured for proxy locking, lock files are |
| 5060 | ** named automatically using the same logic as |
| 5061 | ** PRAGMA lock_proxy_file=":auto:" |
| 5062 | ** |
| 5063 | ** SQLITE_PROXY_DEBUG |
| 5064 | ** |
| 5065 | ** Enables the logging of error messages during host id file |
| 5066 | ** retrieval and creation |
| 5067 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5068 | ** LOCKPROXYDIR |
| 5069 | ** |
| 5070 | ** Overrides the default directory used for lock proxy files that |
| 5071 | ** are named automatically via the ":auto:" setting |
| 5072 | ** |
| 5073 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 5074 | ** |
| 5075 | ** Permissions to use when creating a directory for storing the |
| 5076 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 5077 | ** |
| 5078 | ** |
| 5079 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 5080 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 5081 | ** force proxy locking to be used for every database file opened, and 0 |
| 5082 | ** will force automatic proxy locking to be disabled for all database |
| 5083 | ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or |
| 5084 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 5085 | */ |
| 5086 | |
| 5087 | /* |
| 5088 | ** Proxy locking is only available on MacOSX |
| 5089 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5090 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5091 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5092 | /* |
| 5093 | ** The proxyLockingContext has the path and file structures for the remote |
| 5094 | ** and local proxy files in it |
| 5095 | */ |
| 5096 | typedef struct proxyLockingContext proxyLockingContext; |
| 5097 | struct proxyLockingContext { |
| 5098 | unixFile *conchFile; /* Open conch file */ |
| 5099 | char *conchFilePath; /* Name of the conch file */ |
| 5100 | unixFile *lockProxy; /* Open proxy lock file */ |
| 5101 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 5102 | char *dbPath; /* Name of the open file */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5103 | int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5104 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 5105 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 5106 | }; |
| 5107 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5108 | /* |
| 5109 | ** The proxy lock file path for the database at dbPath is written into lPath, |
| 5110 | ** which must point to valid, writable memory large enough for a maxLen length |
| 5111 | ** file path. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5112 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5113 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 5114 | int len; |
| 5115 | int dbLen; |
| 5116 | int i; |
| 5117 | |
| 5118 | #ifdef LOCKPROXYDIR |
| 5119 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 5120 | #else |
| 5121 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 5122 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5123 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5124 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
| 5125 | lPath, errno, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5126 | return SQLITE_IOERR_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5127 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5128 | len = strlcat(lPath, "sqliteplocks", maxLen); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5129 | } |
| 5130 | # else |
| 5131 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 5132 | # endif |
| 5133 | #endif |
| 5134 | |
| 5135 | if( lPath[len-1]!='/' ){ |
| 5136 | len = strlcat(lPath, "/", maxLen); |
| 5137 | } |
| 5138 | |
| 5139 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5140 | dbLen = (int)strlen(dbPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5141 | for( i=0; i<dbLen && (i+len+7)<maxLen; i++){ |
| 5142 | char c = dbPath[i]; |
| 5143 | lPath[i+len] = (c=='/')?'_':c; |
| 5144 | } |
| 5145 | lPath[i+len]='\0'; |
| 5146 | strlcat(lPath, ":auto:", maxLen); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5147 | OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5148 | return SQLITE_OK; |
| 5149 | } |
| 5150 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5151 | /* |
| 5152 | ** Creates the lock file and any missing directories in lockPath |
| 5153 | */ |
| 5154 | static int proxyCreateLockPath(const char *lockPath){ |
| 5155 | int i, len; |
| 5156 | char buf[MAXPATHLEN]; |
| 5157 | int start = 0; |
| 5158 | |
| 5159 | assert(lockPath!=NULL); |
| 5160 | /* try to create all the intermediate directories */ |
| 5161 | len = (int)strlen(lockPath); |
| 5162 | buf[0] = lockPath[0]; |
| 5163 | for( i=1; i<len; i++ ){ |
| 5164 | if( lockPath[i] == '/' && (i - start > 0) ){ |
| 5165 | /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 5166 | if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 5167 | || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 5168 | buf[i]='\0'; |
| 5169 | if( mkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
| 5170 | int err=errno; |
| 5171 | if( err!=EEXIST ) { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5172 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5173 | "'%s' proxy lock path=%s pid=%d\n", |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5174 | buf, strerror(err), lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5175 | return err; |
| 5176 | } |
| 5177 | } |
| 5178 | } |
| 5179 | start=i+1; |
| 5180 | } |
| 5181 | buf[i] = lockPath[i]; |
| 5182 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5183 | OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5184 | return 0; |
| 5185 | } |
| 5186 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5187 | /* |
| 5188 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 5189 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 5190 | ** |
| 5191 | ** The caller is responsible not only for closing the file descriptor |
| 5192 | ** but also for freeing the memory associated with the file descriptor. |
| 5193 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5194 | static int proxyCreateUnixFile( |
| 5195 | const char *path, /* path for the new unixFile */ |
| 5196 | unixFile **ppFile, /* unixFile created and returned by ref */ |
| 5197 | int islockfile /* if non zero missing dirs will be created */ |
| 5198 | ) { |
| 5199 | int fd = -1; |
| 5200 | int dirfd = -1; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5201 | unixFile *pNew; |
| 5202 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5203 | int openFlags = O_RDWR | O_CREAT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5204 | sqlite3_vfs dummyVfs; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5205 | int terrno = 0; |
| 5206 | UnixUnusedFd *pUnused = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5207 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5208 | /* 1. first try to open/create the file |
| 5209 | ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 5210 | ** the parent directories and then try again. |
| 5211 | ** 3. if that fails, try to open the file read-only |
| 5212 | ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 5213 | */ |
| 5214 | pUnused = findReusableFd(path, openFlags); |
| 5215 | if( pUnused ){ |
| 5216 | fd = pUnused->fd; |
| 5217 | }else{ |
| 5218 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
| 5219 | if( !pUnused ){ |
| 5220 | return SQLITE_NOMEM; |
| 5221 | } |
| 5222 | } |
| 5223 | if( fd<0 ){ |
| 5224 | fd = open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5225 | terrno = errno; |
| 5226 | if( fd<0 && errno==ENOENT && islockfile ){ |
| 5227 | if( proxyCreateLockPath(path) == SQLITE_OK ){ |
| 5228 | fd = open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5229 | } |
| 5230 | } |
| 5231 | } |
| 5232 | if( fd<0 ){ |
| 5233 | openFlags = O_RDONLY; |
| 5234 | fd = open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5235 | terrno = errno; |
| 5236 | } |
| 5237 | if( fd<0 ){ |
| 5238 | if( islockfile ){ |
| 5239 | return SQLITE_BUSY; |
| 5240 | } |
| 5241 | switch (terrno) { |
| 5242 | case EACCES: |
| 5243 | return SQLITE_PERM; |
| 5244 | case EIO: |
| 5245 | return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 5246 | default: |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 5247 | return SQLITE_CANTOPEN_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5248 | } |
| 5249 | } |
| 5250 | |
| 5251 | pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew)); |
| 5252 | if( pNew==NULL ){ |
| 5253 | rc = SQLITE_NOMEM; |
| 5254 | goto end_create_proxy; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5255 | } |
| 5256 | memset(pNew, 0, sizeof(unixFile)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5257 | pNew->openFlags = openFlags; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5258 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5259 | pUnused->fd = fd; |
| 5260 | pUnused->flags = openFlags; |
| 5261 | pNew->pUnused = pUnused; |
| 5262 | |
| 5263 | rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0); |
| 5264 | if( rc==SQLITE_OK ){ |
| 5265 | *ppFile = pNew; |
| 5266 | return SQLITE_OK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5267 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5268 | end_create_proxy: |
| 5269 | close(fd); /* silently leak fd if error, we're already in error */ |
| 5270 | sqlite3_free(pNew); |
| 5271 | sqlite3_free(pUnused); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5272 | return rc; |
| 5273 | } |
| 5274 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5275 | #ifdef SQLITE_TEST |
| 5276 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 5277 | int sqlite3_hostid_num = 0; |
| 5278 | #endif |
| 5279 | |
| 5280 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 5281 | |
| 5282 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 5283 | ** bytes of writable memory. |
| 5284 | */ |
| 5285 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
| 5286 | struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
| 5287 | |
| 5288 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 5289 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
| 5290 | if( gethostuuid(pHostID, &timeout) ){ |
| 5291 | int err = errno; |
| 5292 | if( pError ){ |
| 5293 | *pError = err; |
| 5294 | } |
| 5295 | return SQLITE_IOERR; |
| 5296 | } |
| 5297 | #ifdef SQLITE_TEST |
| 5298 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 5299 | if( sqlite3_hostid_num != 0){ |
| 5300 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 5301 | } |
| 5302 | #endif |
| 5303 | |
| 5304 | return SQLITE_OK; |
| 5305 | } |
| 5306 | |
| 5307 | /* The conch file contains the header, host id and lock file path |
| 5308 | */ |
| 5309 | #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 5310 | #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 5311 | #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 5312 | #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 5313 | |
| 5314 | /* |
| 5315 | ** Takes an open conch file, copies the contents to a new path and then moves |
| 5316 | ** it back. The newly created file's file descriptor is assigned to the |
| 5317 | ** conch file structure and finally the original conch file descriptor is |
| 5318 | ** closed. Returns zero if successful. |
| 5319 | */ |
| 5320 | static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 5321 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5322 | unixFile *conchFile = pCtx->conchFile; |
| 5323 | char tPath[MAXPATHLEN]; |
| 5324 | char buf[PROXY_MAXCONCHLEN]; |
| 5325 | char *cPath = pCtx->conchFilePath; |
| 5326 | size_t readLen = 0; |
| 5327 | size_t pathLen = 0; |
| 5328 | char errmsg[64] = ""; |
| 5329 | int fd = -1; |
| 5330 | int rc = -1; |
| 5331 | |
| 5332 | /* create a new path by replace the trailing '-conch' with '-break' */ |
| 5333 | pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 5334 | if( pathLen>MAXPATHLEN || pathLen<6 || |
| 5335 | (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
| 5336 | sprintf(errmsg, "path error (len %d)", (int)pathLen); |
| 5337 | goto end_breaklock; |
| 5338 | } |
| 5339 | /* read the conch content */ |
| 5340 | readLen = pread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
| 5341 | if( readLen<PROXY_PATHINDEX ){ |
| 5342 | sprintf(errmsg, "read error (len %d)", (int)readLen); |
| 5343 | goto end_breaklock; |
| 5344 | } |
| 5345 | /* write it out to the temporary break file */ |
| 5346 | fd = open(tPath, (O_RDWR|O_CREAT|O_EXCL), SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5347 | if( fd<0 ){ |
| 5348 | sprintf(errmsg, "create failed (%d)", errno); |
| 5349 | goto end_breaklock; |
| 5350 | } |
| 5351 | if( pwrite(fd, buf, readLen, 0) != readLen ){ |
| 5352 | sprintf(errmsg, "write failed (%d)", errno); |
| 5353 | goto end_breaklock; |
| 5354 | } |
| 5355 | if( rename(tPath, cPath) ){ |
| 5356 | sprintf(errmsg, "rename failed (%d)", errno); |
| 5357 | goto end_breaklock; |
| 5358 | } |
| 5359 | rc = 0; |
| 5360 | fprintf(stderr, "broke stale lock on %s\n", cPath); |
| 5361 | close(conchFile->h); |
| 5362 | conchFile->h = fd; |
| 5363 | conchFile->openFlags = O_RDWR | O_CREAT; |
| 5364 | |
| 5365 | end_breaklock: |
| 5366 | if( rc ){ |
| 5367 | if( fd>=0 ){ |
| 5368 | unlink(tPath); |
| 5369 | close(fd); |
| 5370 | } |
| 5371 | fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 5372 | } |
| 5373 | return rc; |
| 5374 | } |
| 5375 | |
| 5376 | /* Take the requested lock on the conch file and break a stale lock if the |
| 5377 | ** host id matches. |
| 5378 | */ |
| 5379 | static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 5380 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5381 | unixFile *conchFile = pCtx->conchFile; |
| 5382 | int rc = SQLITE_OK; |
| 5383 | int nTries = 0; |
| 5384 | struct timespec conchModTime; |
| 5385 | |
| 5386 | do { |
| 5387 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 5388 | nTries ++; |
| 5389 | if( rc==SQLITE_BUSY ){ |
| 5390 | /* If the lock failed (busy): |
| 5391 | * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 5392 | * 2nd try: fail if the mod time changed or host id is different, wait |
| 5393 | * 10 sec and try again |
| 5394 | * 3rd try: break the lock unless the mod time has changed. |
| 5395 | */ |
| 5396 | struct stat buf; |
| 5397 | if( fstat(conchFile->h, &buf) ){ |
| 5398 | pFile->lastErrno = errno; |
| 5399 | return SQLITE_IOERR_LOCK; |
| 5400 | } |
| 5401 | |
| 5402 | if( nTries==1 ){ |
| 5403 | conchModTime = buf.st_mtimespec; |
| 5404 | usleep(500000); /* wait 0.5 sec and try the lock again*/ |
| 5405 | continue; |
| 5406 | } |
| 5407 | |
| 5408 | assert( nTries>1 ); |
| 5409 | if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 5410 | conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 5411 | return SQLITE_BUSY; |
| 5412 | } |
| 5413 | |
| 5414 | if( nTries==2 ){ |
| 5415 | char tBuf[PROXY_MAXCONCHLEN]; |
| 5416 | int len = pread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
| 5417 | if( len<0 ){ |
| 5418 | pFile->lastErrno = errno; |
| 5419 | return SQLITE_IOERR_LOCK; |
| 5420 | } |
| 5421 | if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 5422 | /* don't break the lock if the host id doesn't match */ |
| 5423 | if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 5424 | return SQLITE_BUSY; |
| 5425 | } |
| 5426 | }else{ |
| 5427 | /* don't break the lock on short read or a version mismatch */ |
| 5428 | return SQLITE_BUSY; |
| 5429 | } |
| 5430 | usleep(10000000); /* wait 10 sec and try the lock again */ |
| 5431 | continue; |
| 5432 | } |
| 5433 | |
| 5434 | assert( nTries==3 ); |
| 5435 | if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 5436 | rc = SQLITE_OK; |
| 5437 | if( lockType==EXCLUSIVE_LOCK ){ |
| 5438 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 5439 | } |
| 5440 | if( !rc ){ |
| 5441 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 5442 | } |
| 5443 | } |
| 5444 | } |
| 5445 | } while( rc==SQLITE_BUSY && nTries<3 ); |
| 5446 | |
| 5447 | return rc; |
| 5448 | } |
| 5449 | |
| 5450 | /* 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] | 5451 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 5452 | ** lockPath means that the lockPath in the conch file will be used if the |
| 5453 | ** host IDs match, or a new lock path will be generated automatically |
| 5454 | ** and written to the conch file. |
| 5455 | */ |
| 5456 | static int proxyTakeConch(unixFile *pFile){ |
| 5457 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5458 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5459 | if( pCtx->conchHeld!=0 ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5460 | return SQLITE_OK; |
| 5461 | }else{ |
| 5462 | unixFile *conchFile = pCtx->conchFile; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5463 | uuid_t myHostID; |
| 5464 | int pError = 0; |
| 5465 | char readBuf[PROXY_MAXCONCHLEN]; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5466 | char lockPath[MAXPATHLEN]; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5467 | char *tempLockPath = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5468 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5469 | int createConch = 0; |
| 5470 | int hostIdMatch = 0; |
| 5471 | int readLen = 0; |
| 5472 | int tryOldLockPath = 0; |
| 5473 | int forceNewLockPath = 0; |
| 5474 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5475 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
| 5476 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5477 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5478 | rc = proxyGetHostID(myHostID, &pError); |
| 5479 | if( (rc&0xff)==SQLITE_IOERR ){ |
| 5480 | pFile->lastErrno = pError; |
| 5481 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5482 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5483 | rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5484 | if( rc!=SQLITE_OK ){ |
| 5485 | goto end_takeconch; |
| 5486 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5487 | /* read the existing conch file */ |
| 5488 | readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 5489 | if( readLen<0 ){ |
| 5490 | /* I/O error: lastErrno set by seekAndRead */ |
| 5491 | pFile->lastErrno = conchFile->lastErrno; |
| 5492 | rc = SQLITE_IOERR_READ; |
| 5493 | goto end_takeconch; |
| 5494 | }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 5495 | readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 5496 | /* a short read or version format mismatch means we need to create a new |
| 5497 | ** conch file. |
| 5498 | */ |
| 5499 | createConch = 1; |
| 5500 | } |
| 5501 | /* if the host id matches and the lock path already exists in the conch |
| 5502 | ** we'll try to use the path there, if we can't open that path, we'll |
| 5503 | ** retry with a new auto-generated path |
| 5504 | */ |
| 5505 | do { /* in case we need to try again for an :auto: named lock file */ |
| 5506 | |
| 5507 | if( !createConch && !forceNewLockPath ){ |
| 5508 | hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 5509 | PROXY_HOSTIDLEN); |
| 5510 | /* if the conch has data compare the contents */ |
| 5511 | if( !pCtx->lockProxyPath ){ |
| 5512 | /* for auto-named local lock file, just check the host ID and we'll |
| 5513 | ** use the local lock file path that's already in there |
| 5514 | */ |
| 5515 | if( hostIdMatch ){ |
| 5516 | size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 5517 | |
| 5518 | if( pathLen>=MAXPATHLEN ){ |
| 5519 | pathLen=MAXPATHLEN-1; |
| 5520 | } |
| 5521 | memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 5522 | lockPath[pathLen] = 0; |
| 5523 | tempLockPath = lockPath; |
| 5524 | tryOldLockPath = 1; |
| 5525 | /* create a copy of the lock path if the conch is taken */ |
| 5526 | goto end_takeconch; |
| 5527 | } |
| 5528 | }else if( hostIdMatch |
| 5529 | && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 5530 | readLen-PROXY_PATHINDEX) |
| 5531 | ){ |
| 5532 | /* conch host and lock path match */ |
| 5533 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5534 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5535 | } |
| 5536 | |
| 5537 | /* if the conch isn't writable and doesn't match, we can't take it */ |
| 5538 | if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 5539 | rc = SQLITE_BUSY; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5540 | goto end_takeconch; |
| 5541 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5542 | |
| 5543 | /* 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] | 5544 | if( !pCtx->lockProxyPath ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5545 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 5546 | tempLockPath = lockPath; |
| 5547 | /* create a copy of the lock path _only_ if the conch is taken */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5548 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5549 | |
| 5550 | /* update conch with host and path (this will fail if other process |
| 5551 | ** has a shared lock already), if the host id matches, use the big |
| 5552 | ** stick. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5553 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5554 | futimes(conchFile->h, NULL); |
| 5555 | if( hostIdMatch && !createConch ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5556 | if( conchFile->pInode && conchFile->pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5557 | /* We are trying for an exclusive lock but another thread in this |
| 5558 | ** same process is still holding a shared lock. */ |
| 5559 | rc = SQLITE_BUSY; |
| 5560 | } else { |
| 5561 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5562 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5563 | }else{ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5564 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5565 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5566 | if( rc==SQLITE_OK ){ |
| 5567 | char writeBuffer[PROXY_MAXCONCHLEN]; |
| 5568 | int writeSize = 0; |
| 5569 | |
| 5570 | writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 5571 | memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 5572 | if( pCtx->lockProxyPath!=NULL ){ |
| 5573 | strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN); |
| 5574 | }else{ |
| 5575 | strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 5576 | } |
| 5577 | writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
| 5578 | ftruncate(conchFile->h, writeSize); |
| 5579 | rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
| 5580 | fsync(conchFile->h); |
| 5581 | /* If we created a new conch file (not just updated the contents of a |
| 5582 | ** valid conch file), try to match the permissions of the database |
| 5583 | */ |
| 5584 | if( rc==SQLITE_OK && createConch ){ |
| 5585 | struct stat buf; |
| 5586 | int err = fstat(pFile->h, &buf); |
| 5587 | if( err==0 ){ |
| 5588 | mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 5589 | S_IROTH|S_IWOTH); |
| 5590 | /* try to match the database file R/W permissions, ignore failure */ |
| 5591 | #ifndef SQLITE_PROXY_DEBUG |
| 5592 | fchmod(conchFile->h, cmode); |
| 5593 | #else |
| 5594 | if( fchmod(conchFile->h, cmode)!=0 ){ |
| 5595 | int code = errno; |
| 5596 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 5597 | cmode, code, strerror(code)); |
| 5598 | } else { |
| 5599 | fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 5600 | } |
| 5601 | }else{ |
| 5602 | int code = errno; |
| 5603 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 5604 | err, code, strerror(code)); |
| 5605 | #endif |
| 5606 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5607 | } |
| 5608 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5609 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 5610 | |
| 5611 | end_takeconch: |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5612 | OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5613 | if( rc==SQLITE_OK && pFile->openFlags ){ |
| 5614 | if( pFile->h>=0 ){ |
| 5615 | #ifdef STRICT_CLOSE_ERROR |
| 5616 | if( close(pFile->h) ){ |
| 5617 | pFile->lastErrno = errno; |
| 5618 | return SQLITE_IOERR_CLOSE; |
| 5619 | } |
| 5620 | #else |
| 5621 | close(pFile->h); /* silently leak fd if fail */ |
| 5622 | #endif |
| 5623 | } |
| 5624 | pFile->h = -1; |
| 5625 | int fd = open(pCtx->dbPath, pFile->openFlags, |
| 5626 | SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5627 | OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5628 | if( fd>=0 ){ |
| 5629 | pFile->h = fd; |
| 5630 | }else{ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 5631 | rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5632 | during locking */ |
| 5633 | } |
| 5634 | } |
| 5635 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 5636 | char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 5637 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 5638 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 5639 | /* we couldn't create the proxy lock file with the old lock file path |
| 5640 | ** so try again via auto-naming |
| 5641 | */ |
| 5642 | forceNewLockPath = 1; |
| 5643 | tryOldLockPath = 0; |
dan | 2b0ef47 | 2010-02-16 12:18:47 +0000 | [diff] [blame] | 5644 | continue; /* go back to the do {} while start point, try again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5645 | } |
| 5646 | } |
| 5647 | if( rc==SQLITE_OK ){ |
| 5648 | /* Need to make a copy of path if we extracted the value |
| 5649 | ** from the conch file or the path was allocated on the stack |
| 5650 | */ |
| 5651 | if( tempLockPath ){ |
| 5652 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 5653 | if( !pCtx->lockProxyPath ){ |
| 5654 | rc = SQLITE_NOMEM; |
| 5655 | } |
| 5656 | } |
| 5657 | } |
| 5658 | if( rc==SQLITE_OK ){ |
| 5659 | pCtx->conchHeld = 1; |
| 5660 | |
| 5661 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 5662 | afpLockingContext *afpCtx; |
| 5663 | afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 5664 | afpCtx->dbPath = pCtx->lockProxyPath; |
| 5665 | } |
| 5666 | } else { |
| 5667 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 5668 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5669 | OSTRACE(("TAKECONCH %d %s\n", conchFile->h, |
| 5670 | rc==SQLITE_OK?"ok":"failed")); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5671 | return rc; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5672 | } while (1); /* in case we need to retry the :auto: lock file - |
| 5673 | ** we should never get here except via the 'continue' call. */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5674 | } |
| 5675 | } |
| 5676 | |
| 5677 | /* |
| 5678 | ** If pFile holds a lock on a conch file, then release that lock. |
| 5679 | */ |
| 5680 | static int proxyReleaseConch(unixFile *pFile){ |
drh | 1c5bb4d | 2010-05-10 17:29:28 +0000 | [diff] [blame] | 5681 | int rc = SQLITE_OK; /* Subroutine return code */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5682 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 5683 | unixFile *conchFile; /* Name of the conch file */ |
| 5684 | |
| 5685 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5686 | conchFile = pCtx->conchFile; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5687 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5688 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5689 | getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5690 | if( pCtx->conchHeld>0 ){ |
| 5691 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 5692 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5693 | pCtx->conchHeld = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5694 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| 5695 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5696 | return rc; |
| 5697 | } |
| 5698 | |
| 5699 | /* |
| 5700 | ** Given the name of a database file, compute the name of its conch file. |
| 5701 | ** Store the conch filename in memory obtained from sqlite3_malloc(). |
| 5702 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 5703 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 5704 | ** |
| 5705 | ** The caller is responsible for ensuring that the allocated memory |
| 5706 | ** space is eventually freed. |
| 5707 | ** |
| 5708 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 5709 | */ |
| 5710 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 5711 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5712 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5713 | char *conchPath; /* buffer in which to construct conch name */ |
| 5714 | |
| 5715 | /* Allocate space for the conch filename and initialize the name to |
| 5716 | ** the name of the original database file. */ |
| 5717 | *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8); |
| 5718 | if( conchPath==0 ){ |
| 5719 | return SQLITE_NOMEM; |
| 5720 | } |
| 5721 | memcpy(conchPath, dbPath, len+1); |
| 5722 | |
| 5723 | /* now insert a "." before the last / character */ |
| 5724 | for( i=(len-1); i>=0; i-- ){ |
| 5725 | if( conchPath[i]=='/' ){ |
| 5726 | i++; |
| 5727 | break; |
| 5728 | } |
| 5729 | } |
| 5730 | conchPath[i]='.'; |
| 5731 | while ( i<len ){ |
| 5732 | conchPath[i+1]=dbPath[i]; |
| 5733 | i++; |
| 5734 | } |
| 5735 | |
| 5736 | /* append the "-conch" suffix to the file */ |
| 5737 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5738 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5739 | |
| 5740 | return SQLITE_OK; |
| 5741 | } |
| 5742 | |
| 5743 | |
| 5744 | /* Takes a fully configured proxy locking-style unix file and switches |
| 5745 | ** the local lock file path |
| 5746 | */ |
| 5747 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 5748 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 5749 | char *oldPath = pCtx->lockProxyPath; |
| 5750 | int rc = SQLITE_OK; |
| 5751 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5752 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5753 | return SQLITE_BUSY; |
| 5754 | } |
| 5755 | |
| 5756 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 5757 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 5758 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 5759 | return SQLITE_OK; |
| 5760 | }else{ |
| 5761 | unixFile *lockProxy = pCtx->lockProxy; |
| 5762 | pCtx->lockProxy=NULL; |
| 5763 | pCtx->conchHeld = 0; |
| 5764 | if( lockProxy!=NULL ){ |
| 5765 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 5766 | if( rc ) return rc; |
| 5767 | sqlite3_free(lockProxy); |
| 5768 | } |
| 5769 | sqlite3_free(oldPath); |
| 5770 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 5771 | } |
| 5772 | |
| 5773 | return rc; |
| 5774 | } |
| 5775 | |
| 5776 | /* |
| 5777 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 5778 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 5779 | ** |
| 5780 | ** This routine find the filename associated with pFile and writes it |
| 5781 | ** int dbPath. |
| 5782 | */ |
| 5783 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5784 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5785 | if( pFile->pMethod == &afpIoMethods ){ |
| 5786 | /* afp style keeps a reference to the db path in the filePath field |
| 5787 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5788 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5789 | strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN); |
| 5790 | } else |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5791 | #endif |
| 5792 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 5793 | /* dot lock style uses the locking context to store the dot lock |
| 5794 | ** file path */ |
| 5795 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 5796 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 5797 | }else{ |
| 5798 | /* all other styles use the locking context to store the db file path */ |
| 5799 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5800 | strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5801 | } |
| 5802 | return SQLITE_OK; |
| 5803 | } |
| 5804 | |
| 5805 | /* |
| 5806 | ** Takes an already filled in unix file and alters it so all file locking |
| 5807 | ** will be performed on the local proxy lock file. The following fields |
| 5808 | ** are preserved in the locking context so that they can be restored and |
| 5809 | ** the unix structure properly cleaned up at close time: |
| 5810 | ** ->lockingContext |
| 5811 | ** ->pMethod |
| 5812 | */ |
| 5813 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 5814 | proxyLockingContext *pCtx; |
| 5815 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 5816 | char *lockPath=NULL; |
| 5817 | int rc = SQLITE_OK; |
| 5818 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5819 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5820 | return SQLITE_BUSY; |
| 5821 | } |
| 5822 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 5823 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 5824 | lockPath=NULL; |
| 5825 | }else{ |
| 5826 | lockPath=(char *)path; |
| 5827 | } |
| 5828 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5829 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
| 5830 | (lockPath ? lockPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5831 | |
| 5832 | pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 5833 | if( pCtx==0 ){ |
| 5834 | return SQLITE_NOMEM; |
| 5835 | } |
| 5836 | memset(pCtx, 0, sizeof(*pCtx)); |
| 5837 | |
| 5838 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 5839 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5840 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 5841 | if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 5842 | /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 5843 | ** (c) the file system is read-only, then enable no-locking access. |
| 5844 | ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 5845 | ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 5846 | */ |
| 5847 | struct statfs fsInfo; |
| 5848 | struct stat conchInfo; |
| 5849 | int goLockless = 0; |
| 5850 | |
| 5851 | if( stat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
| 5852 | int err = errno; |
| 5853 | if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 5854 | goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 5855 | } |
| 5856 | } |
| 5857 | if( goLockless ){ |
| 5858 | pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 5859 | rc = SQLITE_OK; |
| 5860 | } |
| 5861 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5862 | } |
| 5863 | if( rc==SQLITE_OK && lockPath ){ |
| 5864 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 5865 | } |
| 5866 | |
| 5867 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5868 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 5869 | if( pCtx->dbPath==NULL ){ |
| 5870 | rc = SQLITE_NOMEM; |
| 5871 | } |
| 5872 | } |
| 5873 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5874 | /* all memory is allocated, proxys are created and assigned, |
| 5875 | ** switch the locking context and pMethod then return. |
| 5876 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5877 | pCtx->oldLockingContext = pFile->lockingContext; |
| 5878 | pFile->lockingContext = pCtx; |
| 5879 | pCtx->pOldMethod = pFile->pMethod; |
| 5880 | pFile->pMethod = &proxyIoMethods; |
| 5881 | }else{ |
| 5882 | if( pCtx->conchFile ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5883 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5884 | sqlite3_free(pCtx->conchFile); |
| 5885 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5886 | sqlite3_free(pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5887 | sqlite3_free(pCtx->conchFilePath); |
| 5888 | sqlite3_free(pCtx); |
| 5889 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5890 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 5891 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5892 | return rc; |
| 5893 | } |
| 5894 | |
| 5895 | |
| 5896 | /* |
| 5897 | ** This routine handles sqlite3_file_control() calls that are specific |
| 5898 | ** to proxy locking. |
| 5899 | */ |
| 5900 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 5901 | switch( op ){ |
| 5902 | case SQLITE_GET_LOCKPROXYFILE: { |
| 5903 | unixFile *pFile = (unixFile*)id; |
| 5904 | if( pFile->pMethod == &proxyIoMethods ){ |
| 5905 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 5906 | proxyTakeConch(pFile); |
| 5907 | if( pCtx->lockProxyPath ){ |
| 5908 | *(const char **)pArg = pCtx->lockProxyPath; |
| 5909 | }else{ |
| 5910 | *(const char **)pArg = ":auto: (not held)"; |
| 5911 | } |
| 5912 | } else { |
| 5913 | *(const char **)pArg = NULL; |
| 5914 | } |
| 5915 | return SQLITE_OK; |
| 5916 | } |
| 5917 | case SQLITE_SET_LOCKPROXYFILE: { |
| 5918 | unixFile *pFile = (unixFile*)id; |
| 5919 | int rc = SQLITE_OK; |
| 5920 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 5921 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 5922 | if( isProxyStyle ){ |
| 5923 | /* turn off proxy locking - not supported */ |
| 5924 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 5925 | }else{ |
| 5926 | /* turn off proxy locking - already off - NOOP */ |
| 5927 | rc = SQLITE_OK; |
| 5928 | } |
| 5929 | }else{ |
| 5930 | const char *proxyPath = (const char *)pArg; |
| 5931 | if( isProxyStyle ){ |
| 5932 | proxyLockingContext *pCtx = |
| 5933 | (proxyLockingContext*)pFile->lockingContext; |
| 5934 | if( !strcmp(pArg, ":auto:") |
| 5935 | || (pCtx->lockProxyPath && |
| 5936 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 5937 | ){ |
| 5938 | rc = SQLITE_OK; |
| 5939 | }else{ |
| 5940 | rc = switchLockProxyPath(pFile, proxyPath); |
| 5941 | } |
| 5942 | }else{ |
| 5943 | /* turn on proxy file locking */ |
| 5944 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 5945 | } |
| 5946 | } |
| 5947 | return rc; |
| 5948 | } |
| 5949 | default: { |
| 5950 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 5951 | } |
| 5952 | } |
| 5953 | /*NOTREACHED*/ |
| 5954 | return SQLITE_ERROR; |
| 5955 | } |
| 5956 | |
| 5957 | /* |
| 5958 | ** Within this division (the proxying locking implementation) the procedures |
| 5959 | ** above this point are all utilities. The lock-related methods of the |
| 5960 | ** proxy-locking sqlite3_io_method object follow. |
| 5961 | */ |
| 5962 | |
| 5963 | |
| 5964 | /* |
| 5965 | ** This routine checks if there is a RESERVED lock held on the specified |
| 5966 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 5967 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 5968 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 5969 | */ |
| 5970 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 5971 | unixFile *pFile = (unixFile*)id; |
| 5972 | int rc = proxyTakeConch(pFile); |
| 5973 | if( rc==SQLITE_OK ){ |
| 5974 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5975 | if( pCtx->conchHeld>0 ){ |
| 5976 | unixFile *proxy = pCtx->lockProxy; |
| 5977 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 5978 | }else{ /* conchHeld < 0 is lockless */ |
| 5979 | pResOut=0; |
| 5980 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5981 | } |
| 5982 | return rc; |
| 5983 | } |
| 5984 | |
| 5985 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5986 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5987 | ** of the following: |
| 5988 | ** |
| 5989 | ** (1) SHARED_LOCK |
| 5990 | ** (2) RESERVED_LOCK |
| 5991 | ** (3) PENDING_LOCK |
| 5992 | ** (4) EXCLUSIVE_LOCK |
| 5993 | ** |
| 5994 | ** Sometimes when requesting one lock state, additional lock states |
| 5995 | ** are inserted in between. The locking might fail on one of the later |
| 5996 | ** transitions leaving the lock state different from what it started but |
| 5997 | ** still short of its goal. The following chart shows the allowed |
| 5998 | ** transitions and the inserted intermediate states: |
| 5999 | ** |
| 6000 | ** UNLOCKED -> SHARED |
| 6001 | ** SHARED -> RESERVED |
| 6002 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 6003 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 6004 | ** PENDING -> EXCLUSIVE |
| 6005 | ** |
| 6006 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 6007 | ** routine to lower a locking level. |
| 6008 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6009 | static int proxyLock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6010 | unixFile *pFile = (unixFile*)id; |
| 6011 | int rc = proxyTakeConch(pFile); |
| 6012 | if( rc==SQLITE_OK ){ |
| 6013 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6014 | if( pCtx->conchHeld>0 ){ |
| 6015 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6016 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); |
| 6017 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6018 | }else{ |
| 6019 | /* conchHeld < 0 is lockless */ |
| 6020 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6021 | } |
| 6022 | return rc; |
| 6023 | } |
| 6024 | |
| 6025 | |
| 6026 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6027 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6028 | ** must be either NO_LOCK or SHARED_LOCK. |
| 6029 | ** |
| 6030 | ** If the locking level of the file descriptor is already at or below |
| 6031 | ** the requested locking level, this routine is a no-op. |
| 6032 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6033 | static int proxyUnlock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6034 | unixFile *pFile = (unixFile*)id; |
| 6035 | int rc = proxyTakeConch(pFile); |
| 6036 | if( rc==SQLITE_OK ){ |
| 6037 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6038 | if( pCtx->conchHeld>0 ){ |
| 6039 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6040 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); |
| 6041 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6042 | }else{ |
| 6043 | /* conchHeld < 0 is lockless */ |
| 6044 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6045 | } |
| 6046 | return rc; |
| 6047 | } |
| 6048 | |
| 6049 | /* |
| 6050 | ** Close a file that uses proxy locks. |
| 6051 | */ |
| 6052 | static int proxyClose(sqlite3_file *id) { |
| 6053 | if( id ){ |
| 6054 | unixFile *pFile = (unixFile*)id; |
| 6055 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6056 | unixFile *lockProxy = pCtx->lockProxy; |
| 6057 | unixFile *conchFile = pCtx->conchFile; |
| 6058 | int rc = SQLITE_OK; |
| 6059 | |
| 6060 | if( lockProxy ){ |
| 6061 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 6062 | if( rc ) return rc; |
| 6063 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 6064 | if( rc ) return rc; |
| 6065 | sqlite3_free(lockProxy); |
| 6066 | pCtx->lockProxy = 0; |
| 6067 | } |
| 6068 | if( conchFile ){ |
| 6069 | if( pCtx->conchHeld ){ |
| 6070 | rc = proxyReleaseConch(pFile); |
| 6071 | if( rc ) return rc; |
| 6072 | } |
| 6073 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 6074 | if( rc ) return rc; |
| 6075 | sqlite3_free(conchFile); |
| 6076 | } |
| 6077 | sqlite3_free(pCtx->lockProxyPath); |
| 6078 | sqlite3_free(pCtx->conchFilePath); |
| 6079 | sqlite3_free(pCtx->dbPath); |
| 6080 | /* restore the original locking context and pMethod then close it */ |
| 6081 | pFile->lockingContext = pCtx->oldLockingContext; |
| 6082 | pFile->pMethod = pCtx->pOldMethod; |
| 6083 | sqlite3_free(pCtx); |
| 6084 | return pFile->pMethod->xClose(id); |
| 6085 | } |
| 6086 | return SQLITE_OK; |
| 6087 | } |
| 6088 | |
| 6089 | |
| 6090 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6091 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6092 | /* |
| 6093 | ** The proxy locking style is intended for use with AFP filesystems. |
| 6094 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 6095 | ** restricted to MacOSX. |
| 6096 | ** |
| 6097 | ** |
| 6098 | ******************* End of the proxy lock implementation ********************** |
| 6099 | ******************************************************************************/ |
| 6100 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6101 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6102 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6103 | ** |
| 6104 | ** This routine registers all VFS implementations for unix-like operating |
| 6105 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 6106 | ** should be the only routines in this file that are visible from other |
| 6107 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6108 | ** |
| 6109 | ** This routine is called once during SQLite initialization and by a |
| 6110 | ** single thread. The memory allocation and mutex subsystems have not |
| 6111 | ** necessarily been initialized when this routine is called, and so they |
| 6112 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6113 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6114 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6115 | /* |
| 6116 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6117 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 6118 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 6119 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 6120 | ** and so we have to go through the intermediate pointer to avoid problems |
| 6121 | ** when compiling with -pedantic-errors on GCC.) |
| 6122 | ** |
| 6123 | ** 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] | 6124 | ** finder-function. The finder-function returns a pointer to the |
| 6125 | ** sqlite_io_methods object that implements the desired locking |
| 6126 | ** behaviors. See the division above that contains the IOMETHODS |
| 6127 | ** macro for addition information on finder-functions. |
| 6128 | ** |
| 6129 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 6130 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 6131 | ** more than that; it looks at the filesystem type that hosts the |
| 6132 | ** database file and tries to choose an locking method appropriate for |
| 6133 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6134 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6135 | #define UNIXVFS(VFSNAME, FINDER) { \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6136 | 2, /* iVersion */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6137 | sizeof(unixFile), /* szOsFile */ \ |
| 6138 | MAX_PATHNAME, /* mxPathname */ \ |
| 6139 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6140 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6141 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6142 | unixOpen, /* xOpen */ \ |
| 6143 | unixDelete, /* xDelete */ \ |
| 6144 | unixAccess, /* xAccess */ \ |
| 6145 | unixFullPathname, /* xFullPathname */ \ |
| 6146 | unixDlOpen, /* xDlOpen */ \ |
| 6147 | unixDlError, /* xDlError */ \ |
| 6148 | unixDlSym, /* xDlSym */ \ |
| 6149 | unixDlClose, /* xDlClose */ \ |
| 6150 | unixRandomness, /* xRandomness */ \ |
| 6151 | unixSleep, /* xSleep */ \ |
| 6152 | unixCurrentTime, /* xCurrentTime */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6153 | unixGetLastError, /* xGetLastError */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6154 | 0, /* xRename */ \ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6155 | unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6156 | } |
| 6157 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6158 | /* |
| 6159 | ** All default VFSes for unix are contained in the following array. |
| 6160 | ** |
| 6161 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 6162 | ** by the SQLite core when the VFS is registered. So the following |
| 6163 | ** array cannot be const. |
| 6164 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6165 | static sqlite3_vfs aVfs[] = { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6166 | #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__)) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6167 | UNIXVFS("unix", autolockIoFinder ), |
| 6168 | #else |
| 6169 | UNIXVFS("unix", posixIoFinder ), |
| 6170 | #endif |
| 6171 | UNIXVFS("unix-none", nolockIoFinder ), |
| 6172 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6173 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6174 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6175 | #endif |
| 6176 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6177 | UNIXVFS("unix-posix", posixIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6178 | #if !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6179 | UNIXVFS("unix-flock", flockIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6180 | #endif |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6181 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6182 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6183 | UNIXVFS("unix-afp", afpIoFinder ), |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6184 | UNIXVFS("unix-nfs", nfsIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6185 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6186 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6187 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6188 | unsigned int i; /* Loop counter */ |
| 6189 | |
| 6190 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6191 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6192 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6193 | } |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6194 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6195 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6196 | |
| 6197 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6198 | ** Shutdown the operating system interface. |
| 6199 | ** |
| 6200 | ** Some operating systems might need to do some cleanup in this routine, |
| 6201 | ** to release dynamically allocated objects. But not on unix. |
| 6202 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6203 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6204 | int sqlite3_os_end(void){ |
| 6205 | return SQLITE_OK; |
| 6206 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 6207 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 6208 | #endif /* SQLITE_OS_UNIX */ |