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 | b469f46 | 2010-12-22 21:48:50 +0000 | [diff] [blame] | 122 | #ifndef SQLITE_OMIT_WAL |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 123 | #include <sys/mman.h> |
drh | b469f46 | 2010-12-22 21:48:50 +0000 | [diff] [blame] | 124 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 125 | |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 126 | #if SQLITE_ENABLE_LOCKING_STYLE |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 127 | # include <sys/ioctl.h> |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 128 | # if OS_VXWORKS |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 129 | # include <semaphore.h> |
| 130 | # include <limits.h> |
| 131 | # else |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 132 | # include <sys/file.h> |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 133 | # include <sys/param.h> |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 134 | # endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 135 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 136 | |
drh | f8b4d8c | 2010-03-05 13:53:22 +0000 | [diff] [blame] | 137 | #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS) |
drh | 84a2bf6 | 2010-03-05 13:41:06 +0000 | [diff] [blame] | 138 | # include <sys/mount.h> |
| 139 | #endif |
| 140 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 141 | /* |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 142 | ** Allowed values of unixFile.fsFlags |
| 143 | */ |
| 144 | #define SQLITE_FSFLAGS_IS_MSDOS 0x1 |
| 145 | |
| 146 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 147 | ** If we are to be thread-safe, include the pthreads header and define |
| 148 | ** the SQLITE_UNIX_THREADS macro. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 149 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 150 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 151 | # include <pthread.h> |
| 152 | # define SQLITE_UNIX_THREADS 1 |
| 153 | #endif |
| 154 | |
| 155 | /* |
| 156 | ** Default permissions when creating a new file |
| 157 | */ |
| 158 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 159 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 160 | #endif |
| 161 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 162 | /* |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 163 | ** Default permissions when creating auto proxy dir |
| 164 | */ |
| 165 | #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 166 | # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 |
| 167 | #endif |
| 168 | |
| 169 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 170 | ** Maximum supported path-length. |
| 171 | */ |
| 172 | #define MAX_PATHNAME 512 |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 173 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 174 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 175 | ** Only set the lastErrno if the error code is a real error and not |
| 176 | ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 177 | */ |
| 178 | #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |
| 179 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 180 | /* Forward references */ |
| 181 | typedef struct unixShm unixShm; /* Connection shared memory */ |
| 182 | typedef struct unixShmNode unixShmNode; /* Shared memory instance */ |
| 183 | typedef struct unixInodeInfo unixInodeInfo; /* An i-node */ |
| 184 | typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 185 | |
| 186 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 187 | ** Sometimes, after a file handle is closed by SQLite, the file descriptor |
| 188 | ** cannot be closed immediately. In these cases, instances of the following |
| 189 | ** structure are used to store the file descriptor while waiting for an |
| 190 | ** opportunity to either close or reuse it. |
| 191 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 192 | struct UnixUnusedFd { |
| 193 | int fd; /* File descriptor to close */ |
| 194 | int flags; /* Flags this file descriptor was opened with */ |
| 195 | UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ |
| 196 | }; |
| 197 | |
| 198 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 199 | ** The unixFile structure is subclass of sqlite3_file specific to the unix |
| 200 | ** VFS implementations. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 201 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 202 | typedef struct unixFile unixFile; |
| 203 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 204 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 205 | unixInodeInfo *pInode; /* Info about locks on this inode */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 206 | int h; /* The file descriptor */ |
| 207 | int dirfd; /* File descriptor for the directory */ |
| 208 | unsigned char eFileLock; /* The type of lock held on this fd */ |
| 209 | int lastErrno; /* The unix errno from last I/O error */ |
| 210 | void *lockingContext; /* Locking style specific state */ |
| 211 | UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ |
| 212 | int fileFlags; /* Miscellanous flags */ |
| 213 | const char *zPath; /* Name of the file */ |
| 214 | unixShm *pShm; /* Shared memory segment information */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 215 | int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 216 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 217 | int openFlags; /* The flags specified at open() */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 218 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 219 | #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 220 | unsigned fsFlags; /* cached details from statfs() */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 221 | #endif |
| 222 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 223 | int isDelete; /* Delete on close if true */ |
| 224 | struct vxworksFileId *pId; /* Unique file ID */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 225 | #endif |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 226 | #ifndef NDEBUG |
| 227 | /* The next group of variables are used to track whether or not the |
| 228 | ** transaction counter in bytes 24-27 of database files are updated |
| 229 | ** whenever any part of the database changes. An assertion fault will |
| 230 | ** occur if a file is updated without also updating the transaction |
| 231 | ** counter. This test is made to avoid new problems similar to the |
| 232 | ** one described by ticket #3584. |
| 233 | */ |
| 234 | unsigned char transCntrChng; /* True if the transaction counter changed */ |
| 235 | unsigned char dbUpdate; /* True if any part of database file changed */ |
| 236 | unsigned char inNormalWrite; /* True if in a normal write operation */ |
| 237 | #endif |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 238 | #ifdef SQLITE_TEST |
| 239 | /* In test mode, increase the size of this structure a bit so that |
| 240 | ** it is larger than the struct CrashFile defined in test6.c. |
| 241 | */ |
| 242 | char aPadding[32]; |
| 243 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 244 | }; |
| 245 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 246 | /* |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 247 | ** The following macros define bits in unixFile.fileFlags |
| 248 | */ |
| 249 | #define SQLITE_WHOLE_FILE_LOCKING 0x0001 /* Use whole-file locking */ |
| 250 | |
| 251 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 252 | ** Include code that is common to all os_*.c files |
| 253 | */ |
| 254 | #include "os_common.h" |
| 255 | |
| 256 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 257 | ** Define various macros that are missing from some systems. |
| 258 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 259 | #ifndef O_LARGEFILE |
| 260 | # define O_LARGEFILE 0 |
| 261 | #endif |
| 262 | #ifdef SQLITE_DISABLE_LFS |
| 263 | # undef O_LARGEFILE |
| 264 | # define O_LARGEFILE 0 |
| 265 | #endif |
| 266 | #ifndef O_NOFOLLOW |
| 267 | # define O_NOFOLLOW 0 |
| 268 | #endif |
| 269 | #ifndef O_BINARY |
| 270 | # define O_BINARY 0 |
| 271 | #endif |
| 272 | |
| 273 | /* |
| 274 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 275 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 276 | ** that always succeeds. This means that locking does not occur under |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 277 | ** DJGPP. But it is DOS - what did you expect? |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 278 | */ |
| 279 | #ifdef __DJGPP__ |
| 280 | # define fcntl(A,B,C) 0 |
| 281 | #endif |
| 282 | |
| 283 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 284 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 285 | ** testing and debugging only. |
| 286 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 287 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 288 | #define threadid pthread_self() |
| 289 | #else |
| 290 | #define threadid 0 |
| 291 | #endif |
| 292 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 293 | |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 294 | /* |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 295 | ** Helper functions to obtain and relinquish the global mutex. The |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 296 | ** global mutex is used to protect the unixInodeInfo and |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 297 | ** vxworksFileId objects used by this file, all of which may be |
| 298 | ** shared by multiple threads. |
| 299 | ** |
| 300 | ** Function unixMutexHeld() is used to assert() that the global mutex |
| 301 | ** is held when required. This function is only used as part of assert() |
| 302 | ** statements. e.g. |
| 303 | ** |
| 304 | ** unixEnterMutex() |
| 305 | ** assert( unixMutexHeld() ); |
| 306 | ** unixEnterLeave() |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 307 | */ |
| 308 | static void unixEnterMutex(void){ |
| 309 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 310 | } |
| 311 | static void unixLeaveMutex(void){ |
| 312 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 313 | } |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 314 | #ifdef SQLITE_DEBUG |
| 315 | static int unixMutexHeld(void) { |
| 316 | return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 317 | } |
| 318 | #endif |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 319 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 320 | |
| 321 | #ifdef SQLITE_DEBUG |
| 322 | /* |
| 323 | ** Helper function for printing out trace information from debugging |
| 324 | ** binaries. This returns the string represetation of the supplied |
| 325 | ** integer lock-type. |
| 326 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 327 | static const char *azFileLock(int eFileLock){ |
| 328 | switch( eFileLock ){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 329 | case NO_LOCK: return "NONE"; |
| 330 | case SHARED_LOCK: return "SHARED"; |
| 331 | case RESERVED_LOCK: return "RESERVED"; |
| 332 | case PENDING_LOCK: return "PENDING"; |
| 333 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 334 | } |
| 335 | return "ERROR"; |
| 336 | } |
| 337 | #endif |
| 338 | |
| 339 | #ifdef SQLITE_LOCK_TRACE |
| 340 | /* |
| 341 | ** Print out information about all locking operations. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 342 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 343 | ** This routine is used for troubleshooting locks on multithreaded |
| 344 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 345 | ** command-line option on the compiler. This code is normally |
| 346 | ** turned off. |
| 347 | */ |
| 348 | static int lockTrace(int fd, int op, struct flock *p){ |
| 349 | char *zOpName, *zType; |
| 350 | int s; |
| 351 | int savedErrno; |
| 352 | if( op==F_GETLK ){ |
| 353 | zOpName = "GETLK"; |
| 354 | }else if( op==F_SETLK ){ |
| 355 | zOpName = "SETLK"; |
| 356 | }else{ |
| 357 | s = fcntl(fd, op, p); |
| 358 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 359 | return s; |
| 360 | } |
| 361 | if( p->l_type==F_RDLCK ){ |
| 362 | zType = "RDLCK"; |
| 363 | }else if( p->l_type==F_WRLCK ){ |
| 364 | zType = "WRLCK"; |
| 365 | }else if( p->l_type==F_UNLCK ){ |
| 366 | zType = "UNLCK"; |
| 367 | }else{ |
| 368 | assert( 0 ); |
| 369 | } |
| 370 | assert( p->l_whence==SEEK_SET ); |
| 371 | s = fcntl(fd, op, p); |
| 372 | savedErrno = errno; |
| 373 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 374 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 375 | (int)p->l_pid, s); |
| 376 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 377 | struct flock l2; |
| 378 | l2 = *p; |
| 379 | fcntl(fd, F_GETLK, &l2); |
| 380 | if( l2.l_type==F_RDLCK ){ |
| 381 | zType = "RDLCK"; |
| 382 | }else if( l2.l_type==F_WRLCK ){ |
| 383 | zType = "WRLCK"; |
| 384 | }else if( l2.l_type==F_UNLCK ){ |
| 385 | zType = "UNLCK"; |
| 386 | }else{ |
| 387 | assert( 0 ); |
| 388 | } |
| 389 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 390 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 391 | } |
| 392 | errno = savedErrno; |
| 393 | return s; |
| 394 | } |
| 395 | #define fcntl lockTrace |
| 396 | #endif /* SQLITE_LOCK_TRACE */ |
| 397 | |
| 398 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 399 | /* |
| 400 | ** Retry ftruncate() calls that fail due to EINTR |
| 401 | */ |
| 402 | #ifdef EINTR |
| 403 | static int robust_ftruncate(int h, sqlite3_int64 sz){ |
| 404 | int rc; |
| 405 | do{ rc = ftruncate(h,sz); }while( rc<0 && errno==EINTR ); |
| 406 | return rc; |
| 407 | } |
| 408 | #else |
| 409 | # define robust_ftruncate(a,b) ftruncate(a,b) |
| 410 | #endif |
| 411 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 412 | |
| 413 | /* |
| 414 | ** This routine translates a standard POSIX errno code into something |
| 415 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 416 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 417 | ** and a variety of "please close the file descriptor NOW" errors into |
| 418 | ** SQLITE_IOERR |
| 419 | ** |
| 420 | ** Errors during initialization of locks, or file system support for locks, |
| 421 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 422 | */ |
| 423 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
| 424 | switch (posixError) { |
| 425 | case 0: |
| 426 | return SQLITE_OK; |
| 427 | |
| 428 | case EAGAIN: |
| 429 | case ETIMEDOUT: |
| 430 | case EBUSY: |
| 431 | case EINTR: |
| 432 | case ENOLCK: |
| 433 | /* random NFS retry error, unless during file system support |
| 434 | * introspection, in which it actually means what it says */ |
| 435 | return SQLITE_BUSY; |
| 436 | |
| 437 | case EACCES: |
| 438 | /* EACCES is like EAGAIN during locking operations, but not any other time*/ |
| 439 | if( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
| 440 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 441 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 442 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){ |
| 443 | return SQLITE_BUSY; |
| 444 | } |
| 445 | /* else fall through */ |
| 446 | case EPERM: |
| 447 | return SQLITE_PERM; |
| 448 | |
| 449 | case EDEADLK: |
| 450 | return SQLITE_IOERR_BLOCKED; |
| 451 | |
| 452 | #if EOPNOTSUPP!=ENOTSUP |
| 453 | case EOPNOTSUPP: |
| 454 | /* something went terribly awry, unless during file system support |
| 455 | * introspection, in which it actually means what it says */ |
| 456 | #endif |
| 457 | #ifdef ENOTSUP |
| 458 | case ENOTSUP: |
| 459 | /* invalid fd, unless during file system support introspection, in which |
| 460 | * it actually means what it says */ |
| 461 | #endif |
| 462 | case EIO: |
| 463 | case EBADF: |
| 464 | case EINVAL: |
| 465 | case ENOTCONN: |
| 466 | case ENODEV: |
| 467 | case ENXIO: |
| 468 | case ENOENT: |
| 469 | case ESTALE: |
| 470 | case ENOSYS: |
| 471 | /* these should force the client to close the file and reconnect */ |
| 472 | |
| 473 | default: |
| 474 | return sqliteIOErr; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | |
| 479 | |
| 480 | /****************************************************************************** |
| 481 | ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 482 | ** |
| 483 | ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 484 | ** the device number and the inode number. But this does not work on VxWorks. |
| 485 | ** On VxWorks, a unique file id must be based on the canonical filename. |
| 486 | ** |
| 487 | ** A pointer to an instance of the following structure can be used as a |
| 488 | ** unique file ID in VxWorks. Each instance of this structure contains |
| 489 | ** a copy of the canonical filename. There is also a reference count. |
| 490 | ** The structure is reclaimed when the number of pointers to it drops to |
| 491 | ** zero. |
| 492 | ** |
| 493 | ** There are never very many files open at one time and lookups are not |
| 494 | ** a performance-critical path, so it is sufficient to put these |
| 495 | ** structures on a linked list. |
| 496 | */ |
| 497 | struct vxworksFileId { |
| 498 | struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 499 | int nRef; /* Number of references to this one */ |
| 500 | int nName; /* Length of the zCanonicalName[] string */ |
| 501 | char *zCanonicalName; /* Canonical filename */ |
| 502 | }; |
| 503 | |
| 504 | #if OS_VXWORKS |
| 505 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 506 | ** All unique filenames are held on a linked list headed by this |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 507 | ** variable: |
| 508 | */ |
| 509 | static struct vxworksFileId *vxworksFileList = 0; |
| 510 | |
| 511 | /* |
| 512 | ** Simplify a filename into its canonical form |
| 513 | ** by making the following changes: |
| 514 | ** |
| 515 | ** * removing any trailing and duplicate / |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 516 | ** * convert /./ into just / |
| 517 | ** * convert /A/../ where A is any simple name into just / |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 518 | ** |
| 519 | ** Changes are made in-place. Return the new name length. |
| 520 | ** |
| 521 | ** The original filename is in z[0..n-1]. Return the number of |
| 522 | ** characters in the simplified name. |
| 523 | */ |
| 524 | static int vxworksSimplifyName(char *z, int n){ |
| 525 | int i, j; |
| 526 | while( n>1 && z[n-1]=='/' ){ n--; } |
| 527 | for(i=j=0; i<n; i++){ |
| 528 | if( z[i]=='/' ){ |
| 529 | if( z[i+1]=='/' ) continue; |
| 530 | if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 531 | i += 1; |
| 532 | continue; |
| 533 | } |
| 534 | if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 535 | while( j>0 && z[j-1]!='/' ){ j--; } |
| 536 | if( j>0 ){ j--; } |
| 537 | i += 2; |
| 538 | continue; |
| 539 | } |
| 540 | } |
| 541 | z[j++] = z[i]; |
| 542 | } |
| 543 | z[j] = 0; |
| 544 | return j; |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | ** Find a unique file ID for the given absolute pathname. Return |
| 549 | ** a pointer to the vxworksFileId object. This pointer is the unique |
| 550 | ** file ID. |
| 551 | ** |
| 552 | ** The nRef field of the vxworksFileId object is incremented before |
| 553 | ** the object is returned. A new vxworksFileId object is created |
| 554 | ** and added to the global list if necessary. |
| 555 | ** |
| 556 | ** If a memory allocation error occurs, return NULL. |
| 557 | */ |
| 558 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 559 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 560 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 561 | int n; /* Length of zAbsoluteName string */ |
| 562 | |
| 563 | assert( zAbsoluteName[0]=='/' ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 564 | n = (int)strlen(zAbsoluteName); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 565 | pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) ); |
| 566 | if( pNew==0 ) return 0; |
| 567 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 568 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 569 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 570 | |
| 571 | /* Search for an existing entry that matching the canonical name. |
| 572 | ** If found, increment the reference count and return a pointer to |
| 573 | ** the existing file ID. |
| 574 | */ |
| 575 | unixEnterMutex(); |
| 576 | for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 577 | if( pCandidate->nName==n |
| 578 | && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 579 | ){ |
| 580 | sqlite3_free(pNew); |
| 581 | pCandidate->nRef++; |
| 582 | unixLeaveMutex(); |
| 583 | return pCandidate; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | /* No match was found. We will make a new file ID */ |
| 588 | pNew->nRef = 1; |
| 589 | pNew->nName = n; |
| 590 | pNew->pNext = vxworksFileList; |
| 591 | vxworksFileList = pNew; |
| 592 | unixLeaveMutex(); |
| 593 | return pNew; |
| 594 | } |
| 595 | |
| 596 | /* |
| 597 | ** Decrement the reference count on a vxworksFileId object. Free |
| 598 | ** the object when the reference count reaches zero. |
| 599 | */ |
| 600 | static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 601 | unixEnterMutex(); |
| 602 | assert( pId->nRef>0 ); |
| 603 | pId->nRef--; |
| 604 | if( pId->nRef==0 ){ |
| 605 | struct vxworksFileId **pp; |
| 606 | for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 607 | assert( *pp==pId ); |
| 608 | *pp = pId->pNext; |
| 609 | sqlite3_free(pId); |
| 610 | } |
| 611 | unixLeaveMutex(); |
| 612 | } |
| 613 | #endif /* OS_VXWORKS */ |
| 614 | /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 615 | ******************************************************************************/ |
| 616 | |
| 617 | |
| 618 | /****************************************************************************** |
| 619 | *************************** Posix Advisory Locking **************************** |
| 620 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 621 | ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 622 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 623 | ** sets or clears a lock, that operation overrides any prior locks set |
| 624 | ** by the same process. It does not explicitly say so, but this implies |
| 625 | ** that it overrides locks set by the same process using a different |
| 626 | ** file descriptor. Consider this test case: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 627 | ** |
| 628 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 629 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 630 | ** |
| 631 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 632 | ** one is a hard or symbolic link to the other) then if you set |
| 633 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 634 | ** on fd2, it works. I would have expected the second lock to |
| 635 | ** fail since there was already a lock on the file due to fd1. |
| 636 | ** But not so. Since both locks came from the same process, the |
| 637 | ** second overrides the first, even though they were on different |
| 638 | ** file descriptors opened on different file names. |
| 639 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 640 | ** This means that we cannot use POSIX locks to synchronize file access |
| 641 | ** among competing threads of the same process. POSIX locks will work fine |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 642 | ** to synchronize access for threads in separate processes, but not |
| 643 | ** threads within the same process. |
| 644 | ** |
| 645 | ** To work around the problem, SQLite has to manage file locks internally |
| 646 | ** on its own. Whenever a new database is opened, we have to find the |
| 647 | ** specific inode of the database file (the inode is determined by the |
| 648 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 649 | ** and check for locks already existing on that inode. When locks are |
| 650 | ** created or removed, we have to look at our own internal record of the |
| 651 | ** locks to see if another thread has previously set a lock on that same |
| 652 | ** inode. |
| 653 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 654 | ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 655 | ** For VxWorks, we have to use the alternative unique ID system based on |
| 656 | ** canonical filename and implemented in the previous division.) |
| 657 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 658 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 659 | ** descriptor. It is now a structure that holds the integer file |
| 660 | ** descriptor and a pointer to a structure that describes the internal |
| 661 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 662 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 663 | ** point to the same locking structure. The locking structure keeps |
| 664 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 665 | ** field that tells us its internal lock status. cnt==0 means the |
| 666 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 667 | ** cnt>0 means there are cnt shared locks on the file. |
| 668 | ** |
| 669 | ** Any attempt to lock or unlock a file first checks the locking |
| 670 | ** structure. The fcntl() system call is only invoked to set a |
| 671 | ** POSIX lock if the internal lock structure transitions between |
| 672 | ** a locked and an unlocked state. |
| 673 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 674 | ** But wait: there are yet more problems with POSIX advisory locks. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 675 | ** |
| 676 | ** If you close a file descriptor that points to a file that has locks, |
| 677 | ** all locks on that file that are owned by the current process are |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 678 | ** released. To work around this problem, each unixInodeInfo object |
| 679 | ** maintains a count of the number of pending locks on tha inode. |
| 680 | ** When an attempt is made to close an unixFile, if there are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 681 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 682 | ** to close() the file descriptor is deferred until all of the locks clear. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 683 | ** The unixInodeInfo structure keeps a list of file descriptors that need to |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 684 | ** be closed and that list is walked (and cleared) when the last lock |
| 685 | ** clears. |
| 686 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 687 | ** Yet another problem: LinuxThreads do not play well with posix locks. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 688 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 689 | ** Many older versions of linux use the LinuxThreads library which is |
| 690 | ** not posix compliant. Under LinuxThreads, a lock created by thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 691 | ** A cannot be modified or overridden by a different thread B. |
| 692 | ** Only thread A can modify the lock. Locking behavior is correct |
| 693 | ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 694 | ** on linux - with NPTL a lock created by thread A can override locks |
| 695 | ** in thread B. But there is no way to know at compile-time which |
| 696 | ** threading library is being used. So there is no way to know at |
| 697 | ** compile-time whether or not thread A can override locks on thread B. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 698 | ** 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] | 699 | ** current process. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 700 | ** |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 701 | ** SQLite used to support LinuxThreads. But support for LinuxThreads |
| 702 | ** was dropped beginning with version 3.7.0. SQLite will still work with |
| 703 | ** LinuxThreads provided that (1) there is no more than one connection |
| 704 | ** per database file in the same process and (2) database connections |
| 705 | ** do not move across threads. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 706 | */ |
| 707 | |
| 708 | /* |
| 709 | ** An instance of the following structure serves as the key used |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 710 | ** to locate a particular unixInodeInfo object. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 711 | */ |
| 712 | struct unixFileId { |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 713 | dev_t dev; /* Device number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 714 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 715 | struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 716 | #else |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 717 | ino_t ino; /* Inode number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 718 | #endif |
| 719 | }; |
| 720 | |
| 721 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 722 | ** An instance of the following structure is allocated for each open |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 723 | ** inode. Or, on LinuxThreads, there is one of these structures for |
| 724 | ** each inode opened by each thread. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 725 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 726 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 727 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 728 | ** object keeps a count of the number of unixFile pointing to it. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 729 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 730 | struct unixInodeInfo { |
| 731 | struct unixFileId fileId; /* The lookup key */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 732 | int nShared; /* Number of SHARED locks held */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 733 | int eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 734 | int nRef; /* Number of pointers to this structure */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 735 | unixShmNode *pShmNode; /* Shared memory associated with this inode */ |
| 736 | int nLock; /* Number of outstanding file locks */ |
| 737 | UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
| 738 | unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ |
| 739 | unixInodeInfo *pPrev; /* .... doubly linked */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 740 | #if defined(SQLITE_ENABLE_LOCKING_STYLE) |
| 741 | unsigned long long sharedByte; /* for AFP simulated shared lock */ |
| 742 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 743 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 744 | sem_t *pSem; /* Named POSIX semaphore */ |
| 745 | char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 746 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 747 | }; |
| 748 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 749 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 750 | ** A lists of all unixInodeInfo objects. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 751 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 752 | static unixInodeInfo *inodeList = 0; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 753 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 754 | /* |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 755 | ** |
| 756 | ** This function - unixLogError_x(), is only ever called via the macro |
| 757 | ** unixLogError(). |
| 758 | ** |
| 759 | ** It is invoked after an error occurs in an OS function and errno has been |
| 760 | ** set. It logs a message using sqlite3_log() containing the current value of |
| 761 | ** errno and, if possible, the human-readable equivalent from strerror() or |
| 762 | ** strerror_r(). |
| 763 | ** |
| 764 | ** The first argument passed to the macro should be the error code that |
| 765 | ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). |
| 766 | ** The two subsequent arguments should be the name of the OS function that |
| 767 | ** failed (e.g. "unlink", "open") and the the associated file-system path, |
| 768 | ** if any. |
| 769 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 770 | #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__) |
| 771 | static int unixLogErrorAtLine( |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 772 | int errcode, /* SQLite error code */ |
| 773 | const char *zFunc, /* Name of OS function that failed */ |
| 774 | const char *zPath, /* File path associated with error */ |
| 775 | int iLine /* Source line number where error occurred */ |
| 776 | ){ |
| 777 | char *zErr; /* Message from strerror() or equivalent */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 778 | int iErrno = errno; /* Saved syscall error number */ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 779 | |
| 780 | /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use |
| 781 | ** the strerror() function to obtain the human-readable error message |
| 782 | ** equivalent to errno. Otherwise, use strerror_r(). |
| 783 | */ |
| 784 | #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) |
| 785 | char aErr[80]; |
| 786 | memset(aErr, 0, sizeof(aErr)); |
| 787 | zErr = aErr; |
| 788 | |
| 789 | /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined, |
| 790 | ** assume that the system provides the the GNU version of strerror_r() that |
| 791 | ** returns a pointer to a buffer containing the error message. That pointer |
| 792 | ** may point to aErr[], or it may point to some static storage somewhere. |
| 793 | ** Otherwise, assume that the system provides the POSIX version of |
| 794 | ** strerror_r(), which always writes an error message into aErr[]. |
| 795 | ** |
| 796 | ** If the code incorrectly assumes that it is the POSIX version that is |
| 797 | ** available, the error message will often be an empty string. Not a |
| 798 | ** huge problem. Incorrectly concluding that the GNU version is available |
| 799 | ** could lead to a segfault though. |
| 800 | */ |
| 801 | #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) |
| 802 | zErr = |
| 803 | # endif |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 804 | strerror_r(iErrno, aErr, sizeof(aErr)-1); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 805 | |
| 806 | #elif SQLITE_THREADSAFE |
| 807 | /* This is a threadsafe build, but strerror_r() is not available. */ |
| 808 | zErr = ""; |
| 809 | #else |
| 810 | /* Non-threadsafe build, use strerror(). */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 811 | zErr = strerror(iErrno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 812 | #endif |
| 813 | |
| 814 | assert( errcode!=SQLITE_OK ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 815 | if( zPath==0 ) zPath = ""; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 816 | sqlite3_log(errcode, |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 817 | "os_unix.c:%d: (%d) %s(%s) - %s", |
| 818 | iLine, iErrno, zFunc, zPath, zErr |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 819 | ); |
| 820 | |
| 821 | return errcode; |
| 822 | } |
| 823 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 824 | /* |
| 825 | ** Close a file descriptor. |
| 826 | ** |
| 827 | ** We assume that close() almost always works, since it is only in a |
| 828 | ** very sick application or on a very sick platform that it might fail. |
| 829 | ** If it does fail, simply leak the file descriptor, but do log the |
| 830 | ** error. |
| 831 | ** |
| 832 | ** Note that it is not safe to retry close() after EINTR since the |
| 833 | ** file descriptor might have already been reused by another thread. |
| 834 | ** So we don't even try to recover from an EINTR. Just log the error |
| 835 | ** and move on. |
| 836 | */ |
| 837 | static void robust_close(unixFile *pFile, int h, int lineno){ |
| 838 | if( close(h) ){ |
| 839 | unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", |
| 840 | pFile ? pFile->zPath : 0, lineno); |
| 841 | } |
| 842 | } |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 843 | |
| 844 | /* |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 845 | ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 846 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 847 | static void closePendingFds(unixFile *pFile){ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 848 | unixInodeInfo *pInode = pFile->pInode; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 849 | UnixUnusedFd *p; |
| 850 | UnixUnusedFd *pNext; |
| 851 | for(p=pInode->pUnused; p; p=pNext){ |
| 852 | pNext = p->pNext; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 853 | robust_close(pFile, p->fd, __LINE__); |
| 854 | sqlite3_free(p); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 855 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 856 | pInode->pUnused = 0; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 860 | ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 861 | ** |
| 862 | ** The mutex entered using the unixEnterMutex() function must be held |
| 863 | ** when this function is called. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 864 | */ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 865 | static void releaseInodeInfo(unixFile *pFile){ |
| 866 | unixInodeInfo *pInode = pFile->pInode; |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 867 | assert( unixMutexHeld() ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 868 | if( pInode ){ |
| 869 | pInode->nRef--; |
| 870 | if( pInode->nRef==0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 871 | assert( pInode->pShmNode==0 ); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 872 | closePendingFds(pFile); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 873 | if( pInode->pPrev ){ |
| 874 | assert( pInode->pPrev->pNext==pInode ); |
| 875 | pInode->pPrev->pNext = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 876 | }else{ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 877 | assert( inodeList==pInode ); |
| 878 | inodeList = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 879 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 880 | if( pInode->pNext ){ |
| 881 | assert( pInode->pNext->pPrev==pInode ); |
| 882 | pInode->pNext->pPrev = pInode->pPrev; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 883 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 884 | sqlite3_free(pInode); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 885 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | |
| 889 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 890 | ** Given a file descriptor, locate the unixInodeInfo object that |
| 891 | ** describes that file descriptor. Create a new one if necessary. The |
| 892 | ** return value might be uninitialized if an error occurs. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 893 | ** |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 894 | ** The mutex entered using the unixEnterMutex() function must be held |
| 895 | ** when this function is called. |
| 896 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 897 | ** Return an appropriate error code. |
| 898 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 899 | static int findInodeInfo( |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 900 | unixFile *pFile, /* Unix file with file desc used in the key */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 901 | unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 902 | ){ |
| 903 | int rc; /* System call return code */ |
| 904 | int fd; /* The file descriptor for pFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 905 | struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ |
| 906 | struct stat statbuf; /* Low-level file information */ |
| 907 | unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 908 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 909 | assert( unixMutexHeld() ); |
| 910 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 911 | /* Get low-level information about the file that we can used to |
| 912 | ** create a unique name for the file. |
| 913 | */ |
| 914 | fd = pFile->h; |
| 915 | rc = fstat(fd, &statbuf); |
| 916 | if( rc!=0 ){ |
| 917 | pFile->lastErrno = errno; |
| 918 | #ifdef EOVERFLOW |
| 919 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 920 | #endif |
| 921 | return SQLITE_IOERR; |
| 922 | } |
| 923 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 924 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 925 | /* On OS X on an msdos filesystem, the inode number is reported |
| 926 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 927 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 928 | ** we always increase the file size to 1 by writing a single byte |
| 929 | ** prior to accessing the inode number. The one byte written is |
| 930 | ** an ASCII 'S' character which also happens to be the first byte |
| 931 | ** in the header of every SQLite database. In this way, if there |
| 932 | ** is a race condition such that another thread has already populated |
| 933 | ** the first page of the database, no damage is done. |
| 934 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 935 | if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 936 | do{ rc = write(fd, "S", 1); }while( rc<0 && errno==EINTR ); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 937 | if( rc!=1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 938 | pFile->lastErrno = errno; |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 939 | return SQLITE_IOERR; |
| 940 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 941 | rc = fstat(fd, &statbuf); |
| 942 | if( rc!=0 ){ |
| 943 | pFile->lastErrno = errno; |
| 944 | return SQLITE_IOERR; |
| 945 | } |
| 946 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 947 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 948 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 949 | memset(&fileId, 0, sizeof(fileId)); |
| 950 | fileId.dev = statbuf.st_dev; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 951 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 952 | fileId.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 953 | #else |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 954 | fileId.ino = statbuf.st_ino; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 955 | #endif |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 956 | pInode = inodeList; |
| 957 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 958 | pInode = pInode->pNext; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 959 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 960 | if( pInode==0 ){ |
| 961 | pInode = sqlite3_malloc( sizeof(*pInode) ); |
| 962 | if( pInode==0 ){ |
| 963 | return SQLITE_NOMEM; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 964 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 965 | memset(pInode, 0, sizeof(*pInode)); |
| 966 | memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
| 967 | pInode->nRef = 1; |
| 968 | pInode->pNext = inodeList; |
| 969 | pInode->pPrev = 0; |
| 970 | if( inodeList ) inodeList->pPrev = pInode; |
| 971 | inodeList = pInode; |
| 972 | }else{ |
| 973 | pInode->nRef++; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 974 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 975 | *ppInode = pInode; |
| 976 | return SQLITE_OK; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 977 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 978 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 979 | |
| 980 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 981 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 982 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 983 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 984 | ** 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] | 985 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 986 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 987 | int rc = SQLITE_OK; |
| 988 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 989 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 990 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 991 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 992 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 993 | assert( pFile ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 994 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 995 | |
| 996 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 997 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 998 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1001 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1002 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1003 | #ifndef __DJGPP__ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1004 | if( !reserved ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1005 | struct flock lock; |
| 1006 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1007 | lock.l_start = RESERVED_BYTE; |
| 1008 | lock.l_len = 1; |
| 1009 | lock.l_type = F_WRLCK; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1010 | if (-1 == fcntl(pFile->h, F_GETLK, &lock)) { |
| 1011 | int tErrno = errno; |
| 1012 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 1013 | pFile->lastErrno = tErrno; |
| 1014 | } else if( lock.l_type!=F_UNLCK ){ |
| 1015 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1018 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1019 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1020 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1021 | OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1022 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1023 | *pResOut = reserved; |
| 1024 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1028 | ** Lock the file with the lock specified by parameter eFileLock - one |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1029 | ** of the following: |
| 1030 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1031 | ** (1) SHARED_LOCK |
| 1032 | ** (2) RESERVED_LOCK |
| 1033 | ** (3) PENDING_LOCK |
| 1034 | ** (4) EXCLUSIVE_LOCK |
| 1035 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1036 | ** Sometimes when requesting one lock state, additional lock states |
| 1037 | ** are inserted in between. The locking might fail on one of the later |
| 1038 | ** transitions leaving the lock state different from what it started but |
| 1039 | ** still short of its goal. The following chart shows the allowed |
| 1040 | ** transitions and the inserted intermediate states: |
| 1041 | ** |
| 1042 | ** UNLOCKED -> SHARED |
| 1043 | ** SHARED -> RESERVED |
| 1044 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1045 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1046 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1047 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1048 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1049 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1050 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1051 | static int unixLock(sqlite3_file *id, int eFileLock){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1052 | /* The following describes the implementation of the various locks and |
| 1053 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1054 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1055 | ** confusion with SQLite lock names). The algorithms are complicated |
| 1056 | ** slightly in order to be compatible with windows systems simultaneously |
| 1057 | ** accessing the same database file, in case that is ever required. |
| 1058 | ** |
| 1059 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1060 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1061 | ** range', a range of 510 bytes at a well known offset. |
| 1062 | ** |
| 1063 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 1064 | ** byte'. If this is successful, a random byte from the 'shared byte |
| 1065 | ** range' is read-locked and the lock on the 'pending byte' released. |
| 1066 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1067 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1068 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1069 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1070 | ** |
| 1071 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1072 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1073 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1074 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1075 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1076 | ** This property is used by the algorithm for rolling back a journal file |
| 1077 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1078 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1079 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1080 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1081 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1082 | ** within this range, this ensures that no other locks are held on the |
| 1083 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1084 | ** |
| 1085 | ** The reason a single byte cannot be used instead of the 'shared byte |
| 1086 | ** range' is that some versions of windows do not support read-locks. By |
| 1087 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 1088 | ** even if the locking primitive used is always a write-lock. |
| 1089 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1090 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1091 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1092 | unixInodeInfo *pInode = pFile->pInode; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1093 | struct flock lock; |
drh | 3f02218 | 2009-09-09 16:10:50 +0000 | [diff] [blame] | 1094 | int s = 0; |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 1095 | int tErrno = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1096 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1097 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1098 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 1099 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1100 | azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1101 | |
| 1102 | /* 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] | 1103 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1104 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1105 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1106 | if( pFile->eFileLock>=eFileLock ){ |
| 1107 | OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
| 1108 | azFileLock(eFileLock))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1109 | return SQLITE_OK; |
| 1110 | } |
| 1111 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1112 | /* Make sure the locking sequence is correct. |
| 1113 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 1114 | ** (2) SQLite never explicitly requests a pendig lock. |
| 1115 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1116 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1117 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 1118 | assert( eFileLock!=PENDING_LOCK ); |
| 1119 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1120 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1121 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1122 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1123 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1124 | pInode = pFile->pInode; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1125 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1126 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1127 | ** handle that precludes the requested lock, return BUSY. |
| 1128 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1129 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 1130 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1131 | ){ |
| 1132 | rc = SQLITE_BUSY; |
| 1133 | goto end_lock; |
| 1134 | } |
| 1135 | |
| 1136 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1137 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1138 | ** return SQLITE_OK. |
| 1139 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1140 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1141 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1142 | assert( eFileLock==SHARED_LOCK ); |
| 1143 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1144 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1145 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1146 | pInode->nShared++; |
| 1147 | pInode->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1148 | goto end_lock; |
| 1149 | } |
| 1150 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1151 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1152 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1153 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1154 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1155 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1156 | lock.l_len = 1L; |
| 1157 | lock.l_whence = SEEK_SET; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1158 | if( eFileLock==SHARED_LOCK |
| 1159 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1160 | ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1161 | lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1162 | lock.l_start = PENDING_BYTE; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1163 | s = fcntl(pFile->h, F_SETLK, &lock); |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1164 | if( s==(-1) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1165 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1166 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1167 | if( IS_LOCK_ERROR(rc) ){ |
| 1168 | pFile->lastErrno = tErrno; |
| 1169 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1170 | goto end_lock; |
| 1171 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | |
| 1175 | /* If control gets to this point, then actually go ahead and make |
| 1176 | ** operating system calls for the specified lock. |
| 1177 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1178 | if( eFileLock==SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1179 | assert( pInode->nShared==0 ); |
| 1180 | assert( pInode->eFileLock==0 ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1181 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1182 | /* Now get the read-lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1183 | lock.l_start = SHARED_FIRST; |
| 1184 | lock.l_len = SHARED_SIZE; |
| 1185 | if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){ |
| 1186 | tErrno = errno; |
| 1187 | } |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1188 | /* Drop the temporary PENDING lock */ |
| 1189 | lock.l_start = PENDING_BYTE; |
| 1190 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1191 | lock.l_type = F_UNLCK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1192 | if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1193 | if( s != -1 ){ |
| 1194 | /* This could happen with a network mount */ |
| 1195 | tErrno = errno; |
| 1196 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1197 | if( IS_LOCK_ERROR(rc) ){ |
| 1198 | pFile->lastErrno = tErrno; |
| 1199 | } |
| 1200 | goto end_lock; |
| 1201 | } |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1202 | } |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1203 | if( s==(-1) ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1204 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1205 | if( IS_LOCK_ERROR(rc) ){ |
| 1206 | pFile->lastErrno = tErrno; |
| 1207 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1208 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1209 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1210 | pInode->nLock++; |
| 1211 | pInode->nShared = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1212 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1213 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1214 | /* We are trying for an exclusive lock but another thread in this |
| 1215 | ** same process is still holding a shared lock. */ |
| 1216 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1217 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1218 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1219 | ** assumed that there is a SHARED or greater lock on the file |
| 1220 | ** already. |
| 1221 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1222 | assert( 0!=pFile->eFileLock ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1223 | lock.l_type = F_WRLCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1224 | switch( eFileLock ){ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1225 | case RESERVED_LOCK: |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1226 | lock.l_start = RESERVED_BYTE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1227 | break; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1228 | case EXCLUSIVE_LOCK: |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1229 | lock.l_start = SHARED_FIRST; |
| 1230 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1231 | break; |
| 1232 | default: |
| 1233 | assert(0); |
| 1234 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1235 | s = fcntl(pFile->h, F_SETLK, &lock); |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1236 | if( s==(-1) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1237 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1238 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1239 | if( IS_LOCK_ERROR(rc) ){ |
| 1240 | pFile->lastErrno = tErrno; |
| 1241 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1242 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1243 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1244 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1245 | |
| 1246 | #ifndef NDEBUG |
| 1247 | /* Set up the transaction-counter change checking flags when |
| 1248 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1249 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1250 | ** write operation (not a hot journal rollback). |
| 1251 | */ |
| 1252 | if( rc==SQLITE_OK |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1253 | && pFile->eFileLock<=SHARED_LOCK |
| 1254 | && eFileLock==RESERVED_LOCK |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1255 | ){ |
| 1256 | pFile->transCntrChng = 0; |
| 1257 | pFile->dbUpdate = 0; |
| 1258 | pFile->inNormalWrite = 1; |
| 1259 | } |
| 1260 | #endif |
| 1261 | |
| 1262 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1263 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1264 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1265 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1266 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 1267 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1268 | pInode->eFileLock = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1269 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1270 | |
| 1271 | end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1272 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1273 | OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), |
| 1274 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1275 | return rc; |
| 1276 | } |
| 1277 | |
| 1278 | /* |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1279 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1280 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1281 | */ |
| 1282 | static void setPendingFd(unixFile *pFile){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1283 | unixInodeInfo *pInode = pFile->pInode; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1284 | UnixUnusedFd *p = pFile->pUnused; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1285 | p->pNext = pInode->pUnused; |
| 1286 | pInode->pUnused = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1287 | pFile->h = -1; |
| 1288 | pFile->pUnused = 0; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1292 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1293 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1294 | ** |
| 1295 | ** If the locking level of the file descriptor is already at or below |
| 1296 | ** the requested locking level, this routine is a no-op. |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1297 | ** |
| 1298 | ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 1299 | ** the byte range is divided into 2 parts and the first part is unlocked then |
| 1300 | ** set to a read lock, then the other part is simply unlocked. This works |
| 1301 | ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 1302 | ** remove the write lock on a region when a read lock is set. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1303 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1304 | static int _posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1305 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1306 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1307 | struct flock lock; |
| 1308 | int rc = SQLITE_OK; |
| 1309 | int h; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1310 | int tErrno; /* Error code from system call errors */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1311 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1312 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1313 | 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] | 1314 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1315 | getpid())); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1316 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1317 | assert( eFileLock<=SHARED_LOCK ); |
| 1318 | if( pFile->eFileLock<=eFileLock ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1319 | return SQLITE_OK; |
| 1320 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1321 | unixEnterMutex(); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1322 | h = pFile->h; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1323 | pInode = pFile->pInode; |
| 1324 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1325 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1326 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1327 | SimulateIOErrorBenign(1); |
| 1328 | SimulateIOError( h=(-1) ) |
| 1329 | SimulateIOErrorBenign(0); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1330 | |
| 1331 | #ifndef NDEBUG |
| 1332 | /* When reducing a lock such that other processes can start |
| 1333 | ** reading the database file again, make sure that the |
| 1334 | ** transaction counter was updated if any part of the database |
| 1335 | ** file changed. If the transaction counter is not updated, |
| 1336 | ** other connections to the same file might not realize that |
| 1337 | ** the file has changed and hence might not know to flush their |
| 1338 | ** cache. The use of a stale cache can lead to database corruption. |
| 1339 | */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1340 | #if 0 |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1341 | assert( pFile->inNormalWrite==0 |
| 1342 | || pFile->dbUpdate==0 |
| 1343 | || pFile->transCntrChng==1 ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1344 | #endif |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1345 | pFile->inNormalWrite = 0; |
| 1346 | #endif |
| 1347 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1348 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 1349 | ** before establishing the readlock - to avoid a race condition we downgrade |
| 1350 | ** the lock in 2 blocks, so that part of the range will be covered by a |
| 1351 | ** write lock until the rest is covered by a read lock: |
| 1352 | ** 1: [WWWWW] |
| 1353 | ** 2: [....W] |
| 1354 | ** 3: [RRRRW] |
| 1355 | ** 4: [RRRR.] |
| 1356 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1357 | if( eFileLock==SHARED_LOCK ){ |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1358 | |
| 1359 | #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE |
drh | 87e79ae | 2011-03-08 13:06:41 +0000 | [diff] [blame^] | 1360 | (void)handleNFSUnlock; |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1361 | assert( handleNFSUnlock==0 ); |
| 1362 | #endif |
| 1363 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1364 | if( handleNFSUnlock ){ |
| 1365 | off_t divSize = SHARED_SIZE - 1; |
| 1366 | |
| 1367 | lock.l_type = F_UNLCK; |
| 1368 | lock.l_whence = SEEK_SET; |
| 1369 | lock.l_start = SHARED_FIRST; |
| 1370 | lock.l_len = divSize; |
| 1371 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1372 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1373 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1374 | if( IS_LOCK_ERROR(rc) ){ |
| 1375 | pFile->lastErrno = tErrno; |
| 1376 | } |
| 1377 | goto end_unlock; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1378 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1379 | lock.l_type = F_RDLCK; |
| 1380 | lock.l_whence = SEEK_SET; |
| 1381 | lock.l_start = SHARED_FIRST; |
| 1382 | lock.l_len = divSize; |
| 1383 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1384 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1385 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1386 | if( IS_LOCK_ERROR(rc) ){ |
| 1387 | pFile->lastErrno = tErrno; |
| 1388 | } |
| 1389 | goto end_unlock; |
| 1390 | } |
| 1391 | lock.l_type = F_UNLCK; |
| 1392 | lock.l_whence = SEEK_SET; |
| 1393 | lock.l_start = SHARED_FIRST+divSize; |
| 1394 | lock.l_len = SHARED_SIZE-divSize; |
| 1395 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1396 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1397 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1398 | if( IS_LOCK_ERROR(rc) ){ |
| 1399 | pFile->lastErrno = tErrno; |
| 1400 | } |
| 1401 | goto end_unlock; |
| 1402 | } |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1403 | }else |
| 1404 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 1405 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1406 | lock.l_type = F_RDLCK; |
| 1407 | lock.l_whence = SEEK_SET; |
| 1408 | lock.l_start = SHARED_FIRST; |
| 1409 | lock.l_len = SHARED_SIZE; |
| 1410 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1411 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1412 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1413 | if( IS_LOCK_ERROR(rc) ){ |
| 1414 | pFile->lastErrno = tErrno; |
| 1415 | } |
| 1416 | goto end_unlock; |
| 1417 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1418 | } |
| 1419 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1420 | lock.l_type = F_UNLCK; |
| 1421 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1422 | lock.l_start = PENDING_BYTE; |
| 1423 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1424 | if( fcntl(h, F_SETLK, &lock)!=(-1) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1425 | pInode->eFileLock = SHARED_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1426 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1427 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1428 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1429 | if( IS_LOCK_ERROR(rc) ){ |
| 1430 | pFile->lastErrno = tErrno; |
| 1431 | } |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 1432 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1433 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1434 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1435 | if( eFileLock==NO_LOCK ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1436 | /* Decrement the shared lock counter. Release the lock using an |
| 1437 | ** OS call only when all threads in this same process have released |
| 1438 | ** the lock. |
| 1439 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1440 | pInode->nShared--; |
| 1441 | if( pInode->nShared==0 ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1442 | lock.l_type = F_UNLCK; |
| 1443 | lock.l_whence = SEEK_SET; |
| 1444 | lock.l_start = lock.l_len = 0L; |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1445 | SimulateIOErrorBenign(1); |
| 1446 | SimulateIOError( h=(-1) ) |
| 1447 | SimulateIOErrorBenign(0); |
| 1448 | if( fcntl(h, F_SETLK, &lock)!=(-1) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1449 | pInode->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1450 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1451 | tErrno = errno; |
danielk1977 | 5ad6a88 | 2008-09-15 04:20:31 +0000 | [diff] [blame] | 1452 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1453 | if( IS_LOCK_ERROR(rc) ){ |
| 1454 | pFile->lastErrno = tErrno; |
| 1455 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1456 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1457 | pFile->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1458 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1461 | /* Decrement the count of locks against this same file. When the |
| 1462 | ** count reaches zero, close any other file descriptors whose close |
| 1463 | ** was deferred because of outstanding locks. |
| 1464 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1465 | pInode->nLock--; |
| 1466 | assert( pInode->nLock>=0 ); |
| 1467 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1468 | closePendingFds(pFile); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1469 | } |
| 1470 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1471 | |
| 1472 | end_unlock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1473 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1474 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1475 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1479 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1480 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1481 | ** |
| 1482 | ** If the locking level of the file descriptor is already at or below |
| 1483 | ** the requested locking level, this routine is a no-op. |
| 1484 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1485 | static int unixUnlock(sqlite3_file *id, int eFileLock){ |
| 1486 | return _posixUnlock(id, eFileLock, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1490 | ** This function performs the parts of the "close file" operation |
| 1491 | ** common to all locking schemes. It closes the directory and file |
| 1492 | ** handles, if they are valid, and sets all fields of the unixFile |
| 1493 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1494 | ** |
| 1495 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 1496 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 1497 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1498 | */ |
| 1499 | static int closeUnixFile(sqlite3_file *id){ |
| 1500 | unixFile *pFile = (unixFile*)id; |
| 1501 | if( pFile ){ |
| 1502 | if( pFile->dirfd>=0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1503 | robust_close(pFile, pFile->dirfd, __LINE__); |
| 1504 | pFile->dirfd=-1; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1505 | } |
| 1506 | if( pFile->h>=0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1507 | robust_close(pFile, pFile->h, __LINE__); |
| 1508 | pFile->h = -1; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1509 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1510 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1511 | if( pFile->pId ){ |
| 1512 | if( pFile->isDelete ){ |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1513 | unlink(pFile->pId->zCanonicalName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1514 | } |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1515 | vxworksReleaseFileId(pFile->pId); |
| 1516 | pFile->pId = 0; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1517 | } |
| 1518 | #endif |
drh | ff59a11 | 2010-05-14 20:15:51 +0000 | [diff] [blame] | 1519 | OSTRACE(("CLOSE %-3d\n", pFile->h)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1520 | OpenCounter(-1); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1521 | sqlite3_free(pFile->pUnused); |
drh | ff59a11 | 2010-05-14 20:15:51 +0000 | [diff] [blame] | 1522 | memset(pFile, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1523 | } |
| 1524 | return SQLITE_OK; |
| 1525 | } |
| 1526 | |
| 1527 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1528 | ** Close a file. |
| 1529 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1530 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1531 | int rc = SQLITE_OK; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1532 | if( id ){ |
| 1533 | unixFile *pFile = (unixFile *)id; |
| 1534 | unixUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1535 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1536 | if( pFile->pInode && pFile->pInode->nLock ){ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1537 | /* If there are outstanding locks, do not actually close the file just |
| 1538 | ** yet because that would clear those locks. Instead, add the file |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1539 | ** descriptor to pInode->pUnused list. It will be automatically closed |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1540 | ** when the last lock is cleared. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1541 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1542 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1543 | } |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1544 | releaseInodeInfo(pFile); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1545 | rc = closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1546 | unixLeaveMutex(); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1547 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1548 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1551 | /************** End of the posix advisory lock implementation ***************** |
| 1552 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1553 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1554 | /****************************************************************************** |
| 1555 | ****************************** No-op Locking ********************************** |
| 1556 | ** |
| 1557 | ** Of the various locking implementations available, this is by far the |
| 1558 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 1559 | ** file for reading or writing. |
| 1560 | ** |
| 1561 | ** This locking mode is appropriate for use on read-only databases |
| 1562 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 1563 | ** also be used if the application employs some external mechanism to |
| 1564 | ** prevent simultaneous access of the same database by two or more |
| 1565 | ** database connections. But there is a serious risk of database |
| 1566 | ** corruption if this locking mode is used in situations where multiple |
| 1567 | ** database connections are accessing the same database file at the same |
| 1568 | ** time and one or more of those connections are writing. |
| 1569 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1570 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1571 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 1572 | UNUSED_PARAMETER(NotUsed); |
| 1573 | *pResOut = 0; |
| 1574 | return SQLITE_OK; |
| 1575 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1576 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1577 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1578 | return SQLITE_OK; |
| 1579 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1580 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1581 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1582 | return SQLITE_OK; |
| 1583 | } |
| 1584 | |
| 1585 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1586 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1587 | */ |
| 1588 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1589 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | /******************* End of the no-op lock implementation ********************* |
| 1593 | ******************************************************************************/ |
| 1594 | |
| 1595 | /****************************************************************************** |
| 1596 | ************************* Begin dot-file Locking ****************************** |
| 1597 | ** |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1598 | ** The dotfile locking implementation uses the existance of separate lock |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1599 | ** files in order to control access to the database. This works on just |
| 1600 | ** about every filesystem imaginable. But there are serious downsides: |
| 1601 | ** |
| 1602 | ** (1) There is zero concurrency. A single reader blocks all other |
| 1603 | ** connections from reading or writing the database. |
| 1604 | ** |
| 1605 | ** (2) An application crash or power loss can leave stale lock files |
| 1606 | ** sitting around that need to be cleared manually. |
| 1607 | ** |
| 1608 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 1609 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1610 | ** |
| 1611 | ** Dotfile locking works by creating a file in the same directory as the |
| 1612 | ** database and with the same name but with a ".lock" extension added. |
| 1613 | ** The existance of a lock file implies an EXCLUSIVE lock. All other lock |
| 1614 | ** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1615 | */ |
| 1616 | |
| 1617 | /* |
| 1618 | ** The file suffix added to the data base filename in order to create the |
| 1619 | ** lock file. |
| 1620 | */ |
| 1621 | #define DOTLOCK_SUFFIX ".lock" |
| 1622 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1623 | /* |
| 1624 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1625 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1626 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1627 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1628 | ** |
| 1629 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 1630 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 1631 | ** is held on the file and false if the file is unlocked. |
| 1632 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1633 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 1634 | int rc = SQLITE_OK; |
| 1635 | int reserved = 0; |
| 1636 | unixFile *pFile = (unixFile*)id; |
| 1637 | |
| 1638 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1639 | |
| 1640 | assert( pFile ); |
| 1641 | |
| 1642 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1643 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1644 | /* Either this connection or some other connection in the same process |
| 1645 | ** holds a lock on the file. No need to check further. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1646 | reserved = 1; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1647 | }else{ |
| 1648 | /* The lock is held if and only if the lockfile exists */ |
| 1649 | const char *zLockFile = (const char*)pFile->lockingContext; |
| 1650 | reserved = access(zLockFile, 0)==0; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1651 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1652 | OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1653 | *pResOut = reserved; |
| 1654 | return rc; |
| 1655 | } |
| 1656 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1657 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1658 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1659 | ** of the following: |
| 1660 | ** |
| 1661 | ** (1) SHARED_LOCK |
| 1662 | ** (2) RESERVED_LOCK |
| 1663 | ** (3) PENDING_LOCK |
| 1664 | ** (4) EXCLUSIVE_LOCK |
| 1665 | ** |
| 1666 | ** Sometimes when requesting one lock state, additional lock states |
| 1667 | ** are inserted in between. The locking might fail on one of the later |
| 1668 | ** transitions leaving the lock state different from what it started but |
| 1669 | ** still short of its goal. The following chart shows the allowed |
| 1670 | ** transitions and the inserted intermediate states: |
| 1671 | ** |
| 1672 | ** UNLOCKED -> SHARED |
| 1673 | ** SHARED -> RESERVED |
| 1674 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1675 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1676 | ** PENDING -> EXCLUSIVE |
| 1677 | ** |
| 1678 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1679 | ** routine to lower a locking level. |
| 1680 | ** |
| 1681 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 1682 | ** But we track the other locking levels internally. |
| 1683 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1684 | static int dotlockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1685 | unixFile *pFile = (unixFile*)id; |
| 1686 | int fd; |
| 1687 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1688 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1689 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1690 | |
| 1691 | /* If we have any lock, then the lock file already exists. All we have |
| 1692 | ** to do is adjust our internal record of the lock level. |
| 1693 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1694 | if( pFile->eFileLock > NO_LOCK ){ |
| 1695 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1696 | #if !OS_VXWORKS |
| 1697 | /* Always update the timestamp on the old file */ |
| 1698 | utimes(zLockFile, NULL); |
| 1699 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1700 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | /* grab an exclusive lock */ |
| 1704 | fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600); |
| 1705 | if( fd<0 ){ |
| 1706 | /* failed to open/create the file, someone else may have stolen the lock */ |
| 1707 | int tErrno = errno; |
| 1708 | if( EEXIST == tErrno ){ |
| 1709 | rc = SQLITE_BUSY; |
| 1710 | } else { |
| 1711 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1712 | if( IS_LOCK_ERROR(rc) ){ |
| 1713 | pFile->lastErrno = tErrno; |
| 1714 | } |
| 1715 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1716 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1717 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1718 | robust_close(pFile, fd, __LINE__); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1719 | |
| 1720 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1721 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1722 | return rc; |
| 1723 | } |
| 1724 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1725 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1726 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1727 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1728 | ** |
| 1729 | ** If the locking level of the file descriptor is already at or below |
| 1730 | ** the requested locking level, this routine is a no-op. |
| 1731 | ** |
| 1732 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 1733 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1734 | static int dotlockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1735 | unixFile *pFile = (unixFile*)id; |
| 1736 | char *zLockFile = (char *)pFile->lockingContext; |
| 1737 | |
| 1738 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1739 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
| 1740 | pFile->eFileLock, getpid())); |
| 1741 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1742 | |
| 1743 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1744 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1745 | return SQLITE_OK; |
| 1746 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1747 | |
| 1748 | /* To downgrade to shared, simply update our internal notion of the |
| 1749 | ** lock state. No need to mess with the file on disk. |
| 1750 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1751 | if( eFileLock==SHARED_LOCK ){ |
| 1752 | pFile->eFileLock = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1753 | return SQLITE_OK; |
| 1754 | } |
| 1755 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1756 | /* To fully unlock the database, delete the lock file */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1757 | assert( eFileLock==NO_LOCK ); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1758 | if( unlink(zLockFile) ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 1759 | int rc = 0; |
| 1760 | int tErrno = errno; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1761 | if( ENOENT != tErrno ){ |
| 1762 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1763 | } |
| 1764 | if( IS_LOCK_ERROR(rc) ){ |
| 1765 | pFile->lastErrno = tErrno; |
| 1766 | } |
| 1767 | return rc; |
| 1768 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1769 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1770 | return SQLITE_OK; |
| 1771 | } |
| 1772 | |
| 1773 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1774 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1775 | */ |
| 1776 | static int dotlockClose(sqlite3_file *id) { |
| 1777 | int rc; |
| 1778 | if( id ){ |
| 1779 | unixFile *pFile = (unixFile*)id; |
| 1780 | dotlockUnlock(id, NO_LOCK); |
| 1781 | sqlite3_free(pFile->lockingContext); |
| 1782 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1783 | rc = closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1784 | return rc; |
| 1785 | } |
| 1786 | /****************** End of the dot-file lock implementation ******************* |
| 1787 | ******************************************************************************/ |
| 1788 | |
| 1789 | /****************************************************************************** |
| 1790 | ************************** Begin flock Locking ******************************** |
| 1791 | ** |
| 1792 | ** Use the flock() system call to do file locking. |
| 1793 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1794 | ** flock() locking is like dot-file locking in that the various |
| 1795 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 1796 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 1797 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 1798 | ** still works when you do this, but concurrency is reduced since |
| 1799 | ** only a single process can be reading the database at a time. |
| 1800 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1801 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if |
| 1802 | ** compiling for VXWORKS. |
| 1803 | */ |
| 1804 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1805 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1806 | /* |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 1807 | ** Retry flock() calls that fail with EINTR |
| 1808 | */ |
| 1809 | #ifdef EINTR |
| 1810 | static int robust_flock(int fd, int op){ |
| 1811 | int rc; |
| 1812 | do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR ); |
| 1813 | return rc; |
| 1814 | } |
| 1815 | #else |
drh | 5c81927 | 2011-02-23 14:00:12 +0000 | [diff] [blame] | 1816 | # define robust_flock(a,b) flock(a,b) |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 1817 | #endif |
| 1818 | |
| 1819 | |
| 1820 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1821 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1822 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1823 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1824 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1825 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1826 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 1827 | int rc = SQLITE_OK; |
| 1828 | int reserved = 0; |
| 1829 | unixFile *pFile = (unixFile*)id; |
| 1830 | |
| 1831 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1832 | |
| 1833 | assert( pFile ); |
| 1834 | |
| 1835 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1836 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1837 | reserved = 1; |
| 1838 | } |
| 1839 | |
| 1840 | /* Otherwise see if some other process holds it. */ |
| 1841 | if( !reserved ){ |
| 1842 | /* attempt to get the lock */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 1843 | int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1844 | if( !lrc ){ |
| 1845 | /* got the lock, unlock it */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 1846 | lrc = robust_flock(pFile->h, LOCK_UN); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1847 | if ( lrc ) { |
| 1848 | int tErrno = errno; |
| 1849 | /* unlock failed with an error */ |
| 1850 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1851 | if( IS_LOCK_ERROR(lrc) ){ |
| 1852 | pFile->lastErrno = tErrno; |
| 1853 | rc = lrc; |
| 1854 | } |
| 1855 | } |
| 1856 | } else { |
| 1857 | int tErrno = errno; |
| 1858 | reserved = 1; |
| 1859 | /* someone else might have it reserved */ |
| 1860 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1861 | if( IS_LOCK_ERROR(lrc) ){ |
| 1862 | pFile->lastErrno = tErrno; |
| 1863 | rc = lrc; |
| 1864 | } |
| 1865 | } |
| 1866 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1867 | OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1868 | |
| 1869 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 1870 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 1871 | rc = SQLITE_OK; |
| 1872 | reserved=1; |
| 1873 | } |
| 1874 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 1875 | *pResOut = reserved; |
| 1876 | return rc; |
| 1877 | } |
| 1878 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1879 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1880 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1881 | ** of the following: |
| 1882 | ** |
| 1883 | ** (1) SHARED_LOCK |
| 1884 | ** (2) RESERVED_LOCK |
| 1885 | ** (3) PENDING_LOCK |
| 1886 | ** (4) EXCLUSIVE_LOCK |
| 1887 | ** |
| 1888 | ** Sometimes when requesting one lock state, additional lock states |
| 1889 | ** are inserted in between. The locking might fail on one of the later |
| 1890 | ** transitions leaving the lock state different from what it started but |
| 1891 | ** still short of its goal. The following chart shows the allowed |
| 1892 | ** transitions and the inserted intermediate states: |
| 1893 | ** |
| 1894 | ** UNLOCKED -> SHARED |
| 1895 | ** SHARED -> RESERVED |
| 1896 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1897 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1898 | ** PENDING -> EXCLUSIVE |
| 1899 | ** |
| 1900 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 1901 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 1902 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 1903 | ** access the file. |
| 1904 | ** |
| 1905 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1906 | ** routine to lower a locking level. |
| 1907 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1908 | static int flockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1909 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1910 | unixFile *pFile = (unixFile*)id; |
| 1911 | |
| 1912 | assert( pFile ); |
| 1913 | |
| 1914 | /* if we already have a lock, it is exclusive. |
| 1915 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1916 | if (pFile->eFileLock > NO_LOCK) { |
| 1917 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1918 | return SQLITE_OK; |
| 1919 | } |
| 1920 | |
| 1921 | /* grab an exclusive lock */ |
| 1922 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 1923 | if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1924 | int tErrno = errno; |
| 1925 | /* didn't get, must be busy */ |
| 1926 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1927 | if( IS_LOCK_ERROR(rc) ){ |
| 1928 | pFile->lastErrno = tErrno; |
| 1929 | } |
| 1930 | } else { |
| 1931 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1932 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1933 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1934 | OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), |
| 1935 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1936 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 1937 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 1938 | rc = SQLITE_BUSY; |
| 1939 | } |
| 1940 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 1941 | return rc; |
| 1942 | } |
| 1943 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1944 | |
| 1945 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1946 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1947 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1948 | ** |
| 1949 | ** If the locking level of the file descriptor is already at or below |
| 1950 | ** the requested locking level, this routine is a no-op. |
| 1951 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1952 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1953 | unixFile *pFile = (unixFile*)id; |
| 1954 | |
| 1955 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1956 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
| 1957 | pFile->eFileLock, getpid())); |
| 1958 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1959 | |
| 1960 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1961 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1962 | return SQLITE_OK; |
| 1963 | } |
| 1964 | |
| 1965 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1966 | if (eFileLock==SHARED_LOCK) { |
| 1967 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1968 | return SQLITE_OK; |
| 1969 | } |
| 1970 | |
| 1971 | /* no, really, unlock. */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 1972 | int rc = robust_flock(pFile->h, LOCK_UN); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1973 | if (rc) { |
| 1974 | int r, tErrno = errno; |
| 1975 | r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1976 | if( IS_LOCK_ERROR(r) ){ |
| 1977 | pFile->lastErrno = tErrno; |
| 1978 | } |
| 1979 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 1980 | if( (r & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 1981 | r = SQLITE_BUSY; |
| 1982 | } |
| 1983 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 1984 | |
| 1985 | return r; |
| 1986 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1987 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1988 | return SQLITE_OK; |
| 1989 | } |
| 1990 | } |
| 1991 | |
| 1992 | /* |
| 1993 | ** Close a file. |
| 1994 | */ |
| 1995 | static int flockClose(sqlite3_file *id) { |
| 1996 | if( id ){ |
| 1997 | flockUnlock(id, NO_LOCK); |
| 1998 | } |
| 1999 | return closeUnixFile(id); |
| 2000 | } |
| 2001 | |
| 2002 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 2003 | |
| 2004 | /******************* End of the flock lock implementation ********************* |
| 2005 | ******************************************************************************/ |
| 2006 | |
| 2007 | /****************************************************************************** |
| 2008 | ************************ Begin Named Semaphore Locking ************************ |
| 2009 | ** |
| 2010 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2011 | ** |
| 2012 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 2013 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 2014 | ** the database file at a time. This reduces potential concurrency, but |
| 2015 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2016 | */ |
| 2017 | #if OS_VXWORKS |
| 2018 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2019 | /* |
| 2020 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2021 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2022 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2023 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2024 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2025 | static int semCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 2026 | int rc = SQLITE_OK; |
| 2027 | int reserved = 0; |
| 2028 | unixFile *pFile = (unixFile*)id; |
| 2029 | |
| 2030 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2031 | |
| 2032 | assert( pFile ); |
| 2033 | |
| 2034 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2035 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2036 | reserved = 1; |
| 2037 | } |
| 2038 | |
| 2039 | /* Otherwise see if some other process holds it. */ |
| 2040 | if( !reserved ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2041 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2042 | struct stat statBuf; |
| 2043 | |
| 2044 | if( sem_trywait(pSem)==-1 ){ |
| 2045 | int tErrno = errno; |
| 2046 | if( EAGAIN != tErrno ){ |
| 2047 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 2048 | pFile->lastErrno = tErrno; |
| 2049 | } else { |
| 2050 | /* someone else has the lock when we are in NO_LOCK */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2051 | reserved = (pFile->eFileLock < SHARED_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2052 | } |
| 2053 | }else{ |
| 2054 | /* we could have it if we want it */ |
| 2055 | sem_post(pSem); |
| 2056 | } |
| 2057 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2058 | OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2059 | |
| 2060 | *pResOut = reserved; |
| 2061 | return rc; |
| 2062 | } |
| 2063 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2064 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2065 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2066 | ** of the following: |
| 2067 | ** |
| 2068 | ** (1) SHARED_LOCK |
| 2069 | ** (2) RESERVED_LOCK |
| 2070 | ** (3) PENDING_LOCK |
| 2071 | ** (4) EXCLUSIVE_LOCK |
| 2072 | ** |
| 2073 | ** Sometimes when requesting one lock state, additional lock states |
| 2074 | ** are inserted in between. The locking might fail on one of the later |
| 2075 | ** transitions leaving the lock state different from what it started but |
| 2076 | ** still short of its goal. The following chart shows the allowed |
| 2077 | ** transitions and the inserted intermediate states: |
| 2078 | ** |
| 2079 | ** UNLOCKED -> SHARED |
| 2080 | ** SHARED -> RESERVED |
| 2081 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2082 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2083 | ** PENDING -> EXCLUSIVE |
| 2084 | ** |
| 2085 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 2086 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2087 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2088 | ** access the file. |
| 2089 | ** |
| 2090 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2091 | ** routine to lower a locking level. |
| 2092 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2093 | static int semLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2094 | unixFile *pFile = (unixFile*)id; |
| 2095 | int fd; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2096 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2097 | int rc = SQLITE_OK; |
| 2098 | |
| 2099 | /* if we already have a lock, it is exclusive. |
| 2100 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2101 | if (pFile->eFileLock > NO_LOCK) { |
| 2102 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2103 | rc = SQLITE_OK; |
| 2104 | goto sem_end_lock; |
| 2105 | } |
| 2106 | |
| 2107 | /* lock semaphore now but bail out when already locked. */ |
| 2108 | if( sem_trywait(pSem)==-1 ){ |
| 2109 | rc = SQLITE_BUSY; |
| 2110 | goto sem_end_lock; |
| 2111 | } |
| 2112 | |
| 2113 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2114 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2115 | |
| 2116 | sem_end_lock: |
| 2117 | return rc; |
| 2118 | } |
| 2119 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2120 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2121 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2122 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2123 | ** |
| 2124 | ** If the locking level of the file descriptor is already at or below |
| 2125 | ** the requested locking level, this routine is a no-op. |
| 2126 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2127 | static int semUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2128 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2129 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2130 | |
| 2131 | assert( pFile ); |
| 2132 | assert( pSem ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2133 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
| 2134 | pFile->eFileLock, getpid())); |
| 2135 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2136 | |
| 2137 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2138 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2139 | return SQLITE_OK; |
| 2140 | } |
| 2141 | |
| 2142 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2143 | if (eFileLock==SHARED_LOCK) { |
| 2144 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2145 | return SQLITE_OK; |
| 2146 | } |
| 2147 | |
| 2148 | /* no, really unlock. */ |
| 2149 | if ( sem_post(pSem)==-1 ) { |
| 2150 | int rc, tErrno = errno; |
| 2151 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2152 | if( IS_LOCK_ERROR(rc) ){ |
| 2153 | pFile->lastErrno = tErrno; |
| 2154 | } |
| 2155 | return rc; |
| 2156 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2157 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2158 | return SQLITE_OK; |
| 2159 | } |
| 2160 | |
| 2161 | /* |
| 2162 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2163 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2164 | static int semClose(sqlite3_file *id) { |
| 2165 | if( id ){ |
| 2166 | unixFile *pFile = (unixFile*)id; |
| 2167 | semUnlock(id, NO_LOCK); |
| 2168 | assert( pFile ); |
| 2169 | unixEnterMutex(); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2170 | releaseInodeInfo(pFile); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2171 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2172 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2173 | } |
| 2174 | return SQLITE_OK; |
| 2175 | } |
| 2176 | |
| 2177 | #endif /* OS_VXWORKS */ |
| 2178 | /* |
| 2179 | ** Named semaphore locking is only available on VxWorks. |
| 2180 | ** |
| 2181 | *************** End of the named semaphore lock implementation **************** |
| 2182 | ******************************************************************************/ |
| 2183 | |
| 2184 | |
| 2185 | /****************************************************************************** |
| 2186 | *************************** Begin AFP Locking ********************************* |
| 2187 | ** |
| 2188 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2189 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2190 | ** |
| 2191 | ** Third-party implementations of AFP are available. But this code here |
| 2192 | ** only works on OSX. |
| 2193 | */ |
| 2194 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2195 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2196 | /* |
| 2197 | ** The afpLockingContext structure contains all afp lock specific state |
| 2198 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2199 | typedef struct afpLockingContext afpLockingContext; |
| 2200 | struct afpLockingContext { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2201 | int reserved; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2202 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2203 | }; |
| 2204 | |
| 2205 | struct ByteRangeLockPB2 |
| 2206 | { |
| 2207 | unsigned long long offset; /* offset to first byte to lock */ |
| 2208 | unsigned long long length; /* nbr of bytes to lock */ |
| 2209 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2210 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2211 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2212 | int fd; /* file desc to assoc this lock with */ |
| 2213 | }; |
| 2214 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2215 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2216 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2217 | /* |
| 2218 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2219 | ** AFP filesystem. |
| 2220 | ** |
| 2221 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2222 | */ |
| 2223 | static int afpSetLock( |
| 2224 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2225 | unixFile *pFile, /* Open file descriptor on path */ |
| 2226 | unsigned long long offset, /* First byte to be locked */ |
| 2227 | unsigned long long length, /* Number of bytes to lock */ |
| 2228 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2229 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2230 | struct ByteRangeLockPB2 pb; |
| 2231 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2232 | |
| 2233 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2234 | pb.startEndFlag = 0; |
| 2235 | pb.offset = offset; |
| 2236 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2237 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2238 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2239 | OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2240 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2241 | offset, length)); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2242 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2243 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2244 | int rc; |
| 2245 | int tErrno = errno; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2246 | OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2247 | path, tErrno, strerror(tErrno))); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2248 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2249 | rc = SQLITE_BUSY; |
| 2250 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2251 | rc = sqliteErrorFromPosixError(tErrno, |
| 2252 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2253 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2254 | if( IS_LOCK_ERROR(rc) ){ |
| 2255 | pFile->lastErrno = tErrno; |
| 2256 | } |
| 2257 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2258 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2259 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2260 | } |
| 2261 | } |
| 2262 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2263 | /* |
| 2264 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2265 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2266 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2267 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2268 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2269 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2270 | int rc = SQLITE_OK; |
| 2271 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2272 | unixFile *pFile = (unixFile*)id; |
| 2273 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2274 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2275 | |
| 2276 | assert( pFile ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2277 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2278 | if( context->reserved ){ |
| 2279 | *pResOut = 1; |
| 2280 | return SQLITE_OK; |
| 2281 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2282 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2283 | |
| 2284 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2285 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2286 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | /* Otherwise see if some other process holds it. |
| 2290 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2291 | if( !reserved ){ |
| 2292 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2293 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2294 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2295 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2296 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2297 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2298 | } else { |
| 2299 | /* if we failed to get the lock then someone else must have it */ |
| 2300 | reserved = 1; |
| 2301 | } |
| 2302 | if( IS_LOCK_ERROR(lrc) ){ |
| 2303 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2304 | } |
| 2305 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2306 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2307 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2308 | OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2309 | |
| 2310 | *pResOut = reserved; |
| 2311 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2314 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2315 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2316 | ** of the following: |
| 2317 | ** |
| 2318 | ** (1) SHARED_LOCK |
| 2319 | ** (2) RESERVED_LOCK |
| 2320 | ** (3) PENDING_LOCK |
| 2321 | ** (4) EXCLUSIVE_LOCK |
| 2322 | ** |
| 2323 | ** Sometimes when requesting one lock state, additional lock states |
| 2324 | ** are inserted in between. The locking might fail on one of the later |
| 2325 | ** transitions leaving the lock state different from what it started but |
| 2326 | ** still short of its goal. The following chart shows the allowed |
| 2327 | ** transitions and the inserted intermediate states: |
| 2328 | ** |
| 2329 | ** UNLOCKED -> SHARED |
| 2330 | ** SHARED -> RESERVED |
| 2331 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2332 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2333 | ** PENDING -> EXCLUSIVE |
| 2334 | ** |
| 2335 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2336 | ** routine to lower a locking level. |
| 2337 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2338 | static int afpLock(sqlite3_file *id, int eFileLock){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2339 | int rc = SQLITE_OK; |
| 2340 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2341 | unixInodeInfo *pInode = pFile->pInode; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2342 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2343 | |
| 2344 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2345 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 2346 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2347 | azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2348 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2349 | /* 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] | 2350 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2351 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2352 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2353 | if( pFile->eFileLock>=eFileLock ){ |
| 2354 | OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
| 2355 | azFileLock(eFileLock))); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2356 | return SQLITE_OK; |
| 2357 | } |
| 2358 | |
| 2359 | /* Make sure the locking sequence is correct |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2360 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 2361 | ** (2) SQLite never explicitly requests a pendig lock. |
| 2362 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2363 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2364 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 2365 | assert( eFileLock!=PENDING_LOCK ); |
| 2366 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2367 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2368 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2369 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2370 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2371 | pInode = pFile->pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2372 | |
| 2373 | /* If some thread using this PID has a lock via a different unixFile* |
| 2374 | ** handle that precludes the requested lock, return BUSY. |
| 2375 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2376 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 2377 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2378 | ){ |
| 2379 | rc = SQLITE_BUSY; |
| 2380 | goto afp_end_lock; |
| 2381 | } |
| 2382 | |
| 2383 | /* If a SHARED lock is requested, and some thread using this PID already |
| 2384 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 2385 | ** return SQLITE_OK. |
| 2386 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2387 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2388 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2389 | assert( eFileLock==SHARED_LOCK ); |
| 2390 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2391 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2392 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2393 | pInode->nShared++; |
| 2394 | pInode->nLock++; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2395 | goto afp_end_lock; |
| 2396 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2397 | |
| 2398 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2399 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2400 | ** be released. |
| 2401 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2402 | if( eFileLock==SHARED_LOCK |
| 2403 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2404 | ){ |
| 2405 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2406 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2407 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2408 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2409 | goto afp_end_lock; |
| 2410 | } |
| 2411 | } |
| 2412 | |
| 2413 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2414 | ** operating system calls for the specified lock. |
| 2415 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2416 | if( eFileLock==SHARED_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2417 | int lrc1, lrc2, lrc1Errno; |
| 2418 | long lk, mask; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2419 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2420 | assert( pInode->nShared==0 ); |
| 2421 | assert( pInode->eFileLock==0 ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2422 | |
| 2423 | mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2424 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2425 | /* note that the quality of the randomness doesn't matter that much */ |
| 2426 | lk = random(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2427 | pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2428 | lrc1 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2429 | SHARED_FIRST+pInode->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2430 | if( IS_LOCK_ERROR(lrc1) ){ |
| 2431 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2432 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2433 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2434 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2435 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2436 | if( IS_LOCK_ERROR(lrc1) ) { |
| 2437 | pFile->lastErrno = lrc1Errno; |
| 2438 | rc = lrc1; |
| 2439 | goto afp_end_lock; |
| 2440 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 2441 | rc = lrc2; |
| 2442 | goto afp_end_lock; |
| 2443 | } else if( lrc1 != SQLITE_OK ) { |
| 2444 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2445 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2446 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2447 | pInode->nLock++; |
| 2448 | pInode->nShared = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2449 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2450 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2451 | /* We are trying for an exclusive lock but another thread in this |
| 2452 | ** same process is still holding a shared lock. */ |
| 2453 | rc = SQLITE_BUSY; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2454 | }else{ |
| 2455 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 2456 | ** assumed that there is a SHARED or greater lock on the file |
| 2457 | ** already. |
| 2458 | */ |
| 2459 | int failed = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2460 | assert( 0!=pFile->eFileLock ); |
| 2461 | if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2462 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2463 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2464 | if( !failed ){ |
| 2465 | context->reserved = 1; |
| 2466 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2467 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2468 | if (!failed && eFileLock == EXCLUSIVE_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2469 | /* Acquire an EXCLUSIVE lock */ |
| 2470 | |
| 2471 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2472 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2473 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2474 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2475 | pInode->sharedByte, 1, 0)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2476 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2477 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2478 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2479 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2480 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2481 | SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2482 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 2483 | ** a critical I/O error |
| 2484 | */ |
| 2485 | rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : |
| 2486 | SQLITE_IOERR_LOCK; |
| 2487 | goto afp_end_lock; |
| 2488 | } |
| 2489 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2490 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2491 | } |
| 2492 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2493 | if( failed ){ |
| 2494 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2495 | } |
| 2496 | } |
| 2497 | |
| 2498 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2499 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2500 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2501 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 2502 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2503 | pInode->eFileLock = PENDING_LOCK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | afp_end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2507 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2508 | OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), |
| 2509 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2510 | return rc; |
| 2511 | } |
| 2512 | |
| 2513 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2514 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2515 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2516 | ** |
| 2517 | ** If the locking level of the file descriptor is already at or below |
| 2518 | ** the requested locking level, this routine is a no-op. |
| 2519 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2520 | static int afpUnlock(sqlite3_file *id, int eFileLock) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2521 | int rc = SQLITE_OK; |
| 2522 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2523 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2524 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 2525 | int skipShared = 0; |
| 2526 | #ifdef SQLITE_TEST |
| 2527 | int h = pFile->h; |
| 2528 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2529 | |
| 2530 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2531 | 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] | 2532 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2533 | getpid())); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2534 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2535 | assert( eFileLock<=SHARED_LOCK ); |
| 2536 | if( pFile->eFileLock<=eFileLock ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2537 | return SQLITE_OK; |
| 2538 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2539 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2540 | pInode = pFile->pInode; |
| 2541 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2542 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2543 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2544 | SimulateIOErrorBenign(1); |
| 2545 | SimulateIOError( h=(-1) ) |
| 2546 | SimulateIOErrorBenign(0); |
| 2547 | |
| 2548 | #ifndef NDEBUG |
| 2549 | /* When reducing a lock such that other processes can start |
| 2550 | ** reading the database file again, make sure that the |
| 2551 | ** transaction counter was updated if any part of the database |
| 2552 | ** file changed. If the transaction counter is not updated, |
| 2553 | ** other connections to the same file might not realize that |
| 2554 | ** the file has changed and hence might not know to flush their |
| 2555 | ** cache. The use of a stale cache can lead to database corruption. |
| 2556 | */ |
| 2557 | assert( pFile->inNormalWrite==0 |
| 2558 | || pFile->dbUpdate==0 |
| 2559 | || pFile->transCntrChng==1 ); |
| 2560 | pFile->inNormalWrite = 0; |
| 2561 | #endif |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2562 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2563 | if( pFile->eFileLock==EXCLUSIVE_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2564 | rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2565 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2566 | /* only re-establish the shared lock if necessary */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2567 | int sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2568 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 2569 | } else { |
| 2570 | skipShared = 1; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2571 | } |
| 2572 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2573 | if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2574 | rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2575 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2576 | if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2577 | rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 2578 | if( !rc ){ |
| 2579 | context->reserved = 0; |
| 2580 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2581 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2582 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ |
| 2583 | pInode->eFileLock = SHARED_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2584 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2585 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2586 | if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2587 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2588 | /* Decrement the shared lock counter. Release the lock using an |
| 2589 | ** OS call only when all threads in this same process have released |
| 2590 | ** the lock. |
| 2591 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2592 | unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 2593 | pInode->nShared--; |
| 2594 | if( pInode->nShared==0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2595 | SimulateIOErrorBenign(1); |
| 2596 | SimulateIOError( h=(-1) ) |
| 2597 | SimulateIOErrorBenign(0); |
| 2598 | if( !skipShared ){ |
| 2599 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 2600 | } |
| 2601 | if( !rc ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2602 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2603 | pFile->eFileLock = NO_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2604 | } |
| 2605 | } |
| 2606 | if( rc==SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2607 | pInode->nLock--; |
| 2608 | assert( pInode->nLock>=0 ); |
| 2609 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 2610 | closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2611 | } |
| 2612 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2613 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2614 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2615 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2616 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2617 | return rc; |
| 2618 | } |
| 2619 | |
| 2620 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2621 | ** Close a file & cleanup AFP specific locking context |
| 2622 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2623 | static int afpClose(sqlite3_file *id) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2624 | int rc = SQLITE_OK; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2625 | if( id ){ |
| 2626 | unixFile *pFile = (unixFile*)id; |
| 2627 | afpUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2628 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2629 | if( pFile->pInode && pFile->pInode->nLock ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2630 | /* If there are outstanding locks, do not actually close the file just |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2631 | ** yet because that would clear those locks. Instead, add the file |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2632 | ** descriptor to pInode->aPending. It will be automatically closed when |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2633 | ** the last lock is cleared. |
| 2634 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2635 | setPendingFd(pFile); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2636 | } |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2637 | releaseInodeInfo(pFile); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2638 | sqlite3_free(pFile->lockingContext); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2639 | rc = closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2640 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2641 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2642 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2645 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2646 | /* |
| 2647 | ** The code above is the AFP lock implementation. The code is specific |
| 2648 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2649 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 2650 | ** VFS is not available. |
| 2651 | ** |
| 2652 | ********************* End of the AFP lock implementation ********************** |
| 2653 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2654 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2655 | /****************************************************************************** |
| 2656 | *************************** Begin NFS Locking ********************************/ |
| 2657 | |
| 2658 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 2659 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2660 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2661 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2662 | ** |
| 2663 | ** If the locking level of the file descriptor is already at or below |
| 2664 | ** the requested locking level, this routine is a no-op. |
| 2665 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2666 | static int nfsUnlock(sqlite3_file *id, int eFileLock){ |
| 2667 | return _posixUnlock(id, eFileLock, 1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 2671 | /* |
| 2672 | ** The code above is the NFS lock implementation. The code is specific |
| 2673 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2674 | ** is available. |
| 2675 | ** |
| 2676 | ********************* End of the NFS lock implementation ********************** |
| 2677 | ******************************************************************************/ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2678 | |
| 2679 | /****************************************************************************** |
| 2680 | **************** Non-locking sqlite3_file methods ***************************** |
| 2681 | ** |
| 2682 | ** The next division contains implementations for all methods of the |
| 2683 | ** sqlite3_file object other than the locking methods. The locking |
| 2684 | ** methods were defined in divisions above (one locking method per |
| 2685 | ** division). Those methods that are common to all locking modes |
| 2686 | ** are gather together into this division. |
| 2687 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2688 | |
| 2689 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2690 | ** Seek to the offset passed as the second argument, then read cnt |
| 2691 | ** bytes into pBuf. Return the number of bytes actually read. |
| 2692 | ** |
| 2693 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 2694 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 2695 | ** one system to another. Since SQLite does not define USE_PREAD |
| 2696 | ** any any form by default, we will not attempt to define _XOPEN_SOURCE. |
| 2697 | ** See tickets #2741 and #2681. |
| 2698 | ** |
| 2699 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 2700 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2701 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2702 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 2703 | int got; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2704 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2705 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2706 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2707 | TIMER_START; |
| 2708 | #if defined(USE_PREAD) |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2709 | do{ got = pread(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2710 | SimulateIOError( got = -1 ); |
| 2711 | #elif defined(USE_PREAD64) |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2712 | do{ got = pread64(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2713 | SimulateIOError( got = -1 ); |
| 2714 | #else |
| 2715 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 2716 | SimulateIOError( newOffset-- ); |
| 2717 | if( newOffset!=offset ){ |
| 2718 | if( newOffset == -1 ){ |
| 2719 | ((unixFile*)id)->lastErrno = errno; |
| 2720 | }else{ |
| 2721 | ((unixFile*)id)->lastErrno = 0; |
| 2722 | } |
| 2723 | return -1; |
| 2724 | } |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2725 | do{ got = read(id->h, pBuf, cnt); }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2726 | #endif |
| 2727 | TIMER_END; |
| 2728 | if( got<0 ){ |
| 2729 | ((unixFile*)id)->lastErrno = errno; |
| 2730 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2731 | OSTRACE(("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2732 | return got; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
| 2735 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2736 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 2737 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 2738 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2739 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2740 | static int unixRead( |
| 2741 | sqlite3_file *id, |
| 2742 | void *pBuf, |
| 2743 | int amt, |
| 2744 | sqlite3_int64 offset |
| 2745 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2746 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2747 | int got; |
| 2748 | assert( id ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2749 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2750 | /* If this is a database file (not a journal, master-journal or temp |
| 2751 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2752 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 2753 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2754 | || offset>=PENDING_BYTE+512 |
| 2755 | || offset+amt<=PENDING_BYTE |
| 2756 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2757 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2758 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2759 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2760 | if( got==amt ){ |
| 2761 | return SQLITE_OK; |
| 2762 | }else if( got<0 ){ |
| 2763 | /* lastErrno set by seekAndRead */ |
| 2764 | return SQLITE_IOERR_READ; |
| 2765 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2766 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2767 | /* Unread parts of the buffer must be zero-filled */ |
| 2768 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 2769 | return SQLITE_IOERR_SHORT_READ; |
| 2770 | } |
| 2771 | } |
| 2772 | |
| 2773 | /* |
| 2774 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 2775 | ** Return the number of bytes actually read. Update the offset. |
| 2776 | ** |
| 2777 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 2778 | ** is set before returning. |
| 2779 | */ |
| 2780 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
| 2781 | int got; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2782 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2783 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2784 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2785 | TIMER_START; |
| 2786 | #if defined(USE_PREAD) |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2787 | do{ got = pwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2788 | #elif defined(USE_PREAD64) |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2789 | do{ got = pwrite64(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2790 | #else |
| 2791 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 2792 | if( newOffset!=offset ){ |
| 2793 | if( newOffset == -1 ){ |
| 2794 | ((unixFile*)id)->lastErrno = errno; |
| 2795 | }else{ |
| 2796 | ((unixFile*)id)->lastErrno = 0; |
| 2797 | } |
| 2798 | return -1; |
| 2799 | } |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2800 | do{ got = write(id->h, pBuf, cnt); }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2801 | #endif |
| 2802 | TIMER_END; |
| 2803 | if( got<0 ){ |
| 2804 | ((unixFile*)id)->lastErrno = errno; |
| 2805 | } |
| 2806 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2807 | OSTRACE(("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2808 | return got; |
| 2809 | } |
| 2810 | |
| 2811 | |
| 2812 | /* |
| 2813 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 2814 | ** or some other error code on failure. |
| 2815 | */ |
| 2816 | static int unixWrite( |
| 2817 | sqlite3_file *id, |
| 2818 | const void *pBuf, |
| 2819 | int amt, |
| 2820 | sqlite3_int64 offset |
| 2821 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2822 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2823 | int wrote = 0; |
| 2824 | assert( id ); |
| 2825 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2826 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2827 | /* If this is a database file (not a journal, master-journal or temp |
| 2828 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2829 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 2830 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2831 | || offset>=PENDING_BYTE+512 |
| 2832 | || offset+amt<=PENDING_BYTE |
| 2833 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2834 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2835 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2836 | #ifndef NDEBUG |
| 2837 | /* If we are doing a normal write to a database file (as opposed to |
| 2838 | ** doing a hot-journal rollback or a write to some file other than a |
| 2839 | ** normal database file) then record the fact that the database |
| 2840 | ** has changed. If the transaction counter is modified, record that |
| 2841 | ** fact too. |
| 2842 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2843 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2844 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 2845 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 2846 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2847 | char oldCntr[4]; |
| 2848 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 2849 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2850 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 2851 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2852 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 2853 | } |
| 2854 | } |
| 2855 | } |
| 2856 | #endif |
| 2857 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2858 | while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2859 | amt -= wrote; |
| 2860 | offset += wrote; |
| 2861 | pBuf = &((char*)pBuf)[wrote]; |
| 2862 | } |
| 2863 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 2864 | SimulateDiskfullError(( wrote=0, amt=1 )); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 2865 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2866 | if( amt>0 ){ |
| 2867 | if( wrote<0 ){ |
| 2868 | /* lastErrno set by seekAndWrite */ |
| 2869 | return SQLITE_IOERR_WRITE; |
| 2870 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2871 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2872 | return SQLITE_FULL; |
| 2873 | } |
| 2874 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 2875 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2876 | return SQLITE_OK; |
| 2877 | } |
| 2878 | |
| 2879 | #ifdef SQLITE_TEST |
| 2880 | /* |
| 2881 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2882 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2883 | */ |
| 2884 | int sqlite3_sync_count = 0; |
| 2885 | int sqlite3_fullsync_count = 0; |
| 2886 | #endif |
| 2887 | |
| 2888 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 2889 | ** We do not trust systems to provide a working fdatasync(). Some do. |
| 2890 | ** Others do no. To be safe, we will stick with the (slower) fsync(). |
| 2891 | ** If you know that your system does support fdatasync() correctly, |
| 2892 | ** then simply compile with -Dfdatasync=fdatasync |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2893 | */ |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 2894 | #if !defined(fdatasync) && !defined(__linux__) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2895 | # define fdatasync fsync |
| 2896 | #endif |
| 2897 | |
| 2898 | /* |
| 2899 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 2900 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 2901 | ** only available on Mac OS X. But that could change. |
| 2902 | */ |
| 2903 | #ifdef F_FULLFSYNC |
| 2904 | # define HAVE_FULLFSYNC 1 |
| 2905 | #else |
| 2906 | # define HAVE_FULLFSYNC 0 |
| 2907 | #endif |
| 2908 | |
| 2909 | |
| 2910 | /* |
| 2911 | ** The fsync() system call does not work as advertised on many |
| 2912 | ** unix systems. The following procedure is an attempt to make |
| 2913 | ** it work better. |
| 2914 | ** |
| 2915 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 2916 | ** for testing when we want to run through the test suite quickly. |
| 2917 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 2918 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 2919 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2920 | ** |
| 2921 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 2922 | ** The idea behind dataOnly is that it should only write the file content |
| 2923 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 2924 | ** unchanged since the file size is part of the inode. However, |
| 2925 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 2926 | ** file size has changed. The only real difference between fdatasync() |
| 2927 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 2928 | ** inode if the mtime or owner or other inode attributes have changed. |
| 2929 | ** We only care about the file size, not the other file attributes, so |
| 2930 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 2931 | ** So, we always use fdatasync() if it is available, regardless of |
| 2932 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2933 | */ |
| 2934 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 2935 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2936 | |
| 2937 | /* The following "ifdef/elif/else/" block has the same structure as |
| 2938 | ** the one below. It is replicated here solely to avoid cluttering |
| 2939 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 2940 | */ |
| 2941 | #ifdef SQLITE_NO_SYNC |
| 2942 | UNUSED_PARAMETER(fd); |
| 2943 | UNUSED_PARAMETER(fullSync); |
| 2944 | UNUSED_PARAMETER(dataOnly); |
| 2945 | #elif HAVE_FULLFSYNC |
| 2946 | UNUSED_PARAMETER(dataOnly); |
| 2947 | #else |
| 2948 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2949 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2950 | #endif |
| 2951 | |
| 2952 | /* Record the number of times that we do a normal fsync() and |
| 2953 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 2954 | ** gets called with the correct arguments. |
| 2955 | */ |
| 2956 | #ifdef SQLITE_TEST |
| 2957 | if( fullSync ) sqlite3_fullsync_count++; |
| 2958 | sqlite3_sync_count++; |
| 2959 | #endif |
| 2960 | |
| 2961 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 2962 | ** no-op |
| 2963 | */ |
| 2964 | #ifdef SQLITE_NO_SYNC |
| 2965 | rc = SQLITE_OK; |
| 2966 | #elif HAVE_FULLFSYNC |
| 2967 | if( fullSync ){ |
| 2968 | rc = fcntl(fd, F_FULLFSYNC, 0); |
| 2969 | }else{ |
| 2970 | rc = 1; |
| 2971 | } |
| 2972 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2973 | ** It shouldn't be possible for fullfsync to fail on the local |
| 2974 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 2975 | ** isn't supported for this file system. So, attempt an fsync |
| 2976 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 2977 | ** It'd be better to detect fullfsync support once and avoid |
| 2978 | ** the fcntl call every time sync is called. |
| 2979 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2980 | if( rc ) rc = fsync(fd); |
| 2981 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2982 | #elif defined(__APPLE__) |
| 2983 | /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 2984 | ** so currently we default to the macro that redefines fdatasync to fsync |
| 2985 | */ |
| 2986 | rc = fsync(fd); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2987 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2988 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 2989 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2990 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2991 | rc = fsync(fd); |
| 2992 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2993 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2994 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 2995 | |
| 2996 | if( OS_VXWORKS && rc!= -1 ){ |
| 2997 | rc = 0; |
| 2998 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 2999 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3002 | /* |
| 3003 | ** Make sure all writes to a particular file are committed to disk. |
| 3004 | ** |
| 3005 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 3006 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 3007 | ** file data is synced. |
| 3008 | ** |
| 3009 | ** Under Unix, also make sure that the directory entry for the file |
| 3010 | ** has been created by fsync-ing the directory that contains the file. |
| 3011 | ** If we do not do this and we encounter a power failure, the directory |
| 3012 | ** entry for the journal might not exist after we reboot. The next |
| 3013 | ** SQLite to access the file will not know that the journal exists (because |
| 3014 | ** the directory entry for the journal was never created) and the transaction |
| 3015 | ** will not roll back - possibly leading to database corruption. |
| 3016 | */ |
| 3017 | static int unixSync(sqlite3_file *id, int flags){ |
| 3018 | int rc; |
| 3019 | unixFile *pFile = (unixFile*)id; |
| 3020 | |
| 3021 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 3022 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 3023 | |
| 3024 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 3025 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 3026 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 3027 | ); |
| 3028 | |
| 3029 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 3030 | ** line is to test that doing so does not cause any problems. |
| 3031 | */ |
| 3032 | SimulateDiskfullError( return SQLITE_FULL ); |
| 3033 | |
| 3034 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3035 | OSTRACE(("SYNC %-3d\n", pFile->h)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3036 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 3037 | SimulateIOError( rc=1 ); |
| 3038 | if( rc ){ |
| 3039 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3040 | return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3041 | } |
| 3042 | if( pFile->dirfd>=0 ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3043 | OSTRACE(("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd, |
| 3044 | HAVE_FULLFSYNC, isFullsync)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3045 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 3046 | /* The directory sync is only attempted if full_fsync is |
| 3047 | ** turned off or unavailable. If a full_fsync occurred above, |
| 3048 | ** then the directory sync is superfluous. |
| 3049 | */ |
| 3050 | if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){ |
| 3051 | /* |
| 3052 | ** We have received multiple reports of fsync() returning |
| 3053 | ** errors when applied to directories on certain file systems. |
| 3054 | ** A failed directory sync is not a big deal. So it seems |
| 3055 | ** better to ignore the error. Ticket #1657 |
| 3056 | */ |
| 3057 | /* pFile->lastErrno = errno; */ |
| 3058 | /* return SQLITE_IOERR; */ |
| 3059 | } |
| 3060 | #endif |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 3061 | /* Only need to sync once, so close the directory when we are done */ |
| 3062 | robust_close(pFile, pFile->dirfd, __LINE__); |
| 3063 | pFile->dirfd = -1; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3064 | } |
| 3065 | return rc; |
| 3066 | } |
| 3067 | |
| 3068 | /* |
| 3069 | ** Truncate an open file to a specified size |
| 3070 | */ |
| 3071 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3072 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3073 | int rc; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3074 | assert( pFile ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3075 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3076 | |
| 3077 | /* If the user has configured a chunk-size for this file, truncate the |
| 3078 | ** file so that it consists of an integer number of chunks (i.e. the |
| 3079 | ** actual file size after the operation may be larger than the requested |
| 3080 | ** size). |
| 3081 | */ |
| 3082 | if( pFile->szChunk ){ |
| 3083 | nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 3084 | } |
| 3085 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3086 | rc = robust_ftruncate(pFile->h, (off_t)nByte); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3087 | if( rc ){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3088 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3089 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3090 | }else{ |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3091 | #ifndef NDEBUG |
| 3092 | /* If we are doing a normal write to a database file (as opposed to |
| 3093 | ** doing a hot-journal rollback or a write to some file other than a |
| 3094 | ** normal database file) and we truncate the file to zero length, |
| 3095 | ** that effectively updates the change counter. This might happen |
| 3096 | ** when restoring a database using the backup API from a zero-length |
| 3097 | ** source. |
| 3098 | */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3099 | if( pFile->inNormalWrite && nByte==0 ){ |
| 3100 | pFile->transCntrChng = 1; |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3101 | } |
| 3102 | #endif |
| 3103 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3104 | return SQLITE_OK; |
| 3105 | } |
| 3106 | } |
| 3107 | |
| 3108 | /* |
| 3109 | ** Determine the current size of a file in bytes |
| 3110 | */ |
| 3111 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 3112 | int rc; |
| 3113 | struct stat buf; |
| 3114 | assert( id ); |
| 3115 | rc = fstat(((unixFile*)id)->h, &buf); |
| 3116 | SimulateIOError( rc=1 ); |
| 3117 | if( rc!=0 ){ |
| 3118 | ((unixFile*)id)->lastErrno = errno; |
| 3119 | return SQLITE_IOERR_FSTAT; |
| 3120 | } |
| 3121 | *pSize = buf.st_size; |
| 3122 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3123 | /* When opening a zero-size database, the findInodeInfo() procedure |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3124 | ** writes a single byte into that file in order to work around a bug |
| 3125 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 3126 | ** layers, we need to report this file size as zero even though it is |
| 3127 | ** really 1. Ticket #3260. |
| 3128 | */ |
| 3129 | if( *pSize==1 ) *pSize = 0; |
| 3130 | |
| 3131 | |
| 3132 | return SQLITE_OK; |
| 3133 | } |
| 3134 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3135 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3136 | /* |
| 3137 | ** Handler for proxy-locking file-control verbs. Defined below in the |
| 3138 | ** proxying locking division. |
| 3139 | */ |
| 3140 | static int proxyFileControl(sqlite3_file*,int,void*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3141 | #endif |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3142 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3143 | /* |
| 3144 | ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT |
| 3145 | ** file-control operation. |
| 3146 | ** |
| 3147 | ** If the user has configured a chunk-size for this file, it could be |
| 3148 | ** that the file needs to be extended at this point. Otherwise, the |
| 3149 | ** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix. |
| 3150 | */ |
| 3151 | static int fcntlSizeHint(unixFile *pFile, i64 nByte){ |
| 3152 | if( pFile->szChunk ){ |
| 3153 | i64 nSize; /* Required file size */ |
| 3154 | struct stat buf; /* Used to hold return values of fstat() */ |
| 3155 | |
| 3156 | if( fstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT; |
| 3157 | |
| 3158 | nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; |
| 3159 | if( nSize>(i64)buf.st_size ){ |
| 3160 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3161 | int rc; |
| 3162 | do{ |
| 3163 | rc = posix_fallocate(pFile-.h, buf.st_size, nSize-buf.st_size; |
| 3164 | }while( rc<0 && errno=EINTR ); |
| 3165 | if( rc ) return SQLITE_IOERR_WRITE; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3166 | #else |
| 3167 | /* If the OS does not have posix_fallocate(), fake it. First use |
| 3168 | ** ftruncate() to set the file size, then write a single byte to |
| 3169 | ** the last byte in each block within the extended region. This |
| 3170 | ** is the same technique used by glibc to implement posix_fallocate() |
| 3171 | ** on systems that do not have a real fallocate() system call. |
| 3172 | */ |
| 3173 | int nBlk = buf.st_blksize; /* File-system block size */ |
| 3174 | i64 iWrite; /* Next offset to write to */ |
| 3175 | int nWrite; /* Return value from seekAndWrite() */ |
| 3176 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3177 | if( robust_ftruncate(pFile->h, nSize) ){ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3178 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3179 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3180 | } |
| 3181 | iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1; |
| 3182 | do { |
| 3183 | nWrite = seekAndWrite(pFile, iWrite, "", 1); |
| 3184 | iWrite += nBlk; |
| 3185 | } while( nWrite==1 && iWrite<nSize ); |
| 3186 | if( nWrite!=1 ) return SQLITE_IOERR_WRITE; |
| 3187 | #endif |
| 3188 | } |
| 3189 | } |
| 3190 | |
| 3191 | return SQLITE_OK; |
| 3192 | } |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3193 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3194 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3195 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3196 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3197 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3198 | switch( op ){ |
| 3199 | case SQLITE_FCNTL_LOCKSTATE: { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3200 | *(int*)pArg = ((unixFile*)id)->eFileLock; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3201 | return SQLITE_OK; |
| 3202 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3203 | case SQLITE_LAST_ERRNO: { |
| 3204 | *(int*)pArg = ((unixFile*)id)->lastErrno; |
| 3205 | return SQLITE_OK; |
| 3206 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3207 | case SQLITE_FCNTL_CHUNK_SIZE: { |
| 3208 | ((unixFile*)id)->szChunk = *(int *)pArg; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3209 | return SQLITE_OK; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3210 | } |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3211 | case SQLITE_FCNTL_SIZE_HINT: { |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3212 | return fcntlSizeHint((unixFile *)id, *(i64 *)pArg); |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3213 | } |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3214 | #ifndef NDEBUG |
| 3215 | /* The pager calls this method to signal that it has done |
| 3216 | ** a rollback and that the database is therefore unchanged and |
| 3217 | ** it hence it is OK for the transaction change counter to be |
| 3218 | ** unchanged. |
| 3219 | */ |
| 3220 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 3221 | ((unixFile*)id)->dbUpdate = 0; |
| 3222 | return SQLITE_OK; |
| 3223 | } |
| 3224 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3225 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3226 | case SQLITE_SET_LOCKPROXYFILE: |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3227 | case SQLITE_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3228 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3229 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3230 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 3231 | case SQLITE_FCNTL_SYNC_OMITTED: { |
| 3232 | return SQLITE_OK; /* A no-op */ |
| 3233 | } |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3234 | } |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 3235 | return SQLITE_NOTFOUND; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | /* |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3239 | ** Return the sector size in bytes of the underlying block device for |
| 3240 | ** the specified file. This is almost always 512 bytes, but may be |
| 3241 | ** larger for some devices. |
| 3242 | ** |
| 3243 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 3244 | ** if two files are created in the same file-system directory (i.e. |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3245 | ** a database and its journal file) that the sector size will be the |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3246 | ** same for both. |
| 3247 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3248 | static int unixSectorSize(sqlite3_file *NotUsed){ |
| 3249 | UNUSED_PARAMETER(NotUsed); |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 3250 | return SQLITE_DEFAULT_SECTOR_SIZE; |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3253 | /* |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3254 | ** Return the device characteristics for the file. This is always 0 for unix. |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3255 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3256 | static int unixDeviceCharacteristics(sqlite3_file *NotUsed){ |
| 3257 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3258 | return 0; |
| 3259 | } |
| 3260 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3261 | #ifndef SQLITE_OMIT_WAL |
| 3262 | |
| 3263 | |
| 3264 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3265 | ** Object used to represent an shared memory buffer. |
| 3266 | ** |
| 3267 | ** When multiple threads all reference the same wal-index, each thread |
| 3268 | ** has its own unixShm object, but they all point to a single instance |
| 3269 | ** of this unixShmNode object. In other words, each wal-index is opened |
| 3270 | ** only once per process. |
| 3271 | ** |
| 3272 | ** Each unixShmNode object is connected to a single unixInodeInfo object. |
| 3273 | ** We could coalesce this object into unixInodeInfo, but that would mean |
| 3274 | ** every open file that does not use shared memory (in other words, most |
| 3275 | ** open files) would have to carry around this extra information. So |
| 3276 | ** the unixInodeInfo object contains a pointer to this unixShmNode object |
| 3277 | ** and the unixShmNode object is created only when needed. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3278 | ** |
| 3279 | ** unixMutexHeld() must be true when creating or destroying |
| 3280 | ** this object or while reading or writing the following fields: |
| 3281 | ** |
| 3282 | ** nRef |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3283 | ** |
| 3284 | ** The following fields are read-only after the object is created: |
| 3285 | ** |
| 3286 | ** fid |
| 3287 | ** zFilename |
| 3288 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3289 | ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3290 | ** unixMutexHeld() is true when reading or writing any other field |
| 3291 | ** in this structure. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3292 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3293 | struct unixShmNode { |
| 3294 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3295 | sqlite3_mutex *mutex; /* Mutex to access this object */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3296 | char *zFilename; /* Name of the mmapped file */ |
| 3297 | int h; /* Open file descriptor */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3298 | int szRegion; /* Size of shared-memory regions */ |
| 3299 | int nRegion; /* Size of array apRegion */ |
| 3300 | char **apRegion; /* Array of mapped shared-memory regions */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3301 | int nRef; /* Number of unixShm objects pointing to this */ |
| 3302 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3303 | #ifdef SQLITE_DEBUG |
| 3304 | u8 exclMask; /* Mask of exclusive locks held */ |
| 3305 | u8 sharedMask; /* Mask of shared locks held */ |
| 3306 | u8 nextShmId; /* Next available unixShm.id value */ |
| 3307 | #endif |
| 3308 | }; |
| 3309 | |
| 3310 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3311 | ** Structure used internally by this VFS to record the state of an |
| 3312 | ** open shared memory connection. |
| 3313 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3314 | ** The following fields are initialized when this object is created and |
| 3315 | ** are read-only thereafter: |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3316 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3317 | ** unixShm.pFile |
| 3318 | ** unixShm.id |
| 3319 | ** |
| 3320 | ** All other fields are read/write. The unixShm.pFile->mutex must be held |
| 3321 | ** while accessing any read/write fields. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3322 | */ |
| 3323 | struct unixShm { |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3324 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 3325 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3326 | u8 hasMutex; /* True if holding the unixShmNode mutex */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3327 | u16 sharedMask; /* Mask of shared locks held */ |
| 3328 | u16 exclMask; /* Mask of exclusive locks held */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3329 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3330 | u8 id; /* Id of this connection within its unixShmNode */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3331 | #endif |
| 3332 | }; |
| 3333 | |
| 3334 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3335 | ** Constants used for locking |
| 3336 | */ |
drh | bd9676c | 2010-06-23 17:58:38 +0000 | [diff] [blame] | 3337 | #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
drh | 4222441 | 2010-05-31 14:28:25 +0000 | [diff] [blame] | 3338 | #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3339 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3340 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3341 | ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3342 | ** |
| 3343 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 3344 | ** otherwise. |
| 3345 | */ |
| 3346 | static int unixShmSystemLock( |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3347 | unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */ |
| 3348 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3349 | int ofst, /* First byte of the locking range */ |
| 3350 | int n /* Number of bytes to lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3351 | ){ |
| 3352 | struct flock f; /* The posix advisory locking structure */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3353 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3354 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3355 | /* Access to the unixShmNode object is serialized by the caller */ |
| 3356 | assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3357 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3358 | /* Shared locks never span more than one byte */ |
| 3359 | assert( n==1 || lockType!=F_RDLCK ); |
| 3360 | |
| 3361 | /* Locks are within range */ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3362 | assert( n>=1 && n<SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3363 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3364 | /* Initialize the locking parameters */ |
| 3365 | memset(&f, 0, sizeof(f)); |
| 3366 | f.l_type = lockType; |
| 3367 | f.l_whence = SEEK_SET; |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3368 | f.l_start = ofst; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3369 | f.l_len = n; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3370 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3371 | rc = fcntl(pShmNode->h, F_SETLK, &f); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3372 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 3373 | |
| 3374 | /* Update the global lock state and do debug tracing */ |
| 3375 | #ifdef SQLITE_DEBUG |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3376 | { u16 mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3377 | OSTRACE(("SHM-LOCK ")); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3378 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3379 | if( rc==SQLITE_OK ){ |
| 3380 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3381 | OSTRACE(("unlock %d ok", ofst)); |
| 3382 | pShmNode->exclMask &= ~mask; |
| 3383 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3384 | }else if( lockType==F_RDLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3385 | OSTRACE(("read-lock %d ok", ofst)); |
| 3386 | pShmNode->exclMask &= ~mask; |
| 3387 | pShmNode->sharedMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3388 | }else{ |
| 3389 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3390 | OSTRACE(("write-lock %d ok", ofst)); |
| 3391 | pShmNode->exclMask |= mask; |
| 3392 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3393 | } |
| 3394 | }else{ |
| 3395 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3396 | OSTRACE(("unlock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3397 | }else if( lockType==F_RDLCK ){ |
| 3398 | OSTRACE(("read-lock failed")); |
| 3399 | }else{ |
| 3400 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3401 | OSTRACE(("write-lock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3402 | } |
| 3403 | } |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 3404 | OSTRACE((" - afterwards %03x,%03x\n", |
| 3405 | pShmNode->sharedMask, pShmNode->exclMask)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3406 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3407 | #endif |
| 3408 | |
| 3409 | return rc; |
| 3410 | } |
| 3411 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3412 | |
| 3413 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3414 | ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3415 | ** |
| 3416 | ** This is not a VFS shared-memory method; it is a utility function called |
| 3417 | ** by VFS shared-memory methods. |
| 3418 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3419 | static void unixShmPurge(unixFile *pFd){ |
| 3420 | unixShmNode *p = pFd->pInode->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3421 | assert( unixMutexHeld() ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3422 | if( p && p->nRef==0 ){ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3423 | int i; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3424 | assert( p->pInode==pFd->pInode ); |
| 3425 | if( p->mutex ) sqlite3_mutex_free(p->mutex); |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3426 | for(i=0; i<p->nRegion; i++){ |
| 3427 | munmap(p->apRegion[i], p->szRegion); |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3428 | } |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3429 | sqlite3_free(p->apRegion); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 3430 | if( p->h>=0 ){ |
| 3431 | robust_close(pFd, p->h, __LINE__); |
| 3432 | p->h = -1; |
| 3433 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3434 | p->pInode->pShmNode = 0; |
| 3435 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3436 | } |
| 3437 | } |
| 3438 | |
| 3439 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3440 | ** Open a shared-memory area associated with open database file pDbFd. |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3441 | ** This particular implementation uses mmapped files. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3442 | ** |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3443 | ** The file used to implement shared-memory is in the same directory |
| 3444 | ** as the open database file and has the same name as the open database |
| 3445 | ** file with the "-shm" suffix added. For example, if the database file |
| 3446 | ** is "/home/user1/config.db" then the file that is created and mmapped |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3447 | ** for shared memory will be called "/home/user1/config.db-shm". |
| 3448 | ** |
| 3449 | ** Another approach to is to use files in /dev/shm or /dev/tmp or an |
| 3450 | ** some other tmpfs mount. But if a file in a different directory |
| 3451 | ** from the database file is used, then differing access permissions |
| 3452 | ** or a chroot() might cause two different processes on the same |
| 3453 | ** database to end up using different files for shared memory - |
| 3454 | ** meaning that their memory would not really be shared - resulting |
| 3455 | ** in database corruption. Nevertheless, this tmpfs file usage |
| 3456 | ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" |
| 3457 | ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time |
| 3458 | ** option results in an incompatible build of SQLite; builds of SQLite |
| 3459 | ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the |
| 3460 | ** same database file at the same time, database corruption will likely |
| 3461 | ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered |
| 3462 | ** "unsupported" and may go away in a future SQLite release. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3463 | ** |
| 3464 | ** When opening a new shared-memory file, if no other instances of that |
| 3465 | ** file are currently open, in this process or in other processes, then |
| 3466 | ** the file must be truncated to zero length or have its header cleared. |
| 3467 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3468 | static int unixOpenSharedMemory(unixFile *pDbFd){ |
| 3469 | struct unixShm *p = 0; /* The connection to be opened */ |
| 3470 | struct unixShmNode *pShmNode; /* The underlying mmapped file */ |
| 3471 | int rc; /* Result code */ |
| 3472 | unixInodeInfo *pInode; /* The inode of fd */ |
| 3473 | char *zShmFilename; /* Name of the file used for SHM */ |
| 3474 | int nShmFilename; /* Size of the SHM filename in bytes */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3475 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3476 | /* Allocate space for the new unixShm object. */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3477 | p = sqlite3_malloc( sizeof(*p) ); |
| 3478 | if( p==0 ) return SQLITE_NOMEM; |
| 3479 | memset(p, 0, sizeof(*p)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3480 | assert( pDbFd->pShm==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3481 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3482 | /* Check to see if a unixShmNode object already exists. Reuse an existing |
| 3483 | ** one if present. Create a new one if necessary. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3484 | */ |
| 3485 | unixEnterMutex(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 3486 | pInode = pDbFd->pInode; |
| 3487 | pShmNode = pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3488 | if( pShmNode==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 3489 | struct stat sStat; /* fstat() info for database file */ |
| 3490 | |
| 3491 | /* Call fstat() to figure out the permissions on the database file. If |
| 3492 | ** a new *-shm file is created, an attempt will be made to create it |
| 3493 | ** with the same permissions. The actual permissions the file is created |
| 3494 | ** with are subject to the current umask setting. |
| 3495 | */ |
| 3496 | if( fstat(pDbFd->h, &sStat) ){ |
| 3497 | rc = SQLITE_IOERR_FSTAT; |
| 3498 | goto shm_open_err; |
| 3499 | } |
| 3500 | |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3501 | #ifdef SQLITE_SHM_DIRECTORY |
| 3502 | nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 30; |
| 3503 | #else |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3504 | nShmFilename = 5 + (int)strlen(pDbFd->zPath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3505 | #endif |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3506 | pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3507 | if( pShmNode==0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3508 | rc = SQLITE_NOMEM; |
| 3509 | goto shm_open_err; |
| 3510 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3511 | memset(pShmNode, 0, sizeof(*pShmNode)); |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3512 | zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1]; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3513 | #ifdef SQLITE_SHM_DIRECTORY |
| 3514 | sqlite3_snprintf(nShmFilename, zShmFilename, |
| 3515 | SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", |
| 3516 | (u32)sStat.st_ino, (u32)sStat.st_dev); |
| 3517 | #else |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3518 | sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3519 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3520 | pShmNode->h = -1; |
| 3521 | pDbFd->pInode->pShmNode = pShmNode; |
| 3522 | pShmNode->pInode = pDbFd->pInode; |
| 3523 | pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 3524 | if( pShmNode->mutex==0 ){ |
| 3525 | rc = SQLITE_NOMEM; |
| 3526 | goto shm_open_err; |
| 3527 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3528 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 3529 | pShmNode->h = open(zShmFilename, O_RDWR|O_CREAT, (sStat.st_mode & 0777)); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3530 | if( pShmNode->h<0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3531 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3532 | goto shm_open_err; |
| 3533 | } |
| 3534 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3535 | /* Check to see if another process is holding the dead-man switch. |
| 3536 | ** If not, truncate the file to zero length. |
| 3537 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3538 | rc = SQLITE_OK; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3539 | if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3540 | if( robust_ftruncate(pShmNode->h, 0) ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3541 | rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3542 | } |
| 3543 | } |
| 3544 | if( rc==SQLITE_OK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3545 | rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3546 | } |
| 3547 | if( rc ) goto shm_open_err; |
| 3548 | } |
| 3549 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3550 | /* Make the new connection a child of the unixShmNode */ |
| 3551 | p->pShmNode = pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3552 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3553 | p->id = pShmNode->nextShmId++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3554 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3555 | pShmNode->nRef++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3556 | pDbFd->pShm = p; |
| 3557 | unixLeaveMutex(); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 3558 | |
| 3559 | /* The reference count on pShmNode has already been incremented under |
| 3560 | ** the cover of the unixEnterMutex() mutex and the pointer from the |
| 3561 | ** new (struct unixShm) object to the pShmNode has been set. All that is |
| 3562 | ** left to do is to link the new object into the linked list starting |
| 3563 | ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex |
| 3564 | ** mutex. |
| 3565 | */ |
| 3566 | sqlite3_mutex_enter(pShmNode->mutex); |
| 3567 | p->pNext = pShmNode->pFirst; |
| 3568 | pShmNode->pFirst = p; |
| 3569 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3570 | return SQLITE_OK; |
| 3571 | |
| 3572 | /* Jump here on any error */ |
| 3573 | shm_open_err: |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3574 | unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3575 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3576 | unixLeaveMutex(); |
| 3577 | return rc; |
| 3578 | } |
| 3579 | |
| 3580 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3581 | ** This function is called to obtain a pointer to region iRegion of the |
| 3582 | ** shared-memory associated with the database file fd. Shared-memory regions |
| 3583 | ** are numbered starting from zero. Each shared-memory region is szRegion |
| 3584 | ** bytes in size. |
| 3585 | ** |
| 3586 | ** If an error occurs, an error code is returned and *pp is set to NULL. |
| 3587 | ** |
| 3588 | ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory |
| 3589 | ** region has not been allocated (by any client, including one running in a |
| 3590 | ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 3591 | ** bExtend is non-zero and the requested shared-memory region has not yet |
| 3592 | ** been allocated, it is allocated by this function. |
| 3593 | ** |
| 3594 | ** If the shared-memory region has already been allocated or is allocated by |
| 3595 | ** this call as described above, then it is mapped into this processes |
| 3596 | ** address space (if it is not already), *pp is set to point to the mapped |
| 3597 | ** memory and SQLITE_OK returned. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3598 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3599 | static int unixShmMap( |
| 3600 | sqlite3_file *fd, /* Handle open on database file */ |
| 3601 | int iRegion, /* Region to retrieve */ |
| 3602 | int szRegion, /* Size of regions */ |
| 3603 | int bExtend, /* True to extend file if necessary */ |
| 3604 | void volatile **pp /* OUT: Mapped memory */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3605 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3606 | unixFile *pDbFd = (unixFile*)fd; |
| 3607 | unixShm *p; |
| 3608 | unixShmNode *pShmNode; |
| 3609 | int rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3610 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3611 | /* If the shared-memory file has not yet been opened, open it now. */ |
| 3612 | if( pDbFd->pShm==0 ){ |
| 3613 | rc = unixOpenSharedMemory(pDbFd); |
| 3614 | if( rc!=SQLITE_OK ) return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3615 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3616 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3617 | p = pDbFd->pShm; |
| 3618 | pShmNode = p->pShmNode; |
| 3619 | sqlite3_mutex_enter(pShmNode->mutex); |
| 3620 | assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
| 3621 | |
| 3622 | if( pShmNode->nRegion<=iRegion ){ |
| 3623 | char **apNew; /* New apRegion[] array */ |
| 3624 | int nByte = (iRegion+1)*szRegion; /* Minimum required file size */ |
| 3625 | struct stat sStat; /* Used by fstat() */ |
| 3626 | |
| 3627 | pShmNode->szRegion = szRegion; |
| 3628 | |
| 3629 | /* The requested region is not mapped into this processes address space. |
| 3630 | ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 3631 | ** large enough to contain the requested region). |
| 3632 | */ |
| 3633 | if( fstat(pShmNode->h, &sStat) ){ |
| 3634 | rc = SQLITE_IOERR_SHMSIZE; |
| 3635 | goto shmpage_out; |
| 3636 | } |
| 3637 | |
| 3638 | if( sStat.st_size<nByte ){ |
| 3639 | /* The requested memory region does not exist. If bExtend is set to |
| 3640 | ** false, exit early. *pp will be set to NULL and SQLITE_OK returned. |
| 3641 | ** |
| 3642 | ** Alternatively, if bExtend is true, use ftruncate() to allocate |
| 3643 | ** the requested memory region. |
| 3644 | */ |
| 3645 | if( !bExtend ) goto shmpage_out; |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3646 | if( robust_ftruncate(pShmNode->h, nByte) ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3647 | rc = unixLogError(SQLITE_IOERR_SHMSIZE,"ftruncate",pShmNode->zFilename); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3648 | goto shmpage_out; |
| 3649 | } |
| 3650 | } |
| 3651 | |
| 3652 | /* Map the requested memory region into this processes address space. */ |
| 3653 | apNew = (char **)sqlite3_realloc( |
| 3654 | pShmNode->apRegion, (iRegion+1)*sizeof(char *) |
| 3655 | ); |
| 3656 | if( !apNew ){ |
| 3657 | rc = SQLITE_IOERR_NOMEM; |
| 3658 | goto shmpage_out; |
| 3659 | } |
| 3660 | pShmNode->apRegion = apNew; |
| 3661 | while(pShmNode->nRegion<=iRegion){ |
| 3662 | void *pMem = mmap(0, szRegion, PROT_READ|PROT_WRITE, |
drh | 37e8b5b | 2010-09-02 14:00:19 +0000 | [diff] [blame] | 3663 | MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3664 | ); |
| 3665 | if( pMem==MAP_FAILED ){ |
| 3666 | rc = SQLITE_IOERR; |
| 3667 | goto shmpage_out; |
| 3668 | } |
| 3669 | pShmNode->apRegion[pShmNode->nRegion] = pMem; |
| 3670 | pShmNode->nRegion++; |
| 3671 | } |
| 3672 | } |
| 3673 | |
| 3674 | shmpage_out: |
| 3675 | if( pShmNode->nRegion>iRegion ){ |
| 3676 | *pp = pShmNode->apRegion[iRegion]; |
| 3677 | }else{ |
| 3678 | *pp = 0; |
| 3679 | } |
| 3680 | sqlite3_mutex_leave(pShmNode->mutex); |
| 3681 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3682 | } |
| 3683 | |
| 3684 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3685 | ** Change the lock state for a shared-memory segment. |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 3686 | ** |
| 3687 | ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little |
| 3688 | ** different here than in posix. In xShmLock(), one can go from unlocked |
| 3689 | ** to shared and back or from unlocked to exclusive and back. But one may |
| 3690 | ** not go from shared to exclusive or from exclusive to shared. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3691 | */ |
| 3692 | static int unixShmLock( |
| 3693 | sqlite3_file *fd, /* Database file holding the shared memory */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3694 | int ofst, /* First lock to acquire or release */ |
| 3695 | int n, /* Number of locks to acquire or release */ |
| 3696 | int flags /* What to do with the lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3697 | ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3698 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 3699 | unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 3700 | unixShm *pX; /* For looping over all siblings */ |
| 3701 | unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 3702 | int rc = SQLITE_OK; /* Result code */ |
| 3703 | u16 mask; /* Mask of locks to take or release */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3704 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3705 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 3706 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3707 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3708 | assert( n>=1 ); |
| 3709 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 3710 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 3711 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 3712 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 3713 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3714 | |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3715 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3716 | assert( n>1 || mask==(1<<ofst) ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3717 | sqlite3_mutex_enter(pShmNode->mutex); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3718 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 3719 | u16 allMask = 0; /* Mask of locks held by siblings */ |
| 3720 | |
| 3721 | /* See if any siblings hold this same lock */ |
| 3722 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 3723 | if( pX==p ) continue; |
| 3724 | assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
| 3725 | allMask |= pX->sharedMask; |
| 3726 | } |
| 3727 | |
| 3728 | /* Unlock the system-level locks */ |
| 3729 | if( (mask & allMask)==0 ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3730 | rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3731 | }else{ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3732 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3733 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3734 | |
| 3735 | /* Undo the local locks */ |
| 3736 | if( rc==SQLITE_OK ){ |
| 3737 | p->exclMask &= ~mask; |
| 3738 | p->sharedMask &= ~mask; |
| 3739 | } |
| 3740 | }else if( flags & SQLITE_SHM_SHARED ){ |
| 3741 | u16 allShared = 0; /* Union of locks held by connections other than "p" */ |
| 3742 | |
| 3743 | /* Find out which shared locks are already held by sibling connections. |
| 3744 | ** If any sibling already holds an exclusive lock, go ahead and return |
| 3745 | ** SQLITE_BUSY. |
| 3746 | */ |
| 3747 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3748 | if( (pX->exclMask & mask)!=0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3749 | rc = SQLITE_BUSY; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3750 | break; |
| 3751 | } |
| 3752 | allShared |= pX->sharedMask; |
| 3753 | } |
| 3754 | |
| 3755 | /* Get shared locks at the system level, if necessary */ |
| 3756 | if( rc==SQLITE_OK ){ |
| 3757 | if( (allShared & mask)==0 ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3758 | rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3759 | }else{ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3760 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3761 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3762 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3763 | |
| 3764 | /* Get the local shared locks */ |
| 3765 | if( rc==SQLITE_OK ){ |
| 3766 | p->sharedMask |= mask; |
| 3767 | } |
| 3768 | }else{ |
| 3769 | /* Make sure no sibling connections hold locks that will block this |
| 3770 | ** lock. If any do, return SQLITE_BUSY right away. |
| 3771 | */ |
| 3772 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3773 | if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ |
| 3774 | rc = SQLITE_BUSY; |
| 3775 | break; |
| 3776 | } |
| 3777 | } |
| 3778 | |
| 3779 | /* Get the exclusive locks at the system level. Then if successful |
| 3780 | ** also mark the local connection as being locked. |
| 3781 | */ |
| 3782 | if( rc==SQLITE_OK ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3783 | rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3784 | if( rc==SQLITE_OK ){ |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 3785 | assert( (p->sharedMask & mask)==0 ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3786 | p->exclMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3787 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3788 | } |
| 3789 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3790 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 3791 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
| 3792 | p->id, getpid(), p->sharedMask, p->exclMask)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3793 | return rc; |
| 3794 | } |
| 3795 | |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3796 | /* |
| 3797 | ** Implement a memory barrier or memory fence on shared memory. |
| 3798 | ** |
| 3799 | ** All loads and stores begun before the barrier must complete before |
| 3800 | ** any load or store begun after the barrier. |
| 3801 | */ |
| 3802 | static void unixShmBarrier( |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3803 | sqlite3_file *fd /* Database file holding the shared memory */ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3804 | ){ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 3805 | UNUSED_PARAMETER(fd); |
drh | b29ad85 | 2010-06-01 00:03:57 +0000 | [diff] [blame] | 3806 | unixEnterMutex(); |
| 3807 | unixLeaveMutex(); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3808 | } |
| 3809 | |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3810 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3811 | ** Close a connection to shared-memory. Delete the underlying |
| 3812 | ** storage if deleteFlag is true. |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 3813 | ** |
| 3814 | ** If there is no shared memory associated with the connection then this |
| 3815 | ** routine is a harmless no-op. |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3816 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3817 | static int unixShmUnmap( |
| 3818 | sqlite3_file *fd, /* The underlying database file */ |
| 3819 | int deleteFlag /* Delete shared-memory if true */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3820 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3821 | unixShm *p; /* The connection to be closed */ |
| 3822 | unixShmNode *pShmNode; /* The underlying shared-memory file */ |
| 3823 | unixShm **pp; /* For looping over sibling connections */ |
| 3824 | unixFile *pDbFd; /* The underlying database file */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3825 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3826 | pDbFd = (unixFile*)fd; |
| 3827 | p = pDbFd->pShm; |
| 3828 | if( p==0 ) return SQLITE_OK; |
| 3829 | pShmNode = p->pShmNode; |
| 3830 | |
| 3831 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 3832 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 3833 | |
| 3834 | /* Remove connection p from the set of connections associated |
| 3835 | ** with pShmNode */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3836 | sqlite3_mutex_enter(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3837 | for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 3838 | *pp = p->pNext; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3839 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3840 | /* Free the connection p */ |
| 3841 | sqlite3_free(p); |
| 3842 | pDbFd->pShm = 0; |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3843 | sqlite3_mutex_leave(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3844 | |
| 3845 | /* If pShmNode->nRef has reached 0, then close the underlying |
| 3846 | ** shared-memory file, too */ |
| 3847 | unixEnterMutex(); |
| 3848 | assert( pShmNode->nRef>0 ); |
| 3849 | pShmNode->nRef--; |
| 3850 | if( pShmNode->nRef==0 ){ |
| 3851 | if( deleteFlag ) unlink(pShmNode->zFilename); |
| 3852 | unixShmPurge(pDbFd); |
| 3853 | } |
| 3854 | unixLeaveMutex(); |
| 3855 | |
| 3856 | return SQLITE_OK; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3857 | } |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3858 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3859 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3860 | #else |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 3861 | # define unixShmMap 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3862 | # define unixShmLock 0 |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3863 | # define unixShmBarrier 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3864 | # define unixShmUnmap 0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3865 | #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 3866 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3867 | /* |
| 3868 | ** Here ends the implementation of all sqlite3_file methods. |
| 3869 | ** |
| 3870 | ********************** End sqlite3_file Methods ******************************* |
| 3871 | ******************************************************************************/ |
| 3872 | |
| 3873 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3874 | ** This division contains definitions of sqlite3_io_methods objects that |
| 3875 | ** implement various file locking strategies. It also contains definitions |
| 3876 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 3877 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 3878 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 3879 | ** the correct finder-function for that VFS. |
| 3880 | ** |
| 3881 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 3882 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 3883 | ** looks at the filesystem type and tries to guess the best locking |
| 3884 | ** strategy from that. |
| 3885 | ** |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3886 | ** For finder-funtion F, two objects are created: |
| 3887 | ** |
| 3888 | ** (1) The real finder-function named "FImpt()". |
| 3889 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3890 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3891 | ** |
| 3892 | ** |
| 3893 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 3894 | ** objects. We have to do this instead of letting pAppData point |
| 3895 | ** directly at the finder-function since C90 rules prevent a void* |
| 3896 | ** from be cast into a function pointer. |
| 3897 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3898 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3899 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3900 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3901 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 3902 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 3903 | ** |
| 3904 | ** * An I/O method finder function called FINDER that returns a pointer |
| 3905 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3906 | */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3907 | #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3908 | static const sqlite3_io_methods METHOD = { \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3909 | VERSION, /* iVersion */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3910 | CLOSE, /* xClose */ \ |
| 3911 | unixRead, /* xRead */ \ |
| 3912 | unixWrite, /* xWrite */ \ |
| 3913 | unixTruncate, /* xTruncate */ \ |
| 3914 | unixSync, /* xSync */ \ |
| 3915 | unixFileSize, /* xFileSize */ \ |
| 3916 | LOCK, /* xLock */ \ |
| 3917 | UNLOCK, /* xUnlock */ \ |
| 3918 | CKLOCK, /* xCheckReservedLock */ \ |
| 3919 | unixFileControl, /* xFileControl */ \ |
| 3920 | unixSectorSize, /* xSectorSize */ \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3921 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 3922 | unixShmMap, /* xShmMap */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3923 | unixShmLock, /* xShmLock */ \ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3924 | unixShmBarrier, /* xShmBarrier */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3925 | unixShmUnmap /* xShmUnmap */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3926 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3927 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 3928 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3929 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3930 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3931 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3932 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3933 | |
| 3934 | /* |
| 3935 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 3936 | ** locking strategies. Functions that return pointers to these methods |
| 3937 | ** are also created. |
| 3938 | */ |
| 3939 | IOMETHODS( |
| 3940 | posixIoFinder, /* Finder function name */ |
| 3941 | posixIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3942 | 2, /* shared memory is enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3943 | unixClose, /* xClose method */ |
| 3944 | unixLock, /* xLock method */ |
| 3945 | unixUnlock, /* xUnlock method */ |
| 3946 | unixCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3947 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3948 | IOMETHODS( |
| 3949 | nolockIoFinder, /* Finder function name */ |
| 3950 | nolockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3951 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3952 | nolockClose, /* xClose method */ |
| 3953 | nolockLock, /* xLock method */ |
| 3954 | nolockUnlock, /* xUnlock method */ |
| 3955 | nolockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3956 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3957 | IOMETHODS( |
| 3958 | dotlockIoFinder, /* Finder function name */ |
| 3959 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3960 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3961 | dotlockClose, /* xClose method */ |
| 3962 | dotlockLock, /* xLock method */ |
| 3963 | dotlockUnlock, /* xUnlock method */ |
| 3964 | dotlockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3965 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3966 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3967 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3968 | IOMETHODS( |
| 3969 | flockIoFinder, /* Finder function name */ |
| 3970 | flockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3971 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3972 | flockClose, /* xClose method */ |
| 3973 | flockLock, /* xLock method */ |
| 3974 | flockUnlock, /* xUnlock method */ |
| 3975 | flockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3976 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3977 | #endif |
| 3978 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3979 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3980 | IOMETHODS( |
| 3981 | semIoFinder, /* Finder function name */ |
| 3982 | semIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3983 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3984 | semClose, /* xClose method */ |
| 3985 | semLock, /* xLock method */ |
| 3986 | semUnlock, /* xUnlock method */ |
| 3987 | semCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3988 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3989 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3990 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3991 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3992 | IOMETHODS( |
| 3993 | afpIoFinder, /* Finder function name */ |
| 3994 | afpIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3995 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3996 | afpClose, /* xClose method */ |
| 3997 | afpLock, /* xLock method */ |
| 3998 | afpUnlock, /* xUnlock method */ |
| 3999 | afpCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4000 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4001 | #endif |
| 4002 | |
| 4003 | /* |
| 4004 | ** The proxy locking method is a "super-method" in the sense that it |
| 4005 | ** opens secondary file descriptors for the conch and lock files and |
| 4006 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 4007 | ** secondary files. For this reason, the division that implements |
| 4008 | ** proxy locking is located much further down in the file. But we need |
| 4009 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 4010 | ** for proxy locking here. So we forward declare the I/O methods. |
| 4011 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4012 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4013 | static int proxyClose(sqlite3_file*); |
| 4014 | static int proxyLock(sqlite3_file*, int); |
| 4015 | static int proxyUnlock(sqlite3_file*, int); |
| 4016 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4017 | IOMETHODS( |
| 4018 | proxyIoFinder, /* Finder function name */ |
| 4019 | proxyIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4020 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4021 | proxyClose, /* xClose method */ |
| 4022 | proxyLock, /* xLock method */ |
| 4023 | proxyUnlock, /* xUnlock method */ |
| 4024 | proxyCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4025 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4026 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4027 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4028 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 4029 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 4030 | IOMETHODS( |
| 4031 | nfsIoFinder, /* Finder function name */ |
| 4032 | nfsIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4033 | 1, /* shared memory is disabled */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4034 | unixClose, /* xClose method */ |
| 4035 | unixLock, /* xLock method */ |
| 4036 | nfsUnlock, /* xUnlock method */ |
| 4037 | unixCheckReservedLock /* xCheckReservedLock method */ |
| 4038 | ) |
| 4039 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4040 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4041 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4042 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4043 | ** This "finder" function attempts to determine the best locking strategy |
| 4044 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4045 | ** object that implements that strategy. |
| 4046 | ** |
| 4047 | ** This is for MacOSX only. |
| 4048 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4049 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4050 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4051 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4052 | ){ |
| 4053 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4054 | const char *zFilesystem; /* Filesystem type name */ |
| 4055 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4056 | } aMap[] = { |
| 4057 | { "hfs", &posixIoMethods }, |
| 4058 | { "ufs", &posixIoMethods }, |
| 4059 | { "afpfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4060 | { "smbfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4061 | { "webdav", &nolockIoMethods }, |
| 4062 | { 0, 0 } |
| 4063 | }; |
| 4064 | int i; |
| 4065 | struct statfs fsInfo; |
| 4066 | struct flock lockInfo; |
| 4067 | |
| 4068 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4069 | /* If filePath==NULL that means we are dealing with a transient file |
| 4070 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4071 | return &nolockIoMethods; |
| 4072 | } |
| 4073 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 4074 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 4075 | return &nolockIoMethods; |
| 4076 | } |
| 4077 | for(i=0; aMap[i].zFilesystem; i++){ |
| 4078 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 4079 | return aMap[i].pMethods; |
| 4080 | } |
| 4081 | } |
| 4082 | } |
| 4083 | |
| 4084 | /* Default case. Handles, amongst others, "nfs". |
| 4085 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 4086 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4087 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4088 | lockInfo.l_len = 1; |
| 4089 | lockInfo.l_start = 0; |
| 4090 | lockInfo.l_whence = SEEK_SET; |
| 4091 | lockInfo.l_type = F_RDLCK; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4092 | if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4093 | if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 4094 | return &nfsIoMethods; |
| 4095 | } else { |
| 4096 | return &posixIoMethods; |
| 4097 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4098 | }else{ |
| 4099 | return &dotlockIoMethods; |
| 4100 | } |
| 4101 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4102 | static const sqlite3_io_methods |
| 4103 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4104 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4105 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4106 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4107 | #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE |
| 4108 | /* |
| 4109 | ** This "finder" function attempts to determine the best locking strategy |
| 4110 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
| 4111 | ** object that implements that strategy. |
| 4112 | ** |
| 4113 | ** This is for VXWorks only. |
| 4114 | */ |
| 4115 | static const sqlite3_io_methods *autolockIoFinderImpl( |
| 4116 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4117 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4118 | ){ |
| 4119 | struct flock lockInfo; |
| 4120 | |
| 4121 | if( !filePath ){ |
| 4122 | /* If filePath==NULL that means we are dealing with a transient file |
| 4123 | ** that does not need to be locked. */ |
| 4124 | return &nolockIoMethods; |
| 4125 | } |
| 4126 | |
| 4127 | /* Test if fcntl() is supported and use POSIX style locks. |
| 4128 | ** Otherwise fall back to the named semaphore method. |
| 4129 | */ |
| 4130 | lockInfo.l_len = 1; |
| 4131 | lockInfo.l_start = 0; |
| 4132 | lockInfo.l_whence = SEEK_SET; |
| 4133 | lockInfo.l_type = F_RDLCK; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4134 | if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4135 | return &posixIoMethods; |
| 4136 | }else{ |
| 4137 | return &semIoMethods; |
| 4138 | } |
| 4139 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4140 | static const sqlite3_io_methods |
| 4141 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4142 | |
| 4143 | #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */ |
| 4144 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4145 | /* |
| 4146 | ** An abstract type for a pointer to a IO method finder function: |
| 4147 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4148 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4149 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4150 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4151 | /**************************************************************************** |
| 4152 | **************************** sqlite3_vfs methods **************************** |
| 4153 | ** |
| 4154 | ** This division contains the implementation of methods on the |
| 4155 | ** sqlite3_vfs object. |
| 4156 | */ |
| 4157 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4158 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4159 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4160 | */ |
| 4161 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4162 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4163 | int h, /* Open file descriptor of file being opened */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4164 | int dirfd, /* Directory file descriptor */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4165 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4166 | const char *zFilename, /* Name of the file being opened */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4167 | int noLock, /* Omit locking if true */ |
| 4168 | int isDelete /* Delete on close if true */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4169 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4170 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4171 | unixFile *pNew = (unixFile *)pId; |
| 4172 | int rc = SQLITE_OK; |
| 4173 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4174 | assert( pNew->pInode==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4175 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4176 | /* Parameter isDelete is only used on vxworks. Express this explicitly |
| 4177 | ** here to prevent compiler warnings about unused parameters. |
danielk1977 | a03396a | 2008-11-19 14:35:46 +0000 | [diff] [blame] | 4178 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4179 | UNUSED_PARAMETER(isDelete); |
danielk1977 | a03396a | 2008-11-19 14:35:46 +0000 | [diff] [blame] | 4180 | |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4181 | /* Usually the path zFilename should not be a relative pathname. The |
| 4182 | ** exception is when opening the proxy "conch" file in builds that |
| 4183 | ** include the special Apple locking styles. |
| 4184 | */ |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4185 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | f7f55ed | 2010-10-05 18:22:47 +0000 | [diff] [blame] | 4186 | assert( zFilename==0 || zFilename[0]=='/' |
| 4187 | || pVfs->pAppData==(void*)&autolockIoFinder ); |
| 4188 | #else |
| 4189 | assert( zFilename==0 || zFilename[0]=='/' ); |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4190 | #endif |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4191 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4192 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4193 | pNew->h = h; |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4194 | pNew->dirfd = dirfd; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4195 | pNew->fileFlags = 0; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4196 | pNew->zPath = zFilename; |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 4197 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4198 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 4199 | pNew->pId = vxworksFindFileId(zFilename); |
| 4200 | if( pNew->pId==0 ){ |
| 4201 | noLock = 1; |
| 4202 | rc = SQLITE_NOMEM; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4203 | } |
| 4204 | #endif |
| 4205 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4206 | if( noLock ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4207 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4208 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4209 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4210 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4211 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 4212 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 4213 | ** zFilename remains valid until file is closed, to support */ |
| 4214 | pNew->lockingContext = (void*)zFilename; |
| 4215 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4216 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4217 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4218 | if( pLockingStyle == &posixIoMethods |
| 4219 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 4220 | || pLockingStyle == &nfsIoMethods |
| 4221 | #endif |
| 4222 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4223 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4224 | rc = findInodeInfo(pNew, &pNew->pInode); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4225 | if( rc!=SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4226 | /* If an error occured in findInodeInfo(), close the file descriptor |
| 4227 | ** immediately, before releasing the mutex. findInodeInfo() may fail |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4228 | ** in two scenarios: |
| 4229 | ** |
| 4230 | ** (a) A call to fstat() failed. |
| 4231 | ** (b) A malloc failed. |
| 4232 | ** |
| 4233 | ** Scenario (b) may only occur if the process is holding no other |
| 4234 | ** file descriptors open on the same file. If there were other file |
| 4235 | ** descriptors on this file, then no malloc would be required by |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4236 | ** findInodeInfo(). If this is the case, it is quite safe to close |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4237 | ** handle h - as it is guaranteed that no posix locks will be released |
| 4238 | ** by doing so. |
| 4239 | ** |
| 4240 | ** If scenario (a) caused the error then things are not so safe. The |
| 4241 | ** implicit assumption here is that if fstat() fails, things are in |
| 4242 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 4243 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4244 | robust_close(pNew, h, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4245 | h = -1; |
| 4246 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4247 | unixLeaveMutex(); |
| 4248 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4249 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4250 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 4251 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4252 | /* AFP locking uses the file path so it needs to be included in |
| 4253 | ** the afpLockingContext. |
| 4254 | */ |
| 4255 | afpLockingContext *pCtx; |
| 4256 | pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 4257 | if( pCtx==0 ){ |
| 4258 | rc = SQLITE_NOMEM; |
| 4259 | }else{ |
| 4260 | /* NB: zFilename exists and remains valid until the file is closed |
| 4261 | ** according to requirement F11141. So we do not need to make a |
| 4262 | ** copy of the filename. */ |
| 4263 | pCtx->dbPath = zFilename; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4264 | pCtx->reserved = 0; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4265 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4266 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4267 | rc = findInodeInfo(pNew, &pNew->pInode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4268 | if( rc!=SQLITE_OK ){ |
| 4269 | sqlite3_free(pNew->lockingContext); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4270 | robust_close(pNew, h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4271 | h = -1; |
| 4272 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4273 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4274 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4275 | } |
| 4276 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4277 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4278 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 4279 | /* Dotfile locking uses the file path so it needs to be included in |
| 4280 | ** the dotlockLockingContext |
| 4281 | */ |
| 4282 | char *zLockFile; |
| 4283 | int nFilename; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4284 | nFilename = (int)strlen(zFilename) + 6; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4285 | zLockFile = (char *)sqlite3_malloc(nFilename); |
| 4286 | if( zLockFile==0 ){ |
| 4287 | rc = SQLITE_NOMEM; |
| 4288 | }else{ |
| 4289 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4290 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4291 | pNew->lockingContext = zLockFile; |
| 4292 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4293 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4294 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4295 | else if( pLockingStyle == &semIoMethods ){ |
| 4296 | /* Named semaphore locking uses the file path so it needs to be |
| 4297 | ** included in the semLockingContext |
| 4298 | */ |
| 4299 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4300 | rc = findInodeInfo(pNew, &pNew->pInode); |
| 4301 | if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 4302 | char *zSemName = pNew->pInode->aSemName; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4303 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 4304 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4305 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 4306 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4307 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4308 | pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 4309 | if( pNew->pInode->pSem == SEM_FAILED ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4310 | rc = SQLITE_NOMEM; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4311 | pNew->pInode->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4312 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4313 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4314 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4315 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4316 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 4317 | |
| 4318 | pNew->lastErrno = 0; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4319 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4320 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4321 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
drh | 309e655 | 2010-02-05 18:00:26 +0000 | [diff] [blame] | 4322 | h = -1; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4323 | unlink(zFilename); |
| 4324 | isDelete = 0; |
| 4325 | } |
| 4326 | pNew->isDelete = isDelete; |
| 4327 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4328 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4329 | if( dirfd>=0 ) robust_close(pNew, dirfd, __LINE__); |
| 4330 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4331 | }else{ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4332 | pNew->pMethod = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4333 | OpenCounter(+1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4334 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4335 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 4336 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 4337 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4338 | /* |
| 4339 | ** Open a file descriptor to the directory containing file zFilename. |
| 4340 | ** If successful, *pFd is set to the opened file descriptor and |
| 4341 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 4342 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 4343 | ** value. |
| 4344 | ** |
| 4345 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 4346 | ** the file descriptor *pFd using close(). |
| 4347 | */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4348 | static int openDirectory(const char *zFilename, int *pFd){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4349 | int ii; |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 4350 | int fd = -1; |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 4351 | char zDirname[MAX_PATHNAME+1]; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4352 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4353 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
drh | 617634e | 2009-01-08 14:36:20 +0000 | [diff] [blame] | 4354 | for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4355 | if( ii>0 ){ |
| 4356 | zDirname[ii] = '\0'; |
| 4357 | fd = open(zDirname, O_RDONLY|O_BINARY, 0); |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 4358 | if( fd>=0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4359 | #ifdef FD_CLOEXEC |
| 4360 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 4361 | #endif |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4362 | OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4363 | } |
| 4364 | } |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4365 | *pFd = fd; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 4366 | return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname)); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4369 | /* |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4370 | ** Return the name of a directory in which to put temporary files. |
| 4371 | ** If no suitable temporary file directory can be found, return NULL. |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4372 | */ |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4373 | static const char *unixTempFileDir(void){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4374 | static const char *azDirs[] = { |
| 4375 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4376 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4377 | "/var/tmp", |
| 4378 | "/usr/tmp", |
| 4379 | "/tmp", |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4380 | 0 /* List terminator */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4381 | }; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4382 | unsigned int i; |
| 4383 | struct stat buf; |
| 4384 | const char *zDir = 0; |
| 4385 | |
| 4386 | azDirs[0] = sqlite3_temp_directory; |
| 4387 | if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); |
drh | 19515c8 | 2010-06-19 23:53:11 +0000 | [diff] [blame] | 4388 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){ |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4389 | if( zDir==0 ) continue; |
| 4390 | if( stat(zDir, &buf) ) continue; |
| 4391 | if( !S_ISDIR(buf.st_mode) ) continue; |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4392 | if( access(zDir, 07) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4393 | break; |
| 4394 | } |
| 4395 | return zDir; |
| 4396 | } |
| 4397 | |
| 4398 | /* |
| 4399 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 4400 | ** by the calling process and must be big enough to hold at least |
| 4401 | ** pVfs->mxPathname bytes. |
| 4402 | */ |
| 4403 | static int unixGetTempname(int nBuf, char *zBuf){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4404 | static const unsigned char zChars[] = |
| 4405 | "abcdefghijklmnopqrstuvwxyz" |
| 4406 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 4407 | "0123456789"; |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 4408 | unsigned int i, j; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4409 | const char *zDir; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4410 | |
| 4411 | /* It's odd to simulate an io-error here, but really this is just |
| 4412 | ** using the io-error infrastructure to test that SQLite handles this |
| 4413 | ** function failing. |
| 4414 | */ |
| 4415 | SimulateIOError( return SQLITE_IOERR ); |
| 4416 | |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4417 | zDir = unixTempFileDir(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4418 | if( zDir==0 ) zDir = "."; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4419 | |
| 4420 | /* Check that the output buffer is large enough for the temporary file |
| 4421 | ** name. If it is not, return SQLITE_ERROR. |
| 4422 | */ |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 4423 | if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4424 | return SQLITE_ERROR; |
| 4425 | } |
| 4426 | |
| 4427 | do{ |
| 4428 | sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4429 | j = (int)strlen(zBuf); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4430 | sqlite3_randomness(15, &zBuf[j]); |
| 4431 | for(i=0; i<15; i++, j++){ |
| 4432 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 4433 | } |
| 4434 | zBuf[j] = 0; |
| 4435 | }while( access(zBuf,0)==0 ); |
| 4436 | return SQLITE_OK; |
| 4437 | } |
| 4438 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4439 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 4440 | /* |
| 4441 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 4442 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 4443 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 4444 | */ |
| 4445 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 4446 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 4447 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4448 | /* |
| 4449 | ** Search for an unused file descriptor that was opened on the database |
| 4450 | ** file (not a journal or master-journal file) identified by pathname |
| 4451 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 4452 | ** argument to this function. |
| 4453 | ** |
| 4454 | ** Such a file descriptor may exist if a database connection was closed |
| 4455 | ** but the associated file descriptor could not be closed because some |
| 4456 | ** other file descriptor open on the same file is holding a file-lock. |
| 4457 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 4458 | ** describing "Posix Advisory Locking" at the start of this file for |
| 4459 | ** further details. Also, ticket #4018. |
| 4460 | ** |
| 4461 | ** If a suitable file descriptor is found, then it is returned. If no |
| 4462 | ** such file descriptor is located, -1 is returned. |
| 4463 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4464 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 4465 | UnixUnusedFd *pUnused = 0; |
| 4466 | |
| 4467 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 4468 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 4469 | ** but because no way to test it is currently available. It is better |
| 4470 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 4471 | ** feature. */ |
| 4472 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4473 | struct stat sStat; /* Results of stat() call */ |
| 4474 | |
| 4475 | /* A stat() call may fail for various reasons. If this happens, it is |
| 4476 | ** almost certain that an open() call on the same path will also fail. |
| 4477 | ** For this reason, if an error occurs in the stat() call here, it is |
| 4478 | ** ignored and -1 is returned. The caller will try to open a new file |
| 4479 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 4480 | ** |
| 4481 | ** Even if a subsequent open() call does succeed, the consequences of |
| 4482 | ** not searching for a resusable file descriptor are not dire. */ |
| 4483 | if( 0==stat(zPath, &sStat) ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4484 | unixInodeInfo *pInode; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4485 | |
| 4486 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4487 | pInode = inodeList; |
| 4488 | while( pInode && (pInode->fileId.dev!=sStat.st_dev |
| 4489 | || pInode->fileId.ino!=sStat.st_ino) ){ |
| 4490 | pInode = pInode->pNext; |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 4491 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4492 | if( pInode ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4493 | UnixUnusedFd **pp; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4494 | for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4495 | pUnused = *pp; |
| 4496 | if( pUnused ){ |
| 4497 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4498 | } |
| 4499 | } |
| 4500 | unixLeaveMutex(); |
| 4501 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4502 | #endif /* if !OS_VXWORKS */ |
| 4503 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4504 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4505 | |
| 4506 | /* |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4507 | ** This function is called by unixOpen() to determine the unix permissions |
drh | f65bc91 | 2010-07-14 20:51:34 +0000 | [diff] [blame] | 4508 | ** to create new files with. If no error occurs, then SQLITE_OK is returned |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4509 | ** and a value suitable for passing as the third argument to open(2) is |
| 4510 | ** written to *pMode. If an IO error occurs, an SQLite error code is |
| 4511 | ** returned and the value of *pMode is not modified. |
| 4512 | ** |
| 4513 | ** If the file being opened is a temporary file, it is always created with |
| 4514 | ** the octal permissions 0600 (read/writable by owner only). If the file |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4515 | ** is a database or master journal file, it is created with the permissions |
| 4516 | ** mask SQLITE_DEFAULT_FILE_PERMISSIONS. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4517 | ** |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4518 | ** Finally, if the file being opened is a WAL or regular journal file, then |
| 4519 | ** this function queries the file-system for the permissions on the |
| 4520 | ** corresponding database file and sets *pMode to this value. Whenever |
| 4521 | ** possible, WAL and journal files are created using the same permissions |
| 4522 | ** as the associated database file. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4523 | */ |
| 4524 | static int findCreateFileMode( |
| 4525 | const char *zPath, /* Path of file (possibly) being created */ |
| 4526 | int flags, /* Flags passed as 4th argument to xOpen() */ |
| 4527 | mode_t *pMode /* OUT: Permissions to open file with */ |
| 4528 | ){ |
| 4529 | int rc = SQLITE_OK; /* Return Code */ |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4530 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4531 | char zDb[MAX_PATHNAME+1]; /* Database file path */ |
| 4532 | int nDb; /* Number of valid bytes in zDb */ |
| 4533 | struct stat sStat; /* Output of stat() on database file */ |
| 4534 | |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 4535 | /* zPath is a path to a WAL or journal file. The following block derives |
| 4536 | ** the path to the associated database file from zPath. This block handles |
| 4537 | ** the following naming conventions: |
| 4538 | ** |
| 4539 | ** "<path to db>-journal" |
| 4540 | ** "<path to db>-wal" |
| 4541 | ** "<path to db>-journal-NNNN" |
| 4542 | ** "<path to db>-wal-NNNN" |
| 4543 | ** |
| 4544 | ** where NNNN is a 4 digit decimal number. The NNNN naming schemes are |
| 4545 | ** used by the test_multiplex.c module. |
| 4546 | */ |
| 4547 | nDb = sqlite3Strlen30(zPath) - 1; |
| 4548 | while( nDb>0 && zPath[nDb]!='l' ) nDb--; |
| 4549 | nDb -= ((flags & SQLITE_OPEN_WAL) ? 3 : 7); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4550 | memcpy(zDb, zPath, nDb); |
| 4551 | zDb[nDb] = '\0'; |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 4552 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4553 | if( 0==stat(zDb, &sStat) ){ |
| 4554 | *pMode = sStat.st_mode & 0777; |
| 4555 | }else{ |
| 4556 | rc = SQLITE_IOERR_FSTAT; |
| 4557 | } |
| 4558 | }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 4559 | *pMode = 0600; |
| 4560 | }else{ |
| 4561 | *pMode = SQLITE_DEFAULT_FILE_PERMISSIONS; |
| 4562 | } |
| 4563 | return rc; |
| 4564 | } |
| 4565 | |
| 4566 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4567 | ** Open the file zPath. |
| 4568 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4569 | ** Previously, the SQLite OS layer used three functions in place of this |
| 4570 | ** one: |
| 4571 | ** |
| 4572 | ** sqlite3OsOpenReadWrite(); |
| 4573 | ** sqlite3OsOpenReadOnly(); |
| 4574 | ** sqlite3OsOpenExclusive(); |
| 4575 | ** |
| 4576 | ** These calls correspond to the following combinations of flags: |
| 4577 | ** |
| 4578 | ** ReadWrite() -> (READWRITE | CREATE) |
| 4579 | ** ReadOnly() -> (READONLY) |
| 4580 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 4581 | ** |
| 4582 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 4583 | ** true, the file was configured to be automatically deleted when the |
| 4584 | ** file handle closed. To achieve the same effect using this new |
| 4585 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 4586 | ** OpenExclusive(). |
| 4587 | */ |
| 4588 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4589 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 4590 | const char *zPath, /* Pathname of file to be opened */ |
| 4591 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 4592 | int flags, /* Input flags to control the opening */ |
| 4593 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4594 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4595 | unixFile *p = (unixFile *)pFile; |
| 4596 | int fd = -1; /* File descriptor returned by open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4597 | int dirfd = -1; /* Directory file descriptor */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4598 | int openFlags = 0; /* Flags to pass to open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4599 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4600 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4601 | int rc = SQLITE_OK; /* Function Return Code */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4602 | |
| 4603 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 4604 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 4605 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 4606 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 4607 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4608 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4609 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 4610 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4611 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4612 | /* If creating a master or main-file journal, this function will open |
| 4613 | ** a file-descriptor on the directory too. The first time unixSync() |
| 4614 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 4615 | */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4616 | int isOpenDirectory = (isCreate && ( |
| 4617 | eType==SQLITE_OPEN_MASTER_JOURNAL |
| 4618 | || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 4619 | || eType==SQLITE_OPEN_WAL |
| 4620 | )); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4621 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4622 | /* If argument zPath is a NULL pointer, this function is required to open |
| 4623 | ** a temporary file. Use this buffer to store the file name in. |
| 4624 | */ |
| 4625 | char zTmpname[MAX_PATHNAME+1]; |
| 4626 | const char *zName = zPath; |
| 4627 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4628 | /* Check the following statements are true: |
| 4629 | ** |
| 4630 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 4631 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 4632 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4633 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4634 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4635 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4636 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4637 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4638 | assert(isDelete==0 || isCreate); |
| 4639 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4640 | /* The main DB, main journal, WAL file and master journal are never |
| 4641 | ** automatically deleted. Nor are they ever temporary files. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4642 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 4643 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 4644 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4645 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4646 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4647 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 4648 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 4649 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 4650 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4651 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4652 | ); |
| 4653 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4654 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4655 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4656 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4657 | UnixUnusedFd *pUnused; |
| 4658 | pUnused = findReusableFd(zName, flags); |
| 4659 | if( pUnused ){ |
| 4660 | fd = pUnused->fd; |
| 4661 | }else{ |
dan | 6aa657f | 2009-08-24 18:57:58 +0000 | [diff] [blame] | 4662 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4663 | if( !pUnused ){ |
| 4664 | return SQLITE_NOMEM; |
| 4665 | } |
| 4666 | } |
| 4667 | p->pUnused = pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4668 | }else if( !zName ){ |
| 4669 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4670 | assert(isDelete && !isOpenDirectory); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4671 | rc = unixGetTempname(MAX_PATHNAME+1, zTmpname); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4672 | if( rc!=SQLITE_OK ){ |
| 4673 | return rc; |
| 4674 | } |
| 4675 | zName = zTmpname; |
| 4676 | } |
| 4677 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4678 | /* Determine the value of the flags parameter passed to POSIX function |
| 4679 | ** open(). These must be calculated even if open() is not called, as |
| 4680 | ** they may be stored as part of the file handle and used by the |
| 4681 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4682 | if( isReadonly ) openFlags |= O_RDONLY; |
| 4683 | if( isReadWrite ) openFlags |= O_RDWR; |
| 4684 | if( isCreate ) openFlags |= O_CREAT; |
| 4685 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 4686 | openFlags |= (O_LARGEFILE|O_BINARY); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4687 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4688 | if( fd<0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4689 | mode_t openMode; /* Permissions to create file with */ |
| 4690 | rc = findCreateFileMode(zName, flags, &openMode); |
| 4691 | if( rc!=SQLITE_OK ){ |
| 4692 | assert( !p->pUnused ); |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4693 | assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4694 | return rc; |
| 4695 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4696 | fd = open(zName, openFlags, openMode); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4697 | OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4698 | if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ |
| 4699 | /* Failed to open the file for read/write access. Try read-only. */ |
| 4700 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4701 | openFlags &= ~(O_RDWR|O_CREAT); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4702 | flags |= SQLITE_OPEN_READONLY; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4703 | openFlags |= O_RDONLY; |
| 4704 | fd = open(zName, openFlags, openMode); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4705 | } |
| 4706 | if( fd<0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 4707 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4708 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4709 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4710 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4711 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4712 | if( pOutFlags ){ |
| 4713 | *pOutFlags = flags; |
| 4714 | } |
| 4715 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4716 | if( p->pUnused ){ |
| 4717 | p->pUnused->fd = fd; |
| 4718 | p->pUnused->flags = flags; |
| 4719 | } |
| 4720 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4721 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4722 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4723 | zPath = zName; |
| 4724 | #else |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4725 | unlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4726 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4727 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 4728 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4729 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4730 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 4731 | } |
| 4732 | #endif |
| 4733 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4734 | if( isOpenDirectory ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4735 | rc = openDirectory(zPath, &dirfd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4736 | if( rc!=SQLITE_OK ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4737 | /* It is safe to close fd at this point, because it is guaranteed not |
| 4738 | ** 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] | 4739 | ** it would not be safe to close as this would release any locks held |
| 4740 | ** on the file by this process. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4741 | assert( eType!=SQLITE_OPEN_MAIN_DB ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4742 | robust_close(p, fd, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4743 | goto open_finished; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4744 | } |
| 4745 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4746 | |
| 4747 | #ifdef FD_CLOEXEC |
| 4748 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 4749 | #endif |
| 4750 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4751 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4752 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4753 | |
| 4754 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 4755 | struct statfs fsInfo; |
| 4756 | if( fstatfs(fd, &fsInfo) == -1 ){ |
| 4757 | ((unixFile*)pFile)->lastErrno = errno; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4758 | if( dirfd>=0 ) robust_close(p, dirfd, __LINE__); |
| 4759 | robust_close(p, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4760 | return SQLITE_IOERR_ACCESS; |
| 4761 | } |
| 4762 | if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 4763 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 4764 | } |
| 4765 | #endif |
| 4766 | |
| 4767 | #if SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4768 | #if SQLITE_PREFER_PROXY_LOCKING |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4769 | isAutoProxy = 1; |
| 4770 | #endif |
| 4771 | if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4772 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 4773 | int useProxy = 0; |
| 4774 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4775 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 4776 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4777 | if( envforce!=NULL ){ |
| 4778 | useProxy = atoi(envforce)>0; |
| 4779 | }else{ |
| 4780 | struct statfs fsInfo; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4781 | if( statfs(zPath, &fsInfo) == -1 ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4782 | /* In theory, the close(fd) call is sub-optimal. If the file opened |
| 4783 | ** with fd is a database file, and there are other connections open |
| 4784 | ** on that file that are currently holding advisory locks on it, |
| 4785 | ** then the call to close() will cancel those locks. In practice, |
| 4786 | ** we're assuming that statfs() doesn't fail very often. At least |
| 4787 | ** not while other file descriptors opened by the same process on |
| 4788 | ** the same file are working. */ |
| 4789 | p->lastErrno = errno; |
| 4790 | if( dirfd>=0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4791 | robust_close(p, dirfd, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4792 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4793 | robust_close(p, fd, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4794 | rc = SQLITE_IOERR_ACCESS; |
| 4795 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4796 | } |
| 4797 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 4798 | } |
| 4799 | if( useProxy ){ |
| 4800 | rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete); |
| 4801 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4802 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4803 | if( rc!=SQLITE_OK ){ |
| 4804 | /* Use unixClose to clean up the resources added in fillInUnixFile |
| 4805 | ** and clear all the structure's references. Specifically, |
| 4806 | ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 4807 | */ |
| 4808 | unixClose(pFile); |
| 4809 | return rc; |
| 4810 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4811 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4812 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4813 | } |
| 4814 | } |
| 4815 | #endif |
| 4816 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4817 | rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete); |
| 4818 | open_finished: |
| 4819 | if( rc!=SQLITE_OK ){ |
| 4820 | sqlite3_free(p->pUnused); |
| 4821 | } |
| 4822 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4823 | } |
| 4824 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4825 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4826 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4827 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 4828 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4829 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4830 | static int unixDelete( |
| 4831 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 4832 | const char *zPath, /* Name of file to be deleted */ |
| 4833 | int dirSync /* If true, fsync() directory after deleting file */ |
| 4834 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4835 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4836 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4837 | SimulateIOError(return SQLITE_IOERR_DELETE); |
drh | 5d4feff | 2010-07-14 01:45:22 +0000 | [diff] [blame] | 4838 | if( unlink(zPath)==(-1) && errno!=ENOENT ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 4839 | return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath); |
drh | 5d4feff | 2010-07-14 01:45:22 +0000 | [diff] [blame] | 4840 | } |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 4841 | #ifndef SQLITE_DISABLE_DIRSYNC |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4842 | if( dirSync ){ |
| 4843 | int fd; |
| 4844 | rc = openDirectory(zPath, &fd); |
| 4845 | if( rc==SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4846 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4847 | if( fsync(fd)==-1 ) |
| 4848 | #else |
| 4849 | if( fsync(fd) ) |
| 4850 | #endif |
| 4851 | { |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 4852 | rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4853 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4854 | robust_close(0, fd, __LINE__); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4855 | } |
| 4856 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 4857 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4858 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4859 | } |
| 4860 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4861 | /* |
| 4862 | ** Test the existance of or access permissions of file zPath. The |
| 4863 | ** test performed depends on the value of flags: |
| 4864 | ** |
| 4865 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 4866 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 4867 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 4868 | ** |
| 4869 | ** Otherwise return 0. |
| 4870 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4871 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4872 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 4873 | const char *zPath, /* Path of the file to examine */ |
| 4874 | int flags, /* What do we want to learn about the zPath file? */ |
| 4875 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4876 | ){ |
rse | 25c0d1a | 2007-09-20 08:38:14 +0000 | [diff] [blame] | 4877 | int amode = 0; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4878 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4879 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4880 | switch( flags ){ |
| 4881 | case SQLITE_ACCESS_EXISTS: |
| 4882 | amode = F_OK; |
| 4883 | break; |
| 4884 | case SQLITE_ACCESS_READWRITE: |
| 4885 | amode = W_OK|R_OK; |
| 4886 | break; |
drh | 50d3f90 | 2007-08-27 21:10:36 +0000 | [diff] [blame] | 4887 | case SQLITE_ACCESS_READ: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4888 | amode = R_OK; |
| 4889 | break; |
| 4890 | |
| 4891 | default: |
| 4892 | assert(!"Invalid flags argument"); |
| 4893 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4894 | *pResOut = (access(zPath, amode)==0); |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 4895 | if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){ |
| 4896 | struct stat buf; |
| 4897 | if( 0==stat(zPath, &buf) && buf.st_size==0 ){ |
| 4898 | *pResOut = 0; |
| 4899 | } |
| 4900 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4901 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4902 | } |
| 4903 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4904 | |
| 4905 | /* |
| 4906 | ** Turn a relative pathname into a full pathname. The relative path |
| 4907 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 4908 | ** zPath. |
| 4909 | ** |
| 4910 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 4911 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 4912 | ** this buffer before returning. |
| 4913 | */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 4914 | static int unixFullPathname( |
| 4915 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 4916 | const char *zPath, /* Possibly relative input path */ |
| 4917 | int nOut, /* Size of output buffer in bytes */ |
| 4918 | char *zOut /* Output buffer */ |
| 4919 | ){ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 4920 | |
| 4921 | /* It's odd to simulate an io-error here, but really this is just |
| 4922 | ** using the io-error infrastructure to test that SQLite handles this |
| 4923 | ** function failing. This function could fail if, for example, the |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4924 | ** current working directory has been unlinked. |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 4925 | */ |
| 4926 | SimulateIOError( return SQLITE_ERROR ); |
| 4927 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4928 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 4929 | UNUSED_PARAMETER(pVfs); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4930 | |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4931 | zOut[nOut-1] = '\0'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4932 | if( zPath[0]=='/' ){ |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4933 | sqlite3_snprintf(nOut, zOut, "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4934 | }else{ |
| 4935 | int nCwd; |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4936 | if( getcwd(zOut, nOut-1)==0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 4937 | return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4938 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4939 | nCwd = (int)strlen(zOut); |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4940 | sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4941 | } |
| 4942 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4943 | } |
| 4944 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 4945 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4946 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 4947 | /* |
| 4948 | ** Interfaces for opening a shared library, finding entry points |
| 4949 | ** within the shared library, and closing the shared library. |
| 4950 | */ |
| 4951 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4952 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 4953 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4954 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 4955 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 4956 | |
| 4957 | /* |
| 4958 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 4959 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 4960 | ** message is available, it is written to zBufOut. If no error message |
| 4961 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 4962 | ** error message. |
| 4963 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4964 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
dan | 3239053 | 2010-11-29 18:36:22 +0000 | [diff] [blame] | 4965 | const char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4966 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4967 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4968 | zErr = dlerror(); |
| 4969 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4970 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4971 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4972 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4973 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4974 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 4975 | /* |
| 4976 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 4977 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 4978 | ** returns a void* which is really a pointer to a function. So how do we |
| 4979 | ** use dlsym() with -pedantic-errors? |
| 4980 | ** |
| 4981 | ** Variable x below is defined to be a pointer to a function taking |
| 4982 | ** parameters void* and const char* and returning a pointer to a function. |
| 4983 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 4984 | ** (That assignment requires a cast.) Then we call the function that |
| 4985 | ** x points to. |
| 4986 | ** |
| 4987 | ** This work-around is unlikely to work correctly on any system where |
| 4988 | ** you really cannot cast a function pointer into void*. But then, on the |
| 4989 | ** other hand, dlsym() will not work on such a system either, so we have |
| 4990 | ** not really lost anything. |
| 4991 | */ |
| 4992 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4993 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4994 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 4995 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4996 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4997 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 4998 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4999 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5000 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5001 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 5002 | #define unixDlOpen 0 |
| 5003 | #define unixDlError 0 |
| 5004 | #define unixDlSym 0 |
| 5005 | #define unixDlClose 0 |
| 5006 | #endif |
| 5007 | |
| 5008 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5009 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5010 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5011 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 5012 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 5013 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5014 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5015 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 5016 | ** errors. The reports issued by valgrind are incorrect - we would |
| 5017 | ** prefer that the randomness be increased by making use of the |
| 5018 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 5019 | ** some users. Rather than argue, it seems easier just to initialize |
| 5020 | ** the whole array and silence valgrind, even if that means less randomness |
| 5021 | ** in the random seed. |
| 5022 | ** |
| 5023 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 5024 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5025 | ** tests repeatable. |
| 5026 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5027 | memset(zBuf, 0, nBuf); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5028 | #if !defined(SQLITE_TEST) |
| 5029 | { |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5030 | int pid, fd; |
| 5031 | fd = open("/dev/urandom", O_RDONLY); |
| 5032 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 5033 | time_t t; |
| 5034 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5035 | memcpy(zBuf, &t, sizeof(t)); |
| 5036 | pid = getpid(); |
| 5037 | memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid)); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 5038 | assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf ); |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 5039 | nBuf = sizeof(t) + sizeof(pid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5040 | }else{ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 5041 | do{ nBuf = read(fd, zBuf, nBuf); }while( nBuf<0 && errno==EINTR ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5042 | robust_close(0, fd, __LINE__); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5043 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5044 | } |
| 5045 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 5046 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5047 | } |
| 5048 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5049 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5050 | /* |
| 5051 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5052 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 5053 | ** The return value is the number of microseconds of sleep actually |
| 5054 | ** requested from the underlying operating system, a number which |
| 5055 | ** might be greater than or equal to the argument, but not less |
| 5056 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5057 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5058 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5059 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5060 | struct timespec sp; |
| 5061 | |
| 5062 | sp.tv_sec = microseconds / 1000000; |
| 5063 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 5064 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 5065 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5066 | return microseconds; |
| 5067 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5068 | usleep(microseconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 5069 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5070 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5071 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5072 | int seconds = (microseconds+999999)/1000000; |
| 5073 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 5074 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 5075 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 5076 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 5077 | } |
| 5078 | |
| 5079 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5080 | ** The following variable, if set to a non-zero value, is interpreted as |
| 5081 | ** the number of seconds since 1970 and is used to set the result of |
| 5082 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5083 | */ |
| 5084 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5085 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5086 | #endif |
| 5087 | |
| 5088 | /* |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5089 | ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 5090 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 5091 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 5092 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 5093 | ** proleptic Gregorian calendar. |
| 5094 | ** |
| 5095 | ** On success, return 0. Return 1 if the time and date cannot be found. |
| 5096 | */ |
| 5097 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 5098 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
| 5099 | #if defined(NO_GETTOD) |
| 5100 | time_t t; |
| 5101 | time(&t); |
dan | 15eac4e | 2010-11-22 17:26:07 +0000 | [diff] [blame] | 5102 | *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5103 | #elif OS_VXWORKS |
| 5104 | struct timespec sNow; |
| 5105 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 5106 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 5107 | #else |
| 5108 | struct timeval sNow; |
| 5109 | gettimeofday(&sNow, 0); |
| 5110 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
| 5111 | #endif |
| 5112 | |
| 5113 | #ifdef SQLITE_TEST |
| 5114 | if( sqlite3_current_time ){ |
| 5115 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 5116 | } |
| 5117 | #endif |
| 5118 | UNUSED_PARAMETER(NotUsed); |
| 5119 | return 0; |
| 5120 | } |
| 5121 | |
| 5122 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5123 | ** Find the current time (in Universal Coordinated Time). Write the |
| 5124 | ** current time and date as a Julian Day number into *prNow and |
| 5125 | ** return 0. Return 1 if the time and date cannot be found. |
| 5126 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5127 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5128 | sqlite3_int64 i; |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 5129 | UNUSED_PARAMETER(NotUsed); |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5130 | unixCurrentTimeInt64(0, &i); |
drh | 0dcb0a7 | 2010-05-03 18:22:52 +0000 | [diff] [blame] | 5131 | *prNow = i/86400000.0; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5132 | return 0; |
| 5133 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5134 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5135 | /* |
| 5136 | ** We added the xGetLastError() method with the intention of providing |
| 5137 | ** better low-level error messages when operating-system problems come up |
| 5138 | ** during SQLite operation. But so far, none of that has been implemented |
| 5139 | ** in the core. So this routine is never called. For now, it is merely |
| 5140 | ** a place-holder. |
| 5141 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5142 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 5143 | UNUSED_PARAMETER(NotUsed); |
| 5144 | UNUSED_PARAMETER(NotUsed2); |
| 5145 | UNUSED_PARAMETER(NotUsed3); |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 5146 | return 0; |
| 5147 | } |
| 5148 | |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5149 | |
| 5150 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5151 | ************************ End of sqlite3_vfs methods *************************** |
| 5152 | ******************************************************************************/ |
| 5153 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5154 | /****************************************************************************** |
| 5155 | ************************** Begin Proxy Locking ******************************** |
| 5156 | ** |
| 5157 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 5158 | ** other locking methods on secondary lock files. Proxy locking is a |
| 5159 | ** meta-layer over top of the primitive locking implemented above. For |
| 5160 | ** this reason, the division that implements of proxy locking is deferred |
| 5161 | ** until late in the file (here) after all of the other I/O methods have |
| 5162 | ** been defined - so that the primitive locking methods are available |
| 5163 | ** as services to help with the implementation of proxy locking. |
| 5164 | ** |
| 5165 | **** |
| 5166 | ** |
| 5167 | ** The default locking schemes in SQLite use byte-range locks on the |
| 5168 | ** database file to coordinate safe, concurrent access by multiple readers |
| 5169 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 5170 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 5171 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 5172 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 5173 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 5174 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 5175 | ** address in the shared range is taken for a SHARED lock, the entire |
| 5176 | ** shared range is taken for an EXCLUSIVE lock): |
| 5177 | ** |
| 5178 | ** PENDING_BYTE 0x40000000 |
| 5179 | ** RESERVED_BYTE 0x40000001 |
| 5180 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 5181 | ** |
| 5182 | ** This works well on the local file system, but shows a nearly 100x |
| 5183 | ** slowdown in read performance on AFP because the AFP client disables |
| 5184 | ** the read cache when byte-range locks are present. Enabling the read |
| 5185 | ** cache exposes a cache coherency problem that is present on all OS X |
| 5186 | ** supported network file systems. NFS and AFP both observe the |
| 5187 | ** close-to-open semantics for ensuring cache coherency |
| 5188 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 5189 | ** address the requirements for concurrent database access by multiple |
| 5190 | ** readers and writers |
| 5191 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 5192 | ** |
| 5193 | ** To address the performance and cache coherency issues, proxy file locking |
| 5194 | ** changes the way database access is controlled by limiting access to a |
| 5195 | ** single host at a time and moving file locks off of the database file |
| 5196 | ** and onto a proxy file on the local file system. |
| 5197 | ** |
| 5198 | ** |
| 5199 | ** Using proxy locks |
| 5200 | ** ----------------- |
| 5201 | ** |
| 5202 | ** C APIs |
| 5203 | ** |
| 5204 | ** sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE, |
| 5205 | ** <proxy_path> | ":auto:"); |
| 5206 | ** sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>); |
| 5207 | ** |
| 5208 | ** |
| 5209 | ** SQL pragmas |
| 5210 | ** |
| 5211 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 5212 | ** PRAGMA [database.]lock_proxy_file |
| 5213 | ** |
| 5214 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 5215 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 5216 | ** a proxy path based on the user's temp dir |
| 5217 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 5218 | ** actual proxy file name is generated from the name and path of the |
| 5219 | ** database file. For example: |
| 5220 | ** |
| 5221 | ** For database path "/Users/me/foo.db" |
| 5222 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 5223 | ** |
| 5224 | ** Once a lock proxy is configured for a database connection, it can not |
| 5225 | ** be removed, however it may be switched to a different proxy path via |
| 5226 | ** the above APIs (assuming the conch file is not being held by another |
| 5227 | ** connection or process). |
| 5228 | ** |
| 5229 | ** |
| 5230 | ** How proxy locking works |
| 5231 | ** ----------------------- |
| 5232 | ** |
| 5233 | ** Proxy file locking relies primarily on two new supporting files: |
| 5234 | ** |
| 5235 | ** * conch file to limit access to the database file to a single host |
| 5236 | ** at a time |
| 5237 | ** |
| 5238 | ** * proxy file to act as a proxy for the advisory locks normally |
| 5239 | ** taken on the database |
| 5240 | ** |
| 5241 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 5242 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 5243 | ** contents and comparing the host's unique host ID (see below) and lock |
| 5244 | ** proxy path against the values stored in the conch. The conch file is |
| 5245 | ** stored in the same directory as the database file and the file name |
| 5246 | ** is patterned after the database file name as ".<databasename>-conch". |
| 5247 | ** If the conch file does not exist, or it's contents do not match the |
| 5248 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 5249 | ** lock and the conch file contents is updated with the host ID and proxy |
| 5250 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 5251 | ** is held by another process (with a shared lock), the exclusive lock |
| 5252 | ** will fail and SQLITE_BUSY is returned. |
| 5253 | ** |
| 5254 | ** The proxy file - a single-byte file used for all advisory file locks |
| 5255 | ** normally taken on the database file. This allows for safe sharing |
| 5256 | ** of the database file for multiple readers and writers on the same |
| 5257 | ** host (the conch ensures that they all use the same local lock file). |
| 5258 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5259 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 5260 | ** only taken when the first request to lock database file is made. |
| 5261 | ** This matches the semantics of the traditional locking behavior, where |
| 5262 | ** opening a connection to a database file does not take a lock on it. |
| 5263 | ** The shared lock and an open file descriptor are maintained until |
| 5264 | ** the connection to the database is closed. |
| 5265 | ** |
| 5266 | ** The proxy file and the lock file are never deleted so they only need |
| 5267 | ** to be created the first time they are used. |
| 5268 | ** |
| 5269 | ** Configuration options |
| 5270 | ** --------------------- |
| 5271 | ** |
| 5272 | ** SQLITE_PREFER_PROXY_LOCKING |
| 5273 | ** |
| 5274 | ** Database files accessed on non-local file systems are |
| 5275 | ** automatically configured for proxy locking, lock files are |
| 5276 | ** named automatically using the same logic as |
| 5277 | ** PRAGMA lock_proxy_file=":auto:" |
| 5278 | ** |
| 5279 | ** SQLITE_PROXY_DEBUG |
| 5280 | ** |
| 5281 | ** Enables the logging of error messages during host id file |
| 5282 | ** retrieval and creation |
| 5283 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5284 | ** LOCKPROXYDIR |
| 5285 | ** |
| 5286 | ** Overrides the default directory used for lock proxy files that |
| 5287 | ** are named automatically via the ":auto:" setting |
| 5288 | ** |
| 5289 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 5290 | ** |
| 5291 | ** Permissions to use when creating a directory for storing the |
| 5292 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 5293 | ** |
| 5294 | ** |
| 5295 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 5296 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 5297 | ** force proxy locking to be used for every database file opened, and 0 |
| 5298 | ** will force automatic proxy locking to be disabled for all database |
| 5299 | ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or |
| 5300 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 5301 | */ |
| 5302 | |
| 5303 | /* |
| 5304 | ** Proxy locking is only available on MacOSX |
| 5305 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5306 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5307 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5308 | /* |
| 5309 | ** The proxyLockingContext has the path and file structures for the remote |
| 5310 | ** and local proxy files in it |
| 5311 | */ |
| 5312 | typedef struct proxyLockingContext proxyLockingContext; |
| 5313 | struct proxyLockingContext { |
| 5314 | unixFile *conchFile; /* Open conch file */ |
| 5315 | char *conchFilePath; /* Name of the conch file */ |
| 5316 | unixFile *lockProxy; /* Open proxy lock file */ |
| 5317 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 5318 | char *dbPath; /* Name of the open file */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5319 | int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5320 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 5321 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 5322 | }; |
| 5323 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5324 | /* |
| 5325 | ** The proxy lock file path for the database at dbPath is written into lPath, |
| 5326 | ** which must point to valid, writable memory large enough for a maxLen length |
| 5327 | ** file path. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5328 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5329 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 5330 | int len; |
| 5331 | int dbLen; |
| 5332 | int i; |
| 5333 | |
| 5334 | #ifdef LOCKPROXYDIR |
| 5335 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 5336 | #else |
| 5337 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 5338 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5339 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5340 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
| 5341 | lPath, errno, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5342 | return SQLITE_IOERR_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5343 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5344 | len = strlcat(lPath, "sqliteplocks", maxLen); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5345 | } |
| 5346 | # else |
| 5347 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 5348 | # endif |
| 5349 | #endif |
| 5350 | |
| 5351 | if( lPath[len-1]!='/' ){ |
| 5352 | len = strlcat(lPath, "/", maxLen); |
| 5353 | } |
| 5354 | |
| 5355 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5356 | dbLen = (int)strlen(dbPath); |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5357 | for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5358 | char c = dbPath[i]; |
| 5359 | lPath[i+len] = (c=='/')?'_':c; |
| 5360 | } |
| 5361 | lPath[i+len]='\0'; |
| 5362 | strlcat(lPath, ":auto:", maxLen); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5363 | OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5364 | return SQLITE_OK; |
| 5365 | } |
| 5366 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5367 | /* |
| 5368 | ** Creates the lock file and any missing directories in lockPath |
| 5369 | */ |
| 5370 | static int proxyCreateLockPath(const char *lockPath){ |
| 5371 | int i, len; |
| 5372 | char buf[MAXPATHLEN]; |
| 5373 | int start = 0; |
| 5374 | |
| 5375 | assert(lockPath!=NULL); |
| 5376 | /* try to create all the intermediate directories */ |
| 5377 | len = (int)strlen(lockPath); |
| 5378 | buf[0] = lockPath[0]; |
| 5379 | for( i=1; i<len; i++ ){ |
| 5380 | if( lockPath[i] == '/' && (i - start > 0) ){ |
| 5381 | /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 5382 | if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 5383 | || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 5384 | buf[i]='\0'; |
| 5385 | if( mkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
| 5386 | int err=errno; |
| 5387 | if( err!=EEXIST ) { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5388 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5389 | "'%s' proxy lock path=%s pid=%d\n", |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5390 | buf, strerror(err), lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5391 | return err; |
| 5392 | } |
| 5393 | } |
| 5394 | } |
| 5395 | start=i+1; |
| 5396 | } |
| 5397 | buf[i] = lockPath[i]; |
| 5398 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5399 | OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5400 | return 0; |
| 5401 | } |
| 5402 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5403 | /* |
| 5404 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 5405 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 5406 | ** |
| 5407 | ** The caller is responsible not only for closing the file descriptor |
| 5408 | ** but also for freeing the memory associated with the file descriptor. |
| 5409 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5410 | static int proxyCreateUnixFile( |
| 5411 | const char *path, /* path for the new unixFile */ |
| 5412 | unixFile **ppFile, /* unixFile created and returned by ref */ |
| 5413 | int islockfile /* if non zero missing dirs will be created */ |
| 5414 | ) { |
| 5415 | int fd = -1; |
| 5416 | int dirfd = -1; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5417 | unixFile *pNew; |
| 5418 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5419 | int openFlags = O_RDWR | O_CREAT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5420 | sqlite3_vfs dummyVfs; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5421 | int terrno = 0; |
| 5422 | UnixUnusedFd *pUnused = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5423 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5424 | /* 1. first try to open/create the file |
| 5425 | ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 5426 | ** the parent directories and then try again. |
| 5427 | ** 3. if that fails, try to open the file read-only |
| 5428 | ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 5429 | */ |
| 5430 | pUnused = findReusableFd(path, openFlags); |
| 5431 | if( pUnused ){ |
| 5432 | fd = pUnused->fd; |
| 5433 | }else{ |
| 5434 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
| 5435 | if( !pUnused ){ |
| 5436 | return SQLITE_NOMEM; |
| 5437 | } |
| 5438 | } |
| 5439 | if( fd<0 ){ |
| 5440 | fd = open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5441 | terrno = errno; |
| 5442 | if( fd<0 && errno==ENOENT && islockfile ){ |
| 5443 | if( proxyCreateLockPath(path) == SQLITE_OK ){ |
| 5444 | fd = open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5445 | } |
| 5446 | } |
| 5447 | } |
| 5448 | if( fd<0 ){ |
| 5449 | openFlags = O_RDONLY; |
| 5450 | fd = open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5451 | terrno = errno; |
| 5452 | } |
| 5453 | if( fd<0 ){ |
| 5454 | if( islockfile ){ |
| 5455 | return SQLITE_BUSY; |
| 5456 | } |
| 5457 | switch (terrno) { |
| 5458 | case EACCES: |
| 5459 | return SQLITE_PERM; |
| 5460 | case EIO: |
| 5461 | return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 5462 | default: |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 5463 | return SQLITE_CANTOPEN_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5464 | } |
| 5465 | } |
| 5466 | |
| 5467 | pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew)); |
| 5468 | if( pNew==NULL ){ |
| 5469 | rc = SQLITE_NOMEM; |
| 5470 | goto end_create_proxy; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5471 | } |
| 5472 | memset(pNew, 0, sizeof(unixFile)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5473 | pNew->openFlags = openFlags; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5474 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5475 | pUnused->fd = fd; |
| 5476 | pUnused->flags = openFlags; |
| 5477 | pNew->pUnused = pUnused; |
| 5478 | |
| 5479 | rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0); |
| 5480 | if( rc==SQLITE_OK ){ |
| 5481 | *ppFile = pNew; |
| 5482 | return SQLITE_OK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5483 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5484 | end_create_proxy: |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5485 | robust_close(pNew, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5486 | sqlite3_free(pNew); |
| 5487 | sqlite3_free(pUnused); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5488 | return rc; |
| 5489 | } |
| 5490 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5491 | #ifdef SQLITE_TEST |
| 5492 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 5493 | int sqlite3_hostid_num = 0; |
| 5494 | #endif |
| 5495 | |
| 5496 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 5497 | |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5498 | /* Not always defined in the headers as it ought to be */ |
| 5499 | extern int gethostuuid(uuid_t id, const struct timespec *wait); |
| 5500 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5501 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 5502 | ** bytes of writable memory. |
| 5503 | */ |
| 5504 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5505 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 5506 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 5507 | #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\ |
| 5508 | && __MAC_OS_X_VERSION_MIN_REQUIRED<1050 |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 5509 | { |
| 5510 | static const struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
| 5511 | if( gethostuuid(pHostID, &timeout) ){ |
| 5512 | int err = errno; |
| 5513 | if( pError ){ |
| 5514 | *pError = err; |
| 5515 | } |
| 5516 | return SQLITE_IOERR; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5517 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5518 | } |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 5519 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5520 | #ifdef SQLITE_TEST |
| 5521 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 5522 | if( sqlite3_hostid_num != 0){ |
| 5523 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 5524 | } |
| 5525 | #endif |
| 5526 | |
| 5527 | return SQLITE_OK; |
| 5528 | } |
| 5529 | |
| 5530 | /* The conch file contains the header, host id and lock file path |
| 5531 | */ |
| 5532 | #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 5533 | #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 5534 | #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 5535 | #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 5536 | |
| 5537 | /* |
| 5538 | ** Takes an open conch file, copies the contents to a new path and then moves |
| 5539 | ** it back. The newly created file's file descriptor is assigned to the |
| 5540 | ** conch file structure and finally the original conch file descriptor is |
| 5541 | ** closed. Returns zero if successful. |
| 5542 | */ |
| 5543 | static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 5544 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5545 | unixFile *conchFile = pCtx->conchFile; |
| 5546 | char tPath[MAXPATHLEN]; |
| 5547 | char buf[PROXY_MAXCONCHLEN]; |
| 5548 | char *cPath = pCtx->conchFilePath; |
| 5549 | size_t readLen = 0; |
| 5550 | size_t pathLen = 0; |
| 5551 | char errmsg[64] = ""; |
| 5552 | int fd = -1; |
| 5553 | int rc = -1; |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5554 | UNUSED_PARAMETER(myHostID); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5555 | |
| 5556 | /* create a new path by replace the trailing '-conch' with '-break' */ |
| 5557 | pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 5558 | if( pathLen>MAXPATHLEN || pathLen<6 || |
| 5559 | (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5560 | sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5561 | goto end_breaklock; |
| 5562 | } |
| 5563 | /* read the conch content */ |
| 5564 | readLen = pread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
| 5565 | if( readLen<PROXY_PATHINDEX ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5566 | sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5567 | goto end_breaklock; |
| 5568 | } |
| 5569 | /* write it out to the temporary break file */ |
| 5570 | fd = open(tPath, (O_RDWR|O_CREAT|O_EXCL), SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5571 | if( fd<0 ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5572 | sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5573 | goto end_breaklock; |
| 5574 | } |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5575 | if( pwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5576 | sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5577 | goto end_breaklock; |
| 5578 | } |
| 5579 | if( rename(tPath, cPath) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5580 | sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5581 | goto end_breaklock; |
| 5582 | } |
| 5583 | rc = 0; |
| 5584 | fprintf(stderr, "broke stale lock on %s\n", cPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5585 | robust_close(pFile, conchFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5586 | conchFile->h = fd; |
| 5587 | conchFile->openFlags = O_RDWR | O_CREAT; |
| 5588 | |
| 5589 | end_breaklock: |
| 5590 | if( rc ){ |
| 5591 | if( fd>=0 ){ |
| 5592 | unlink(tPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5593 | robust_close(pFile, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5594 | } |
| 5595 | fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 5596 | } |
| 5597 | return rc; |
| 5598 | } |
| 5599 | |
| 5600 | /* Take the requested lock on the conch file and break a stale lock if the |
| 5601 | ** host id matches. |
| 5602 | */ |
| 5603 | static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 5604 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5605 | unixFile *conchFile = pCtx->conchFile; |
| 5606 | int rc = SQLITE_OK; |
| 5607 | int nTries = 0; |
| 5608 | struct timespec conchModTime; |
| 5609 | |
| 5610 | do { |
| 5611 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 5612 | nTries ++; |
| 5613 | if( rc==SQLITE_BUSY ){ |
| 5614 | /* If the lock failed (busy): |
| 5615 | * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 5616 | * 2nd try: fail if the mod time changed or host id is different, wait |
| 5617 | * 10 sec and try again |
| 5618 | * 3rd try: break the lock unless the mod time has changed. |
| 5619 | */ |
| 5620 | struct stat buf; |
| 5621 | if( fstat(conchFile->h, &buf) ){ |
| 5622 | pFile->lastErrno = errno; |
| 5623 | return SQLITE_IOERR_LOCK; |
| 5624 | } |
| 5625 | |
| 5626 | if( nTries==1 ){ |
| 5627 | conchModTime = buf.st_mtimespec; |
| 5628 | usleep(500000); /* wait 0.5 sec and try the lock again*/ |
| 5629 | continue; |
| 5630 | } |
| 5631 | |
| 5632 | assert( nTries>1 ); |
| 5633 | if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 5634 | conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 5635 | return SQLITE_BUSY; |
| 5636 | } |
| 5637 | |
| 5638 | if( nTries==2 ){ |
| 5639 | char tBuf[PROXY_MAXCONCHLEN]; |
| 5640 | int len = pread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
| 5641 | if( len<0 ){ |
| 5642 | pFile->lastErrno = errno; |
| 5643 | return SQLITE_IOERR_LOCK; |
| 5644 | } |
| 5645 | if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 5646 | /* don't break the lock if the host id doesn't match */ |
| 5647 | if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 5648 | return SQLITE_BUSY; |
| 5649 | } |
| 5650 | }else{ |
| 5651 | /* don't break the lock on short read or a version mismatch */ |
| 5652 | return SQLITE_BUSY; |
| 5653 | } |
| 5654 | usleep(10000000); /* wait 10 sec and try the lock again */ |
| 5655 | continue; |
| 5656 | } |
| 5657 | |
| 5658 | assert( nTries==3 ); |
| 5659 | if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 5660 | rc = SQLITE_OK; |
| 5661 | if( lockType==EXCLUSIVE_LOCK ){ |
| 5662 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 5663 | } |
| 5664 | if( !rc ){ |
| 5665 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 5666 | } |
| 5667 | } |
| 5668 | } |
| 5669 | } while( rc==SQLITE_BUSY && nTries<3 ); |
| 5670 | |
| 5671 | return rc; |
| 5672 | } |
| 5673 | |
| 5674 | /* 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] | 5675 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 5676 | ** lockPath means that the lockPath in the conch file will be used if the |
| 5677 | ** host IDs match, or a new lock path will be generated automatically |
| 5678 | ** and written to the conch file. |
| 5679 | */ |
| 5680 | static int proxyTakeConch(unixFile *pFile){ |
| 5681 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5682 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5683 | if( pCtx->conchHeld!=0 ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5684 | return SQLITE_OK; |
| 5685 | }else{ |
| 5686 | unixFile *conchFile = pCtx->conchFile; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5687 | uuid_t myHostID; |
| 5688 | int pError = 0; |
| 5689 | char readBuf[PROXY_MAXCONCHLEN]; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5690 | char lockPath[MAXPATHLEN]; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5691 | char *tempLockPath = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5692 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5693 | int createConch = 0; |
| 5694 | int hostIdMatch = 0; |
| 5695 | int readLen = 0; |
| 5696 | int tryOldLockPath = 0; |
| 5697 | int forceNewLockPath = 0; |
| 5698 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5699 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
| 5700 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5701 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5702 | rc = proxyGetHostID(myHostID, &pError); |
| 5703 | if( (rc&0xff)==SQLITE_IOERR ){ |
| 5704 | pFile->lastErrno = pError; |
| 5705 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5706 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5707 | rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5708 | if( rc!=SQLITE_OK ){ |
| 5709 | goto end_takeconch; |
| 5710 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5711 | /* read the existing conch file */ |
| 5712 | readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 5713 | if( readLen<0 ){ |
| 5714 | /* I/O error: lastErrno set by seekAndRead */ |
| 5715 | pFile->lastErrno = conchFile->lastErrno; |
| 5716 | rc = SQLITE_IOERR_READ; |
| 5717 | goto end_takeconch; |
| 5718 | }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 5719 | readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 5720 | /* a short read or version format mismatch means we need to create a new |
| 5721 | ** conch file. |
| 5722 | */ |
| 5723 | createConch = 1; |
| 5724 | } |
| 5725 | /* if the host id matches and the lock path already exists in the conch |
| 5726 | ** we'll try to use the path there, if we can't open that path, we'll |
| 5727 | ** retry with a new auto-generated path |
| 5728 | */ |
| 5729 | do { /* in case we need to try again for an :auto: named lock file */ |
| 5730 | |
| 5731 | if( !createConch && !forceNewLockPath ){ |
| 5732 | hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 5733 | PROXY_HOSTIDLEN); |
| 5734 | /* if the conch has data compare the contents */ |
| 5735 | if( !pCtx->lockProxyPath ){ |
| 5736 | /* for auto-named local lock file, just check the host ID and we'll |
| 5737 | ** use the local lock file path that's already in there |
| 5738 | */ |
| 5739 | if( hostIdMatch ){ |
| 5740 | size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 5741 | |
| 5742 | if( pathLen>=MAXPATHLEN ){ |
| 5743 | pathLen=MAXPATHLEN-1; |
| 5744 | } |
| 5745 | memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 5746 | lockPath[pathLen] = 0; |
| 5747 | tempLockPath = lockPath; |
| 5748 | tryOldLockPath = 1; |
| 5749 | /* create a copy of the lock path if the conch is taken */ |
| 5750 | goto end_takeconch; |
| 5751 | } |
| 5752 | }else if( hostIdMatch |
| 5753 | && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 5754 | readLen-PROXY_PATHINDEX) |
| 5755 | ){ |
| 5756 | /* conch host and lock path match */ |
| 5757 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5758 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5759 | } |
| 5760 | |
| 5761 | /* if the conch isn't writable and doesn't match, we can't take it */ |
| 5762 | if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 5763 | rc = SQLITE_BUSY; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5764 | goto end_takeconch; |
| 5765 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5766 | |
| 5767 | /* 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] | 5768 | if( !pCtx->lockProxyPath ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5769 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 5770 | tempLockPath = lockPath; |
| 5771 | /* create a copy of the lock path _only_ if the conch is taken */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5772 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5773 | |
| 5774 | /* update conch with host and path (this will fail if other process |
| 5775 | ** has a shared lock already), if the host id matches, use the big |
| 5776 | ** stick. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5777 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5778 | futimes(conchFile->h, NULL); |
| 5779 | if( hostIdMatch && !createConch ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5780 | if( conchFile->pInode && conchFile->pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5781 | /* We are trying for an exclusive lock but another thread in this |
| 5782 | ** same process is still holding a shared lock. */ |
| 5783 | rc = SQLITE_BUSY; |
| 5784 | } else { |
| 5785 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5786 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5787 | }else{ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5788 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5789 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5790 | if( rc==SQLITE_OK ){ |
| 5791 | char writeBuffer[PROXY_MAXCONCHLEN]; |
| 5792 | int writeSize = 0; |
| 5793 | |
| 5794 | writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 5795 | memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 5796 | if( pCtx->lockProxyPath!=NULL ){ |
| 5797 | strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN); |
| 5798 | }else{ |
| 5799 | strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 5800 | } |
| 5801 | writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 5802 | robust_ftruncate(conchFile->h, writeSize); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5803 | rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
| 5804 | fsync(conchFile->h); |
| 5805 | /* If we created a new conch file (not just updated the contents of a |
| 5806 | ** valid conch file), try to match the permissions of the database |
| 5807 | */ |
| 5808 | if( rc==SQLITE_OK && createConch ){ |
| 5809 | struct stat buf; |
| 5810 | int err = fstat(pFile->h, &buf); |
| 5811 | if( err==0 ){ |
| 5812 | mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 5813 | S_IROTH|S_IWOTH); |
| 5814 | /* try to match the database file R/W permissions, ignore failure */ |
| 5815 | #ifndef SQLITE_PROXY_DEBUG |
| 5816 | fchmod(conchFile->h, cmode); |
| 5817 | #else |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 5818 | do{ |
| 5819 | rc = fchmod(conchFile->h, cmode); |
| 5820 | }while( rc==(-1) && errno==EINTR ); |
| 5821 | if( rc!=0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5822 | int code = errno; |
| 5823 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 5824 | cmode, code, strerror(code)); |
| 5825 | } else { |
| 5826 | fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 5827 | } |
| 5828 | }else{ |
| 5829 | int code = errno; |
| 5830 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 5831 | err, code, strerror(code)); |
| 5832 | #endif |
| 5833 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5834 | } |
| 5835 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5836 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 5837 | |
| 5838 | end_takeconch: |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5839 | OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5840 | if( rc==SQLITE_OK && pFile->openFlags ){ |
| 5841 | if( pFile->h>=0 ){ |
drh | e84009f | 2011-03-02 17:54:32 +0000 | [diff] [blame] | 5842 | robust_close(pFile, pFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5843 | } |
| 5844 | pFile->h = -1; |
| 5845 | int fd = open(pCtx->dbPath, pFile->openFlags, |
| 5846 | SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5847 | OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5848 | if( fd>=0 ){ |
| 5849 | pFile->h = fd; |
| 5850 | }else{ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 5851 | rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5852 | during locking */ |
| 5853 | } |
| 5854 | } |
| 5855 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 5856 | char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 5857 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 5858 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 5859 | /* we couldn't create the proxy lock file with the old lock file path |
| 5860 | ** so try again via auto-naming |
| 5861 | */ |
| 5862 | forceNewLockPath = 1; |
| 5863 | tryOldLockPath = 0; |
dan | 2b0ef47 | 2010-02-16 12:18:47 +0000 | [diff] [blame] | 5864 | continue; /* go back to the do {} while start point, try again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5865 | } |
| 5866 | } |
| 5867 | if( rc==SQLITE_OK ){ |
| 5868 | /* Need to make a copy of path if we extracted the value |
| 5869 | ** from the conch file or the path was allocated on the stack |
| 5870 | */ |
| 5871 | if( tempLockPath ){ |
| 5872 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 5873 | if( !pCtx->lockProxyPath ){ |
| 5874 | rc = SQLITE_NOMEM; |
| 5875 | } |
| 5876 | } |
| 5877 | } |
| 5878 | if( rc==SQLITE_OK ){ |
| 5879 | pCtx->conchHeld = 1; |
| 5880 | |
| 5881 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 5882 | afpLockingContext *afpCtx; |
| 5883 | afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 5884 | afpCtx->dbPath = pCtx->lockProxyPath; |
| 5885 | } |
| 5886 | } else { |
| 5887 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 5888 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5889 | OSTRACE(("TAKECONCH %d %s\n", conchFile->h, |
| 5890 | rc==SQLITE_OK?"ok":"failed")); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5891 | return rc; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5892 | } while (1); /* in case we need to retry the :auto: lock file - |
| 5893 | ** we should never get here except via the 'continue' call. */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5894 | } |
| 5895 | } |
| 5896 | |
| 5897 | /* |
| 5898 | ** If pFile holds a lock on a conch file, then release that lock. |
| 5899 | */ |
| 5900 | static int proxyReleaseConch(unixFile *pFile){ |
drh | 1c5bb4d | 2010-05-10 17:29:28 +0000 | [diff] [blame] | 5901 | int rc = SQLITE_OK; /* Subroutine return code */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5902 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 5903 | unixFile *conchFile; /* Name of the conch file */ |
| 5904 | |
| 5905 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5906 | conchFile = pCtx->conchFile; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5907 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5908 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5909 | getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5910 | if( pCtx->conchHeld>0 ){ |
| 5911 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 5912 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5913 | pCtx->conchHeld = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5914 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| 5915 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5916 | return rc; |
| 5917 | } |
| 5918 | |
| 5919 | /* |
| 5920 | ** Given the name of a database file, compute the name of its conch file. |
| 5921 | ** Store the conch filename in memory obtained from sqlite3_malloc(). |
| 5922 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 5923 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 5924 | ** |
| 5925 | ** The caller is responsible for ensuring that the allocated memory |
| 5926 | ** space is eventually freed. |
| 5927 | ** |
| 5928 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 5929 | */ |
| 5930 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 5931 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5932 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5933 | char *conchPath; /* buffer in which to construct conch name */ |
| 5934 | |
| 5935 | /* Allocate space for the conch filename and initialize the name to |
| 5936 | ** the name of the original database file. */ |
| 5937 | *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8); |
| 5938 | if( conchPath==0 ){ |
| 5939 | return SQLITE_NOMEM; |
| 5940 | } |
| 5941 | memcpy(conchPath, dbPath, len+1); |
| 5942 | |
| 5943 | /* now insert a "." before the last / character */ |
| 5944 | for( i=(len-1); i>=0; i-- ){ |
| 5945 | if( conchPath[i]=='/' ){ |
| 5946 | i++; |
| 5947 | break; |
| 5948 | } |
| 5949 | } |
| 5950 | conchPath[i]='.'; |
| 5951 | while ( i<len ){ |
| 5952 | conchPath[i+1]=dbPath[i]; |
| 5953 | i++; |
| 5954 | } |
| 5955 | |
| 5956 | /* append the "-conch" suffix to the file */ |
| 5957 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5958 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5959 | |
| 5960 | return SQLITE_OK; |
| 5961 | } |
| 5962 | |
| 5963 | |
| 5964 | /* Takes a fully configured proxy locking-style unix file and switches |
| 5965 | ** the local lock file path |
| 5966 | */ |
| 5967 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 5968 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 5969 | char *oldPath = pCtx->lockProxyPath; |
| 5970 | int rc = SQLITE_OK; |
| 5971 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5972 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5973 | return SQLITE_BUSY; |
| 5974 | } |
| 5975 | |
| 5976 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 5977 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 5978 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 5979 | return SQLITE_OK; |
| 5980 | }else{ |
| 5981 | unixFile *lockProxy = pCtx->lockProxy; |
| 5982 | pCtx->lockProxy=NULL; |
| 5983 | pCtx->conchHeld = 0; |
| 5984 | if( lockProxy!=NULL ){ |
| 5985 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 5986 | if( rc ) return rc; |
| 5987 | sqlite3_free(lockProxy); |
| 5988 | } |
| 5989 | sqlite3_free(oldPath); |
| 5990 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 5991 | } |
| 5992 | |
| 5993 | return rc; |
| 5994 | } |
| 5995 | |
| 5996 | /* |
| 5997 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 5998 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 5999 | ** |
| 6000 | ** This routine find the filename associated with pFile and writes it |
| 6001 | ** int dbPath. |
| 6002 | */ |
| 6003 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6004 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6005 | if( pFile->pMethod == &afpIoMethods ){ |
| 6006 | /* afp style keeps a reference to the db path in the filePath field |
| 6007 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6008 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6009 | strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN); |
| 6010 | } else |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6011 | #endif |
| 6012 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 6013 | /* dot lock style uses the locking context to store the dot lock |
| 6014 | ** file path */ |
| 6015 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 6016 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 6017 | }else{ |
| 6018 | /* all other styles use the locking context to store the db file path */ |
| 6019 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6020 | strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6021 | } |
| 6022 | return SQLITE_OK; |
| 6023 | } |
| 6024 | |
| 6025 | /* |
| 6026 | ** Takes an already filled in unix file and alters it so all file locking |
| 6027 | ** will be performed on the local proxy lock file. The following fields |
| 6028 | ** are preserved in the locking context so that they can be restored and |
| 6029 | ** the unix structure properly cleaned up at close time: |
| 6030 | ** ->lockingContext |
| 6031 | ** ->pMethod |
| 6032 | */ |
| 6033 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 6034 | proxyLockingContext *pCtx; |
| 6035 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 6036 | char *lockPath=NULL; |
| 6037 | int rc = SQLITE_OK; |
| 6038 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6039 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6040 | return SQLITE_BUSY; |
| 6041 | } |
| 6042 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 6043 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 6044 | lockPath=NULL; |
| 6045 | }else{ |
| 6046 | lockPath=(char *)path; |
| 6047 | } |
| 6048 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6049 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
| 6050 | (lockPath ? lockPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6051 | |
| 6052 | pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 6053 | if( pCtx==0 ){ |
| 6054 | return SQLITE_NOMEM; |
| 6055 | } |
| 6056 | memset(pCtx, 0, sizeof(*pCtx)); |
| 6057 | |
| 6058 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 6059 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6060 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 6061 | if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 6062 | /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 6063 | ** (c) the file system is read-only, then enable no-locking access. |
| 6064 | ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 6065 | ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 6066 | */ |
| 6067 | struct statfs fsInfo; |
| 6068 | struct stat conchInfo; |
| 6069 | int goLockless = 0; |
| 6070 | |
| 6071 | if( stat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
| 6072 | int err = errno; |
| 6073 | if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 6074 | goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 6075 | } |
| 6076 | } |
| 6077 | if( goLockless ){ |
| 6078 | pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 6079 | rc = SQLITE_OK; |
| 6080 | } |
| 6081 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6082 | } |
| 6083 | if( rc==SQLITE_OK && lockPath ){ |
| 6084 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 6085 | } |
| 6086 | |
| 6087 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6088 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 6089 | if( pCtx->dbPath==NULL ){ |
| 6090 | rc = SQLITE_NOMEM; |
| 6091 | } |
| 6092 | } |
| 6093 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6094 | /* all memory is allocated, proxys are created and assigned, |
| 6095 | ** switch the locking context and pMethod then return. |
| 6096 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6097 | pCtx->oldLockingContext = pFile->lockingContext; |
| 6098 | pFile->lockingContext = pCtx; |
| 6099 | pCtx->pOldMethod = pFile->pMethod; |
| 6100 | pFile->pMethod = &proxyIoMethods; |
| 6101 | }else{ |
| 6102 | if( pCtx->conchFile ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6103 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6104 | sqlite3_free(pCtx->conchFile); |
| 6105 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 6106 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6107 | sqlite3_free(pCtx->conchFilePath); |
| 6108 | sqlite3_free(pCtx); |
| 6109 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6110 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 6111 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6112 | return rc; |
| 6113 | } |
| 6114 | |
| 6115 | |
| 6116 | /* |
| 6117 | ** This routine handles sqlite3_file_control() calls that are specific |
| 6118 | ** to proxy locking. |
| 6119 | */ |
| 6120 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 6121 | switch( op ){ |
| 6122 | case SQLITE_GET_LOCKPROXYFILE: { |
| 6123 | unixFile *pFile = (unixFile*)id; |
| 6124 | if( pFile->pMethod == &proxyIoMethods ){ |
| 6125 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 6126 | proxyTakeConch(pFile); |
| 6127 | if( pCtx->lockProxyPath ){ |
| 6128 | *(const char **)pArg = pCtx->lockProxyPath; |
| 6129 | }else{ |
| 6130 | *(const char **)pArg = ":auto: (not held)"; |
| 6131 | } |
| 6132 | } else { |
| 6133 | *(const char **)pArg = NULL; |
| 6134 | } |
| 6135 | return SQLITE_OK; |
| 6136 | } |
| 6137 | case SQLITE_SET_LOCKPROXYFILE: { |
| 6138 | unixFile *pFile = (unixFile*)id; |
| 6139 | int rc = SQLITE_OK; |
| 6140 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 6141 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 6142 | if( isProxyStyle ){ |
| 6143 | /* turn off proxy locking - not supported */ |
| 6144 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 6145 | }else{ |
| 6146 | /* turn off proxy locking - already off - NOOP */ |
| 6147 | rc = SQLITE_OK; |
| 6148 | } |
| 6149 | }else{ |
| 6150 | const char *proxyPath = (const char *)pArg; |
| 6151 | if( isProxyStyle ){ |
| 6152 | proxyLockingContext *pCtx = |
| 6153 | (proxyLockingContext*)pFile->lockingContext; |
| 6154 | if( !strcmp(pArg, ":auto:") |
| 6155 | || (pCtx->lockProxyPath && |
| 6156 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 6157 | ){ |
| 6158 | rc = SQLITE_OK; |
| 6159 | }else{ |
| 6160 | rc = switchLockProxyPath(pFile, proxyPath); |
| 6161 | } |
| 6162 | }else{ |
| 6163 | /* turn on proxy file locking */ |
| 6164 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 6165 | } |
| 6166 | } |
| 6167 | return rc; |
| 6168 | } |
| 6169 | default: { |
| 6170 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 6171 | } |
| 6172 | } |
| 6173 | /*NOTREACHED*/ |
| 6174 | return SQLITE_ERROR; |
| 6175 | } |
| 6176 | |
| 6177 | /* |
| 6178 | ** Within this division (the proxying locking implementation) the procedures |
| 6179 | ** above this point are all utilities. The lock-related methods of the |
| 6180 | ** proxy-locking sqlite3_io_method object follow. |
| 6181 | */ |
| 6182 | |
| 6183 | |
| 6184 | /* |
| 6185 | ** This routine checks if there is a RESERVED lock held on the specified |
| 6186 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 6187 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 6188 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 6189 | */ |
| 6190 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 6191 | unixFile *pFile = (unixFile*)id; |
| 6192 | int rc = proxyTakeConch(pFile); |
| 6193 | if( rc==SQLITE_OK ){ |
| 6194 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6195 | if( pCtx->conchHeld>0 ){ |
| 6196 | unixFile *proxy = pCtx->lockProxy; |
| 6197 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 6198 | }else{ /* conchHeld < 0 is lockless */ |
| 6199 | pResOut=0; |
| 6200 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6201 | } |
| 6202 | return rc; |
| 6203 | } |
| 6204 | |
| 6205 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6206 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6207 | ** of the following: |
| 6208 | ** |
| 6209 | ** (1) SHARED_LOCK |
| 6210 | ** (2) RESERVED_LOCK |
| 6211 | ** (3) PENDING_LOCK |
| 6212 | ** (4) EXCLUSIVE_LOCK |
| 6213 | ** |
| 6214 | ** Sometimes when requesting one lock state, additional lock states |
| 6215 | ** are inserted in between. The locking might fail on one of the later |
| 6216 | ** transitions leaving the lock state different from what it started but |
| 6217 | ** still short of its goal. The following chart shows the allowed |
| 6218 | ** transitions and the inserted intermediate states: |
| 6219 | ** |
| 6220 | ** UNLOCKED -> SHARED |
| 6221 | ** SHARED -> RESERVED |
| 6222 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 6223 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 6224 | ** PENDING -> EXCLUSIVE |
| 6225 | ** |
| 6226 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 6227 | ** routine to lower a locking level. |
| 6228 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6229 | static int proxyLock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6230 | unixFile *pFile = (unixFile*)id; |
| 6231 | int rc = proxyTakeConch(pFile); |
| 6232 | if( rc==SQLITE_OK ){ |
| 6233 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6234 | if( pCtx->conchHeld>0 ){ |
| 6235 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6236 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); |
| 6237 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6238 | }else{ |
| 6239 | /* conchHeld < 0 is lockless */ |
| 6240 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6241 | } |
| 6242 | return rc; |
| 6243 | } |
| 6244 | |
| 6245 | |
| 6246 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6247 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6248 | ** must be either NO_LOCK or SHARED_LOCK. |
| 6249 | ** |
| 6250 | ** If the locking level of the file descriptor is already at or below |
| 6251 | ** the requested locking level, this routine is a no-op. |
| 6252 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6253 | static int proxyUnlock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6254 | unixFile *pFile = (unixFile*)id; |
| 6255 | int rc = proxyTakeConch(pFile); |
| 6256 | if( rc==SQLITE_OK ){ |
| 6257 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6258 | if( pCtx->conchHeld>0 ){ |
| 6259 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6260 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); |
| 6261 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6262 | }else{ |
| 6263 | /* conchHeld < 0 is lockless */ |
| 6264 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6265 | } |
| 6266 | return rc; |
| 6267 | } |
| 6268 | |
| 6269 | /* |
| 6270 | ** Close a file that uses proxy locks. |
| 6271 | */ |
| 6272 | static int proxyClose(sqlite3_file *id) { |
| 6273 | if( id ){ |
| 6274 | unixFile *pFile = (unixFile*)id; |
| 6275 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6276 | unixFile *lockProxy = pCtx->lockProxy; |
| 6277 | unixFile *conchFile = pCtx->conchFile; |
| 6278 | int rc = SQLITE_OK; |
| 6279 | |
| 6280 | if( lockProxy ){ |
| 6281 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 6282 | if( rc ) return rc; |
| 6283 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 6284 | if( rc ) return rc; |
| 6285 | sqlite3_free(lockProxy); |
| 6286 | pCtx->lockProxy = 0; |
| 6287 | } |
| 6288 | if( conchFile ){ |
| 6289 | if( pCtx->conchHeld ){ |
| 6290 | rc = proxyReleaseConch(pFile); |
| 6291 | if( rc ) return rc; |
| 6292 | } |
| 6293 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 6294 | if( rc ) return rc; |
| 6295 | sqlite3_free(conchFile); |
| 6296 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 6297 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6298 | sqlite3_free(pCtx->conchFilePath); |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 6299 | sqlite3DbFree(0, pCtx->dbPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6300 | /* restore the original locking context and pMethod then close it */ |
| 6301 | pFile->lockingContext = pCtx->oldLockingContext; |
| 6302 | pFile->pMethod = pCtx->pOldMethod; |
| 6303 | sqlite3_free(pCtx); |
| 6304 | return pFile->pMethod->xClose(id); |
| 6305 | } |
| 6306 | return SQLITE_OK; |
| 6307 | } |
| 6308 | |
| 6309 | |
| 6310 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6311 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6312 | /* |
| 6313 | ** The proxy locking style is intended for use with AFP filesystems. |
| 6314 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 6315 | ** restricted to MacOSX. |
| 6316 | ** |
| 6317 | ** |
| 6318 | ******************* End of the proxy lock implementation ********************** |
| 6319 | ******************************************************************************/ |
| 6320 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6321 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6322 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6323 | ** |
| 6324 | ** This routine registers all VFS implementations for unix-like operating |
| 6325 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 6326 | ** should be the only routines in this file that are visible from other |
| 6327 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6328 | ** |
| 6329 | ** This routine is called once during SQLite initialization and by a |
| 6330 | ** single thread. The memory allocation and mutex subsystems have not |
| 6331 | ** necessarily been initialized when this routine is called, and so they |
| 6332 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6333 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6334 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6335 | /* |
| 6336 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6337 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 6338 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 6339 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 6340 | ** and so we have to go through the intermediate pointer to avoid problems |
| 6341 | ** when compiling with -pedantic-errors on GCC.) |
| 6342 | ** |
| 6343 | ** 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] | 6344 | ** finder-function. The finder-function returns a pointer to the |
| 6345 | ** sqlite_io_methods object that implements the desired locking |
| 6346 | ** behaviors. See the division above that contains the IOMETHODS |
| 6347 | ** macro for addition information on finder-functions. |
| 6348 | ** |
| 6349 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 6350 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 6351 | ** more than that; it looks at the filesystem type that hosts the |
| 6352 | ** database file and tries to choose an locking method appropriate for |
| 6353 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6354 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6355 | #define UNIXVFS(VFSNAME, FINDER) { \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6356 | 2, /* iVersion */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6357 | sizeof(unixFile), /* szOsFile */ \ |
| 6358 | MAX_PATHNAME, /* mxPathname */ \ |
| 6359 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6360 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6361 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6362 | unixOpen, /* xOpen */ \ |
| 6363 | unixDelete, /* xDelete */ \ |
| 6364 | unixAccess, /* xAccess */ \ |
| 6365 | unixFullPathname, /* xFullPathname */ \ |
| 6366 | unixDlOpen, /* xDlOpen */ \ |
| 6367 | unixDlError, /* xDlError */ \ |
| 6368 | unixDlSym, /* xDlSym */ \ |
| 6369 | unixDlClose, /* xDlClose */ \ |
| 6370 | unixRandomness, /* xRandomness */ \ |
| 6371 | unixSleep, /* xSleep */ \ |
| 6372 | unixCurrentTime, /* xCurrentTime */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6373 | unixGetLastError, /* xGetLastError */ \ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6374 | unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6375 | } |
| 6376 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6377 | /* |
| 6378 | ** All default VFSes for unix are contained in the following array. |
| 6379 | ** |
| 6380 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 6381 | ** by the SQLite core when the VFS is registered. So the following |
| 6382 | ** array cannot be const. |
| 6383 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6384 | static sqlite3_vfs aVfs[] = { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6385 | #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__)) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6386 | UNIXVFS("unix", autolockIoFinder ), |
| 6387 | #else |
| 6388 | UNIXVFS("unix", posixIoFinder ), |
| 6389 | #endif |
| 6390 | UNIXVFS("unix-none", nolockIoFinder ), |
| 6391 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6392 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6393 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6394 | #endif |
| 6395 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6396 | UNIXVFS("unix-posix", posixIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6397 | #if !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6398 | UNIXVFS("unix-flock", flockIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6399 | #endif |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6400 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6401 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6402 | UNIXVFS("unix-afp", afpIoFinder ), |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6403 | UNIXVFS("unix-nfs", nfsIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6404 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6405 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6406 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6407 | unsigned int i; /* Loop counter */ |
| 6408 | |
| 6409 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6410 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6411 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6412 | } |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6413 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6414 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6415 | |
| 6416 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6417 | ** Shutdown the operating system interface. |
| 6418 | ** |
| 6419 | ** Some operating systems might need to do some cleanup in this routine, |
| 6420 | ** to release dynamically allocated objects. But not on unix. |
| 6421 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6422 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6423 | int sqlite3_os_end(void){ |
| 6424 | return SQLITE_OK; |
| 6425 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 6426 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 6427 | #endif /* SQLITE_OS_UNIX */ |