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 */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 209 | unsigned char ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 210 | int lastErrno; /* The unix errno from last I/O error */ |
| 211 | void *lockingContext; /* Locking style specific state */ |
| 212 | UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 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 | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 247 | ** Allowed values for the unixFile.ctrlFlags bitmask: |
| 248 | */ |
| 249 | #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 250 | #define UNIXFILE_RDONLY 0x02 /* Connection is read only */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 251 | |
| 252 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 253 | ** Include code that is common to all os_*.c files |
| 254 | */ |
| 255 | #include "os_common.h" |
| 256 | |
| 257 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 258 | ** Define various macros that are missing from some systems. |
| 259 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 260 | #ifndef O_LARGEFILE |
| 261 | # define O_LARGEFILE 0 |
| 262 | #endif |
| 263 | #ifdef SQLITE_DISABLE_LFS |
| 264 | # undef O_LARGEFILE |
| 265 | # define O_LARGEFILE 0 |
| 266 | #endif |
| 267 | #ifndef O_NOFOLLOW |
| 268 | # define O_NOFOLLOW 0 |
| 269 | #endif |
| 270 | #ifndef O_BINARY |
| 271 | # define O_BINARY 0 |
| 272 | #endif |
| 273 | |
| 274 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 275 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 276 | ** testing and debugging only. |
| 277 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 278 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 279 | #define threadid pthread_self() |
| 280 | #else |
| 281 | #define threadid 0 |
| 282 | #endif |
| 283 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 284 | /* |
| 285 | ** Many system calls are accessed through pointer-to-functions so that |
| 286 | ** they may be overridden at runtime to facilitate fault injection during |
| 287 | ** testing and sandboxing. The following array holds the names and pointers |
| 288 | ** to all overrideable system calls. |
| 289 | */ |
| 290 | static struct unix_syscall { |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 291 | const char *zName; /* Name of the sytem call */ |
| 292 | sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ |
| 293 | sqlite3_syscall_ptr pDefault; /* Default value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 294 | } aSyscall[] = { |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 295 | { "open", (sqlite3_syscall_ptr)open, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 296 | #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent) |
| 297 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 298 | { "close", (sqlite3_syscall_ptr)close, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 299 | #define osClose ((int(*)(int))aSyscall[1].pCurrent) |
| 300 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 301 | { "access", (sqlite3_syscall_ptr)access, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 302 | #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent) |
| 303 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 304 | { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 305 | #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent) |
| 306 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 307 | { "stat", (sqlite3_syscall_ptr)stat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 308 | #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) |
| 309 | |
| 310 | /* |
| 311 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 312 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 313 | ** that always succeeds. This means that locking does not occur under |
| 314 | ** DJGPP. But it is DOS - what did you expect? |
| 315 | */ |
| 316 | #ifdef __DJGPP__ |
| 317 | { "fstat", 0, 0 }, |
| 318 | #define osFstat(a,b,c) 0 |
| 319 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 320 | { "fstat", (sqlite3_syscall_ptr)fstat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 321 | #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent) |
| 322 | #endif |
| 323 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 324 | { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 325 | #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent) |
| 326 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 327 | { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 328 | #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 329 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 330 | { "read", (sqlite3_syscall_ptr)read, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 331 | #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) |
| 332 | |
| 333 | #if defined(USE_PREAD) || defined(SQLITE_ENABLE_LOCKING_STYLE) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 334 | { "pread", (sqlite3_syscall_ptr)pread, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 335 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 336 | { "pread", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 337 | #endif |
| 338 | #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) |
| 339 | |
| 340 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 341 | { "pread64", (sqlite3_syscall_ptr)pread64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 342 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 343 | { "pread64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 344 | #endif |
| 345 | #define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent) |
| 346 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 347 | { "write", (sqlite3_syscall_ptr)write, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 348 | #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) |
| 349 | |
| 350 | #if defined(USE_PREAD) || defined(SQLITE_ENABLE_LOCKING_STYLE) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 351 | { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 352 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 353 | { "pwrite", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 354 | #endif |
| 355 | #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 356 | aSyscall[12].pCurrent) |
| 357 | |
| 358 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 359 | { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 360 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 361 | { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 362 | #endif |
| 363 | #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 364 | aSyscall[13].pCurrent) |
| 365 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 366 | { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 367 | #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) |
| 368 | |
| 369 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 370 | { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 371 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 372 | { "fallocate", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 373 | #endif |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 374 | #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 375 | |
| 376 | }; /* End of the overrideable system calls */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 377 | |
| 378 | /* |
| 379 | ** This is the xSetSystemCall() method of sqlite3_vfs for all of the |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 380 | ** "unix" VFSes. Return SQLITE_OK opon successfully updating the |
| 381 | ** system call pointer, or SQLITE_NOTFOUND if there is no configurable |
| 382 | ** system call named zName. |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 383 | */ |
| 384 | static int unixSetSystemCall( |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 385 | sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ |
| 386 | const char *zName, /* Name of system call to override */ |
| 387 | sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 388 | ){ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 389 | unsigned int i; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 390 | int rc = SQLITE_NOTFOUND; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 391 | |
| 392 | UNUSED_PARAMETER(pNotUsed); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 393 | if( zName==0 ){ |
| 394 | /* If no zName is given, restore all system calls to their default |
| 395 | ** settings and return NULL |
| 396 | */ |
| 397 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 398 | if( aSyscall[i].pDefault ){ |
| 399 | aSyscall[i].pCurrent = aSyscall[i].pDefault; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 400 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | }else{ |
| 404 | /* If zName is specified, operate on only the one system call |
| 405 | ** specified. |
| 406 | */ |
| 407 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 408 | if( strcmp(zName, aSyscall[i].zName)==0 ){ |
| 409 | if( aSyscall[i].pDefault==0 ){ |
| 410 | aSyscall[i].pDefault = aSyscall[i].pCurrent; |
| 411 | } |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 412 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 413 | if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault; |
| 414 | aSyscall[i].pCurrent = pNewFunc; |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | return rc; |
| 420 | } |
| 421 | |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 422 | /* |
| 423 | ** Return the value of a system call. Return NULL if zName is not a |
| 424 | ** recognized system call name. NULL is also returned if the system call |
| 425 | ** is currently undefined. |
| 426 | */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 427 | static sqlite3_syscall_ptr unixGetSystemCall( |
| 428 | sqlite3_vfs *pNotUsed, |
| 429 | const char *zName |
| 430 | ){ |
| 431 | unsigned int i; |
| 432 | |
| 433 | UNUSED_PARAMETER(pNotUsed); |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 434 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 435 | if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent; |
| 436 | } |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | ** Return the name of the first system call after zName. If zName==NULL |
| 442 | ** then return the name of the first system call. Return NULL if zName |
| 443 | ** is the last system call or if zName is not the name of a valid |
| 444 | ** system call. |
| 445 | */ |
| 446 | static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){ |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 447 | int i = -1; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 448 | |
| 449 | UNUSED_PARAMETER(p); |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 450 | if( zName ){ |
| 451 | for(i=0; i<ArraySize(aSyscall)-1; i++){ |
| 452 | if( strcmp(zName, aSyscall[i].zName)==0 ) break; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 455 | for(i++; i<ArraySize(aSyscall); i++){ |
| 456 | if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 457 | } |
| 458 | return 0; |
| 459 | } |
| 460 | |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 461 | /* |
| 462 | ** Retry open() calls that fail due to EINTR |
| 463 | */ |
| 464 | static int robust_open(const char *z, int f, int m){ |
| 465 | int rc; |
| 466 | do{ rc = osOpen(z,f,m); }while( rc<0 && errno==EINTR ); |
| 467 | return rc; |
| 468 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 469 | |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 470 | /* |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 471 | ** Helper functions to obtain and relinquish the global mutex. The |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 472 | ** global mutex is used to protect the unixInodeInfo and |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 473 | ** vxworksFileId objects used by this file, all of which may be |
| 474 | ** shared by multiple threads. |
| 475 | ** |
| 476 | ** Function unixMutexHeld() is used to assert() that the global mutex |
| 477 | ** is held when required. This function is only used as part of assert() |
| 478 | ** statements. e.g. |
| 479 | ** |
| 480 | ** unixEnterMutex() |
| 481 | ** assert( unixMutexHeld() ); |
| 482 | ** unixEnterLeave() |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 483 | */ |
| 484 | static void unixEnterMutex(void){ |
| 485 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 486 | } |
| 487 | static void unixLeaveMutex(void){ |
| 488 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 489 | } |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 490 | #ifdef SQLITE_DEBUG |
| 491 | static int unixMutexHeld(void) { |
| 492 | return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 493 | } |
| 494 | #endif |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 495 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 496 | |
| 497 | #ifdef SQLITE_DEBUG |
| 498 | /* |
| 499 | ** Helper function for printing out trace information from debugging |
| 500 | ** binaries. This returns the string represetation of the supplied |
| 501 | ** integer lock-type. |
| 502 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 503 | static const char *azFileLock(int eFileLock){ |
| 504 | switch( eFileLock ){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 505 | case NO_LOCK: return "NONE"; |
| 506 | case SHARED_LOCK: return "SHARED"; |
| 507 | case RESERVED_LOCK: return "RESERVED"; |
| 508 | case PENDING_LOCK: return "PENDING"; |
| 509 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 510 | } |
| 511 | return "ERROR"; |
| 512 | } |
| 513 | #endif |
| 514 | |
| 515 | #ifdef SQLITE_LOCK_TRACE |
| 516 | /* |
| 517 | ** Print out information about all locking operations. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 518 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 519 | ** This routine is used for troubleshooting locks on multithreaded |
| 520 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 521 | ** command-line option on the compiler. This code is normally |
| 522 | ** turned off. |
| 523 | */ |
| 524 | static int lockTrace(int fd, int op, struct flock *p){ |
| 525 | char *zOpName, *zType; |
| 526 | int s; |
| 527 | int savedErrno; |
| 528 | if( op==F_GETLK ){ |
| 529 | zOpName = "GETLK"; |
| 530 | }else if( op==F_SETLK ){ |
| 531 | zOpName = "SETLK"; |
| 532 | }else{ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 533 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 534 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 535 | return s; |
| 536 | } |
| 537 | if( p->l_type==F_RDLCK ){ |
| 538 | zType = "RDLCK"; |
| 539 | }else if( p->l_type==F_WRLCK ){ |
| 540 | zType = "WRLCK"; |
| 541 | }else if( p->l_type==F_UNLCK ){ |
| 542 | zType = "UNLCK"; |
| 543 | }else{ |
| 544 | assert( 0 ); |
| 545 | } |
| 546 | assert( p->l_whence==SEEK_SET ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 547 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 548 | savedErrno = errno; |
| 549 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 550 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 551 | (int)p->l_pid, s); |
| 552 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 553 | struct flock l2; |
| 554 | l2 = *p; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 555 | osFcntl(fd, F_GETLK, &l2); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 556 | if( l2.l_type==F_RDLCK ){ |
| 557 | zType = "RDLCK"; |
| 558 | }else if( l2.l_type==F_WRLCK ){ |
| 559 | zType = "WRLCK"; |
| 560 | }else if( l2.l_type==F_UNLCK ){ |
| 561 | zType = "UNLCK"; |
| 562 | }else{ |
| 563 | assert( 0 ); |
| 564 | } |
| 565 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 566 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 567 | } |
| 568 | errno = savedErrno; |
| 569 | return s; |
| 570 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 571 | #undef osFcntl |
| 572 | #define osFcntl lockTrace |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 573 | #endif /* SQLITE_LOCK_TRACE */ |
| 574 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 575 | /* |
| 576 | ** Retry ftruncate() calls that fail due to EINTR |
| 577 | */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 578 | static int robust_ftruncate(int h, sqlite3_int64 sz){ |
| 579 | int rc; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 580 | do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR ); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 581 | return rc; |
| 582 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 583 | |
| 584 | /* |
| 585 | ** This routine translates a standard POSIX errno code into something |
| 586 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 587 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 588 | ** and a variety of "please close the file descriptor NOW" errors into |
| 589 | ** SQLITE_IOERR |
| 590 | ** |
| 591 | ** Errors during initialization of locks, or file system support for locks, |
| 592 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 593 | */ |
| 594 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
| 595 | switch (posixError) { |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 596 | #if 0 |
| 597 | /* At one point this code was not commented out. In theory, this branch |
| 598 | ** should never be hit, as this function should only be called after |
| 599 | ** a locking-related function (i.e. fcntl()) has returned non-zero with |
| 600 | ** the value of errno as the first argument. Since a system call has failed, |
| 601 | ** errno should be non-zero. |
| 602 | ** |
| 603 | ** Despite this, if errno really is zero, we still don't want to return |
| 604 | ** SQLITE_OK. The system call failed, and *some* SQLite error should be |
| 605 | ** propagated back to the caller. Commenting this branch out means errno==0 |
| 606 | ** will be handled by the "default:" case below. |
| 607 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 608 | case 0: |
| 609 | return SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 610 | #endif |
| 611 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 612 | case EAGAIN: |
| 613 | case ETIMEDOUT: |
| 614 | case EBUSY: |
| 615 | case EINTR: |
| 616 | case ENOLCK: |
| 617 | /* random NFS retry error, unless during file system support |
| 618 | * introspection, in which it actually means what it says */ |
| 619 | return SQLITE_BUSY; |
| 620 | |
| 621 | case EACCES: |
| 622 | /* EACCES is like EAGAIN during locking operations, but not any other time*/ |
| 623 | if( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
| 624 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 625 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 626 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){ |
| 627 | return SQLITE_BUSY; |
| 628 | } |
| 629 | /* else fall through */ |
| 630 | case EPERM: |
| 631 | return SQLITE_PERM; |
| 632 | |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 633 | /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And |
| 634 | ** this module never makes such a call. And the code in SQLite itself |
| 635 | ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons |
| 636 | ** this case is also commented out. If the system does set errno to EDEADLK, |
| 637 | ** the default SQLITE_IOERR_XXX code will be returned. */ |
| 638 | #if 0 |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 639 | case EDEADLK: |
| 640 | return SQLITE_IOERR_BLOCKED; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 641 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 642 | |
| 643 | #if EOPNOTSUPP!=ENOTSUP |
| 644 | case EOPNOTSUPP: |
| 645 | /* something went terribly awry, unless during file system support |
| 646 | * introspection, in which it actually means what it says */ |
| 647 | #endif |
| 648 | #ifdef ENOTSUP |
| 649 | case ENOTSUP: |
| 650 | /* invalid fd, unless during file system support introspection, in which |
| 651 | * it actually means what it says */ |
| 652 | #endif |
| 653 | case EIO: |
| 654 | case EBADF: |
| 655 | case EINVAL: |
| 656 | case ENOTCONN: |
| 657 | case ENODEV: |
| 658 | case ENXIO: |
| 659 | case ENOENT: |
| 660 | case ESTALE: |
| 661 | case ENOSYS: |
| 662 | /* these should force the client to close the file and reconnect */ |
| 663 | |
| 664 | default: |
| 665 | return sqliteIOErr; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | |
| 670 | |
| 671 | /****************************************************************************** |
| 672 | ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 673 | ** |
| 674 | ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 675 | ** the device number and the inode number. But this does not work on VxWorks. |
| 676 | ** On VxWorks, a unique file id must be based on the canonical filename. |
| 677 | ** |
| 678 | ** A pointer to an instance of the following structure can be used as a |
| 679 | ** unique file ID in VxWorks. Each instance of this structure contains |
| 680 | ** a copy of the canonical filename. There is also a reference count. |
| 681 | ** The structure is reclaimed when the number of pointers to it drops to |
| 682 | ** zero. |
| 683 | ** |
| 684 | ** There are never very many files open at one time and lookups are not |
| 685 | ** a performance-critical path, so it is sufficient to put these |
| 686 | ** structures on a linked list. |
| 687 | */ |
| 688 | struct vxworksFileId { |
| 689 | struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 690 | int nRef; /* Number of references to this one */ |
| 691 | int nName; /* Length of the zCanonicalName[] string */ |
| 692 | char *zCanonicalName; /* Canonical filename */ |
| 693 | }; |
| 694 | |
| 695 | #if OS_VXWORKS |
| 696 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 697 | ** All unique filenames are held on a linked list headed by this |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 698 | ** variable: |
| 699 | */ |
| 700 | static struct vxworksFileId *vxworksFileList = 0; |
| 701 | |
| 702 | /* |
| 703 | ** Simplify a filename into its canonical form |
| 704 | ** by making the following changes: |
| 705 | ** |
| 706 | ** * removing any trailing and duplicate / |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 707 | ** * convert /./ into just / |
| 708 | ** * convert /A/../ where A is any simple name into just / |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 709 | ** |
| 710 | ** Changes are made in-place. Return the new name length. |
| 711 | ** |
| 712 | ** The original filename is in z[0..n-1]. Return the number of |
| 713 | ** characters in the simplified name. |
| 714 | */ |
| 715 | static int vxworksSimplifyName(char *z, int n){ |
| 716 | int i, j; |
| 717 | while( n>1 && z[n-1]=='/' ){ n--; } |
| 718 | for(i=j=0; i<n; i++){ |
| 719 | if( z[i]=='/' ){ |
| 720 | if( z[i+1]=='/' ) continue; |
| 721 | if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 722 | i += 1; |
| 723 | continue; |
| 724 | } |
| 725 | if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 726 | while( j>0 && z[j-1]!='/' ){ j--; } |
| 727 | if( j>0 ){ j--; } |
| 728 | i += 2; |
| 729 | continue; |
| 730 | } |
| 731 | } |
| 732 | z[j++] = z[i]; |
| 733 | } |
| 734 | z[j] = 0; |
| 735 | return j; |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | ** Find a unique file ID for the given absolute pathname. Return |
| 740 | ** a pointer to the vxworksFileId object. This pointer is the unique |
| 741 | ** file ID. |
| 742 | ** |
| 743 | ** The nRef field of the vxworksFileId object is incremented before |
| 744 | ** the object is returned. A new vxworksFileId object is created |
| 745 | ** and added to the global list if necessary. |
| 746 | ** |
| 747 | ** If a memory allocation error occurs, return NULL. |
| 748 | */ |
| 749 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 750 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 751 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 752 | int n; /* Length of zAbsoluteName string */ |
| 753 | |
| 754 | assert( zAbsoluteName[0]=='/' ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 755 | n = (int)strlen(zAbsoluteName); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 756 | pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) ); |
| 757 | if( pNew==0 ) return 0; |
| 758 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 759 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 760 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 761 | |
| 762 | /* Search for an existing entry that matching the canonical name. |
| 763 | ** If found, increment the reference count and return a pointer to |
| 764 | ** the existing file ID. |
| 765 | */ |
| 766 | unixEnterMutex(); |
| 767 | for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 768 | if( pCandidate->nName==n |
| 769 | && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 770 | ){ |
| 771 | sqlite3_free(pNew); |
| 772 | pCandidate->nRef++; |
| 773 | unixLeaveMutex(); |
| 774 | return pCandidate; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | /* No match was found. We will make a new file ID */ |
| 779 | pNew->nRef = 1; |
| 780 | pNew->nName = n; |
| 781 | pNew->pNext = vxworksFileList; |
| 782 | vxworksFileList = pNew; |
| 783 | unixLeaveMutex(); |
| 784 | return pNew; |
| 785 | } |
| 786 | |
| 787 | /* |
| 788 | ** Decrement the reference count on a vxworksFileId object. Free |
| 789 | ** the object when the reference count reaches zero. |
| 790 | */ |
| 791 | static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 792 | unixEnterMutex(); |
| 793 | assert( pId->nRef>0 ); |
| 794 | pId->nRef--; |
| 795 | if( pId->nRef==0 ){ |
| 796 | struct vxworksFileId **pp; |
| 797 | for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 798 | assert( *pp==pId ); |
| 799 | *pp = pId->pNext; |
| 800 | sqlite3_free(pId); |
| 801 | } |
| 802 | unixLeaveMutex(); |
| 803 | } |
| 804 | #endif /* OS_VXWORKS */ |
| 805 | /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 806 | ******************************************************************************/ |
| 807 | |
| 808 | |
| 809 | /****************************************************************************** |
| 810 | *************************** Posix Advisory Locking **************************** |
| 811 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 812 | ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 813 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 814 | ** sets or clears a lock, that operation overrides any prior locks set |
| 815 | ** by the same process. It does not explicitly say so, but this implies |
| 816 | ** that it overrides locks set by the same process using a different |
| 817 | ** file descriptor. Consider this test case: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 818 | ** |
| 819 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 820 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 821 | ** |
| 822 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 823 | ** one is a hard or symbolic link to the other) then if you set |
| 824 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 825 | ** on fd2, it works. I would have expected the second lock to |
| 826 | ** fail since there was already a lock on the file due to fd1. |
| 827 | ** But not so. Since both locks came from the same process, the |
| 828 | ** second overrides the first, even though they were on different |
| 829 | ** file descriptors opened on different file names. |
| 830 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 831 | ** This means that we cannot use POSIX locks to synchronize file access |
| 832 | ** among competing threads of the same process. POSIX locks will work fine |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 833 | ** to synchronize access for threads in separate processes, but not |
| 834 | ** threads within the same process. |
| 835 | ** |
| 836 | ** To work around the problem, SQLite has to manage file locks internally |
| 837 | ** on its own. Whenever a new database is opened, we have to find the |
| 838 | ** specific inode of the database file (the inode is determined by the |
| 839 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 840 | ** and check for locks already existing on that inode. When locks are |
| 841 | ** created or removed, we have to look at our own internal record of the |
| 842 | ** locks to see if another thread has previously set a lock on that same |
| 843 | ** inode. |
| 844 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 845 | ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 846 | ** For VxWorks, we have to use the alternative unique ID system based on |
| 847 | ** canonical filename and implemented in the previous division.) |
| 848 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 849 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 850 | ** descriptor. It is now a structure that holds the integer file |
| 851 | ** descriptor and a pointer to a structure that describes the internal |
| 852 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 853 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 854 | ** point to the same locking structure. The locking structure keeps |
| 855 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 856 | ** field that tells us its internal lock status. cnt==0 means the |
| 857 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 858 | ** cnt>0 means there are cnt shared locks on the file. |
| 859 | ** |
| 860 | ** Any attempt to lock or unlock a file first checks the locking |
| 861 | ** structure. The fcntl() system call is only invoked to set a |
| 862 | ** POSIX lock if the internal lock structure transitions between |
| 863 | ** a locked and an unlocked state. |
| 864 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 865 | ** But wait: there are yet more problems with POSIX advisory locks. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 866 | ** |
| 867 | ** If you close a file descriptor that points to a file that has locks, |
| 868 | ** all locks on that file that are owned by the current process are |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 869 | ** released. To work around this problem, each unixInodeInfo object |
| 870 | ** maintains a count of the number of pending locks on tha inode. |
| 871 | ** When an attempt is made to close an unixFile, if there are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 872 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 873 | ** to close() the file descriptor is deferred until all of the locks clear. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 874 | ** The unixInodeInfo structure keeps a list of file descriptors that need to |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 875 | ** be closed and that list is walked (and cleared) when the last lock |
| 876 | ** clears. |
| 877 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 878 | ** Yet another problem: LinuxThreads do not play well with posix locks. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 879 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 880 | ** Many older versions of linux use the LinuxThreads library which is |
| 881 | ** not posix compliant. Under LinuxThreads, a lock created by thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 882 | ** A cannot be modified or overridden by a different thread B. |
| 883 | ** Only thread A can modify the lock. Locking behavior is correct |
| 884 | ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 885 | ** on linux - with NPTL a lock created by thread A can override locks |
| 886 | ** in thread B. But there is no way to know at compile-time which |
| 887 | ** threading library is being used. So there is no way to know at |
| 888 | ** compile-time whether or not thread A can override locks on thread B. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 889 | ** 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] | 890 | ** current process. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 891 | ** |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 892 | ** SQLite used to support LinuxThreads. But support for LinuxThreads |
| 893 | ** was dropped beginning with version 3.7.0. SQLite will still work with |
| 894 | ** LinuxThreads provided that (1) there is no more than one connection |
| 895 | ** per database file in the same process and (2) database connections |
| 896 | ** do not move across threads. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 897 | */ |
| 898 | |
| 899 | /* |
| 900 | ** An instance of the following structure serves as the key used |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 901 | ** to locate a particular unixInodeInfo object. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 902 | */ |
| 903 | struct unixFileId { |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 904 | dev_t dev; /* Device number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 905 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 906 | struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 907 | #else |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 908 | ino_t ino; /* Inode number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 909 | #endif |
| 910 | }; |
| 911 | |
| 912 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 913 | ** An instance of the following structure is allocated for each open |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 914 | ** inode. Or, on LinuxThreads, there is one of these structures for |
| 915 | ** each inode opened by each thread. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 916 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 917 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 918 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 919 | ** object keeps a count of the number of unixFile pointing to it. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 920 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 921 | struct unixInodeInfo { |
| 922 | struct unixFileId fileId; /* The lookup key */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 923 | int nShared; /* Number of SHARED locks held */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 924 | unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
| 925 | unsigned char bProcessLock; /* An exclusive process lock is held */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 926 | int nRef; /* Number of pointers to this structure */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 927 | unixShmNode *pShmNode; /* Shared memory associated with this inode */ |
| 928 | int nLock; /* Number of outstanding file locks */ |
| 929 | UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
| 930 | unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ |
| 931 | unixInodeInfo *pPrev; /* .... doubly linked */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 932 | #if defined(SQLITE_ENABLE_LOCKING_STYLE) |
| 933 | unsigned long long sharedByte; /* for AFP simulated shared lock */ |
| 934 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 935 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 936 | sem_t *pSem; /* Named POSIX semaphore */ |
| 937 | char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 938 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 939 | }; |
| 940 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 941 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 942 | ** A lists of all unixInodeInfo objects. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 943 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 944 | static unixInodeInfo *inodeList = 0; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 945 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 946 | /* |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 947 | ** |
| 948 | ** This function - unixLogError_x(), is only ever called via the macro |
| 949 | ** unixLogError(). |
| 950 | ** |
| 951 | ** It is invoked after an error occurs in an OS function and errno has been |
| 952 | ** set. It logs a message using sqlite3_log() containing the current value of |
| 953 | ** errno and, if possible, the human-readable equivalent from strerror() or |
| 954 | ** strerror_r(). |
| 955 | ** |
| 956 | ** The first argument passed to the macro should be the error code that |
| 957 | ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). |
| 958 | ** The two subsequent arguments should be the name of the OS function that |
| 959 | ** failed (e.g. "unlink", "open") and the the associated file-system path, |
| 960 | ** if any. |
| 961 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 962 | #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__) |
| 963 | static int unixLogErrorAtLine( |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 964 | int errcode, /* SQLite error code */ |
| 965 | const char *zFunc, /* Name of OS function that failed */ |
| 966 | const char *zPath, /* File path associated with error */ |
| 967 | int iLine /* Source line number where error occurred */ |
| 968 | ){ |
| 969 | char *zErr; /* Message from strerror() or equivalent */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 970 | int iErrno = errno; /* Saved syscall error number */ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 971 | |
| 972 | /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use |
| 973 | ** the strerror() function to obtain the human-readable error message |
| 974 | ** equivalent to errno. Otherwise, use strerror_r(). |
| 975 | */ |
| 976 | #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) |
| 977 | char aErr[80]; |
| 978 | memset(aErr, 0, sizeof(aErr)); |
| 979 | zErr = aErr; |
| 980 | |
| 981 | /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined, |
| 982 | ** assume that the system provides the the GNU version of strerror_r() that |
| 983 | ** returns a pointer to a buffer containing the error message. That pointer |
| 984 | ** may point to aErr[], or it may point to some static storage somewhere. |
| 985 | ** Otherwise, assume that the system provides the POSIX version of |
| 986 | ** strerror_r(), which always writes an error message into aErr[]. |
| 987 | ** |
| 988 | ** If the code incorrectly assumes that it is the POSIX version that is |
| 989 | ** available, the error message will often be an empty string. Not a |
| 990 | ** huge problem. Incorrectly concluding that the GNU version is available |
| 991 | ** could lead to a segfault though. |
| 992 | */ |
| 993 | #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) |
| 994 | zErr = |
| 995 | # endif |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 996 | strerror_r(iErrno, aErr, sizeof(aErr)-1); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 997 | |
| 998 | #elif SQLITE_THREADSAFE |
| 999 | /* This is a threadsafe build, but strerror_r() is not available. */ |
| 1000 | zErr = ""; |
| 1001 | #else |
| 1002 | /* Non-threadsafe build, use strerror(). */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1003 | zErr = strerror(iErrno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1004 | #endif |
| 1005 | |
| 1006 | assert( errcode!=SQLITE_OK ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1007 | if( zPath==0 ) zPath = ""; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1008 | sqlite3_log(errcode, |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1009 | "os_unix.c:%d: (%d) %s(%s) - %s", |
| 1010 | iLine, iErrno, zFunc, zPath, zErr |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1011 | ); |
| 1012 | |
| 1013 | return errcode; |
| 1014 | } |
| 1015 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1016 | /* |
| 1017 | ** Close a file descriptor. |
| 1018 | ** |
| 1019 | ** We assume that close() almost always works, since it is only in a |
| 1020 | ** very sick application or on a very sick platform that it might fail. |
| 1021 | ** If it does fail, simply leak the file descriptor, but do log the |
| 1022 | ** error. |
| 1023 | ** |
| 1024 | ** Note that it is not safe to retry close() after EINTR since the |
| 1025 | ** file descriptor might have already been reused by another thread. |
| 1026 | ** So we don't even try to recover from an EINTR. Just log the error |
| 1027 | ** and move on. |
| 1028 | */ |
| 1029 | static void robust_close(unixFile *pFile, int h, int lineno){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1030 | if( osClose(h) ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1031 | unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", |
| 1032 | pFile ? pFile->zPath : 0, lineno); |
| 1033 | } |
| 1034 | } |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1035 | |
| 1036 | /* |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1037 | ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1038 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1039 | static void closePendingFds(unixFile *pFile){ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1040 | unixInodeInfo *pInode = pFile->pInode; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1041 | UnixUnusedFd *p; |
| 1042 | UnixUnusedFd *pNext; |
| 1043 | for(p=pInode->pUnused; p; p=pNext){ |
| 1044 | pNext = p->pNext; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1045 | robust_close(pFile, p->fd, __LINE__); |
| 1046 | sqlite3_free(p); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1047 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1048 | pInode->pUnused = 0; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1052 | ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1053 | ** |
| 1054 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1055 | ** when this function is called. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1056 | */ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1057 | static void releaseInodeInfo(unixFile *pFile){ |
| 1058 | unixInodeInfo *pInode = pFile->pInode; |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1059 | assert( unixMutexHeld() ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1060 | if( ALWAYS(pInode) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1061 | pInode->nRef--; |
| 1062 | if( pInode->nRef==0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1063 | assert( pInode->pShmNode==0 ); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1064 | closePendingFds(pFile); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1065 | if( pInode->pPrev ){ |
| 1066 | assert( pInode->pPrev->pNext==pInode ); |
| 1067 | pInode->pPrev->pNext = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1068 | }else{ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1069 | assert( inodeList==pInode ); |
| 1070 | inodeList = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1071 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1072 | if( pInode->pNext ){ |
| 1073 | assert( pInode->pNext->pPrev==pInode ); |
| 1074 | pInode->pNext->pPrev = pInode->pPrev; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1075 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1076 | sqlite3_free(pInode); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1077 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1082 | ** Given a file descriptor, locate the unixInodeInfo object that |
| 1083 | ** describes that file descriptor. Create a new one if necessary. The |
| 1084 | ** return value might be uninitialized if an error occurs. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1085 | ** |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1086 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1087 | ** when this function is called. |
| 1088 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1089 | ** Return an appropriate error code. |
| 1090 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1091 | static int findInodeInfo( |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1092 | unixFile *pFile, /* Unix file with file desc used in the key */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1093 | unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1094 | ){ |
| 1095 | int rc; /* System call return code */ |
| 1096 | int fd; /* The file descriptor for pFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1097 | struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ |
| 1098 | struct stat statbuf; /* Low-level file information */ |
| 1099 | unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1100 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1101 | assert( unixMutexHeld() ); |
| 1102 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1103 | /* Get low-level information about the file that we can used to |
| 1104 | ** create a unique name for the file. |
| 1105 | */ |
| 1106 | fd = pFile->h; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1107 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1108 | if( rc!=0 ){ |
| 1109 | pFile->lastErrno = errno; |
| 1110 | #ifdef EOVERFLOW |
| 1111 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 1112 | #endif |
| 1113 | return SQLITE_IOERR; |
| 1114 | } |
| 1115 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1116 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1117 | /* On OS X on an msdos filesystem, the inode number is reported |
| 1118 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 1119 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 1120 | ** we always increase the file size to 1 by writing a single byte |
| 1121 | ** prior to accessing the inode number. The one byte written is |
| 1122 | ** an ASCII 'S' character which also happens to be the first byte |
| 1123 | ** in the header of every SQLite database. In this way, if there |
| 1124 | ** is a race condition such that another thread has already populated |
| 1125 | ** the first page of the database, no damage is done. |
| 1126 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1127 | if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 1128 | do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR ); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1129 | if( rc!=1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1130 | pFile->lastErrno = errno; |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1131 | return SQLITE_IOERR; |
| 1132 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1133 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1134 | if( rc!=0 ){ |
| 1135 | pFile->lastErrno = errno; |
| 1136 | return SQLITE_IOERR; |
| 1137 | } |
| 1138 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1139 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1140 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1141 | memset(&fileId, 0, sizeof(fileId)); |
| 1142 | fileId.dev = statbuf.st_dev; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1143 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1144 | fileId.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1145 | #else |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1146 | fileId.ino = statbuf.st_ino; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1147 | #endif |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1148 | pInode = inodeList; |
| 1149 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 1150 | pInode = pInode->pNext; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1151 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1152 | if( pInode==0 ){ |
| 1153 | pInode = sqlite3_malloc( sizeof(*pInode) ); |
| 1154 | if( pInode==0 ){ |
| 1155 | return SQLITE_NOMEM; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1156 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1157 | memset(pInode, 0, sizeof(*pInode)); |
| 1158 | memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
| 1159 | pInode->nRef = 1; |
| 1160 | pInode->pNext = inodeList; |
| 1161 | pInode->pPrev = 0; |
| 1162 | if( inodeList ) inodeList->pPrev = pInode; |
| 1163 | inodeList = pInode; |
| 1164 | }else{ |
| 1165 | pInode->nRef++; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1166 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1167 | *ppInode = pInode; |
| 1168 | return SQLITE_OK; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1169 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1170 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1171 | |
| 1172 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1173 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1174 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1175 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1176 | ** 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] | 1177 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1178 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1179 | int rc = SQLITE_OK; |
| 1180 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1181 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1182 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1183 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1184 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1185 | assert( pFile ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1186 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1187 | |
| 1188 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1189 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1190 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1193 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1194 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1195 | #ifndef __DJGPP__ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1196 | if( !reserved && !pFile->pInode->bProcessLock ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1197 | struct flock lock; |
| 1198 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1199 | lock.l_start = RESERVED_BYTE; |
| 1200 | lock.l_len = 1; |
| 1201 | lock.l_type = F_WRLCK; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1202 | if( osFcntl(pFile->h, F_GETLK, &lock) ){ |
| 1203 | rc = SQLITE_IOERR_CHECKRESERVEDLOCK; |
| 1204 | pFile->lastErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1205 | } else if( lock.l_type!=F_UNLCK ){ |
| 1206 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1207 | } |
| 1208 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1209 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1210 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1211 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1212 | OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1213 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1214 | *pResOut = reserved; |
| 1215 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1219 | ** Attempt to set a system-lock on the file pFile. The lock is |
| 1220 | ** described by pLock. |
| 1221 | ** |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1222 | ** If the pFile was opened read/write from unix-excl, then the only lock |
| 1223 | ** ever obtained is an exclusive lock, and it is obtained exactly once |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1224 | ** the first time any lock is attempted. All subsequent system locking |
| 1225 | ** operations become no-ops. Locking operations still happen internally, |
| 1226 | ** in order to coordinate access between separate database connections |
| 1227 | ** within this process, but all of that is handled in memory and the |
| 1228 | ** operating system does not participate. |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1229 | ** |
| 1230 | ** This function is a pass-through to fcntl(F_SETLK) if pFile is using |
| 1231 | ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl" |
| 1232 | ** and is read-only. |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1233 | ** |
| 1234 | ** Zero is returned if the call completes successfully, or -1 if a call |
| 1235 | ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()). |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1236 | */ |
| 1237 | static int unixFileLock(unixFile *pFile, struct flock *pLock){ |
| 1238 | int rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1239 | unixInodeInfo *pInode = pFile->pInode; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1240 | assert( unixMutexHeld() ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1241 | assert( pInode!=0 ); |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1242 | if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock) |
| 1243 | && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0) |
| 1244 | ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1245 | if( pInode->bProcessLock==0 ){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1246 | struct flock lock; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1247 | assert( pInode->nLock==0 ); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1248 | lock.l_whence = SEEK_SET; |
| 1249 | lock.l_start = SHARED_FIRST; |
| 1250 | lock.l_len = SHARED_SIZE; |
| 1251 | lock.l_type = F_WRLCK; |
| 1252 | rc = osFcntl(pFile->h, F_SETLK, &lock); |
| 1253 | if( rc<0 ) return rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1254 | pInode->bProcessLock = 1; |
| 1255 | pInode->nLock++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1256 | }else{ |
| 1257 | rc = 0; |
| 1258 | } |
| 1259 | }else{ |
| 1260 | rc = osFcntl(pFile->h, F_SETLK, pLock); |
| 1261 | } |
| 1262 | return rc; |
| 1263 | } |
| 1264 | |
| 1265 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1266 | ** Lock the file with the lock specified by parameter eFileLock - one |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1267 | ** of the following: |
| 1268 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1269 | ** (1) SHARED_LOCK |
| 1270 | ** (2) RESERVED_LOCK |
| 1271 | ** (3) PENDING_LOCK |
| 1272 | ** (4) EXCLUSIVE_LOCK |
| 1273 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1274 | ** Sometimes when requesting one lock state, additional lock states |
| 1275 | ** are inserted in between. The locking might fail on one of the later |
| 1276 | ** transitions leaving the lock state different from what it started but |
| 1277 | ** still short of its goal. The following chart shows the allowed |
| 1278 | ** transitions and the inserted intermediate states: |
| 1279 | ** |
| 1280 | ** UNLOCKED -> SHARED |
| 1281 | ** SHARED -> RESERVED |
| 1282 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1283 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1284 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1285 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1286 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1287 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1288 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1289 | static int unixLock(sqlite3_file *id, int eFileLock){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1290 | /* The following describes the implementation of the various locks and |
| 1291 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1292 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1293 | ** confusion with SQLite lock names). The algorithms are complicated |
| 1294 | ** slightly in order to be compatible with windows systems simultaneously |
| 1295 | ** accessing the same database file, in case that is ever required. |
| 1296 | ** |
| 1297 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1298 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1299 | ** range', a range of 510 bytes at a well known offset. |
| 1300 | ** |
| 1301 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 1302 | ** byte'. If this is successful, a random byte from the 'shared byte |
| 1303 | ** range' is read-locked and the lock on the 'pending byte' released. |
| 1304 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1305 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1306 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1307 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1308 | ** |
| 1309 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1310 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1311 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1312 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1313 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1314 | ** This property is used by the algorithm for rolling back a journal file |
| 1315 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1316 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1317 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1318 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1319 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1320 | ** within this range, this ensures that no other locks are held on the |
| 1321 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1322 | ** |
| 1323 | ** The reason a single byte cannot be used instead of the 'shared byte |
| 1324 | ** range' is that some versions of windows do not support read-locks. By |
| 1325 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 1326 | ** even if the locking primitive used is always a write-lock. |
| 1327 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1328 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1329 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1330 | unixInodeInfo *pInode = pFile->pInode; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1331 | struct flock lock; |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 1332 | int tErrno = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1333 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1334 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1335 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 1336 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1337 | azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1338 | |
| 1339 | /* 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] | 1340 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1341 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1342 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1343 | if( pFile->eFileLock>=eFileLock ){ |
| 1344 | OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
| 1345 | azFileLock(eFileLock))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1346 | return SQLITE_OK; |
| 1347 | } |
| 1348 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1349 | /* Make sure the locking sequence is correct. |
| 1350 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 1351 | ** (2) SQLite never explicitly requests a pendig lock. |
| 1352 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1353 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1354 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 1355 | assert( eFileLock!=PENDING_LOCK ); |
| 1356 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1357 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1358 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1359 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1360 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1361 | pInode = pFile->pInode; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1362 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1363 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1364 | ** handle that precludes the requested lock, return BUSY. |
| 1365 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1366 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 1367 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1368 | ){ |
| 1369 | rc = SQLITE_BUSY; |
| 1370 | goto end_lock; |
| 1371 | } |
| 1372 | |
| 1373 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1374 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1375 | ** return SQLITE_OK. |
| 1376 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1377 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1378 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1379 | assert( eFileLock==SHARED_LOCK ); |
| 1380 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1381 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1382 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1383 | pInode->nShared++; |
| 1384 | pInode->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1385 | goto end_lock; |
| 1386 | } |
| 1387 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1388 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1389 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1390 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1391 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1392 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1393 | lock.l_len = 1L; |
| 1394 | lock.l_whence = SEEK_SET; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1395 | if( eFileLock==SHARED_LOCK |
| 1396 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1397 | ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1398 | lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1399 | lock.l_start = PENDING_BYTE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1400 | if( unixFileLock(pFile, &lock) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1401 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1402 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1403 | if( rc!=SQLITE_BUSY ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1404 | pFile->lastErrno = tErrno; |
| 1405 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1406 | goto end_lock; |
| 1407 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | |
| 1411 | /* If control gets to this point, then actually go ahead and make |
| 1412 | ** operating system calls for the specified lock. |
| 1413 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1414 | if( eFileLock==SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1415 | assert( pInode->nShared==0 ); |
| 1416 | assert( pInode->eFileLock==0 ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1417 | assert( rc==SQLITE_OK ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1418 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1419 | /* Now get the read-lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1420 | lock.l_start = SHARED_FIRST; |
| 1421 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1422 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1423 | tErrno = errno; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1424 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1425 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1426 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1427 | /* Drop the temporary PENDING lock */ |
| 1428 | lock.l_start = PENDING_BYTE; |
| 1429 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1430 | lock.l_type = F_UNLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1431 | if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){ |
| 1432 | /* This could happen with a network mount */ |
| 1433 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1434 | rc = SQLITE_IOERR_UNLOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1435 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1436 | |
| 1437 | if( rc ){ |
| 1438 | if( rc!=SQLITE_BUSY ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1439 | pFile->lastErrno = tErrno; |
| 1440 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1441 | goto end_lock; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1442 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1443 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1444 | pInode->nLock++; |
| 1445 | pInode->nShared = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1446 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1447 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1448 | /* We are trying for an exclusive lock but another thread in this |
| 1449 | ** same process is still holding a shared lock. */ |
| 1450 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1451 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1452 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1453 | ** assumed that there is a SHARED or greater lock on the file |
| 1454 | ** already. |
| 1455 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1456 | assert( 0!=pFile->eFileLock ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1457 | lock.l_type = F_WRLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1458 | |
| 1459 | assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK ); |
| 1460 | if( eFileLock==RESERVED_LOCK ){ |
| 1461 | lock.l_start = RESERVED_BYTE; |
| 1462 | lock.l_len = 1L; |
| 1463 | }else{ |
| 1464 | lock.l_start = SHARED_FIRST; |
| 1465 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1466 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1467 | |
| 1468 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1469 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1470 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1471 | if( rc!=SQLITE_BUSY ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1472 | pFile->lastErrno = tErrno; |
| 1473 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1474 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1475 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1476 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1477 | |
| 1478 | #ifndef NDEBUG |
| 1479 | /* Set up the transaction-counter change checking flags when |
| 1480 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1481 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1482 | ** write operation (not a hot journal rollback). |
| 1483 | */ |
| 1484 | if( rc==SQLITE_OK |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1485 | && pFile->eFileLock<=SHARED_LOCK |
| 1486 | && eFileLock==RESERVED_LOCK |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1487 | ){ |
| 1488 | pFile->transCntrChng = 0; |
| 1489 | pFile->dbUpdate = 0; |
| 1490 | pFile->inNormalWrite = 1; |
| 1491 | } |
| 1492 | #endif |
| 1493 | |
| 1494 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1495 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1496 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1497 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1498 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 1499 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1500 | pInode->eFileLock = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1501 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1502 | |
| 1503 | end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1504 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1505 | OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), |
| 1506 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1507 | return rc; |
| 1508 | } |
| 1509 | |
| 1510 | /* |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1511 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1512 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1513 | */ |
| 1514 | static void setPendingFd(unixFile *pFile){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1515 | unixInodeInfo *pInode = pFile->pInode; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1516 | UnixUnusedFd *p = pFile->pUnused; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1517 | p->pNext = pInode->pUnused; |
| 1518 | pInode->pUnused = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1519 | pFile->h = -1; |
| 1520 | pFile->pUnused = 0; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1524 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1525 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1526 | ** |
| 1527 | ** If the locking level of the file descriptor is already at or below |
| 1528 | ** the requested locking level, this routine is a no-op. |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1529 | ** |
| 1530 | ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 1531 | ** the byte range is divided into 2 parts and the first part is unlocked then |
| 1532 | ** set to a read lock, then the other part is simply unlocked. This works |
| 1533 | ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 1534 | ** remove the write lock on a region when a read lock is set. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1535 | */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1536 | static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1537 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1538 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1539 | struct flock lock; |
| 1540 | int rc = SQLITE_OK; |
| 1541 | int h; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1542 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1543 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1544 | 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] | 1545 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1546 | getpid())); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1547 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1548 | assert( eFileLock<=SHARED_LOCK ); |
| 1549 | if( pFile->eFileLock<=eFileLock ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1550 | return SQLITE_OK; |
| 1551 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1552 | unixEnterMutex(); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1553 | h = pFile->h; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1554 | pInode = pFile->pInode; |
| 1555 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1556 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1557 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1558 | SimulateIOErrorBenign(1); |
| 1559 | SimulateIOError( h=(-1) ) |
| 1560 | SimulateIOErrorBenign(0); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1561 | |
| 1562 | #ifndef NDEBUG |
| 1563 | /* When reducing a lock such that other processes can start |
| 1564 | ** reading the database file again, make sure that the |
| 1565 | ** transaction counter was updated if any part of the database |
| 1566 | ** file changed. If the transaction counter is not updated, |
| 1567 | ** other connections to the same file might not realize that |
| 1568 | ** the file has changed and hence might not know to flush their |
| 1569 | ** cache. The use of a stale cache can lead to database corruption. |
| 1570 | */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1571 | #if 0 |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1572 | assert( pFile->inNormalWrite==0 |
| 1573 | || pFile->dbUpdate==0 |
| 1574 | || pFile->transCntrChng==1 ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1575 | #endif |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1576 | pFile->inNormalWrite = 0; |
| 1577 | #endif |
| 1578 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1579 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 1580 | ** before establishing the readlock - to avoid a race condition we downgrade |
| 1581 | ** the lock in 2 blocks, so that part of the range will be covered by a |
| 1582 | ** write lock until the rest is covered by a read lock: |
| 1583 | ** 1: [WWWWW] |
| 1584 | ** 2: [....W] |
| 1585 | ** 3: [RRRRW] |
| 1586 | ** 4: [RRRR.] |
| 1587 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1588 | if( eFileLock==SHARED_LOCK ){ |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1589 | |
| 1590 | #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE |
drh | 87e79ae | 2011-03-08 13:06:41 +0000 | [diff] [blame] | 1591 | (void)handleNFSUnlock; |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1592 | assert( handleNFSUnlock==0 ); |
| 1593 | #endif |
| 1594 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1595 | if( handleNFSUnlock ){ |
drh | 026663d | 2011-04-01 13:29:29 +0000 | [diff] [blame] | 1596 | int tErrno; /* Error code from system call errors */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1597 | off_t divSize = SHARED_SIZE - 1; |
| 1598 | |
| 1599 | lock.l_type = F_UNLCK; |
| 1600 | lock.l_whence = SEEK_SET; |
| 1601 | lock.l_start = SHARED_FIRST; |
| 1602 | lock.l_len = divSize; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 1603 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1604 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1605 | rc = SQLITE_IOERR_UNLOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1606 | if( IS_LOCK_ERROR(rc) ){ |
| 1607 | pFile->lastErrno = tErrno; |
| 1608 | } |
| 1609 | goto end_unlock; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1610 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1611 | lock.l_type = F_RDLCK; |
| 1612 | lock.l_whence = SEEK_SET; |
| 1613 | lock.l_start = SHARED_FIRST; |
| 1614 | lock.l_len = divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1615 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1616 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1617 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1618 | if( IS_LOCK_ERROR(rc) ){ |
| 1619 | pFile->lastErrno = tErrno; |
| 1620 | } |
| 1621 | goto end_unlock; |
| 1622 | } |
| 1623 | lock.l_type = F_UNLCK; |
| 1624 | lock.l_whence = SEEK_SET; |
| 1625 | lock.l_start = SHARED_FIRST+divSize; |
| 1626 | lock.l_len = SHARED_SIZE-divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1627 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1628 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1629 | rc = SQLITE_IOERR_UNLOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1630 | if( IS_LOCK_ERROR(rc) ){ |
| 1631 | pFile->lastErrno = tErrno; |
| 1632 | } |
| 1633 | goto end_unlock; |
| 1634 | } |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1635 | }else |
| 1636 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 1637 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1638 | lock.l_type = F_RDLCK; |
| 1639 | lock.l_whence = SEEK_SET; |
| 1640 | lock.l_start = SHARED_FIRST; |
| 1641 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1642 | if( unixFileLock(pFile, &lock) ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1643 | /* In theory, the call to unixFileLock() cannot fail because another |
| 1644 | ** process is holding an incompatible lock. If it does, this |
| 1645 | ** indicates that the other process is not following the locking |
| 1646 | ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning |
| 1647 | ** SQLITE_BUSY would confuse the upper layer (in practice it causes |
| 1648 | ** an assert to fail). */ |
| 1649 | rc = SQLITE_IOERR_RDLOCK; |
| 1650 | pFile->lastErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1651 | goto end_unlock; |
| 1652 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1653 | } |
| 1654 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1655 | lock.l_type = F_UNLCK; |
| 1656 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1657 | lock.l_start = PENDING_BYTE; |
| 1658 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1659 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1660 | pInode->eFileLock = SHARED_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1661 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1662 | rc = SQLITE_IOERR_UNLOCK; |
| 1663 | pFile->lastErrno = errno; |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 1664 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1665 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1666 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1667 | if( eFileLock==NO_LOCK ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1668 | /* Decrement the shared lock counter. Release the lock using an |
| 1669 | ** OS call only when all threads in this same process have released |
| 1670 | ** the lock. |
| 1671 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1672 | pInode->nShared--; |
| 1673 | if( pInode->nShared==0 ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1674 | lock.l_type = F_UNLCK; |
| 1675 | lock.l_whence = SEEK_SET; |
| 1676 | lock.l_start = lock.l_len = 0L; |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1677 | SimulateIOErrorBenign(1); |
| 1678 | SimulateIOError( h=(-1) ) |
| 1679 | SimulateIOErrorBenign(0); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1680 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1681 | pInode->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1682 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1683 | rc = SQLITE_IOERR_UNLOCK; |
| 1684 | pFile->lastErrno = errno; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1685 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1686 | pFile->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1687 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1690 | /* Decrement the count of locks against this same file. When the |
| 1691 | ** count reaches zero, close any other file descriptors whose close |
| 1692 | ** was deferred because of outstanding locks. |
| 1693 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1694 | pInode->nLock--; |
| 1695 | assert( pInode->nLock>=0 ); |
| 1696 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1697 | closePendingFds(pFile); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1698 | } |
| 1699 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1700 | |
| 1701 | end_unlock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1702 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1703 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1704 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1708 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1709 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1710 | ** |
| 1711 | ** If the locking level of the file descriptor is already at or below |
| 1712 | ** the requested locking level, this routine is a no-op. |
| 1713 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1714 | static int unixUnlock(sqlite3_file *id, int eFileLock){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1715 | return posixUnlock(id, eFileLock, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1719 | ** This function performs the parts of the "close file" operation |
| 1720 | ** common to all locking schemes. It closes the directory and file |
| 1721 | ** handles, if they are valid, and sets all fields of the unixFile |
| 1722 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1723 | ** |
| 1724 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 1725 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 1726 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1727 | */ |
| 1728 | static int closeUnixFile(sqlite3_file *id){ |
| 1729 | unixFile *pFile = (unixFile*)id; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1730 | if( pFile->dirfd>=0 ){ |
| 1731 | robust_close(pFile, pFile->dirfd, __LINE__); |
| 1732 | pFile->dirfd=-1; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1733 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1734 | if( pFile->h>=0 ){ |
| 1735 | robust_close(pFile, pFile->h, __LINE__); |
| 1736 | pFile->h = -1; |
| 1737 | } |
| 1738 | #if OS_VXWORKS |
| 1739 | if( pFile->pId ){ |
| 1740 | if( pFile->isDelete ){ |
| 1741 | unlink(pFile->pId->zCanonicalName); |
| 1742 | } |
| 1743 | vxworksReleaseFileId(pFile->pId); |
| 1744 | pFile->pId = 0; |
| 1745 | } |
| 1746 | #endif |
| 1747 | OSTRACE(("CLOSE %-3d\n", pFile->h)); |
| 1748 | OpenCounter(-1); |
| 1749 | sqlite3_free(pFile->pUnused); |
| 1750 | memset(pFile, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1751 | return SQLITE_OK; |
| 1752 | } |
| 1753 | |
| 1754 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1755 | ** Close a file. |
| 1756 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1757 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1758 | int rc = SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1759 | unixFile *pFile = (unixFile *)id; |
| 1760 | unixUnlock(id, NO_LOCK); |
| 1761 | unixEnterMutex(); |
| 1762 | |
| 1763 | /* unixFile.pInode is always valid here. Otherwise, a different close |
| 1764 | ** routine (e.g. nolockClose()) would be called instead. |
| 1765 | */ |
| 1766 | assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); |
| 1767 | if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){ |
| 1768 | /* If there are outstanding locks, do not actually close the file just |
| 1769 | ** yet because that would clear those locks. Instead, add the file |
| 1770 | ** descriptor to pInode->pUnused list. It will be automatically closed |
| 1771 | ** when the last lock is cleared. |
| 1772 | */ |
| 1773 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1774 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1775 | releaseInodeInfo(pFile); |
| 1776 | rc = closeUnixFile(id); |
| 1777 | unixLeaveMutex(); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1778 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1781 | /************** End of the posix advisory lock implementation ***************** |
| 1782 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1783 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1784 | /****************************************************************************** |
| 1785 | ****************************** No-op Locking ********************************** |
| 1786 | ** |
| 1787 | ** Of the various locking implementations available, this is by far the |
| 1788 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 1789 | ** file for reading or writing. |
| 1790 | ** |
| 1791 | ** This locking mode is appropriate for use on read-only databases |
| 1792 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 1793 | ** also be used if the application employs some external mechanism to |
| 1794 | ** prevent simultaneous access of the same database by two or more |
| 1795 | ** database connections. But there is a serious risk of database |
| 1796 | ** corruption if this locking mode is used in situations where multiple |
| 1797 | ** database connections are accessing the same database file at the same |
| 1798 | ** time and one or more of those connections are writing. |
| 1799 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1800 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1801 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 1802 | UNUSED_PARAMETER(NotUsed); |
| 1803 | *pResOut = 0; |
| 1804 | return SQLITE_OK; |
| 1805 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1806 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1807 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1808 | return SQLITE_OK; |
| 1809 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1810 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1811 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1812 | return SQLITE_OK; |
| 1813 | } |
| 1814 | |
| 1815 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1816 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1817 | */ |
| 1818 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1819 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | /******************* End of the no-op lock implementation ********************* |
| 1823 | ******************************************************************************/ |
| 1824 | |
| 1825 | /****************************************************************************** |
| 1826 | ************************* Begin dot-file Locking ****************************** |
| 1827 | ** |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1828 | ** The dotfile locking implementation uses the existance of separate lock |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1829 | ** files in order to control access to the database. This works on just |
| 1830 | ** about every filesystem imaginable. But there are serious downsides: |
| 1831 | ** |
| 1832 | ** (1) There is zero concurrency. A single reader blocks all other |
| 1833 | ** connections from reading or writing the database. |
| 1834 | ** |
| 1835 | ** (2) An application crash or power loss can leave stale lock files |
| 1836 | ** sitting around that need to be cleared manually. |
| 1837 | ** |
| 1838 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 1839 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1840 | ** |
| 1841 | ** Dotfile locking works by creating a file in the same directory as the |
| 1842 | ** database and with the same name but with a ".lock" extension added. |
| 1843 | ** The existance of a lock file implies an EXCLUSIVE lock. All other lock |
| 1844 | ** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1845 | */ |
| 1846 | |
| 1847 | /* |
| 1848 | ** The file suffix added to the data base filename in order to create the |
| 1849 | ** lock file. |
| 1850 | */ |
| 1851 | #define DOTLOCK_SUFFIX ".lock" |
| 1852 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1853 | /* |
| 1854 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1855 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1856 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1857 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1858 | ** |
| 1859 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 1860 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 1861 | ** is held on the file and false if the file is unlocked. |
| 1862 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1863 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 1864 | int rc = SQLITE_OK; |
| 1865 | int reserved = 0; |
| 1866 | unixFile *pFile = (unixFile*)id; |
| 1867 | |
| 1868 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1869 | |
| 1870 | assert( pFile ); |
| 1871 | |
| 1872 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1873 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1874 | /* Either this connection or some other connection in the same process |
| 1875 | ** holds a lock on the file. No need to check further. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1876 | reserved = 1; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1877 | }else{ |
| 1878 | /* The lock is held if and only if the lockfile exists */ |
| 1879 | const char *zLockFile = (const char*)pFile->lockingContext; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1880 | reserved = osAccess(zLockFile, 0)==0; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1881 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1882 | OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1883 | *pResOut = reserved; |
| 1884 | return rc; |
| 1885 | } |
| 1886 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1887 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1888 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1889 | ** of the following: |
| 1890 | ** |
| 1891 | ** (1) SHARED_LOCK |
| 1892 | ** (2) RESERVED_LOCK |
| 1893 | ** (3) PENDING_LOCK |
| 1894 | ** (4) EXCLUSIVE_LOCK |
| 1895 | ** |
| 1896 | ** Sometimes when requesting one lock state, additional lock states |
| 1897 | ** are inserted in between. The locking might fail on one of the later |
| 1898 | ** transitions leaving the lock state different from what it started but |
| 1899 | ** still short of its goal. The following chart shows the allowed |
| 1900 | ** transitions and the inserted intermediate states: |
| 1901 | ** |
| 1902 | ** UNLOCKED -> SHARED |
| 1903 | ** SHARED -> RESERVED |
| 1904 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1905 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1906 | ** PENDING -> EXCLUSIVE |
| 1907 | ** |
| 1908 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1909 | ** routine to lower a locking level. |
| 1910 | ** |
| 1911 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 1912 | ** But we track the other locking levels internally. |
| 1913 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1914 | static int dotlockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1915 | unixFile *pFile = (unixFile*)id; |
| 1916 | int fd; |
| 1917 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1918 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1919 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1920 | |
| 1921 | /* If we have any lock, then the lock file already exists. All we have |
| 1922 | ** to do is adjust our internal record of the lock level. |
| 1923 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1924 | if( pFile->eFileLock > NO_LOCK ){ |
| 1925 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1926 | #if !OS_VXWORKS |
| 1927 | /* Always update the timestamp on the old file */ |
| 1928 | utimes(zLockFile, NULL); |
| 1929 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1930 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | /* grab an exclusive lock */ |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 1934 | fd = robust_open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1935 | if( fd<0 ){ |
| 1936 | /* failed to open/create the file, someone else may have stolen the lock */ |
| 1937 | int tErrno = errno; |
| 1938 | if( EEXIST == tErrno ){ |
| 1939 | rc = SQLITE_BUSY; |
| 1940 | } else { |
| 1941 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1942 | if( IS_LOCK_ERROR(rc) ){ |
| 1943 | pFile->lastErrno = tErrno; |
| 1944 | } |
| 1945 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1946 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1947 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1948 | robust_close(pFile, fd, __LINE__); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1949 | |
| 1950 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1951 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1952 | return rc; |
| 1953 | } |
| 1954 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1955 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1956 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1957 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1958 | ** |
| 1959 | ** If the locking level of the file descriptor is already at or below |
| 1960 | ** the requested locking level, this routine is a no-op. |
| 1961 | ** |
| 1962 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 1963 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1964 | static int dotlockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1965 | unixFile *pFile = (unixFile*)id; |
| 1966 | char *zLockFile = (char *)pFile->lockingContext; |
| 1967 | |
| 1968 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1969 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
| 1970 | pFile->eFileLock, getpid())); |
| 1971 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1972 | |
| 1973 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1974 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1975 | return SQLITE_OK; |
| 1976 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1977 | |
| 1978 | /* To downgrade to shared, simply update our internal notion of the |
| 1979 | ** lock state. No need to mess with the file on disk. |
| 1980 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1981 | if( eFileLock==SHARED_LOCK ){ |
| 1982 | pFile->eFileLock = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1983 | return SQLITE_OK; |
| 1984 | } |
| 1985 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1986 | /* To fully unlock the database, delete the lock file */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1987 | assert( eFileLock==NO_LOCK ); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1988 | if( unlink(zLockFile) ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 1989 | int rc = 0; |
| 1990 | int tErrno = errno; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1991 | if( ENOENT != tErrno ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1992 | rc = SQLITE_IOERR_UNLOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1993 | } |
| 1994 | if( IS_LOCK_ERROR(rc) ){ |
| 1995 | pFile->lastErrno = tErrno; |
| 1996 | } |
| 1997 | return rc; |
| 1998 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1999 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2000 | return SQLITE_OK; |
| 2001 | } |
| 2002 | |
| 2003 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2004 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2005 | */ |
| 2006 | static int dotlockClose(sqlite3_file *id) { |
| 2007 | int rc; |
| 2008 | if( id ){ |
| 2009 | unixFile *pFile = (unixFile*)id; |
| 2010 | dotlockUnlock(id, NO_LOCK); |
| 2011 | sqlite3_free(pFile->lockingContext); |
| 2012 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2013 | rc = closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2014 | return rc; |
| 2015 | } |
| 2016 | /****************** End of the dot-file lock implementation ******************* |
| 2017 | ******************************************************************************/ |
| 2018 | |
| 2019 | /****************************************************************************** |
| 2020 | ************************** Begin flock Locking ******************************** |
| 2021 | ** |
| 2022 | ** Use the flock() system call to do file locking. |
| 2023 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2024 | ** flock() locking is like dot-file locking in that the various |
| 2025 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 2026 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 2027 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 2028 | ** still works when you do this, but concurrency is reduced since |
| 2029 | ** only a single process can be reading the database at a time. |
| 2030 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2031 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if |
| 2032 | ** compiling for VXWORKS. |
| 2033 | */ |
| 2034 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2035 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2036 | /* |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2037 | ** Retry flock() calls that fail with EINTR |
| 2038 | */ |
| 2039 | #ifdef EINTR |
| 2040 | static int robust_flock(int fd, int op){ |
| 2041 | int rc; |
| 2042 | do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR ); |
| 2043 | return rc; |
| 2044 | } |
| 2045 | #else |
drh | 5c81927 | 2011-02-23 14:00:12 +0000 | [diff] [blame] | 2046 | # define robust_flock(a,b) flock(a,b) |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2047 | #endif |
| 2048 | |
| 2049 | |
| 2050 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2051 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2052 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2053 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2054 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2055 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2056 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 2057 | int rc = SQLITE_OK; |
| 2058 | int reserved = 0; |
| 2059 | unixFile *pFile = (unixFile*)id; |
| 2060 | |
| 2061 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2062 | |
| 2063 | assert( pFile ); |
| 2064 | |
| 2065 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2066 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2067 | reserved = 1; |
| 2068 | } |
| 2069 | |
| 2070 | /* Otherwise see if some other process holds it. */ |
| 2071 | if( !reserved ){ |
| 2072 | /* attempt to get the lock */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2073 | int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2074 | if( !lrc ){ |
| 2075 | /* got the lock, unlock it */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2076 | lrc = robust_flock(pFile->h, LOCK_UN); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2077 | if ( lrc ) { |
| 2078 | int tErrno = errno; |
| 2079 | /* unlock failed with an error */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2080 | lrc = SQLITE_IOERR_UNLOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2081 | if( IS_LOCK_ERROR(lrc) ){ |
| 2082 | pFile->lastErrno = tErrno; |
| 2083 | rc = lrc; |
| 2084 | } |
| 2085 | } |
| 2086 | } else { |
| 2087 | int tErrno = errno; |
| 2088 | reserved = 1; |
| 2089 | /* someone else might have it reserved */ |
| 2090 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2091 | if( IS_LOCK_ERROR(lrc) ){ |
| 2092 | pFile->lastErrno = tErrno; |
| 2093 | rc = lrc; |
| 2094 | } |
| 2095 | } |
| 2096 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2097 | OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2098 | |
| 2099 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2100 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2101 | rc = SQLITE_OK; |
| 2102 | reserved=1; |
| 2103 | } |
| 2104 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2105 | *pResOut = reserved; |
| 2106 | return rc; |
| 2107 | } |
| 2108 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2109 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2110 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2111 | ** of the following: |
| 2112 | ** |
| 2113 | ** (1) SHARED_LOCK |
| 2114 | ** (2) RESERVED_LOCK |
| 2115 | ** (3) PENDING_LOCK |
| 2116 | ** (4) EXCLUSIVE_LOCK |
| 2117 | ** |
| 2118 | ** Sometimes when requesting one lock state, additional lock states |
| 2119 | ** are inserted in between. The locking might fail on one of the later |
| 2120 | ** transitions leaving the lock state different from what it started but |
| 2121 | ** still short of its goal. The following chart shows the allowed |
| 2122 | ** transitions and the inserted intermediate states: |
| 2123 | ** |
| 2124 | ** UNLOCKED -> SHARED |
| 2125 | ** SHARED -> RESERVED |
| 2126 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2127 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2128 | ** PENDING -> EXCLUSIVE |
| 2129 | ** |
| 2130 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 2131 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2132 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2133 | ** access the file. |
| 2134 | ** |
| 2135 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2136 | ** routine to lower a locking level. |
| 2137 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2138 | static int flockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2139 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2140 | unixFile *pFile = (unixFile*)id; |
| 2141 | |
| 2142 | assert( pFile ); |
| 2143 | |
| 2144 | /* if we already have a lock, it is exclusive. |
| 2145 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2146 | if (pFile->eFileLock > NO_LOCK) { |
| 2147 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2148 | return SQLITE_OK; |
| 2149 | } |
| 2150 | |
| 2151 | /* grab an exclusive lock */ |
| 2152 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2153 | if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2154 | int tErrno = errno; |
| 2155 | /* didn't get, must be busy */ |
| 2156 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2157 | if( IS_LOCK_ERROR(rc) ){ |
| 2158 | pFile->lastErrno = tErrno; |
| 2159 | } |
| 2160 | } else { |
| 2161 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2162 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2163 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2164 | OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), |
| 2165 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2166 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2167 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2168 | rc = SQLITE_BUSY; |
| 2169 | } |
| 2170 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2171 | return rc; |
| 2172 | } |
| 2173 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2174 | |
| 2175 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2176 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2177 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2178 | ** |
| 2179 | ** If the locking level of the file descriptor is already at or below |
| 2180 | ** the requested locking level, this routine is a no-op. |
| 2181 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2182 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2183 | unixFile *pFile = (unixFile*)id; |
| 2184 | |
| 2185 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2186 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
| 2187 | pFile->eFileLock, getpid())); |
| 2188 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2189 | |
| 2190 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2191 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2192 | return SQLITE_OK; |
| 2193 | } |
| 2194 | |
| 2195 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2196 | if (eFileLock==SHARED_LOCK) { |
| 2197 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2198 | return SQLITE_OK; |
| 2199 | } |
| 2200 | |
| 2201 | /* no, really, unlock. */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2202 | if( robust_flock(pFile->h, LOCK_UN) ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2203 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2204 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2205 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2206 | return SQLITE_IOERR_UNLOCK; |
| 2207 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2208 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2209 | return SQLITE_OK; |
| 2210 | } |
| 2211 | } |
| 2212 | |
| 2213 | /* |
| 2214 | ** Close a file. |
| 2215 | */ |
| 2216 | static int flockClose(sqlite3_file *id) { |
| 2217 | if( id ){ |
| 2218 | flockUnlock(id, NO_LOCK); |
| 2219 | } |
| 2220 | return closeUnixFile(id); |
| 2221 | } |
| 2222 | |
| 2223 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 2224 | |
| 2225 | /******************* End of the flock lock implementation ********************* |
| 2226 | ******************************************************************************/ |
| 2227 | |
| 2228 | /****************************************************************************** |
| 2229 | ************************ Begin Named Semaphore Locking ************************ |
| 2230 | ** |
| 2231 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2232 | ** |
| 2233 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 2234 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 2235 | ** the database file at a time. This reduces potential concurrency, but |
| 2236 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2237 | */ |
| 2238 | #if OS_VXWORKS |
| 2239 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2240 | /* |
| 2241 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2242 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2243 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2244 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2245 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2246 | static int semCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 2247 | int rc = SQLITE_OK; |
| 2248 | int reserved = 0; |
| 2249 | unixFile *pFile = (unixFile*)id; |
| 2250 | |
| 2251 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2252 | |
| 2253 | assert( pFile ); |
| 2254 | |
| 2255 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2256 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2257 | reserved = 1; |
| 2258 | } |
| 2259 | |
| 2260 | /* Otherwise see if some other process holds it. */ |
| 2261 | if( !reserved ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2262 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2263 | struct stat statBuf; |
| 2264 | |
| 2265 | if( sem_trywait(pSem)==-1 ){ |
| 2266 | int tErrno = errno; |
| 2267 | if( EAGAIN != tErrno ){ |
| 2268 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 2269 | pFile->lastErrno = tErrno; |
| 2270 | } else { |
| 2271 | /* someone else has the lock when we are in NO_LOCK */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2272 | reserved = (pFile->eFileLock < SHARED_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2273 | } |
| 2274 | }else{ |
| 2275 | /* we could have it if we want it */ |
| 2276 | sem_post(pSem); |
| 2277 | } |
| 2278 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2279 | OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2280 | |
| 2281 | *pResOut = reserved; |
| 2282 | return rc; |
| 2283 | } |
| 2284 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2285 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2286 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2287 | ** of the following: |
| 2288 | ** |
| 2289 | ** (1) SHARED_LOCK |
| 2290 | ** (2) RESERVED_LOCK |
| 2291 | ** (3) PENDING_LOCK |
| 2292 | ** (4) EXCLUSIVE_LOCK |
| 2293 | ** |
| 2294 | ** Sometimes when requesting one lock state, additional lock states |
| 2295 | ** are inserted in between. The locking might fail on one of the later |
| 2296 | ** transitions leaving the lock state different from what it started but |
| 2297 | ** still short of its goal. The following chart shows the allowed |
| 2298 | ** transitions and the inserted intermediate states: |
| 2299 | ** |
| 2300 | ** UNLOCKED -> SHARED |
| 2301 | ** SHARED -> RESERVED |
| 2302 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2303 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2304 | ** PENDING -> EXCLUSIVE |
| 2305 | ** |
| 2306 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 2307 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2308 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2309 | ** access the file. |
| 2310 | ** |
| 2311 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2312 | ** routine to lower a locking level. |
| 2313 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2314 | static int semLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2315 | unixFile *pFile = (unixFile*)id; |
| 2316 | int fd; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2317 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2318 | int rc = SQLITE_OK; |
| 2319 | |
| 2320 | /* if we already have a lock, it is exclusive. |
| 2321 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2322 | if (pFile->eFileLock > NO_LOCK) { |
| 2323 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2324 | rc = SQLITE_OK; |
| 2325 | goto sem_end_lock; |
| 2326 | } |
| 2327 | |
| 2328 | /* lock semaphore now but bail out when already locked. */ |
| 2329 | if( sem_trywait(pSem)==-1 ){ |
| 2330 | rc = SQLITE_BUSY; |
| 2331 | goto sem_end_lock; |
| 2332 | } |
| 2333 | |
| 2334 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2335 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2336 | |
| 2337 | sem_end_lock: |
| 2338 | return rc; |
| 2339 | } |
| 2340 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2341 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2342 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2343 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2344 | ** |
| 2345 | ** If the locking level of the file descriptor is already at or below |
| 2346 | ** the requested locking level, this routine is a no-op. |
| 2347 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2348 | static int semUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2349 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2350 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2351 | |
| 2352 | assert( pFile ); |
| 2353 | assert( pSem ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2354 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
| 2355 | pFile->eFileLock, getpid())); |
| 2356 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2357 | |
| 2358 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2359 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2360 | return SQLITE_OK; |
| 2361 | } |
| 2362 | |
| 2363 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2364 | if (eFileLock==SHARED_LOCK) { |
| 2365 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2366 | return SQLITE_OK; |
| 2367 | } |
| 2368 | |
| 2369 | /* no, really unlock. */ |
| 2370 | if ( sem_post(pSem)==-1 ) { |
| 2371 | int rc, tErrno = errno; |
| 2372 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2373 | if( IS_LOCK_ERROR(rc) ){ |
| 2374 | pFile->lastErrno = tErrno; |
| 2375 | } |
| 2376 | return rc; |
| 2377 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2378 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2379 | return SQLITE_OK; |
| 2380 | } |
| 2381 | |
| 2382 | /* |
| 2383 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2384 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2385 | static int semClose(sqlite3_file *id) { |
| 2386 | if( id ){ |
| 2387 | unixFile *pFile = (unixFile*)id; |
| 2388 | semUnlock(id, NO_LOCK); |
| 2389 | assert( pFile ); |
| 2390 | unixEnterMutex(); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2391 | releaseInodeInfo(pFile); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2392 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2393 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2394 | } |
| 2395 | return SQLITE_OK; |
| 2396 | } |
| 2397 | |
| 2398 | #endif /* OS_VXWORKS */ |
| 2399 | /* |
| 2400 | ** Named semaphore locking is only available on VxWorks. |
| 2401 | ** |
| 2402 | *************** End of the named semaphore lock implementation **************** |
| 2403 | ******************************************************************************/ |
| 2404 | |
| 2405 | |
| 2406 | /****************************************************************************** |
| 2407 | *************************** Begin AFP Locking ********************************* |
| 2408 | ** |
| 2409 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2410 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2411 | ** |
| 2412 | ** Third-party implementations of AFP are available. But this code here |
| 2413 | ** only works on OSX. |
| 2414 | */ |
| 2415 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2416 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2417 | /* |
| 2418 | ** The afpLockingContext structure contains all afp lock specific state |
| 2419 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2420 | typedef struct afpLockingContext afpLockingContext; |
| 2421 | struct afpLockingContext { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2422 | int reserved; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2423 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2424 | }; |
| 2425 | |
| 2426 | struct ByteRangeLockPB2 |
| 2427 | { |
| 2428 | unsigned long long offset; /* offset to first byte to lock */ |
| 2429 | unsigned long long length; /* nbr of bytes to lock */ |
| 2430 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2431 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2432 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2433 | int fd; /* file desc to assoc this lock with */ |
| 2434 | }; |
| 2435 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2436 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2437 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2438 | /* |
| 2439 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2440 | ** AFP filesystem. |
| 2441 | ** |
| 2442 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2443 | */ |
| 2444 | static int afpSetLock( |
| 2445 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2446 | unixFile *pFile, /* Open file descriptor on path */ |
| 2447 | unsigned long long offset, /* First byte to be locked */ |
| 2448 | unsigned long long length, /* Number of bytes to lock */ |
| 2449 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2450 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2451 | struct ByteRangeLockPB2 pb; |
| 2452 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2453 | |
| 2454 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2455 | pb.startEndFlag = 0; |
| 2456 | pb.offset = offset; |
| 2457 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2458 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2459 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2460 | OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2461 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2462 | offset, length)); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2463 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2464 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2465 | int rc; |
| 2466 | int tErrno = errno; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2467 | OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2468 | path, tErrno, strerror(tErrno))); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2469 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2470 | rc = SQLITE_BUSY; |
| 2471 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2472 | rc = sqliteErrorFromPosixError(tErrno, |
| 2473 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2474 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2475 | if( IS_LOCK_ERROR(rc) ){ |
| 2476 | pFile->lastErrno = tErrno; |
| 2477 | } |
| 2478 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2479 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2480 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2481 | } |
| 2482 | } |
| 2483 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2484 | /* |
| 2485 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2486 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2487 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2488 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2489 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2490 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2491 | int rc = SQLITE_OK; |
| 2492 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2493 | unixFile *pFile = (unixFile*)id; |
| 2494 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2495 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2496 | |
| 2497 | assert( pFile ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2498 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2499 | if( context->reserved ){ |
| 2500 | *pResOut = 1; |
| 2501 | return SQLITE_OK; |
| 2502 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2503 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2504 | |
| 2505 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2506 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2507 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | /* Otherwise see if some other process holds it. |
| 2511 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2512 | if( !reserved ){ |
| 2513 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2514 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2515 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2516 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2517 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2518 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2519 | } else { |
| 2520 | /* if we failed to get the lock then someone else must have it */ |
| 2521 | reserved = 1; |
| 2522 | } |
| 2523 | if( IS_LOCK_ERROR(lrc) ){ |
| 2524 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2525 | } |
| 2526 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2527 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2528 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2529 | OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2530 | |
| 2531 | *pResOut = reserved; |
| 2532 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2533 | } |
| 2534 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2535 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2536 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2537 | ** of the following: |
| 2538 | ** |
| 2539 | ** (1) SHARED_LOCK |
| 2540 | ** (2) RESERVED_LOCK |
| 2541 | ** (3) PENDING_LOCK |
| 2542 | ** (4) EXCLUSIVE_LOCK |
| 2543 | ** |
| 2544 | ** Sometimes when requesting one lock state, additional lock states |
| 2545 | ** are inserted in between. The locking might fail on one of the later |
| 2546 | ** transitions leaving the lock state different from what it started but |
| 2547 | ** still short of its goal. The following chart shows the allowed |
| 2548 | ** transitions and the inserted intermediate states: |
| 2549 | ** |
| 2550 | ** UNLOCKED -> SHARED |
| 2551 | ** SHARED -> RESERVED |
| 2552 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2553 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2554 | ** PENDING -> EXCLUSIVE |
| 2555 | ** |
| 2556 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2557 | ** routine to lower a locking level. |
| 2558 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2559 | static int afpLock(sqlite3_file *id, int eFileLock){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2560 | int rc = SQLITE_OK; |
| 2561 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2562 | unixInodeInfo *pInode = pFile->pInode; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2563 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2564 | |
| 2565 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2566 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 2567 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2568 | azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2569 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2570 | /* 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] | 2571 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2572 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2573 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2574 | if( pFile->eFileLock>=eFileLock ){ |
| 2575 | OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
| 2576 | azFileLock(eFileLock))); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2577 | return SQLITE_OK; |
| 2578 | } |
| 2579 | |
| 2580 | /* Make sure the locking sequence is correct |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2581 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 2582 | ** (2) SQLite never explicitly requests a pendig lock. |
| 2583 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2584 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2585 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 2586 | assert( eFileLock!=PENDING_LOCK ); |
| 2587 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2588 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2589 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2590 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2591 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2592 | pInode = pFile->pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2593 | |
| 2594 | /* If some thread using this PID has a lock via a different unixFile* |
| 2595 | ** handle that precludes the requested lock, return BUSY. |
| 2596 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2597 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 2598 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2599 | ){ |
| 2600 | rc = SQLITE_BUSY; |
| 2601 | goto afp_end_lock; |
| 2602 | } |
| 2603 | |
| 2604 | /* If a SHARED lock is requested, and some thread using this PID already |
| 2605 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 2606 | ** return SQLITE_OK. |
| 2607 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2608 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2609 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2610 | assert( eFileLock==SHARED_LOCK ); |
| 2611 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2612 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2613 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2614 | pInode->nShared++; |
| 2615 | pInode->nLock++; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2616 | goto afp_end_lock; |
| 2617 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2618 | |
| 2619 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2620 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2621 | ** be released. |
| 2622 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2623 | if( eFileLock==SHARED_LOCK |
| 2624 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2625 | ){ |
| 2626 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2627 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2628 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2629 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2630 | goto afp_end_lock; |
| 2631 | } |
| 2632 | } |
| 2633 | |
| 2634 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2635 | ** operating system calls for the specified lock. |
| 2636 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2637 | if( eFileLock==SHARED_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2638 | int lrc1, lrc2, lrc1Errno; |
| 2639 | long lk, mask; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2640 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2641 | assert( pInode->nShared==0 ); |
| 2642 | assert( pInode->eFileLock==0 ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2643 | |
| 2644 | mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2645 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2646 | /* note that the quality of the randomness doesn't matter that much */ |
| 2647 | lk = random(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2648 | pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2649 | lrc1 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2650 | SHARED_FIRST+pInode->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2651 | if( IS_LOCK_ERROR(lrc1) ){ |
| 2652 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2653 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2654 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2655 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2656 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2657 | if( IS_LOCK_ERROR(lrc1) ) { |
| 2658 | pFile->lastErrno = lrc1Errno; |
| 2659 | rc = lrc1; |
| 2660 | goto afp_end_lock; |
| 2661 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 2662 | rc = lrc2; |
| 2663 | goto afp_end_lock; |
| 2664 | } else if( lrc1 != SQLITE_OK ) { |
| 2665 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2666 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2667 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2668 | pInode->nLock++; |
| 2669 | pInode->nShared = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2670 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2671 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2672 | /* We are trying for an exclusive lock but another thread in this |
| 2673 | ** same process is still holding a shared lock. */ |
| 2674 | rc = SQLITE_BUSY; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2675 | }else{ |
| 2676 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 2677 | ** assumed that there is a SHARED or greater lock on the file |
| 2678 | ** already. |
| 2679 | */ |
| 2680 | int failed = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2681 | assert( 0!=pFile->eFileLock ); |
| 2682 | if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2683 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2684 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2685 | if( !failed ){ |
| 2686 | context->reserved = 1; |
| 2687 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2688 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2689 | if (!failed && eFileLock == EXCLUSIVE_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2690 | /* Acquire an EXCLUSIVE lock */ |
| 2691 | |
| 2692 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2693 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2694 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2695 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2696 | pInode->sharedByte, 1, 0)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2697 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2698 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2699 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2700 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2701 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2702 | SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2703 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 2704 | ** a critical I/O error |
| 2705 | */ |
| 2706 | rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : |
| 2707 | SQLITE_IOERR_LOCK; |
| 2708 | goto afp_end_lock; |
| 2709 | } |
| 2710 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2711 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2712 | } |
| 2713 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2714 | if( failed ){ |
| 2715 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2716 | } |
| 2717 | } |
| 2718 | |
| 2719 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2720 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2721 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2722 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 2723 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2724 | pInode->eFileLock = PENDING_LOCK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | afp_end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2728 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2729 | OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), |
| 2730 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2731 | return rc; |
| 2732 | } |
| 2733 | |
| 2734 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2735 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2736 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2737 | ** |
| 2738 | ** If the locking level of the file descriptor is already at or below |
| 2739 | ** the requested locking level, this routine is a no-op. |
| 2740 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2741 | static int afpUnlock(sqlite3_file *id, int eFileLock) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2742 | int rc = SQLITE_OK; |
| 2743 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2744 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2745 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 2746 | int skipShared = 0; |
| 2747 | #ifdef SQLITE_TEST |
| 2748 | int h = pFile->h; |
| 2749 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2750 | |
| 2751 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2752 | 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] | 2753 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2754 | getpid())); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2755 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2756 | assert( eFileLock<=SHARED_LOCK ); |
| 2757 | if( pFile->eFileLock<=eFileLock ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2758 | return SQLITE_OK; |
| 2759 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2760 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2761 | pInode = pFile->pInode; |
| 2762 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2763 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2764 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2765 | SimulateIOErrorBenign(1); |
| 2766 | SimulateIOError( h=(-1) ) |
| 2767 | SimulateIOErrorBenign(0); |
| 2768 | |
| 2769 | #ifndef NDEBUG |
| 2770 | /* When reducing a lock such that other processes can start |
| 2771 | ** reading the database file again, make sure that the |
| 2772 | ** transaction counter was updated if any part of the database |
| 2773 | ** file changed. If the transaction counter is not updated, |
| 2774 | ** other connections to the same file might not realize that |
| 2775 | ** the file has changed and hence might not know to flush their |
| 2776 | ** cache. The use of a stale cache can lead to database corruption. |
| 2777 | */ |
| 2778 | assert( pFile->inNormalWrite==0 |
| 2779 | || pFile->dbUpdate==0 |
| 2780 | || pFile->transCntrChng==1 ); |
| 2781 | pFile->inNormalWrite = 0; |
| 2782 | #endif |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2783 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2784 | if( pFile->eFileLock==EXCLUSIVE_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2785 | rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2786 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2787 | /* only re-establish the shared lock if necessary */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2788 | int sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2789 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 2790 | } else { |
| 2791 | skipShared = 1; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2792 | } |
| 2793 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2794 | if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2795 | rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2796 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2797 | if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2798 | rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 2799 | if( !rc ){ |
| 2800 | context->reserved = 0; |
| 2801 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2802 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2803 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ |
| 2804 | pInode->eFileLock = SHARED_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2805 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2806 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2807 | if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2808 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2809 | /* Decrement the shared lock counter. Release the lock using an |
| 2810 | ** OS call only when all threads in this same process have released |
| 2811 | ** the lock. |
| 2812 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2813 | unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 2814 | pInode->nShared--; |
| 2815 | if( pInode->nShared==0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2816 | SimulateIOErrorBenign(1); |
| 2817 | SimulateIOError( h=(-1) ) |
| 2818 | SimulateIOErrorBenign(0); |
| 2819 | if( !skipShared ){ |
| 2820 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 2821 | } |
| 2822 | if( !rc ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2823 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2824 | pFile->eFileLock = NO_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2825 | } |
| 2826 | } |
| 2827 | if( rc==SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2828 | pInode->nLock--; |
| 2829 | assert( pInode->nLock>=0 ); |
| 2830 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 2831 | closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2832 | } |
| 2833 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2834 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2835 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2836 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2837 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2838 | return rc; |
| 2839 | } |
| 2840 | |
| 2841 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2842 | ** Close a file & cleanup AFP specific locking context |
| 2843 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2844 | static int afpClose(sqlite3_file *id) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2845 | int rc = SQLITE_OK; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2846 | if( id ){ |
| 2847 | unixFile *pFile = (unixFile*)id; |
| 2848 | afpUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2849 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2850 | if( pFile->pInode && pFile->pInode->nLock ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2851 | /* If there are outstanding locks, do not actually close the file just |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2852 | ** yet because that would clear those locks. Instead, add the file |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2853 | ** descriptor to pInode->aPending. It will be automatically closed when |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2854 | ** the last lock is cleared. |
| 2855 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2856 | setPendingFd(pFile); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2857 | } |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2858 | releaseInodeInfo(pFile); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2859 | sqlite3_free(pFile->lockingContext); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2860 | rc = closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2861 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2862 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2863 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2866 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2867 | /* |
| 2868 | ** The code above is the AFP lock implementation. The code is specific |
| 2869 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2870 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 2871 | ** VFS is not available. |
| 2872 | ** |
| 2873 | ********************* End of the AFP lock implementation ********************** |
| 2874 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2875 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2876 | /****************************************************************************** |
| 2877 | *************************** Begin NFS Locking ********************************/ |
| 2878 | |
| 2879 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 2880 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2881 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2882 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2883 | ** |
| 2884 | ** If the locking level of the file descriptor is already at or below |
| 2885 | ** the requested locking level, this routine is a no-op. |
| 2886 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2887 | static int nfsUnlock(sqlite3_file *id, int eFileLock){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 2888 | return posixUnlock(id, eFileLock, 1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2889 | } |
| 2890 | |
| 2891 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 2892 | /* |
| 2893 | ** The code above is the NFS lock implementation. The code is specific |
| 2894 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2895 | ** is available. |
| 2896 | ** |
| 2897 | ********************* End of the NFS lock implementation ********************** |
| 2898 | ******************************************************************************/ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2899 | |
| 2900 | /****************************************************************************** |
| 2901 | **************** Non-locking sqlite3_file methods ***************************** |
| 2902 | ** |
| 2903 | ** The next division contains implementations for all methods of the |
| 2904 | ** sqlite3_file object other than the locking methods. The locking |
| 2905 | ** methods were defined in divisions above (one locking method per |
| 2906 | ** division). Those methods that are common to all locking modes |
| 2907 | ** are gather together into this division. |
| 2908 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2909 | |
| 2910 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2911 | ** Seek to the offset passed as the second argument, then read cnt |
| 2912 | ** bytes into pBuf. Return the number of bytes actually read. |
| 2913 | ** |
| 2914 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 2915 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 2916 | ** one system to another. Since SQLite does not define USE_PREAD |
| 2917 | ** any any form by default, we will not attempt to define _XOPEN_SOURCE. |
| 2918 | ** See tickets #2741 and #2681. |
| 2919 | ** |
| 2920 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 2921 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2922 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2923 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 2924 | int got; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2925 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2926 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2927 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2928 | TIMER_START; |
| 2929 | #if defined(USE_PREAD) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 2930 | do{ got = osPread(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2931 | SimulateIOError( got = -1 ); |
| 2932 | #elif defined(USE_PREAD64) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 2933 | do{ got = osPread64(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2934 | SimulateIOError( got = -1 ); |
| 2935 | #else |
| 2936 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 2937 | SimulateIOError( newOffset-- ); |
| 2938 | if( newOffset!=offset ){ |
| 2939 | if( newOffset == -1 ){ |
| 2940 | ((unixFile*)id)->lastErrno = errno; |
| 2941 | }else{ |
| 2942 | ((unixFile*)id)->lastErrno = 0; |
| 2943 | } |
| 2944 | return -1; |
| 2945 | } |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 2946 | do{ got = osRead(id->h, pBuf, cnt); }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2947 | #endif |
| 2948 | TIMER_END; |
| 2949 | if( got<0 ){ |
| 2950 | ((unixFile*)id)->lastErrno = errno; |
| 2951 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2952 | OSTRACE(("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2953 | return got; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2954 | } |
| 2955 | |
| 2956 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2957 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 2958 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 2959 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2960 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2961 | static int unixRead( |
| 2962 | sqlite3_file *id, |
| 2963 | void *pBuf, |
| 2964 | int amt, |
| 2965 | sqlite3_int64 offset |
| 2966 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2967 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2968 | int got; |
| 2969 | assert( id ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2970 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2971 | /* If this is a database file (not a journal, master-journal or temp |
| 2972 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2973 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 2974 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2975 | || offset>=PENDING_BYTE+512 |
| 2976 | || offset+amt<=PENDING_BYTE |
| 2977 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2978 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2979 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2980 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2981 | if( got==amt ){ |
| 2982 | return SQLITE_OK; |
| 2983 | }else if( got<0 ){ |
| 2984 | /* lastErrno set by seekAndRead */ |
| 2985 | return SQLITE_IOERR_READ; |
| 2986 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2987 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2988 | /* Unread parts of the buffer must be zero-filled */ |
| 2989 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 2990 | return SQLITE_IOERR_SHORT_READ; |
| 2991 | } |
| 2992 | } |
| 2993 | |
| 2994 | /* |
| 2995 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 2996 | ** Return the number of bytes actually read. Update the offset. |
| 2997 | ** |
| 2998 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 2999 | ** is set before returning. |
| 3000 | */ |
| 3001 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
| 3002 | int got; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3003 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3004 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3005 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3006 | TIMER_START; |
| 3007 | #if defined(USE_PREAD) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 3008 | do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3009 | #elif defined(USE_PREAD64) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 3010 | do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3011 | #else |
| 3012 | newOffset = lseek(id->h, offset, SEEK_SET); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3013 | SimulateIOError( newOffset-- ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3014 | if( newOffset!=offset ){ |
| 3015 | if( newOffset == -1 ){ |
| 3016 | ((unixFile*)id)->lastErrno = errno; |
| 3017 | }else{ |
| 3018 | ((unixFile*)id)->lastErrno = 0; |
| 3019 | } |
| 3020 | return -1; |
| 3021 | } |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 3022 | do{ got = osWrite(id->h, pBuf, cnt); }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3023 | #endif |
| 3024 | TIMER_END; |
| 3025 | if( got<0 ){ |
| 3026 | ((unixFile*)id)->lastErrno = errno; |
| 3027 | } |
| 3028 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3029 | OSTRACE(("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3030 | return got; |
| 3031 | } |
| 3032 | |
| 3033 | |
| 3034 | /* |
| 3035 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 3036 | ** or some other error code on failure. |
| 3037 | */ |
| 3038 | static int unixWrite( |
| 3039 | sqlite3_file *id, |
| 3040 | const void *pBuf, |
| 3041 | int amt, |
| 3042 | sqlite3_int64 offset |
| 3043 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3044 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3045 | int wrote = 0; |
| 3046 | assert( id ); |
| 3047 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3048 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3049 | /* If this is a database file (not a journal, master-journal or temp |
| 3050 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3051 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3052 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3053 | || offset>=PENDING_BYTE+512 |
| 3054 | || offset+amt<=PENDING_BYTE |
| 3055 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3056 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3057 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3058 | #ifndef NDEBUG |
| 3059 | /* If we are doing a normal write to a database file (as opposed to |
| 3060 | ** doing a hot-journal rollback or a write to some file other than a |
| 3061 | ** normal database file) then record the fact that the database |
| 3062 | ** has changed. If the transaction counter is modified, record that |
| 3063 | ** fact too. |
| 3064 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3065 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3066 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 3067 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3068 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3069 | char oldCntr[4]; |
| 3070 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3071 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3072 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3073 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3074 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 3075 | } |
| 3076 | } |
| 3077 | } |
| 3078 | #endif |
| 3079 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3080 | while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3081 | amt -= wrote; |
| 3082 | offset += wrote; |
| 3083 | pBuf = &((char*)pBuf)[wrote]; |
| 3084 | } |
| 3085 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 3086 | SimulateDiskfullError(( wrote=0, amt=1 )); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3087 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3088 | if( amt>0 ){ |
| 3089 | if( wrote<0 ){ |
| 3090 | /* lastErrno set by seekAndWrite */ |
| 3091 | return SQLITE_IOERR_WRITE; |
| 3092 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3093 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3094 | return SQLITE_FULL; |
| 3095 | } |
| 3096 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3097 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3098 | return SQLITE_OK; |
| 3099 | } |
| 3100 | |
| 3101 | #ifdef SQLITE_TEST |
| 3102 | /* |
| 3103 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3104 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3105 | */ |
| 3106 | int sqlite3_sync_count = 0; |
| 3107 | int sqlite3_fullsync_count = 0; |
| 3108 | #endif |
| 3109 | |
| 3110 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3111 | ** We do not trust systems to provide a working fdatasync(). Some do. |
| 3112 | ** Others do no. To be safe, we will stick with the (slower) fsync(). |
| 3113 | ** If you know that your system does support fdatasync() correctly, |
| 3114 | ** then simply compile with -Dfdatasync=fdatasync |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3115 | */ |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3116 | #if !defined(fdatasync) && !defined(__linux__) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3117 | # define fdatasync fsync |
| 3118 | #endif |
| 3119 | |
| 3120 | /* |
| 3121 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 3122 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 3123 | ** only available on Mac OS X. But that could change. |
| 3124 | */ |
| 3125 | #ifdef F_FULLFSYNC |
| 3126 | # define HAVE_FULLFSYNC 1 |
| 3127 | #else |
| 3128 | # define HAVE_FULLFSYNC 0 |
| 3129 | #endif |
| 3130 | |
| 3131 | |
| 3132 | /* |
| 3133 | ** The fsync() system call does not work as advertised on many |
| 3134 | ** unix systems. The following procedure is an attempt to make |
| 3135 | ** it work better. |
| 3136 | ** |
| 3137 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 3138 | ** for testing when we want to run through the test suite quickly. |
| 3139 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 3140 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 3141 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3142 | ** |
| 3143 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 3144 | ** The idea behind dataOnly is that it should only write the file content |
| 3145 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 3146 | ** unchanged since the file size is part of the inode. However, |
| 3147 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 3148 | ** file size has changed. The only real difference between fdatasync() |
| 3149 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 3150 | ** inode if the mtime or owner or other inode attributes have changed. |
| 3151 | ** We only care about the file size, not the other file attributes, so |
| 3152 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 3153 | ** So, we always use fdatasync() if it is available, regardless of |
| 3154 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3155 | */ |
| 3156 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3157 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3158 | |
| 3159 | /* The following "ifdef/elif/else/" block has the same structure as |
| 3160 | ** the one below. It is replicated here solely to avoid cluttering |
| 3161 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 3162 | */ |
| 3163 | #ifdef SQLITE_NO_SYNC |
| 3164 | UNUSED_PARAMETER(fd); |
| 3165 | UNUSED_PARAMETER(fullSync); |
| 3166 | UNUSED_PARAMETER(dataOnly); |
| 3167 | #elif HAVE_FULLFSYNC |
| 3168 | UNUSED_PARAMETER(dataOnly); |
| 3169 | #else |
| 3170 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3171 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3172 | #endif |
| 3173 | |
| 3174 | /* Record the number of times that we do a normal fsync() and |
| 3175 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 3176 | ** gets called with the correct arguments. |
| 3177 | */ |
| 3178 | #ifdef SQLITE_TEST |
| 3179 | if( fullSync ) sqlite3_fullsync_count++; |
| 3180 | sqlite3_sync_count++; |
| 3181 | #endif |
| 3182 | |
| 3183 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 3184 | ** no-op |
| 3185 | */ |
| 3186 | #ifdef SQLITE_NO_SYNC |
| 3187 | rc = SQLITE_OK; |
| 3188 | #elif HAVE_FULLFSYNC |
| 3189 | if( fullSync ){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3190 | rc = osFcntl(fd, F_FULLFSYNC, 0); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3191 | }else{ |
| 3192 | rc = 1; |
| 3193 | } |
| 3194 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3195 | ** It shouldn't be possible for fullfsync to fail on the local |
| 3196 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 3197 | ** isn't supported for this file system. So, attempt an fsync |
| 3198 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 3199 | ** It'd be better to detect fullfsync support once and avoid |
| 3200 | ** the fcntl call every time sync is called. |
| 3201 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3202 | if( rc ) rc = fsync(fd); |
| 3203 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3204 | #elif defined(__APPLE__) |
| 3205 | /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 3206 | ** so currently we default to the macro that redefines fdatasync to fsync |
| 3207 | */ |
| 3208 | rc = fsync(fd); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3209 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3210 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 3211 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3212 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3213 | rc = fsync(fd); |
| 3214 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3215 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3216 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 3217 | |
| 3218 | if( OS_VXWORKS && rc!= -1 ){ |
| 3219 | rc = 0; |
| 3220 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3221 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3222 | } |
| 3223 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3224 | /* |
| 3225 | ** Make sure all writes to a particular file are committed to disk. |
| 3226 | ** |
| 3227 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 3228 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 3229 | ** file data is synced. |
| 3230 | ** |
| 3231 | ** Under Unix, also make sure that the directory entry for the file |
| 3232 | ** has been created by fsync-ing the directory that contains the file. |
| 3233 | ** If we do not do this and we encounter a power failure, the directory |
| 3234 | ** entry for the journal might not exist after we reboot. The next |
| 3235 | ** SQLite to access the file will not know that the journal exists (because |
| 3236 | ** the directory entry for the journal was never created) and the transaction |
| 3237 | ** will not roll back - possibly leading to database corruption. |
| 3238 | */ |
| 3239 | static int unixSync(sqlite3_file *id, int flags){ |
| 3240 | int rc; |
| 3241 | unixFile *pFile = (unixFile*)id; |
| 3242 | |
| 3243 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 3244 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 3245 | |
| 3246 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 3247 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 3248 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 3249 | ); |
| 3250 | |
| 3251 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 3252 | ** line is to test that doing so does not cause any problems. |
| 3253 | */ |
| 3254 | SimulateDiskfullError( return SQLITE_FULL ); |
| 3255 | |
| 3256 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3257 | OSTRACE(("SYNC %-3d\n", pFile->h)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3258 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 3259 | SimulateIOError( rc=1 ); |
| 3260 | if( rc ){ |
| 3261 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3262 | return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3263 | } |
| 3264 | if( pFile->dirfd>=0 ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3265 | OSTRACE(("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd, |
| 3266 | HAVE_FULLFSYNC, isFullsync)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3267 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 3268 | /* The directory sync is only attempted if full_fsync is |
| 3269 | ** turned off or unavailable. If a full_fsync occurred above, |
| 3270 | ** then the directory sync is superfluous. |
| 3271 | */ |
| 3272 | if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){ |
| 3273 | /* |
| 3274 | ** We have received multiple reports of fsync() returning |
| 3275 | ** errors when applied to directories on certain file systems. |
| 3276 | ** A failed directory sync is not a big deal. So it seems |
| 3277 | ** better to ignore the error. Ticket #1657 |
| 3278 | */ |
| 3279 | /* pFile->lastErrno = errno; */ |
| 3280 | /* return SQLITE_IOERR; */ |
| 3281 | } |
| 3282 | #endif |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 3283 | /* Only need to sync once, so close the directory when we are done */ |
| 3284 | robust_close(pFile, pFile->dirfd, __LINE__); |
| 3285 | pFile->dirfd = -1; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3286 | } |
| 3287 | return rc; |
| 3288 | } |
| 3289 | |
| 3290 | /* |
| 3291 | ** Truncate an open file to a specified size |
| 3292 | */ |
| 3293 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3294 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3295 | int rc; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3296 | assert( pFile ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3297 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3298 | |
| 3299 | /* If the user has configured a chunk-size for this file, truncate the |
| 3300 | ** file so that it consists of an integer number of chunks (i.e. the |
| 3301 | ** actual file size after the operation may be larger than the requested |
| 3302 | ** size). |
| 3303 | */ |
| 3304 | if( pFile->szChunk ){ |
| 3305 | nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 3306 | } |
| 3307 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3308 | rc = robust_ftruncate(pFile->h, (off_t)nByte); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3309 | if( rc ){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3310 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3311 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3312 | }else{ |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3313 | #ifndef NDEBUG |
| 3314 | /* If we are doing a normal write to a database file (as opposed to |
| 3315 | ** doing a hot-journal rollback or a write to some file other than a |
| 3316 | ** normal database file) and we truncate the file to zero length, |
| 3317 | ** that effectively updates the change counter. This might happen |
| 3318 | ** when restoring a database using the backup API from a zero-length |
| 3319 | ** source. |
| 3320 | */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3321 | if( pFile->inNormalWrite && nByte==0 ){ |
| 3322 | pFile->transCntrChng = 1; |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3323 | } |
| 3324 | #endif |
| 3325 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3326 | return SQLITE_OK; |
| 3327 | } |
| 3328 | } |
| 3329 | |
| 3330 | /* |
| 3331 | ** Determine the current size of a file in bytes |
| 3332 | */ |
| 3333 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 3334 | int rc; |
| 3335 | struct stat buf; |
| 3336 | assert( id ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3337 | rc = osFstat(((unixFile*)id)->h, &buf); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3338 | SimulateIOError( rc=1 ); |
| 3339 | if( rc!=0 ){ |
| 3340 | ((unixFile*)id)->lastErrno = errno; |
| 3341 | return SQLITE_IOERR_FSTAT; |
| 3342 | } |
| 3343 | *pSize = buf.st_size; |
| 3344 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3345 | /* When opening a zero-size database, the findInodeInfo() procedure |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3346 | ** writes a single byte into that file in order to work around a bug |
| 3347 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 3348 | ** layers, we need to report this file size as zero even though it is |
| 3349 | ** really 1. Ticket #3260. |
| 3350 | */ |
| 3351 | if( *pSize==1 ) *pSize = 0; |
| 3352 | |
| 3353 | |
| 3354 | return SQLITE_OK; |
| 3355 | } |
| 3356 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3357 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3358 | /* |
| 3359 | ** Handler for proxy-locking file-control verbs. Defined below in the |
| 3360 | ** proxying locking division. |
| 3361 | */ |
| 3362 | static int proxyFileControl(sqlite3_file*,int,void*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3363 | #endif |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3364 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3365 | /* |
| 3366 | ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT |
| 3367 | ** file-control operation. |
| 3368 | ** |
| 3369 | ** If the user has configured a chunk-size for this file, it could be |
| 3370 | ** that the file needs to be extended at this point. Otherwise, the |
| 3371 | ** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix. |
| 3372 | */ |
| 3373 | static int fcntlSizeHint(unixFile *pFile, i64 nByte){ |
| 3374 | if( pFile->szChunk ){ |
| 3375 | i64 nSize; /* Required file size */ |
| 3376 | struct stat buf; /* Used to hold return values of fstat() */ |
| 3377 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3378 | if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3379 | |
| 3380 | nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; |
| 3381 | if( nSize>(i64)buf.st_size ){ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3382 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3383 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3384 | /* The code below is handling the return value of osFallocate() |
| 3385 | ** correctly. posix_fallocate() is defined to "returns zero on success, |
| 3386 | ** or an error number on failure". See the manpage for details. */ |
| 3387 | int err; |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3388 | do{ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3389 | err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size); |
| 3390 | }while( err==EINTR ); |
| 3391 | if( err ) return SQLITE_IOERR_WRITE; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3392 | #else |
| 3393 | /* If the OS does not have posix_fallocate(), fake it. First use |
| 3394 | ** ftruncate() to set the file size, then write a single byte to |
| 3395 | ** the last byte in each block within the extended region. This |
| 3396 | ** is the same technique used by glibc to implement posix_fallocate() |
| 3397 | ** on systems that do not have a real fallocate() system call. |
| 3398 | */ |
| 3399 | int nBlk = buf.st_blksize; /* File-system block size */ |
| 3400 | i64 iWrite; /* Next offset to write to */ |
| 3401 | int nWrite; /* Return value from seekAndWrite() */ |
| 3402 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3403 | if( robust_ftruncate(pFile->h, nSize) ){ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3404 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3405 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3406 | } |
| 3407 | iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1; |
| 3408 | do { |
| 3409 | nWrite = seekAndWrite(pFile, iWrite, "", 1); |
| 3410 | iWrite += nBlk; |
| 3411 | } while( nWrite==1 && iWrite<nSize ); |
| 3412 | if( nWrite!=1 ) return SQLITE_IOERR_WRITE; |
| 3413 | #endif |
| 3414 | } |
| 3415 | } |
| 3416 | |
| 3417 | return SQLITE_OK; |
| 3418 | } |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3419 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3420 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3421 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3422 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3423 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3424 | switch( op ){ |
| 3425 | case SQLITE_FCNTL_LOCKSTATE: { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3426 | *(int*)pArg = ((unixFile*)id)->eFileLock; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3427 | return SQLITE_OK; |
| 3428 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3429 | case SQLITE_LAST_ERRNO: { |
| 3430 | *(int*)pArg = ((unixFile*)id)->lastErrno; |
| 3431 | return SQLITE_OK; |
| 3432 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3433 | case SQLITE_FCNTL_CHUNK_SIZE: { |
| 3434 | ((unixFile*)id)->szChunk = *(int *)pArg; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3435 | return SQLITE_OK; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3436 | } |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3437 | case SQLITE_FCNTL_SIZE_HINT: { |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3438 | return fcntlSizeHint((unixFile *)id, *(i64 *)pArg); |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3439 | } |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3440 | #ifndef NDEBUG |
| 3441 | /* The pager calls this method to signal that it has done |
| 3442 | ** a rollback and that the database is therefore unchanged and |
| 3443 | ** it hence it is OK for the transaction change counter to be |
| 3444 | ** unchanged. |
| 3445 | */ |
| 3446 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 3447 | ((unixFile*)id)->dbUpdate = 0; |
| 3448 | return SQLITE_OK; |
| 3449 | } |
| 3450 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3451 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3452 | case SQLITE_SET_LOCKPROXYFILE: |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3453 | case SQLITE_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3454 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3455 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3456 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 3457 | case SQLITE_FCNTL_SYNC_OMITTED: { |
| 3458 | return SQLITE_OK; /* A no-op */ |
| 3459 | } |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3460 | } |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 3461 | return SQLITE_NOTFOUND; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 3462 | } |
| 3463 | |
| 3464 | /* |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3465 | ** Return the sector size in bytes of the underlying block device for |
| 3466 | ** the specified file. This is almost always 512 bytes, but may be |
| 3467 | ** larger for some devices. |
| 3468 | ** |
| 3469 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 3470 | ** if two files are created in the same file-system directory (i.e. |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3471 | ** a database and its journal file) that the sector size will be the |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3472 | ** same for both. |
| 3473 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3474 | static int unixSectorSize(sqlite3_file *NotUsed){ |
| 3475 | UNUSED_PARAMETER(NotUsed); |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 3476 | return SQLITE_DEFAULT_SECTOR_SIZE; |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3477 | } |
| 3478 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3479 | /* |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3480 | ** Return the device characteristics for the file. This is always 0 for unix. |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3481 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3482 | static int unixDeviceCharacteristics(sqlite3_file *NotUsed){ |
| 3483 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3484 | return 0; |
| 3485 | } |
| 3486 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3487 | #ifndef SQLITE_OMIT_WAL |
| 3488 | |
| 3489 | |
| 3490 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3491 | ** Object used to represent an shared memory buffer. |
| 3492 | ** |
| 3493 | ** When multiple threads all reference the same wal-index, each thread |
| 3494 | ** has its own unixShm object, but they all point to a single instance |
| 3495 | ** of this unixShmNode object. In other words, each wal-index is opened |
| 3496 | ** only once per process. |
| 3497 | ** |
| 3498 | ** Each unixShmNode object is connected to a single unixInodeInfo object. |
| 3499 | ** We could coalesce this object into unixInodeInfo, but that would mean |
| 3500 | ** every open file that does not use shared memory (in other words, most |
| 3501 | ** open files) would have to carry around this extra information. So |
| 3502 | ** the unixInodeInfo object contains a pointer to this unixShmNode object |
| 3503 | ** and the unixShmNode object is created only when needed. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3504 | ** |
| 3505 | ** unixMutexHeld() must be true when creating or destroying |
| 3506 | ** this object or while reading or writing the following fields: |
| 3507 | ** |
| 3508 | ** nRef |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3509 | ** |
| 3510 | ** The following fields are read-only after the object is created: |
| 3511 | ** |
| 3512 | ** fid |
| 3513 | ** zFilename |
| 3514 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3515 | ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3516 | ** unixMutexHeld() is true when reading or writing any other field |
| 3517 | ** in this structure. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3518 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3519 | struct unixShmNode { |
| 3520 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3521 | sqlite3_mutex *mutex; /* Mutex to access this object */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3522 | char *zFilename; /* Name of the mmapped file */ |
| 3523 | int h; /* Open file descriptor */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3524 | int szRegion; /* Size of shared-memory regions */ |
| 3525 | int nRegion; /* Size of array apRegion */ |
| 3526 | char **apRegion; /* Array of mapped shared-memory regions */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3527 | int nRef; /* Number of unixShm objects pointing to this */ |
| 3528 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3529 | #ifdef SQLITE_DEBUG |
| 3530 | u8 exclMask; /* Mask of exclusive locks held */ |
| 3531 | u8 sharedMask; /* Mask of shared locks held */ |
| 3532 | u8 nextShmId; /* Next available unixShm.id value */ |
| 3533 | #endif |
| 3534 | }; |
| 3535 | |
| 3536 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3537 | ** Structure used internally by this VFS to record the state of an |
| 3538 | ** open shared memory connection. |
| 3539 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3540 | ** The following fields are initialized when this object is created and |
| 3541 | ** are read-only thereafter: |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3542 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3543 | ** unixShm.pFile |
| 3544 | ** unixShm.id |
| 3545 | ** |
| 3546 | ** All other fields are read/write. The unixShm.pFile->mutex must be held |
| 3547 | ** while accessing any read/write fields. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3548 | */ |
| 3549 | struct unixShm { |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3550 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 3551 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3552 | u8 hasMutex; /* True if holding the unixShmNode mutex */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3553 | u16 sharedMask; /* Mask of shared locks held */ |
| 3554 | u16 exclMask; /* Mask of exclusive locks held */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3555 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3556 | u8 id; /* Id of this connection within its unixShmNode */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3557 | #endif |
| 3558 | }; |
| 3559 | |
| 3560 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3561 | ** Constants used for locking |
| 3562 | */ |
drh | bd9676c | 2010-06-23 17:58:38 +0000 | [diff] [blame] | 3563 | #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
drh | 4222441 | 2010-05-31 14:28:25 +0000 | [diff] [blame] | 3564 | #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3565 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3566 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3567 | ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3568 | ** |
| 3569 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 3570 | ** otherwise. |
| 3571 | */ |
| 3572 | static int unixShmSystemLock( |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3573 | unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */ |
| 3574 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3575 | int ofst, /* First byte of the locking range */ |
| 3576 | int n /* Number of bytes to lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3577 | ){ |
| 3578 | struct flock f; /* The posix advisory locking structure */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3579 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3580 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3581 | /* Access to the unixShmNode object is serialized by the caller */ |
| 3582 | assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3583 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3584 | /* Shared locks never span more than one byte */ |
| 3585 | assert( n==1 || lockType!=F_RDLCK ); |
| 3586 | |
| 3587 | /* Locks are within range */ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3588 | assert( n>=1 && n<SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3589 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3590 | if( pShmNode->h>=0 ){ |
| 3591 | /* Initialize the locking parameters */ |
| 3592 | memset(&f, 0, sizeof(f)); |
| 3593 | f.l_type = lockType; |
| 3594 | f.l_whence = SEEK_SET; |
| 3595 | f.l_start = ofst; |
| 3596 | f.l_len = n; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3597 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3598 | rc = osFcntl(pShmNode->h, F_SETLK, &f); |
| 3599 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 3600 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3601 | |
| 3602 | /* Update the global lock state and do debug tracing */ |
| 3603 | #ifdef SQLITE_DEBUG |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3604 | { u16 mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3605 | OSTRACE(("SHM-LOCK ")); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3606 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3607 | if( rc==SQLITE_OK ){ |
| 3608 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3609 | OSTRACE(("unlock %d ok", ofst)); |
| 3610 | pShmNode->exclMask &= ~mask; |
| 3611 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3612 | }else if( lockType==F_RDLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3613 | OSTRACE(("read-lock %d ok", ofst)); |
| 3614 | pShmNode->exclMask &= ~mask; |
| 3615 | pShmNode->sharedMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3616 | }else{ |
| 3617 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3618 | OSTRACE(("write-lock %d ok", ofst)); |
| 3619 | pShmNode->exclMask |= mask; |
| 3620 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3621 | } |
| 3622 | }else{ |
| 3623 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3624 | OSTRACE(("unlock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3625 | }else if( lockType==F_RDLCK ){ |
| 3626 | OSTRACE(("read-lock failed")); |
| 3627 | }else{ |
| 3628 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3629 | OSTRACE(("write-lock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3630 | } |
| 3631 | } |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 3632 | OSTRACE((" - afterwards %03x,%03x\n", |
| 3633 | pShmNode->sharedMask, pShmNode->exclMask)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3634 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3635 | #endif |
| 3636 | |
| 3637 | return rc; |
| 3638 | } |
| 3639 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3640 | |
| 3641 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3642 | ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3643 | ** |
| 3644 | ** This is not a VFS shared-memory method; it is a utility function called |
| 3645 | ** by VFS shared-memory methods. |
| 3646 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3647 | static void unixShmPurge(unixFile *pFd){ |
| 3648 | unixShmNode *p = pFd->pInode->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3649 | assert( unixMutexHeld() ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3650 | if( p && p->nRef==0 ){ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3651 | int i; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3652 | assert( p->pInode==pFd->pInode ); |
| 3653 | if( p->mutex ) sqlite3_mutex_free(p->mutex); |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3654 | for(i=0; i<p->nRegion; i++){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3655 | if( p->h>=0 ){ |
| 3656 | munmap(p->apRegion[i], p->szRegion); |
| 3657 | }else{ |
| 3658 | sqlite3_free(p->apRegion[i]); |
| 3659 | } |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3660 | } |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3661 | sqlite3_free(p->apRegion); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 3662 | if( p->h>=0 ){ |
| 3663 | robust_close(pFd, p->h, __LINE__); |
| 3664 | p->h = -1; |
| 3665 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3666 | p->pInode->pShmNode = 0; |
| 3667 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3668 | } |
| 3669 | } |
| 3670 | |
| 3671 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3672 | ** Open a shared-memory area associated with open database file pDbFd. |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3673 | ** This particular implementation uses mmapped files. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3674 | ** |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3675 | ** The file used to implement shared-memory is in the same directory |
| 3676 | ** as the open database file and has the same name as the open database |
| 3677 | ** file with the "-shm" suffix added. For example, if the database file |
| 3678 | ** is "/home/user1/config.db" then the file that is created and mmapped |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3679 | ** for shared memory will be called "/home/user1/config.db-shm". |
| 3680 | ** |
| 3681 | ** Another approach to is to use files in /dev/shm or /dev/tmp or an |
| 3682 | ** some other tmpfs mount. But if a file in a different directory |
| 3683 | ** from the database file is used, then differing access permissions |
| 3684 | ** or a chroot() might cause two different processes on the same |
| 3685 | ** database to end up using different files for shared memory - |
| 3686 | ** meaning that their memory would not really be shared - resulting |
| 3687 | ** in database corruption. Nevertheless, this tmpfs file usage |
| 3688 | ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" |
| 3689 | ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time |
| 3690 | ** option results in an incompatible build of SQLite; builds of SQLite |
| 3691 | ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the |
| 3692 | ** same database file at the same time, database corruption will likely |
| 3693 | ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered |
| 3694 | ** "unsupported" and may go away in a future SQLite release. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3695 | ** |
| 3696 | ** When opening a new shared-memory file, if no other instances of that |
| 3697 | ** file are currently open, in this process or in other processes, then |
| 3698 | ** the file must be truncated to zero length or have its header cleared. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3699 | ** |
| 3700 | ** If the original database file (pDbFd) is using the "unix-excl" VFS |
| 3701 | ** that means that an exclusive lock is held on the database file and |
| 3702 | ** that no other processes are able to read or write the database. In |
| 3703 | ** that case, we do not really need shared memory. No shared memory |
| 3704 | ** file is created. The shared memory will be simulated with heap memory. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3705 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3706 | static int unixOpenSharedMemory(unixFile *pDbFd){ |
| 3707 | struct unixShm *p = 0; /* The connection to be opened */ |
| 3708 | struct unixShmNode *pShmNode; /* The underlying mmapped file */ |
| 3709 | int rc; /* Result code */ |
| 3710 | unixInodeInfo *pInode; /* The inode of fd */ |
| 3711 | char *zShmFilename; /* Name of the file used for SHM */ |
| 3712 | int nShmFilename; /* Size of the SHM filename in bytes */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3713 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3714 | /* Allocate space for the new unixShm object. */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3715 | p = sqlite3_malloc( sizeof(*p) ); |
| 3716 | if( p==0 ) return SQLITE_NOMEM; |
| 3717 | memset(p, 0, sizeof(*p)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3718 | assert( pDbFd->pShm==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3719 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3720 | /* Check to see if a unixShmNode object already exists. Reuse an existing |
| 3721 | ** one if present. Create a new one if necessary. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3722 | */ |
| 3723 | unixEnterMutex(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 3724 | pInode = pDbFd->pInode; |
| 3725 | pShmNode = pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3726 | if( pShmNode==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 3727 | struct stat sStat; /* fstat() info for database file */ |
| 3728 | |
| 3729 | /* Call fstat() to figure out the permissions on the database file. If |
| 3730 | ** a new *-shm file is created, an attempt will be made to create it |
| 3731 | ** with the same permissions. The actual permissions the file is created |
| 3732 | ** with are subject to the current umask setting. |
| 3733 | */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3734 | if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 3735 | rc = SQLITE_IOERR_FSTAT; |
| 3736 | goto shm_open_err; |
| 3737 | } |
| 3738 | |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3739 | #ifdef SQLITE_SHM_DIRECTORY |
| 3740 | nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 30; |
| 3741 | #else |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3742 | nShmFilename = 5 + (int)strlen(pDbFd->zPath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3743 | #endif |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3744 | pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3745 | if( pShmNode==0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3746 | rc = SQLITE_NOMEM; |
| 3747 | goto shm_open_err; |
| 3748 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3749 | memset(pShmNode, 0, sizeof(*pShmNode)); |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3750 | zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1]; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3751 | #ifdef SQLITE_SHM_DIRECTORY |
| 3752 | sqlite3_snprintf(nShmFilename, zShmFilename, |
| 3753 | SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", |
| 3754 | (u32)sStat.st_ino, (u32)sStat.st_dev); |
| 3755 | #else |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3756 | sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3757 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3758 | pShmNode->h = -1; |
| 3759 | pDbFd->pInode->pShmNode = pShmNode; |
| 3760 | pShmNode->pInode = pDbFd->pInode; |
| 3761 | pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 3762 | if( pShmNode->mutex==0 ){ |
| 3763 | rc = SQLITE_NOMEM; |
| 3764 | goto shm_open_err; |
| 3765 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3766 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3767 | if( pInode->bProcessLock==0 ){ |
| 3768 | pShmNode->h = robust_open(zShmFilename, O_RDWR|O_CREAT, |
| 3769 | (sStat.st_mode & 0777)); |
| 3770 | if( pShmNode->h<0 ){ |
| 3771 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename); |
| 3772 | goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3773 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3774 | |
| 3775 | /* Check to see if another process is holding the dead-man switch. |
| 3776 | ** If not, truncate the file to zero length. |
| 3777 | */ |
| 3778 | rc = SQLITE_OK; |
| 3779 | if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ |
| 3780 | if( robust_ftruncate(pShmNode->h, 0) ){ |
| 3781 | rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename); |
| 3782 | } |
| 3783 | } |
| 3784 | if( rc==SQLITE_OK ){ |
| 3785 | rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1); |
| 3786 | } |
| 3787 | if( rc ) goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3788 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3789 | } |
| 3790 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3791 | /* Make the new connection a child of the unixShmNode */ |
| 3792 | p->pShmNode = pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3793 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3794 | p->id = pShmNode->nextShmId++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3795 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3796 | pShmNode->nRef++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3797 | pDbFd->pShm = p; |
| 3798 | unixLeaveMutex(); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 3799 | |
| 3800 | /* The reference count on pShmNode has already been incremented under |
| 3801 | ** the cover of the unixEnterMutex() mutex and the pointer from the |
| 3802 | ** new (struct unixShm) object to the pShmNode has been set. All that is |
| 3803 | ** left to do is to link the new object into the linked list starting |
| 3804 | ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex |
| 3805 | ** mutex. |
| 3806 | */ |
| 3807 | sqlite3_mutex_enter(pShmNode->mutex); |
| 3808 | p->pNext = pShmNode->pFirst; |
| 3809 | pShmNode->pFirst = p; |
| 3810 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3811 | return SQLITE_OK; |
| 3812 | |
| 3813 | /* Jump here on any error */ |
| 3814 | shm_open_err: |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3815 | unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3816 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3817 | unixLeaveMutex(); |
| 3818 | return rc; |
| 3819 | } |
| 3820 | |
| 3821 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3822 | ** This function is called to obtain a pointer to region iRegion of the |
| 3823 | ** shared-memory associated with the database file fd. Shared-memory regions |
| 3824 | ** are numbered starting from zero. Each shared-memory region is szRegion |
| 3825 | ** bytes in size. |
| 3826 | ** |
| 3827 | ** If an error occurs, an error code is returned and *pp is set to NULL. |
| 3828 | ** |
| 3829 | ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory |
| 3830 | ** region has not been allocated (by any client, including one running in a |
| 3831 | ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 3832 | ** bExtend is non-zero and the requested shared-memory region has not yet |
| 3833 | ** been allocated, it is allocated by this function. |
| 3834 | ** |
| 3835 | ** If the shared-memory region has already been allocated or is allocated by |
| 3836 | ** this call as described above, then it is mapped into this processes |
| 3837 | ** address space (if it is not already), *pp is set to point to the mapped |
| 3838 | ** memory and SQLITE_OK returned. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3839 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3840 | static int unixShmMap( |
| 3841 | sqlite3_file *fd, /* Handle open on database file */ |
| 3842 | int iRegion, /* Region to retrieve */ |
| 3843 | int szRegion, /* Size of regions */ |
| 3844 | int bExtend, /* True to extend file if necessary */ |
| 3845 | void volatile **pp /* OUT: Mapped memory */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3846 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3847 | unixFile *pDbFd = (unixFile*)fd; |
| 3848 | unixShm *p; |
| 3849 | unixShmNode *pShmNode; |
| 3850 | int rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3851 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3852 | /* If the shared-memory file has not yet been opened, open it now. */ |
| 3853 | if( pDbFd->pShm==0 ){ |
| 3854 | rc = unixOpenSharedMemory(pDbFd); |
| 3855 | if( rc!=SQLITE_OK ) return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3856 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3857 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3858 | p = pDbFd->pShm; |
| 3859 | pShmNode = p->pShmNode; |
| 3860 | sqlite3_mutex_enter(pShmNode->mutex); |
| 3861 | assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3862 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 3863 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 3864 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3865 | |
| 3866 | if( pShmNode->nRegion<=iRegion ){ |
| 3867 | char **apNew; /* New apRegion[] array */ |
| 3868 | int nByte = (iRegion+1)*szRegion; /* Minimum required file size */ |
| 3869 | struct stat sStat; /* Used by fstat() */ |
| 3870 | |
| 3871 | pShmNode->szRegion = szRegion; |
| 3872 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3873 | if( pShmNode->h>=0 ){ |
| 3874 | /* The requested region is not mapped into this processes address space. |
| 3875 | ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 3876 | ** large enough to contain the requested region). |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3877 | */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3878 | if( osFstat(pShmNode->h, &sStat) ){ |
| 3879 | rc = SQLITE_IOERR_SHMSIZE; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3880 | goto shmpage_out; |
| 3881 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3882 | |
| 3883 | if( sStat.st_size<nByte ){ |
| 3884 | /* The requested memory region does not exist. If bExtend is set to |
| 3885 | ** false, exit early. *pp will be set to NULL and SQLITE_OK returned. |
| 3886 | ** |
| 3887 | ** Alternatively, if bExtend is true, use ftruncate() to allocate |
| 3888 | ** the requested memory region. |
| 3889 | */ |
| 3890 | if( !bExtend ) goto shmpage_out; |
| 3891 | if( robust_ftruncate(pShmNode->h, nByte) ){ |
| 3892 | rc = unixLogError(SQLITE_IOERR_SHMSIZE, "ftruncate", |
| 3893 | pShmNode->zFilename); |
| 3894 | goto shmpage_out; |
| 3895 | } |
| 3896 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3897 | } |
| 3898 | |
| 3899 | /* Map the requested memory region into this processes address space. */ |
| 3900 | apNew = (char **)sqlite3_realloc( |
| 3901 | pShmNode->apRegion, (iRegion+1)*sizeof(char *) |
| 3902 | ); |
| 3903 | if( !apNew ){ |
| 3904 | rc = SQLITE_IOERR_NOMEM; |
| 3905 | goto shmpage_out; |
| 3906 | } |
| 3907 | pShmNode->apRegion = apNew; |
| 3908 | while(pShmNode->nRegion<=iRegion){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3909 | void *pMem; |
| 3910 | if( pShmNode->h>=0 ){ |
| 3911 | pMem = mmap(0, szRegion, PROT_READ|PROT_WRITE, |
| 3912 | MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion |
| 3913 | ); |
| 3914 | if( pMem==MAP_FAILED ){ |
| 3915 | rc = SQLITE_IOERR; |
| 3916 | goto shmpage_out; |
| 3917 | } |
| 3918 | }else{ |
| 3919 | pMem = sqlite3_malloc(szRegion); |
| 3920 | if( pMem==0 ){ |
| 3921 | rc = SQLITE_NOMEM; |
| 3922 | goto shmpage_out; |
| 3923 | } |
| 3924 | memset(pMem, 0, szRegion); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3925 | } |
| 3926 | pShmNode->apRegion[pShmNode->nRegion] = pMem; |
| 3927 | pShmNode->nRegion++; |
| 3928 | } |
| 3929 | } |
| 3930 | |
| 3931 | shmpage_out: |
| 3932 | if( pShmNode->nRegion>iRegion ){ |
| 3933 | *pp = pShmNode->apRegion[iRegion]; |
| 3934 | }else{ |
| 3935 | *pp = 0; |
| 3936 | } |
| 3937 | sqlite3_mutex_leave(pShmNode->mutex); |
| 3938 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
| 3941 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3942 | ** Change the lock state for a shared-memory segment. |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 3943 | ** |
| 3944 | ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little |
| 3945 | ** different here than in posix. In xShmLock(), one can go from unlocked |
| 3946 | ** to shared and back or from unlocked to exclusive and back. But one may |
| 3947 | ** not go from shared to exclusive or from exclusive to shared. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3948 | */ |
| 3949 | static int unixShmLock( |
| 3950 | sqlite3_file *fd, /* Database file holding the shared memory */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3951 | int ofst, /* First lock to acquire or release */ |
| 3952 | int n, /* Number of locks to acquire or release */ |
| 3953 | int flags /* What to do with the lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3954 | ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3955 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 3956 | unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 3957 | unixShm *pX; /* For looping over all siblings */ |
| 3958 | unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 3959 | int rc = SQLITE_OK; /* Result code */ |
| 3960 | u16 mask; /* Mask of locks to take or release */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3961 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3962 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 3963 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3964 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3965 | assert( n>=1 ); |
| 3966 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 3967 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 3968 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 3969 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 3970 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3971 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 3972 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3973 | |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3974 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3975 | assert( n>1 || mask==(1<<ofst) ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3976 | sqlite3_mutex_enter(pShmNode->mutex); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3977 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 3978 | u16 allMask = 0; /* Mask of locks held by siblings */ |
| 3979 | |
| 3980 | /* See if any siblings hold this same lock */ |
| 3981 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 3982 | if( pX==p ) continue; |
| 3983 | assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
| 3984 | allMask |= pX->sharedMask; |
| 3985 | } |
| 3986 | |
| 3987 | /* Unlock the system-level locks */ |
| 3988 | if( (mask & allMask)==0 ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3989 | rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3990 | }else{ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3991 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3992 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3993 | |
| 3994 | /* Undo the local locks */ |
| 3995 | if( rc==SQLITE_OK ){ |
| 3996 | p->exclMask &= ~mask; |
| 3997 | p->sharedMask &= ~mask; |
| 3998 | } |
| 3999 | }else if( flags & SQLITE_SHM_SHARED ){ |
| 4000 | u16 allShared = 0; /* Union of locks held by connections other than "p" */ |
| 4001 | |
| 4002 | /* Find out which shared locks are already held by sibling connections. |
| 4003 | ** If any sibling already holds an exclusive lock, go ahead and return |
| 4004 | ** SQLITE_BUSY. |
| 4005 | */ |
| 4006 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4007 | if( (pX->exclMask & mask)!=0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4008 | rc = SQLITE_BUSY; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4009 | break; |
| 4010 | } |
| 4011 | allShared |= pX->sharedMask; |
| 4012 | } |
| 4013 | |
| 4014 | /* Get shared locks at the system level, if necessary */ |
| 4015 | if( rc==SQLITE_OK ){ |
| 4016 | if( (allShared & mask)==0 ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4017 | rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4018 | }else{ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4019 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4020 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4021 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4022 | |
| 4023 | /* Get the local shared locks */ |
| 4024 | if( rc==SQLITE_OK ){ |
| 4025 | p->sharedMask |= mask; |
| 4026 | } |
| 4027 | }else{ |
| 4028 | /* Make sure no sibling connections hold locks that will block this |
| 4029 | ** lock. If any do, return SQLITE_BUSY right away. |
| 4030 | */ |
| 4031 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4032 | if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ |
| 4033 | rc = SQLITE_BUSY; |
| 4034 | break; |
| 4035 | } |
| 4036 | } |
| 4037 | |
| 4038 | /* Get the exclusive locks at the system level. Then if successful |
| 4039 | ** also mark the local connection as being locked. |
| 4040 | */ |
| 4041 | if( rc==SQLITE_OK ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4042 | rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4043 | if( rc==SQLITE_OK ){ |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4044 | assert( (p->sharedMask & mask)==0 ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4045 | p->exclMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4046 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4047 | } |
| 4048 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4049 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 4050 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
| 4051 | p->id, getpid(), p->sharedMask, p->exclMask)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4052 | return rc; |
| 4053 | } |
| 4054 | |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4055 | /* |
| 4056 | ** Implement a memory barrier or memory fence on shared memory. |
| 4057 | ** |
| 4058 | ** All loads and stores begun before the barrier must complete before |
| 4059 | ** any load or store begun after the barrier. |
| 4060 | */ |
| 4061 | static void unixShmBarrier( |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4062 | sqlite3_file *fd /* Database file holding the shared memory */ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4063 | ){ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 4064 | UNUSED_PARAMETER(fd); |
drh | b29ad85 | 2010-06-01 00:03:57 +0000 | [diff] [blame] | 4065 | unixEnterMutex(); |
| 4066 | unixLeaveMutex(); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4067 | } |
| 4068 | |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4069 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4070 | ** Close a connection to shared-memory. Delete the underlying |
| 4071 | ** storage if deleteFlag is true. |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 4072 | ** |
| 4073 | ** If there is no shared memory associated with the connection then this |
| 4074 | ** routine is a harmless no-op. |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4075 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4076 | static int unixShmUnmap( |
| 4077 | sqlite3_file *fd, /* The underlying database file */ |
| 4078 | int deleteFlag /* Delete shared-memory if true */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4079 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4080 | unixShm *p; /* The connection to be closed */ |
| 4081 | unixShmNode *pShmNode; /* The underlying shared-memory file */ |
| 4082 | unixShm **pp; /* For looping over sibling connections */ |
| 4083 | unixFile *pDbFd; /* The underlying database file */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4084 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4085 | pDbFd = (unixFile*)fd; |
| 4086 | p = pDbFd->pShm; |
| 4087 | if( p==0 ) return SQLITE_OK; |
| 4088 | pShmNode = p->pShmNode; |
| 4089 | |
| 4090 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4091 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4092 | |
| 4093 | /* Remove connection p from the set of connections associated |
| 4094 | ** with pShmNode */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4095 | sqlite3_mutex_enter(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4096 | for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 4097 | *pp = p->pNext; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4098 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4099 | /* Free the connection p */ |
| 4100 | sqlite3_free(p); |
| 4101 | pDbFd->pShm = 0; |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4102 | sqlite3_mutex_leave(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4103 | |
| 4104 | /* If pShmNode->nRef has reached 0, then close the underlying |
| 4105 | ** shared-memory file, too */ |
| 4106 | unixEnterMutex(); |
| 4107 | assert( pShmNode->nRef>0 ); |
| 4108 | pShmNode->nRef--; |
| 4109 | if( pShmNode->nRef==0 ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4110 | if( deleteFlag && pShmNode->h>=0 ) unlink(pShmNode->zFilename); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4111 | unixShmPurge(pDbFd); |
| 4112 | } |
| 4113 | unixLeaveMutex(); |
| 4114 | |
| 4115 | return SQLITE_OK; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4116 | } |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4117 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4118 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4119 | #else |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 4120 | # define unixShmMap 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4121 | # define unixShmLock 0 |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4122 | # define unixShmBarrier 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4123 | # define unixShmUnmap 0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4124 | #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 4125 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4126 | /* |
| 4127 | ** Here ends the implementation of all sqlite3_file methods. |
| 4128 | ** |
| 4129 | ********************** End sqlite3_file Methods ******************************* |
| 4130 | ******************************************************************************/ |
| 4131 | |
| 4132 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4133 | ** This division contains definitions of sqlite3_io_methods objects that |
| 4134 | ** implement various file locking strategies. It also contains definitions |
| 4135 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 4136 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 4137 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 4138 | ** the correct finder-function for that VFS. |
| 4139 | ** |
| 4140 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 4141 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 4142 | ** looks at the filesystem type and tries to guess the best locking |
| 4143 | ** strategy from that. |
| 4144 | ** |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4145 | ** For finder-funtion F, two objects are created: |
| 4146 | ** |
| 4147 | ** (1) The real finder-function named "FImpt()". |
| 4148 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4149 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4150 | ** |
| 4151 | ** |
| 4152 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 4153 | ** objects. We have to do this instead of letting pAppData point |
| 4154 | ** directly at the finder-function since C90 rules prevent a void* |
| 4155 | ** from be cast into a function pointer. |
| 4156 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4157 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4158 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4159 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4160 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 4161 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 4162 | ** |
| 4163 | ** * An I/O method finder function called FINDER that returns a pointer |
| 4164 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4165 | */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4166 | #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4167 | static const sqlite3_io_methods METHOD = { \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4168 | VERSION, /* iVersion */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4169 | CLOSE, /* xClose */ \ |
| 4170 | unixRead, /* xRead */ \ |
| 4171 | unixWrite, /* xWrite */ \ |
| 4172 | unixTruncate, /* xTruncate */ \ |
| 4173 | unixSync, /* xSync */ \ |
| 4174 | unixFileSize, /* xFileSize */ \ |
| 4175 | LOCK, /* xLock */ \ |
| 4176 | UNLOCK, /* xUnlock */ \ |
| 4177 | CKLOCK, /* xCheckReservedLock */ \ |
| 4178 | unixFileControl, /* xFileControl */ \ |
| 4179 | unixSectorSize, /* xSectorSize */ \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4180 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 4181 | unixShmMap, /* xShmMap */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4182 | unixShmLock, /* xShmLock */ \ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4183 | unixShmBarrier, /* xShmBarrier */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4184 | unixShmUnmap /* xShmUnmap */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4185 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4186 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 4187 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4188 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4189 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4190 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4191 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4192 | |
| 4193 | /* |
| 4194 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 4195 | ** locking strategies. Functions that return pointers to these methods |
| 4196 | ** are also created. |
| 4197 | */ |
| 4198 | IOMETHODS( |
| 4199 | posixIoFinder, /* Finder function name */ |
| 4200 | posixIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4201 | 2, /* shared memory is enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4202 | unixClose, /* xClose method */ |
| 4203 | unixLock, /* xLock method */ |
| 4204 | unixUnlock, /* xUnlock method */ |
| 4205 | unixCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4206 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4207 | IOMETHODS( |
| 4208 | nolockIoFinder, /* Finder function name */ |
| 4209 | nolockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4210 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4211 | nolockClose, /* xClose method */ |
| 4212 | nolockLock, /* xLock method */ |
| 4213 | nolockUnlock, /* xUnlock method */ |
| 4214 | nolockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4215 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4216 | IOMETHODS( |
| 4217 | dotlockIoFinder, /* Finder function name */ |
| 4218 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4219 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4220 | dotlockClose, /* xClose method */ |
| 4221 | dotlockLock, /* xLock method */ |
| 4222 | dotlockUnlock, /* xUnlock method */ |
| 4223 | dotlockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4224 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4225 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4226 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4227 | IOMETHODS( |
| 4228 | flockIoFinder, /* Finder function name */ |
| 4229 | flockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4230 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4231 | flockClose, /* xClose method */ |
| 4232 | flockLock, /* xLock method */ |
| 4233 | flockUnlock, /* xUnlock method */ |
| 4234 | flockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4235 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4236 | #endif |
| 4237 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4238 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4239 | IOMETHODS( |
| 4240 | semIoFinder, /* Finder function name */ |
| 4241 | semIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4242 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4243 | semClose, /* xClose method */ |
| 4244 | semLock, /* xLock method */ |
| 4245 | semUnlock, /* xUnlock method */ |
| 4246 | semCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4247 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4248 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4249 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4250 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4251 | IOMETHODS( |
| 4252 | afpIoFinder, /* Finder function name */ |
| 4253 | afpIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4254 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4255 | afpClose, /* xClose method */ |
| 4256 | afpLock, /* xLock method */ |
| 4257 | afpUnlock, /* xUnlock method */ |
| 4258 | afpCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4259 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4260 | #endif |
| 4261 | |
| 4262 | /* |
| 4263 | ** The proxy locking method is a "super-method" in the sense that it |
| 4264 | ** opens secondary file descriptors for the conch and lock files and |
| 4265 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 4266 | ** secondary files. For this reason, the division that implements |
| 4267 | ** proxy locking is located much further down in the file. But we need |
| 4268 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 4269 | ** for proxy locking here. So we forward declare the I/O methods. |
| 4270 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4271 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4272 | static int proxyClose(sqlite3_file*); |
| 4273 | static int proxyLock(sqlite3_file*, int); |
| 4274 | static int proxyUnlock(sqlite3_file*, int); |
| 4275 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4276 | IOMETHODS( |
| 4277 | proxyIoFinder, /* Finder function name */ |
| 4278 | proxyIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4279 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4280 | proxyClose, /* xClose method */ |
| 4281 | proxyLock, /* xLock method */ |
| 4282 | proxyUnlock, /* xUnlock method */ |
| 4283 | proxyCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4284 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4285 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4286 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4287 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 4288 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 4289 | IOMETHODS( |
| 4290 | nfsIoFinder, /* Finder function name */ |
| 4291 | nfsIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4292 | 1, /* shared memory is disabled */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4293 | unixClose, /* xClose method */ |
| 4294 | unixLock, /* xLock method */ |
| 4295 | nfsUnlock, /* xUnlock method */ |
| 4296 | unixCheckReservedLock /* xCheckReservedLock method */ |
| 4297 | ) |
| 4298 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4299 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4300 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4301 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4302 | ** This "finder" function attempts to determine the best locking strategy |
| 4303 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4304 | ** object that implements that strategy. |
| 4305 | ** |
| 4306 | ** This is for MacOSX only. |
| 4307 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4308 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4309 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4310 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4311 | ){ |
| 4312 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4313 | const char *zFilesystem; /* Filesystem type name */ |
| 4314 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4315 | } aMap[] = { |
| 4316 | { "hfs", &posixIoMethods }, |
| 4317 | { "ufs", &posixIoMethods }, |
| 4318 | { "afpfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4319 | { "smbfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4320 | { "webdav", &nolockIoMethods }, |
| 4321 | { 0, 0 } |
| 4322 | }; |
| 4323 | int i; |
| 4324 | struct statfs fsInfo; |
| 4325 | struct flock lockInfo; |
| 4326 | |
| 4327 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4328 | /* If filePath==NULL that means we are dealing with a transient file |
| 4329 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4330 | return &nolockIoMethods; |
| 4331 | } |
| 4332 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 4333 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 4334 | return &nolockIoMethods; |
| 4335 | } |
| 4336 | for(i=0; aMap[i].zFilesystem; i++){ |
| 4337 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 4338 | return aMap[i].pMethods; |
| 4339 | } |
| 4340 | } |
| 4341 | } |
| 4342 | |
| 4343 | /* Default case. Handles, amongst others, "nfs". |
| 4344 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 4345 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4346 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4347 | lockInfo.l_len = 1; |
| 4348 | lockInfo.l_start = 0; |
| 4349 | lockInfo.l_whence = SEEK_SET; |
| 4350 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4351 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4352 | if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 4353 | return &nfsIoMethods; |
| 4354 | } else { |
| 4355 | return &posixIoMethods; |
| 4356 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4357 | }else{ |
| 4358 | return &dotlockIoMethods; |
| 4359 | } |
| 4360 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4361 | static const sqlite3_io_methods |
| 4362 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4363 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4364 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4365 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4366 | #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE |
| 4367 | /* |
| 4368 | ** This "finder" function attempts to determine the best locking strategy |
| 4369 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
| 4370 | ** object that implements that strategy. |
| 4371 | ** |
| 4372 | ** This is for VXWorks only. |
| 4373 | */ |
| 4374 | static const sqlite3_io_methods *autolockIoFinderImpl( |
| 4375 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4376 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4377 | ){ |
| 4378 | struct flock lockInfo; |
| 4379 | |
| 4380 | if( !filePath ){ |
| 4381 | /* If filePath==NULL that means we are dealing with a transient file |
| 4382 | ** that does not need to be locked. */ |
| 4383 | return &nolockIoMethods; |
| 4384 | } |
| 4385 | |
| 4386 | /* Test if fcntl() is supported and use POSIX style locks. |
| 4387 | ** Otherwise fall back to the named semaphore method. |
| 4388 | */ |
| 4389 | lockInfo.l_len = 1; |
| 4390 | lockInfo.l_start = 0; |
| 4391 | lockInfo.l_whence = SEEK_SET; |
| 4392 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4393 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4394 | return &posixIoMethods; |
| 4395 | }else{ |
| 4396 | return &semIoMethods; |
| 4397 | } |
| 4398 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4399 | static const sqlite3_io_methods |
| 4400 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4401 | |
| 4402 | #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */ |
| 4403 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4404 | /* |
| 4405 | ** An abstract type for a pointer to a IO method finder function: |
| 4406 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4407 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4408 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4409 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4410 | /**************************************************************************** |
| 4411 | **************************** sqlite3_vfs methods **************************** |
| 4412 | ** |
| 4413 | ** This division contains the implementation of methods on the |
| 4414 | ** sqlite3_vfs object. |
| 4415 | */ |
| 4416 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4417 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4418 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4419 | */ |
| 4420 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4421 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4422 | int h, /* Open file descriptor of file being opened */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4423 | int dirfd, /* Directory file descriptor */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4424 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4425 | const char *zFilename, /* Name of the file being opened */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4426 | int noLock, /* Omit locking if true */ |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 4427 | int isDelete, /* Delete on close if true */ |
| 4428 | int isReadOnly /* True if the file is opened read-only */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4429 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4430 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4431 | unixFile *pNew = (unixFile *)pId; |
| 4432 | int rc = SQLITE_OK; |
| 4433 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4434 | assert( pNew->pInode==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4435 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4436 | /* Parameter isDelete is only used on vxworks. Express this explicitly |
| 4437 | ** here to prevent compiler warnings about unused parameters. |
danielk1977 | a03396a | 2008-11-19 14:35:46 +0000 | [diff] [blame] | 4438 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4439 | UNUSED_PARAMETER(isDelete); |
danielk1977 | a03396a | 2008-11-19 14:35:46 +0000 | [diff] [blame] | 4440 | |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4441 | /* Usually the path zFilename should not be a relative pathname. The |
| 4442 | ** exception is when opening the proxy "conch" file in builds that |
| 4443 | ** include the special Apple locking styles. |
| 4444 | */ |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4445 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | f7f55ed | 2010-10-05 18:22:47 +0000 | [diff] [blame] | 4446 | assert( zFilename==0 || zFilename[0]=='/' |
| 4447 | || pVfs->pAppData==(void*)&autolockIoFinder ); |
| 4448 | #else |
| 4449 | assert( zFilename==0 || zFilename[0]=='/' ); |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4450 | #endif |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4451 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4452 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4453 | pNew->h = h; |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4454 | pNew->dirfd = dirfd; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4455 | pNew->zPath = zFilename; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 4456 | if( memcmp(pVfs->zName,"unix-excl",10)==0 ){ |
| 4457 | pNew->ctrlFlags = UNIXFILE_EXCL; |
| 4458 | }else{ |
| 4459 | pNew->ctrlFlags = 0; |
| 4460 | } |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 4461 | if( isReadOnly ){ |
| 4462 | pNew->ctrlFlags |= UNIXFILE_RDONLY; |
| 4463 | } |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 4464 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4465 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 4466 | pNew->pId = vxworksFindFileId(zFilename); |
| 4467 | if( pNew->pId==0 ){ |
| 4468 | noLock = 1; |
| 4469 | rc = SQLITE_NOMEM; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4470 | } |
| 4471 | #endif |
| 4472 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4473 | if( noLock ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4474 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4475 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4476 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4477 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4478 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 4479 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 4480 | ** zFilename remains valid until file is closed, to support */ |
| 4481 | pNew->lockingContext = (void*)zFilename; |
| 4482 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4483 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4484 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4485 | if( pLockingStyle == &posixIoMethods |
| 4486 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 4487 | || pLockingStyle == &nfsIoMethods |
| 4488 | #endif |
| 4489 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4490 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4491 | rc = findInodeInfo(pNew, &pNew->pInode); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4492 | if( rc!=SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4493 | /* If an error occured in findInodeInfo(), close the file descriptor |
| 4494 | ** immediately, before releasing the mutex. findInodeInfo() may fail |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4495 | ** in two scenarios: |
| 4496 | ** |
| 4497 | ** (a) A call to fstat() failed. |
| 4498 | ** (b) A malloc failed. |
| 4499 | ** |
| 4500 | ** Scenario (b) may only occur if the process is holding no other |
| 4501 | ** file descriptors open on the same file. If there were other file |
| 4502 | ** descriptors on this file, then no malloc would be required by |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4503 | ** findInodeInfo(). If this is the case, it is quite safe to close |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4504 | ** handle h - as it is guaranteed that no posix locks will be released |
| 4505 | ** by doing so. |
| 4506 | ** |
| 4507 | ** If scenario (a) caused the error then things are not so safe. The |
| 4508 | ** implicit assumption here is that if fstat() fails, things are in |
| 4509 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 4510 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4511 | robust_close(pNew, h, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4512 | h = -1; |
| 4513 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4514 | unixLeaveMutex(); |
| 4515 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4516 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4517 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 4518 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4519 | /* AFP locking uses the file path so it needs to be included in |
| 4520 | ** the afpLockingContext. |
| 4521 | */ |
| 4522 | afpLockingContext *pCtx; |
| 4523 | pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 4524 | if( pCtx==0 ){ |
| 4525 | rc = SQLITE_NOMEM; |
| 4526 | }else{ |
| 4527 | /* NB: zFilename exists and remains valid until the file is closed |
| 4528 | ** according to requirement F11141. So we do not need to make a |
| 4529 | ** copy of the filename. */ |
| 4530 | pCtx->dbPath = zFilename; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4531 | pCtx->reserved = 0; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4532 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4533 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4534 | rc = findInodeInfo(pNew, &pNew->pInode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4535 | if( rc!=SQLITE_OK ){ |
| 4536 | sqlite3_free(pNew->lockingContext); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4537 | robust_close(pNew, h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4538 | h = -1; |
| 4539 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4540 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4541 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4542 | } |
| 4543 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4544 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4545 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 4546 | /* Dotfile locking uses the file path so it needs to be included in |
| 4547 | ** the dotlockLockingContext |
| 4548 | */ |
| 4549 | char *zLockFile; |
| 4550 | int nFilename; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4551 | nFilename = (int)strlen(zFilename) + 6; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4552 | zLockFile = (char *)sqlite3_malloc(nFilename); |
| 4553 | if( zLockFile==0 ){ |
| 4554 | rc = SQLITE_NOMEM; |
| 4555 | }else{ |
| 4556 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4557 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4558 | pNew->lockingContext = zLockFile; |
| 4559 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4560 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4561 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4562 | else if( pLockingStyle == &semIoMethods ){ |
| 4563 | /* Named semaphore locking uses the file path so it needs to be |
| 4564 | ** included in the semLockingContext |
| 4565 | */ |
| 4566 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4567 | rc = findInodeInfo(pNew, &pNew->pInode); |
| 4568 | if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 4569 | char *zSemName = pNew->pInode->aSemName; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4570 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 4571 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4572 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 4573 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4574 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4575 | pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 4576 | if( pNew->pInode->pSem == SEM_FAILED ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4577 | rc = SQLITE_NOMEM; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4578 | pNew->pInode->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4579 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4580 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4581 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4582 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4583 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 4584 | |
| 4585 | pNew->lastErrno = 0; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4586 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4587 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4588 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
drh | 309e655 | 2010-02-05 18:00:26 +0000 | [diff] [blame] | 4589 | h = -1; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4590 | unlink(zFilename); |
| 4591 | isDelete = 0; |
| 4592 | } |
| 4593 | pNew->isDelete = isDelete; |
| 4594 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4595 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4596 | if( dirfd>=0 ) robust_close(pNew, dirfd, __LINE__); |
| 4597 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4598 | }else{ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4599 | pNew->pMethod = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4600 | OpenCounter(+1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4601 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4602 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 4603 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 4604 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4605 | /* |
| 4606 | ** Open a file descriptor to the directory containing file zFilename. |
| 4607 | ** If successful, *pFd is set to the opened file descriptor and |
| 4608 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 4609 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 4610 | ** value. |
| 4611 | ** |
| 4612 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 4613 | ** the file descriptor *pFd using close(). |
| 4614 | */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4615 | static int openDirectory(const char *zFilename, int *pFd){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4616 | int ii; |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 4617 | int fd = -1; |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 4618 | char zDirname[MAX_PATHNAME+1]; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4619 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4620 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
drh | 617634e | 2009-01-08 14:36:20 +0000 | [diff] [blame] | 4621 | for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4622 | if( ii>0 ){ |
| 4623 | zDirname[ii] = '\0'; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 4624 | fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0); |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 4625 | if( fd>=0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4626 | #ifdef FD_CLOEXEC |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4627 | osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4628 | #endif |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4629 | OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4630 | } |
| 4631 | } |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4632 | *pFd = fd; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 4633 | return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname)); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4634 | } |
| 4635 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4636 | /* |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4637 | ** Return the name of a directory in which to put temporary files. |
| 4638 | ** If no suitable temporary file directory can be found, return NULL. |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4639 | */ |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4640 | static const char *unixTempFileDir(void){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4641 | static const char *azDirs[] = { |
| 4642 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4643 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4644 | "/var/tmp", |
| 4645 | "/usr/tmp", |
| 4646 | "/tmp", |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4647 | 0 /* List terminator */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4648 | }; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4649 | unsigned int i; |
| 4650 | struct stat buf; |
| 4651 | const char *zDir = 0; |
| 4652 | |
| 4653 | azDirs[0] = sqlite3_temp_directory; |
| 4654 | if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); |
drh | 19515c8 | 2010-06-19 23:53:11 +0000 | [diff] [blame] | 4655 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){ |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4656 | if( zDir==0 ) continue; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4657 | if( osStat(zDir, &buf) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4658 | if( !S_ISDIR(buf.st_mode) ) continue; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4659 | if( osAccess(zDir, 07) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4660 | break; |
| 4661 | } |
| 4662 | return zDir; |
| 4663 | } |
| 4664 | |
| 4665 | /* |
| 4666 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 4667 | ** by the calling process and must be big enough to hold at least |
| 4668 | ** pVfs->mxPathname bytes. |
| 4669 | */ |
| 4670 | static int unixGetTempname(int nBuf, char *zBuf){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4671 | static const unsigned char zChars[] = |
| 4672 | "abcdefghijklmnopqrstuvwxyz" |
| 4673 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 4674 | "0123456789"; |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 4675 | unsigned int i, j; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4676 | const char *zDir; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4677 | |
| 4678 | /* It's odd to simulate an io-error here, but really this is just |
| 4679 | ** using the io-error infrastructure to test that SQLite handles this |
| 4680 | ** function failing. |
| 4681 | */ |
| 4682 | SimulateIOError( return SQLITE_IOERR ); |
| 4683 | |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4684 | zDir = unixTempFileDir(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4685 | if( zDir==0 ) zDir = "."; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4686 | |
| 4687 | /* Check that the output buffer is large enough for the temporary file |
| 4688 | ** name. If it is not, return SQLITE_ERROR. |
| 4689 | */ |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 4690 | if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4691 | return SQLITE_ERROR; |
| 4692 | } |
| 4693 | |
| 4694 | do{ |
| 4695 | sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4696 | j = (int)strlen(zBuf); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4697 | sqlite3_randomness(15, &zBuf[j]); |
| 4698 | for(i=0; i<15; i++, j++){ |
| 4699 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 4700 | } |
| 4701 | zBuf[j] = 0; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4702 | }while( osAccess(zBuf,0)==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4703 | return SQLITE_OK; |
| 4704 | } |
| 4705 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4706 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 4707 | /* |
| 4708 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 4709 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 4710 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 4711 | */ |
| 4712 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 4713 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 4714 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4715 | /* |
| 4716 | ** Search for an unused file descriptor that was opened on the database |
| 4717 | ** file (not a journal or master-journal file) identified by pathname |
| 4718 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 4719 | ** argument to this function. |
| 4720 | ** |
| 4721 | ** Such a file descriptor may exist if a database connection was closed |
| 4722 | ** but the associated file descriptor could not be closed because some |
| 4723 | ** other file descriptor open on the same file is holding a file-lock. |
| 4724 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 4725 | ** describing "Posix Advisory Locking" at the start of this file for |
| 4726 | ** further details. Also, ticket #4018. |
| 4727 | ** |
| 4728 | ** If a suitable file descriptor is found, then it is returned. If no |
| 4729 | ** such file descriptor is located, -1 is returned. |
| 4730 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4731 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 4732 | UnixUnusedFd *pUnused = 0; |
| 4733 | |
| 4734 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 4735 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 4736 | ** but because no way to test it is currently available. It is better |
| 4737 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 4738 | ** feature. */ |
| 4739 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4740 | struct stat sStat; /* Results of stat() call */ |
| 4741 | |
| 4742 | /* A stat() call may fail for various reasons. If this happens, it is |
| 4743 | ** almost certain that an open() call on the same path will also fail. |
| 4744 | ** For this reason, if an error occurs in the stat() call here, it is |
| 4745 | ** ignored and -1 is returned. The caller will try to open a new file |
| 4746 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 4747 | ** |
| 4748 | ** Even if a subsequent open() call does succeed, the consequences of |
| 4749 | ** not searching for a resusable file descriptor are not dire. */ |
| 4750 | if( 0==stat(zPath, &sStat) ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4751 | unixInodeInfo *pInode; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4752 | |
| 4753 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4754 | pInode = inodeList; |
| 4755 | while( pInode && (pInode->fileId.dev!=sStat.st_dev |
| 4756 | || pInode->fileId.ino!=sStat.st_ino) ){ |
| 4757 | pInode = pInode->pNext; |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 4758 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4759 | if( pInode ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4760 | UnixUnusedFd **pp; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4761 | for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4762 | pUnused = *pp; |
| 4763 | if( pUnused ){ |
| 4764 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4765 | } |
| 4766 | } |
| 4767 | unixLeaveMutex(); |
| 4768 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4769 | #endif /* if !OS_VXWORKS */ |
| 4770 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4771 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4772 | |
| 4773 | /* |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4774 | ** This function is called by unixOpen() to determine the unix permissions |
drh | f65bc91 | 2010-07-14 20:51:34 +0000 | [diff] [blame] | 4775 | ** 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] | 4776 | ** and a value suitable for passing as the third argument to open(2) is |
| 4777 | ** written to *pMode. If an IO error occurs, an SQLite error code is |
| 4778 | ** returned and the value of *pMode is not modified. |
| 4779 | ** |
| 4780 | ** If the file being opened is a temporary file, it is always created with |
| 4781 | ** the octal permissions 0600 (read/writable by owner only). If the file |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4782 | ** is a database or master journal file, it is created with the permissions |
| 4783 | ** mask SQLITE_DEFAULT_FILE_PERMISSIONS. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4784 | ** |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4785 | ** Finally, if the file being opened is a WAL or regular journal file, then |
| 4786 | ** this function queries the file-system for the permissions on the |
| 4787 | ** corresponding database file and sets *pMode to this value. Whenever |
| 4788 | ** possible, WAL and journal files are created using the same permissions |
| 4789 | ** as the associated database file. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4790 | */ |
| 4791 | static int findCreateFileMode( |
| 4792 | const char *zPath, /* Path of file (possibly) being created */ |
| 4793 | int flags, /* Flags passed as 4th argument to xOpen() */ |
| 4794 | mode_t *pMode /* OUT: Permissions to open file with */ |
| 4795 | ){ |
| 4796 | int rc = SQLITE_OK; /* Return Code */ |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4797 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4798 | char zDb[MAX_PATHNAME+1]; /* Database file path */ |
| 4799 | int nDb; /* Number of valid bytes in zDb */ |
| 4800 | struct stat sStat; /* Output of stat() on database file */ |
| 4801 | |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 4802 | /* zPath is a path to a WAL or journal file. The following block derives |
| 4803 | ** the path to the associated database file from zPath. This block handles |
| 4804 | ** the following naming conventions: |
| 4805 | ** |
| 4806 | ** "<path to db>-journal" |
| 4807 | ** "<path to db>-wal" |
| 4808 | ** "<path to db>-journal-NNNN" |
| 4809 | ** "<path to db>-wal-NNNN" |
| 4810 | ** |
| 4811 | ** where NNNN is a 4 digit decimal number. The NNNN naming schemes are |
| 4812 | ** used by the test_multiplex.c module. |
| 4813 | */ |
| 4814 | nDb = sqlite3Strlen30(zPath) - 1; |
| 4815 | while( nDb>0 && zPath[nDb]!='l' ) nDb--; |
| 4816 | nDb -= ((flags & SQLITE_OPEN_WAL) ? 3 : 7); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4817 | memcpy(zDb, zPath, nDb); |
| 4818 | zDb[nDb] = '\0'; |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 4819 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4820 | if( 0==stat(zDb, &sStat) ){ |
| 4821 | *pMode = sStat.st_mode & 0777; |
| 4822 | }else{ |
| 4823 | rc = SQLITE_IOERR_FSTAT; |
| 4824 | } |
| 4825 | }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 4826 | *pMode = 0600; |
| 4827 | }else{ |
| 4828 | *pMode = SQLITE_DEFAULT_FILE_PERMISSIONS; |
| 4829 | } |
| 4830 | return rc; |
| 4831 | } |
| 4832 | |
| 4833 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4834 | ** Open the file zPath. |
| 4835 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4836 | ** Previously, the SQLite OS layer used three functions in place of this |
| 4837 | ** one: |
| 4838 | ** |
| 4839 | ** sqlite3OsOpenReadWrite(); |
| 4840 | ** sqlite3OsOpenReadOnly(); |
| 4841 | ** sqlite3OsOpenExclusive(); |
| 4842 | ** |
| 4843 | ** These calls correspond to the following combinations of flags: |
| 4844 | ** |
| 4845 | ** ReadWrite() -> (READWRITE | CREATE) |
| 4846 | ** ReadOnly() -> (READONLY) |
| 4847 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 4848 | ** |
| 4849 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 4850 | ** true, the file was configured to be automatically deleted when the |
| 4851 | ** file handle closed. To achieve the same effect using this new |
| 4852 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 4853 | ** OpenExclusive(). |
| 4854 | */ |
| 4855 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4856 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 4857 | const char *zPath, /* Pathname of file to be opened */ |
| 4858 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 4859 | int flags, /* Input flags to control the opening */ |
| 4860 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4861 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4862 | unixFile *p = (unixFile *)pFile; |
| 4863 | int fd = -1; /* File descriptor returned by open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4864 | int dirfd = -1; /* Directory file descriptor */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4865 | int openFlags = 0; /* Flags to pass to open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4866 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4867 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4868 | int rc = SQLITE_OK; /* Function Return Code */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4869 | |
| 4870 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 4871 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 4872 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 4873 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 4874 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4875 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4876 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 4877 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4878 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4879 | /* If creating a master or main-file journal, this function will open |
| 4880 | ** a file-descriptor on the directory too. The first time unixSync() |
| 4881 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 4882 | */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4883 | int isOpenDirectory = (isCreate && ( |
| 4884 | eType==SQLITE_OPEN_MASTER_JOURNAL |
| 4885 | || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 4886 | || eType==SQLITE_OPEN_WAL |
| 4887 | )); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4888 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4889 | /* If argument zPath is a NULL pointer, this function is required to open |
| 4890 | ** a temporary file. Use this buffer to store the file name in. |
| 4891 | */ |
| 4892 | char zTmpname[MAX_PATHNAME+1]; |
| 4893 | const char *zName = zPath; |
| 4894 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4895 | /* Check the following statements are true: |
| 4896 | ** |
| 4897 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 4898 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 4899 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4900 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4901 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4902 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4903 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4904 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4905 | assert(isDelete==0 || isCreate); |
| 4906 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4907 | /* The main DB, main journal, WAL file and master journal are never |
| 4908 | ** automatically deleted. Nor are they ever temporary files. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4909 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 4910 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 4911 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4912 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4913 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4914 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 4915 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 4916 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 4917 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4918 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4919 | ); |
| 4920 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4921 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4922 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4923 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4924 | UnixUnusedFd *pUnused; |
| 4925 | pUnused = findReusableFd(zName, flags); |
| 4926 | if( pUnused ){ |
| 4927 | fd = pUnused->fd; |
| 4928 | }else{ |
dan | 6aa657f | 2009-08-24 18:57:58 +0000 | [diff] [blame] | 4929 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4930 | if( !pUnused ){ |
| 4931 | return SQLITE_NOMEM; |
| 4932 | } |
| 4933 | } |
| 4934 | p->pUnused = pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4935 | }else if( !zName ){ |
| 4936 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4937 | assert(isDelete && !isOpenDirectory); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4938 | rc = unixGetTempname(MAX_PATHNAME+1, zTmpname); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4939 | if( rc!=SQLITE_OK ){ |
| 4940 | return rc; |
| 4941 | } |
| 4942 | zName = zTmpname; |
| 4943 | } |
| 4944 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4945 | /* Determine the value of the flags parameter passed to POSIX function |
| 4946 | ** open(). These must be calculated even if open() is not called, as |
| 4947 | ** they may be stored as part of the file handle and used by the |
| 4948 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4949 | if( isReadonly ) openFlags |= O_RDONLY; |
| 4950 | if( isReadWrite ) openFlags |= O_RDWR; |
| 4951 | if( isCreate ) openFlags |= O_CREAT; |
| 4952 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 4953 | openFlags |= (O_LARGEFILE|O_BINARY); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4954 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4955 | if( fd<0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4956 | mode_t openMode; /* Permissions to create file with */ |
| 4957 | rc = findCreateFileMode(zName, flags, &openMode); |
| 4958 | if( rc!=SQLITE_OK ){ |
| 4959 | assert( !p->pUnused ); |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4960 | assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4961 | return rc; |
| 4962 | } |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 4963 | fd = robust_open(zName, openFlags, openMode); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4964 | OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4965 | if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ |
| 4966 | /* Failed to open the file for read/write access. Try read-only. */ |
| 4967 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4968 | openFlags &= ~(O_RDWR|O_CREAT); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4969 | flags |= SQLITE_OPEN_READONLY; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4970 | openFlags |= O_RDONLY; |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 4971 | isReadonly = 1; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 4972 | fd = robust_open(zName, openFlags, openMode); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4973 | } |
| 4974 | if( fd<0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 4975 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4976 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4977 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4978 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4979 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4980 | if( pOutFlags ){ |
| 4981 | *pOutFlags = flags; |
| 4982 | } |
| 4983 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4984 | if( p->pUnused ){ |
| 4985 | p->pUnused->fd = fd; |
| 4986 | p->pUnused->flags = flags; |
| 4987 | } |
| 4988 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4989 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4990 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4991 | zPath = zName; |
| 4992 | #else |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4993 | unlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4994 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4995 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 4996 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4997 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4998 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 4999 | } |
| 5000 | #endif |
| 5001 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5002 | if( isOpenDirectory ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5003 | rc = openDirectory(zPath, &dirfd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5004 | if( rc!=SQLITE_OK ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5005 | /* It is safe to close fd at this point, because it is guaranteed not |
| 5006 | ** 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] | 5007 | ** it would not be safe to close as this would release any locks held |
| 5008 | ** on the file by this process. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5009 | assert( eType!=SQLITE_OPEN_MAIN_DB ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5010 | robust_close(p, fd, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5011 | goto open_finished; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5012 | } |
| 5013 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5014 | |
| 5015 | #ifdef FD_CLOEXEC |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5016 | osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5017 | #endif |
| 5018 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5019 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5020 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5021 | |
| 5022 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 5023 | struct statfs fsInfo; |
| 5024 | if( fstatfs(fd, &fsInfo) == -1 ){ |
| 5025 | ((unixFile*)pFile)->lastErrno = errno; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5026 | if( dirfd>=0 ) robust_close(p, dirfd, __LINE__); |
| 5027 | robust_close(p, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5028 | return SQLITE_IOERR_ACCESS; |
| 5029 | } |
| 5030 | if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 5031 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 5032 | } |
| 5033 | #endif |
| 5034 | |
| 5035 | #if SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5036 | #if SQLITE_PREFER_PROXY_LOCKING |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5037 | isAutoProxy = 1; |
| 5038 | #endif |
| 5039 | if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5040 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 5041 | int useProxy = 0; |
| 5042 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5043 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 5044 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5045 | if( envforce!=NULL ){ |
| 5046 | useProxy = atoi(envforce)>0; |
| 5047 | }else{ |
| 5048 | struct statfs fsInfo; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5049 | if( statfs(zPath, &fsInfo) == -1 ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5050 | /* In theory, the close(fd) call is sub-optimal. If the file opened |
| 5051 | ** with fd is a database file, and there are other connections open |
| 5052 | ** on that file that are currently holding advisory locks on it, |
| 5053 | ** then the call to close() will cancel those locks. In practice, |
| 5054 | ** we're assuming that statfs() doesn't fail very often. At least |
| 5055 | ** not while other file descriptors opened by the same process on |
| 5056 | ** the same file are working. */ |
| 5057 | p->lastErrno = errno; |
| 5058 | if( dirfd>=0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5059 | robust_close(p, dirfd, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5060 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5061 | robust_close(p, fd, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5062 | rc = SQLITE_IOERR_ACCESS; |
| 5063 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5064 | } |
| 5065 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 5066 | } |
| 5067 | if( useProxy ){ |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 5068 | rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, |
| 5069 | isDelete, isReadonly); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5070 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5071 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5072 | if( rc!=SQLITE_OK ){ |
| 5073 | /* Use unixClose to clean up the resources added in fillInUnixFile |
| 5074 | ** and clear all the structure's references. Specifically, |
| 5075 | ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 5076 | */ |
| 5077 | unixClose(pFile); |
| 5078 | return rc; |
| 5079 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5080 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5081 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5082 | } |
| 5083 | } |
| 5084 | #endif |
| 5085 | |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 5086 | rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, |
| 5087 | isDelete, isReadonly); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5088 | open_finished: |
| 5089 | if( rc!=SQLITE_OK ){ |
| 5090 | sqlite3_free(p->pUnused); |
| 5091 | } |
| 5092 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5093 | } |
| 5094 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5095 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5096 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5097 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 5098 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5099 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5100 | static int unixDelete( |
| 5101 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 5102 | const char *zPath, /* Name of file to be deleted */ |
| 5103 | int dirSync /* If true, fsync() directory after deleting file */ |
| 5104 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5105 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5106 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5107 | SimulateIOError(return SQLITE_IOERR_DELETE); |
drh | 5d4feff | 2010-07-14 01:45:22 +0000 | [diff] [blame] | 5108 | if( unlink(zPath)==(-1) && errno!=ENOENT ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5109 | return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath); |
drh | 5d4feff | 2010-07-14 01:45:22 +0000 | [diff] [blame] | 5110 | } |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 5111 | #ifndef SQLITE_DISABLE_DIRSYNC |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5112 | if( dirSync ){ |
| 5113 | int fd; |
| 5114 | rc = openDirectory(zPath, &fd); |
| 5115 | if( rc==SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5116 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5117 | if( fsync(fd)==-1 ) |
| 5118 | #else |
| 5119 | if( fsync(fd) ) |
| 5120 | #endif |
| 5121 | { |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5122 | rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5123 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5124 | robust_close(0, fd, __LINE__); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5125 | } |
| 5126 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 5127 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5128 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5129 | } |
| 5130 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5131 | /* |
| 5132 | ** Test the existance of or access permissions of file zPath. The |
| 5133 | ** test performed depends on the value of flags: |
| 5134 | ** |
| 5135 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 5136 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 5137 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 5138 | ** |
| 5139 | ** Otherwise return 0. |
| 5140 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5141 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5142 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 5143 | const char *zPath, /* Path of the file to examine */ |
| 5144 | int flags, /* What do we want to learn about the zPath file? */ |
| 5145 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5146 | ){ |
rse | 25c0d1a | 2007-09-20 08:38:14 +0000 | [diff] [blame] | 5147 | int amode = 0; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5148 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5149 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5150 | switch( flags ){ |
| 5151 | case SQLITE_ACCESS_EXISTS: |
| 5152 | amode = F_OK; |
| 5153 | break; |
| 5154 | case SQLITE_ACCESS_READWRITE: |
| 5155 | amode = W_OK|R_OK; |
| 5156 | break; |
drh | 50d3f90 | 2007-08-27 21:10:36 +0000 | [diff] [blame] | 5157 | case SQLITE_ACCESS_READ: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5158 | amode = R_OK; |
| 5159 | break; |
| 5160 | |
| 5161 | default: |
| 5162 | assert(!"Invalid flags argument"); |
| 5163 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5164 | *pResOut = (osAccess(zPath, amode)==0); |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 5165 | if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){ |
| 5166 | struct stat buf; |
| 5167 | if( 0==stat(zPath, &buf) && buf.st_size==0 ){ |
| 5168 | *pResOut = 0; |
| 5169 | } |
| 5170 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5171 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5172 | } |
| 5173 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5174 | |
| 5175 | /* |
| 5176 | ** Turn a relative pathname into a full pathname. The relative path |
| 5177 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 5178 | ** zPath. |
| 5179 | ** |
| 5180 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 5181 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 5182 | ** this buffer before returning. |
| 5183 | */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 5184 | static int unixFullPathname( |
| 5185 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 5186 | const char *zPath, /* Possibly relative input path */ |
| 5187 | int nOut, /* Size of output buffer in bytes */ |
| 5188 | char *zOut /* Output buffer */ |
| 5189 | ){ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 5190 | |
| 5191 | /* It's odd to simulate an io-error here, but really this is just |
| 5192 | ** using the io-error infrastructure to test that SQLite handles this |
| 5193 | ** function failing. This function could fail if, for example, the |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5194 | ** current working directory has been unlinked. |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 5195 | */ |
| 5196 | SimulateIOError( return SQLITE_ERROR ); |
| 5197 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 5198 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 5199 | UNUSED_PARAMETER(pVfs); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5200 | |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 5201 | zOut[nOut-1] = '\0'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5202 | if( zPath[0]=='/' ){ |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 5203 | sqlite3_snprintf(nOut, zOut, "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5204 | }else{ |
| 5205 | int nCwd; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5206 | if( osGetcwd(zOut, nOut-1)==0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5207 | return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5208 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5209 | nCwd = (int)strlen(zOut); |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 5210 | sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5211 | } |
| 5212 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5213 | } |
| 5214 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 5215 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5216 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 5217 | /* |
| 5218 | ** Interfaces for opening a shared library, finding entry points |
| 5219 | ** within the shared library, and closing the shared library. |
| 5220 | */ |
| 5221 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5222 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 5223 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5224 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 5225 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 5226 | |
| 5227 | /* |
| 5228 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 5229 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 5230 | ** message is available, it is written to zBufOut. If no error message |
| 5231 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 5232 | ** error message. |
| 5233 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5234 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
dan | 3239053 | 2010-11-29 18:36:22 +0000 | [diff] [blame] | 5235 | const char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5236 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5237 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5238 | zErr = dlerror(); |
| 5239 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 5240 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5241 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5242 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5243 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5244 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 5245 | /* |
| 5246 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 5247 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 5248 | ** returns a void* which is really a pointer to a function. So how do we |
| 5249 | ** use dlsym() with -pedantic-errors? |
| 5250 | ** |
| 5251 | ** Variable x below is defined to be a pointer to a function taking |
| 5252 | ** parameters void* and const char* and returning a pointer to a function. |
| 5253 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 5254 | ** (That assignment requires a cast.) Then we call the function that |
| 5255 | ** x points to. |
| 5256 | ** |
| 5257 | ** This work-around is unlikely to work correctly on any system where |
| 5258 | ** you really cannot cast a function pointer into void*. But then, on the |
| 5259 | ** other hand, dlsym() will not work on such a system either, so we have |
| 5260 | ** not really lost anything. |
| 5261 | */ |
| 5262 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5263 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5264 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 5265 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5266 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5267 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 5268 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5269 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5270 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5271 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 5272 | #define unixDlOpen 0 |
| 5273 | #define unixDlError 0 |
| 5274 | #define unixDlSym 0 |
| 5275 | #define unixDlClose 0 |
| 5276 | #endif |
| 5277 | |
| 5278 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5279 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5280 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5281 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 5282 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 5283 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5284 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5285 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 5286 | ** errors. The reports issued by valgrind are incorrect - we would |
| 5287 | ** prefer that the randomness be increased by making use of the |
| 5288 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 5289 | ** some users. Rather than argue, it seems easier just to initialize |
| 5290 | ** the whole array and silence valgrind, even if that means less randomness |
| 5291 | ** in the random seed. |
| 5292 | ** |
| 5293 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 5294 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5295 | ** tests repeatable. |
| 5296 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5297 | memset(zBuf, 0, nBuf); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5298 | #if !defined(SQLITE_TEST) |
| 5299 | { |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5300 | int pid, fd; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5301 | fd = robust_open("/dev/urandom", O_RDONLY, 0); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5302 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 5303 | time_t t; |
| 5304 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5305 | memcpy(zBuf, &t, sizeof(t)); |
| 5306 | pid = getpid(); |
| 5307 | memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid)); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 5308 | assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf ); |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 5309 | nBuf = sizeof(t) + sizeof(pid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5310 | }else{ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 5311 | do{ nBuf = osRead(fd, zBuf, nBuf); }while( nBuf<0 && errno==EINTR ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5312 | robust_close(0, fd, __LINE__); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5313 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5314 | } |
| 5315 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 5316 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5317 | } |
| 5318 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5319 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5320 | /* |
| 5321 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5322 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 5323 | ** The return value is the number of microseconds of sleep actually |
| 5324 | ** requested from the underlying operating system, a number which |
| 5325 | ** might be greater than or equal to the argument, but not less |
| 5326 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5327 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5328 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5329 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5330 | struct timespec sp; |
| 5331 | |
| 5332 | sp.tv_sec = microseconds / 1000000; |
| 5333 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 5334 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 5335 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5336 | return microseconds; |
| 5337 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5338 | usleep(microseconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 5339 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5340 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5341 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5342 | int seconds = (microseconds+999999)/1000000; |
| 5343 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 5344 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 5345 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 5346 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 5347 | } |
| 5348 | |
| 5349 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5350 | ** The following variable, if set to a non-zero value, is interpreted as |
| 5351 | ** the number of seconds since 1970 and is used to set the result of |
| 5352 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5353 | */ |
| 5354 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5355 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5356 | #endif |
| 5357 | |
| 5358 | /* |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5359 | ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 5360 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 5361 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 5362 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 5363 | ** proleptic Gregorian calendar. |
| 5364 | ** |
| 5365 | ** On success, return 0. Return 1 if the time and date cannot be found. |
| 5366 | */ |
| 5367 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 5368 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
| 5369 | #if defined(NO_GETTOD) |
| 5370 | time_t t; |
| 5371 | time(&t); |
dan | 15eac4e | 2010-11-22 17:26:07 +0000 | [diff] [blame] | 5372 | *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5373 | #elif OS_VXWORKS |
| 5374 | struct timespec sNow; |
| 5375 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 5376 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 5377 | #else |
| 5378 | struct timeval sNow; |
| 5379 | gettimeofday(&sNow, 0); |
| 5380 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
| 5381 | #endif |
| 5382 | |
| 5383 | #ifdef SQLITE_TEST |
| 5384 | if( sqlite3_current_time ){ |
| 5385 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 5386 | } |
| 5387 | #endif |
| 5388 | UNUSED_PARAMETER(NotUsed); |
| 5389 | return 0; |
| 5390 | } |
| 5391 | |
| 5392 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5393 | ** Find the current time (in Universal Coordinated Time). Write the |
| 5394 | ** current time and date as a Julian Day number into *prNow and |
| 5395 | ** return 0. Return 1 if the time and date cannot be found. |
| 5396 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5397 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5398 | sqlite3_int64 i; |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 5399 | UNUSED_PARAMETER(NotUsed); |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5400 | unixCurrentTimeInt64(0, &i); |
drh | 0dcb0a7 | 2010-05-03 18:22:52 +0000 | [diff] [blame] | 5401 | *prNow = i/86400000.0; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5402 | return 0; |
| 5403 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5404 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5405 | /* |
| 5406 | ** We added the xGetLastError() method with the intention of providing |
| 5407 | ** better low-level error messages when operating-system problems come up |
| 5408 | ** during SQLite operation. But so far, none of that has been implemented |
| 5409 | ** in the core. So this routine is never called. For now, it is merely |
| 5410 | ** a place-holder. |
| 5411 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5412 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 5413 | UNUSED_PARAMETER(NotUsed); |
| 5414 | UNUSED_PARAMETER(NotUsed2); |
| 5415 | UNUSED_PARAMETER(NotUsed3); |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 5416 | return 0; |
| 5417 | } |
| 5418 | |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5419 | |
| 5420 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5421 | ************************ End of sqlite3_vfs methods *************************** |
| 5422 | ******************************************************************************/ |
| 5423 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5424 | /****************************************************************************** |
| 5425 | ************************** Begin Proxy Locking ******************************** |
| 5426 | ** |
| 5427 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 5428 | ** other locking methods on secondary lock files. Proxy locking is a |
| 5429 | ** meta-layer over top of the primitive locking implemented above. For |
| 5430 | ** this reason, the division that implements of proxy locking is deferred |
| 5431 | ** until late in the file (here) after all of the other I/O methods have |
| 5432 | ** been defined - so that the primitive locking methods are available |
| 5433 | ** as services to help with the implementation of proxy locking. |
| 5434 | ** |
| 5435 | **** |
| 5436 | ** |
| 5437 | ** The default locking schemes in SQLite use byte-range locks on the |
| 5438 | ** database file to coordinate safe, concurrent access by multiple readers |
| 5439 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 5440 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 5441 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 5442 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 5443 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 5444 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 5445 | ** address in the shared range is taken for a SHARED lock, the entire |
| 5446 | ** shared range is taken for an EXCLUSIVE lock): |
| 5447 | ** |
| 5448 | ** PENDING_BYTE 0x40000000 |
| 5449 | ** RESERVED_BYTE 0x40000001 |
| 5450 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 5451 | ** |
| 5452 | ** This works well on the local file system, but shows a nearly 100x |
| 5453 | ** slowdown in read performance on AFP because the AFP client disables |
| 5454 | ** the read cache when byte-range locks are present. Enabling the read |
| 5455 | ** cache exposes a cache coherency problem that is present on all OS X |
| 5456 | ** supported network file systems. NFS and AFP both observe the |
| 5457 | ** close-to-open semantics for ensuring cache coherency |
| 5458 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 5459 | ** address the requirements for concurrent database access by multiple |
| 5460 | ** readers and writers |
| 5461 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 5462 | ** |
| 5463 | ** To address the performance and cache coherency issues, proxy file locking |
| 5464 | ** changes the way database access is controlled by limiting access to a |
| 5465 | ** single host at a time and moving file locks off of the database file |
| 5466 | ** and onto a proxy file on the local file system. |
| 5467 | ** |
| 5468 | ** |
| 5469 | ** Using proxy locks |
| 5470 | ** ----------------- |
| 5471 | ** |
| 5472 | ** C APIs |
| 5473 | ** |
| 5474 | ** sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE, |
| 5475 | ** <proxy_path> | ":auto:"); |
| 5476 | ** sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>); |
| 5477 | ** |
| 5478 | ** |
| 5479 | ** SQL pragmas |
| 5480 | ** |
| 5481 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 5482 | ** PRAGMA [database.]lock_proxy_file |
| 5483 | ** |
| 5484 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 5485 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 5486 | ** a proxy path based on the user's temp dir |
| 5487 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 5488 | ** actual proxy file name is generated from the name and path of the |
| 5489 | ** database file. For example: |
| 5490 | ** |
| 5491 | ** For database path "/Users/me/foo.db" |
| 5492 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 5493 | ** |
| 5494 | ** Once a lock proxy is configured for a database connection, it can not |
| 5495 | ** be removed, however it may be switched to a different proxy path via |
| 5496 | ** the above APIs (assuming the conch file is not being held by another |
| 5497 | ** connection or process). |
| 5498 | ** |
| 5499 | ** |
| 5500 | ** How proxy locking works |
| 5501 | ** ----------------------- |
| 5502 | ** |
| 5503 | ** Proxy file locking relies primarily on two new supporting files: |
| 5504 | ** |
| 5505 | ** * conch file to limit access to the database file to a single host |
| 5506 | ** at a time |
| 5507 | ** |
| 5508 | ** * proxy file to act as a proxy for the advisory locks normally |
| 5509 | ** taken on the database |
| 5510 | ** |
| 5511 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 5512 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 5513 | ** contents and comparing the host's unique host ID (see below) and lock |
| 5514 | ** proxy path against the values stored in the conch. The conch file is |
| 5515 | ** stored in the same directory as the database file and the file name |
| 5516 | ** is patterned after the database file name as ".<databasename>-conch". |
| 5517 | ** If the conch file does not exist, or it's contents do not match the |
| 5518 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 5519 | ** lock and the conch file contents is updated with the host ID and proxy |
| 5520 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 5521 | ** is held by another process (with a shared lock), the exclusive lock |
| 5522 | ** will fail and SQLITE_BUSY is returned. |
| 5523 | ** |
| 5524 | ** The proxy file - a single-byte file used for all advisory file locks |
| 5525 | ** normally taken on the database file. This allows for safe sharing |
| 5526 | ** of the database file for multiple readers and writers on the same |
| 5527 | ** host (the conch ensures that they all use the same local lock file). |
| 5528 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5529 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 5530 | ** only taken when the first request to lock database file is made. |
| 5531 | ** This matches the semantics of the traditional locking behavior, where |
| 5532 | ** opening a connection to a database file does not take a lock on it. |
| 5533 | ** The shared lock and an open file descriptor are maintained until |
| 5534 | ** the connection to the database is closed. |
| 5535 | ** |
| 5536 | ** The proxy file and the lock file are never deleted so they only need |
| 5537 | ** to be created the first time they are used. |
| 5538 | ** |
| 5539 | ** Configuration options |
| 5540 | ** --------------------- |
| 5541 | ** |
| 5542 | ** SQLITE_PREFER_PROXY_LOCKING |
| 5543 | ** |
| 5544 | ** Database files accessed on non-local file systems are |
| 5545 | ** automatically configured for proxy locking, lock files are |
| 5546 | ** named automatically using the same logic as |
| 5547 | ** PRAGMA lock_proxy_file=":auto:" |
| 5548 | ** |
| 5549 | ** SQLITE_PROXY_DEBUG |
| 5550 | ** |
| 5551 | ** Enables the logging of error messages during host id file |
| 5552 | ** retrieval and creation |
| 5553 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5554 | ** LOCKPROXYDIR |
| 5555 | ** |
| 5556 | ** Overrides the default directory used for lock proxy files that |
| 5557 | ** are named automatically via the ":auto:" setting |
| 5558 | ** |
| 5559 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 5560 | ** |
| 5561 | ** Permissions to use when creating a directory for storing the |
| 5562 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 5563 | ** |
| 5564 | ** |
| 5565 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 5566 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 5567 | ** force proxy locking to be used for every database file opened, and 0 |
| 5568 | ** will force automatic proxy locking to be disabled for all database |
| 5569 | ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or |
| 5570 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 5571 | */ |
| 5572 | |
| 5573 | /* |
| 5574 | ** Proxy locking is only available on MacOSX |
| 5575 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5576 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5577 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5578 | /* |
| 5579 | ** The proxyLockingContext has the path and file structures for the remote |
| 5580 | ** and local proxy files in it |
| 5581 | */ |
| 5582 | typedef struct proxyLockingContext proxyLockingContext; |
| 5583 | struct proxyLockingContext { |
| 5584 | unixFile *conchFile; /* Open conch file */ |
| 5585 | char *conchFilePath; /* Name of the conch file */ |
| 5586 | unixFile *lockProxy; /* Open proxy lock file */ |
| 5587 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 5588 | char *dbPath; /* Name of the open file */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5589 | int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5590 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 5591 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 5592 | }; |
| 5593 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5594 | /* |
| 5595 | ** The proxy lock file path for the database at dbPath is written into lPath, |
| 5596 | ** which must point to valid, writable memory large enough for a maxLen length |
| 5597 | ** file path. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5598 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5599 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 5600 | int len; |
| 5601 | int dbLen; |
| 5602 | int i; |
| 5603 | |
| 5604 | #ifdef LOCKPROXYDIR |
| 5605 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 5606 | #else |
| 5607 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 5608 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5609 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5610 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
| 5611 | lPath, errno, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5612 | return SQLITE_IOERR_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5613 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5614 | len = strlcat(lPath, "sqliteplocks", maxLen); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5615 | } |
| 5616 | # else |
| 5617 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 5618 | # endif |
| 5619 | #endif |
| 5620 | |
| 5621 | if( lPath[len-1]!='/' ){ |
| 5622 | len = strlcat(lPath, "/", maxLen); |
| 5623 | } |
| 5624 | |
| 5625 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5626 | dbLen = (int)strlen(dbPath); |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5627 | for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5628 | char c = dbPath[i]; |
| 5629 | lPath[i+len] = (c=='/')?'_':c; |
| 5630 | } |
| 5631 | lPath[i+len]='\0'; |
| 5632 | strlcat(lPath, ":auto:", maxLen); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5633 | OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5634 | return SQLITE_OK; |
| 5635 | } |
| 5636 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5637 | /* |
| 5638 | ** Creates the lock file and any missing directories in lockPath |
| 5639 | */ |
| 5640 | static int proxyCreateLockPath(const char *lockPath){ |
| 5641 | int i, len; |
| 5642 | char buf[MAXPATHLEN]; |
| 5643 | int start = 0; |
| 5644 | |
| 5645 | assert(lockPath!=NULL); |
| 5646 | /* try to create all the intermediate directories */ |
| 5647 | len = (int)strlen(lockPath); |
| 5648 | buf[0] = lockPath[0]; |
| 5649 | for( i=1; i<len; i++ ){ |
| 5650 | if( lockPath[i] == '/' && (i - start > 0) ){ |
| 5651 | /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 5652 | if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 5653 | || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 5654 | buf[i]='\0'; |
| 5655 | if( mkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
| 5656 | int err=errno; |
| 5657 | if( err!=EEXIST ) { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5658 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5659 | "'%s' proxy lock path=%s pid=%d\n", |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5660 | buf, strerror(err), lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5661 | return err; |
| 5662 | } |
| 5663 | } |
| 5664 | } |
| 5665 | start=i+1; |
| 5666 | } |
| 5667 | buf[i] = lockPath[i]; |
| 5668 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5669 | OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5670 | return 0; |
| 5671 | } |
| 5672 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5673 | /* |
| 5674 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 5675 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 5676 | ** |
| 5677 | ** The caller is responsible not only for closing the file descriptor |
| 5678 | ** but also for freeing the memory associated with the file descriptor. |
| 5679 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5680 | static int proxyCreateUnixFile( |
| 5681 | const char *path, /* path for the new unixFile */ |
| 5682 | unixFile **ppFile, /* unixFile created and returned by ref */ |
| 5683 | int islockfile /* if non zero missing dirs will be created */ |
| 5684 | ) { |
| 5685 | int fd = -1; |
| 5686 | int dirfd = -1; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5687 | unixFile *pNew; |
| 5688 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5689 | int openFlags = O_RDWR | O_CREAT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5690 | sqlite3_vfs dummyVfs; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5691 | int terrno = 0; |
| 5692 | UnixUnusedFd *pUnused = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5693 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5694 | /* 1. first try to open/create the file |
| 5695 | ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 5696 | ** the parent directories and then try again. |
| 5697 | ** 3. if that fails, try to open the file read-only |
| 5698 | ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 5699 | */ |
| 5700 | pUnused = findReusableFd(path, openFlags); |
| 5701 | if( pUnused ){ |
| 5702 | fd = pUnused->fd; |
| 5703 | }else{ |
| 5704 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
| 5705 | if( !pUnused ){ |
| 5706 | return SQLITE_NOMEM; |
| 5707 | } |
| 5708 | } |
| 5709 | if( fd<0 ){ |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5710 | fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5711 | terrno = errno; |
| 5712 | if( fd<0 && errno==ENOENT && islockfile ){ |
| 5713 | if( proxyCreateLockPath(path) == SQLITE_OK ){ |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5714 | fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5715 | } |
| 5716 | } |
| 5717 | } |
| 5718 | if( fd<0 ){ |
| 5719 | openFlags = O_RDONLY; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5720 | fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5721 | terrno = errno; |
| 5722 | } |
| 5723 | if( fd<0 ){ |
| 5724 | if( islockfile ){ |
| 5725 | return SQLITE_BUSY; |
| 5726 | } |
| 5727 | switch (terrno) { |
| 5728 | case EACCES: |
| 5729 | return SQLITE_PERM; |
| 5730 | case EIO: |
| 5731 | return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 5732 | default: |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 5733 | return SQLITE_CANTOPEN_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5734 | } |
| 5735 | } |
| 5736 | |
| 5737 | pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew)); |
| 5738 | if( pNew==NULL ){ |
| 5739 | rc = SQLITE_NOMEM; |
| 5740 | goto end_create_proxy; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5741 | } |
| 5742 | memset(pNew, 0, sizeof(unixFile)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5743 | pNew->openFlags = openFlags; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 5744 | memset(&dummyVfs, 0, sizeof(dummyVfs)); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5745 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 5746 | dummyVfs.zName = "dummy"; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5747 | pUnused->fd = fd; |
| 5748 | pUnused->flags = openFlags; |
| 5749 | pNew->pUnused = pUnused; |
| 5750 | |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 5751 | rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5752 | if( rc==SQLITE_OK ){ |
| 5753 | *ppFile = pNew; |
| 5754 | return SQLITE_OK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5755 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5756 | end_create_proxy: |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5757 | robust_close(pNew, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5758 | sqlite3_free(pNew); |
| 5759 | sqlite3_free(pUnused); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5760 | return rc; |
| 5761 | } |
| 5762 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5763 | #ifdef SQLITE_TEST |
| 5764 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 5765 | int sqlite3_hostid_num = 0; |
| 5766 | #endif |
| 5767 | |
| 5768 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 5769 | |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5770 | /* Not always defined in the headers as it ought to be */ |
| 5771 | extern int gethostuuid(uuid_t id, const struct timespec *wait); |
| 5772 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5773 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 5774 | ** bytes of writable memory. |
| 5775 | */ |
| 5776 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5777 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 5778 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 5779 | #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\ |
| 5780 | && __MAC_OS_X_VERSION_MIN_REQUIRED<1050 |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 5781 | { |
| 5782 | static const struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
| 5783 | if( gethostuuid(pHostID, &timeout) ){ |
| 5784 | int err = errno; |
| 5785 | if( pError ){ |
| 5786 | *pError = err; |
| 5787 | } |
| 5788 | return SQLITE_IOERR; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5789 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5790 | } |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 5791 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5792 | #ifdef SQLITE_TEST |
| 5793 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 5794 | if( sqlite3_hostid_num != 0){ |
| 5795 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 5796 | } |
| 5797 | #endif |
| 5798 | |
| 5799 | return SQLITE_OK; |
| 5800 | } |
| 5801 | |
| 5802 | /* The conch file contains the header, host id and lock file path |
| 5803 | */ |
| 5804 | #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 5805 | #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 5806 | #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 5807 | #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 5808 | |
| 5809 | /* |
| 5810 | ** Takes an open conch file, copies the contents to a new path and then moves |
| 5811 | ** it back. The newly created file's file descriptor is assigned to the |
| 5812 | ** conch file structure and finally the original conch file descriptor is |
| 5813 | ** closed. Returns zero if successful. |
| 5814 | */ |
| 5815 | static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 5816 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5817 | unixFile *conchFile = pCtx->conchFile; |
| 5818 | char tPath[MAXPATHLEN]; |
| 5819 | char buf[PROXY_MAXCONCHLEN]; |
| 5820 | char *cPath = pCtx->conchFilePath; |
| 5821 | size_t readLen = 0; |
| 5822 | size_t pathLen = 0; |
| 5823 | char errmsg[64] = ""; |
| 5824 | int fd = -1; |
| 5825 | int rc = -1; |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5826 | UNUSED_PARAMETER(myHostID); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5827 | |
| 5828 | /* create a new path by replace the trailing '-conch' with '-break' */ |
| 5829 | pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 5830 | if( pathLen>MAXPATHLEN || pathLen<6 || |
| 5831 | (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5832 | sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5833 | goto end_breaklock; |
| 5834 | } |
| 5835 | /* read the conch content */ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 5836 | readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5837 | if( readLen<PROXY_PATHINDEX ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5838 | sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5839 | goto end_breaklock; |
| 5840 | } |
| 5841 | /* write it out to the temporary break file */ |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5842 | fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), |
| 5843 | SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5844 | if( fd<0 ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5845 | sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5846 | goto end_breaklock; |
| 5847 | } |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 5848 | if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5849 | sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5850 | goto end_breaklock; |
| 5851 | } |
| 5852 | if( rename(tPath, cPath) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5853 | sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5854 | goto end_breaklock; |
| 5855 | } |
| 5856 | rc = 0; |
| 5857 | fprintf(stderr, "broke stale lock on %s\n", cPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5858 | robust_close(pFile, conchFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5859 | conchFile->h = fd; |
| 5860 | conchFile->openFlags = O_RDWR | O_CREAT; |
| 5861 | |
| 5862 | end_breaklock: |
| 5863 | if( rc ){ |
| 5864 | if( fd>=0 ){ |
| 5865 | unlink(tPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5866 | robust_close(pFile, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5867 | } |
| 5868 | fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 5869 | } |
| 5870 | return rc; |
| 5871 | } |
| 5872 | |
| 5873 | /* Take the requested lock on the conch file and break a stale lock if the |
| 5874 | ** host id matches. |
| 5875 | */ |
| 5876 | static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 5877 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5878 | unixFile *conchFile = pCtx->conchFile; |
| 5879 | int rc = SQLITE_OK; |
| 5880 | int nTries = 0; |
| 5881 | struct timespec conchModTime; |
| 5882 | |
| 5883 | do { |
| 5884 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 5885 | nTries ++; |
| 5886 | if( rc==SQLITE_BUSY ){ |
| 5887 | /* If the lock failed (busy): |
| 5888 | * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 5889 | * 2nd try: fail if the mod time changed or host id is different, wait |
| 5890 | * 10 sec and try again |
| 5891 | * 3rd try: break the lock unless the mod time has changed. |
| 5892 | */ |
| 5893 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5894 | if( osFstat(conchFile->h, &buf) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5895 | pFile->lastErrno = errno; |
| 5896 | return SQLITE_IOERR_LOCK; |
| 5897 | } |
| 5898 | |
| 5899 | if( nTries==1 ){ |
| 5900 | conchModTime = buf.st_mtimespec; |
| 5901 | usleep(500000); /* wait 0.5 sec and try the lock again*/ |
| 5902 | continue; |
| 5903 | } |
| 5904 | |
| 5905 | assert( nTries>1 ); |
| 5906 | if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 5907 | conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 5908 | return SQLITE_BUSY; |
| 5909 | } |
| 5910 | |
| 5911 | if( nTries==2 ){ |
| 5912 | char tBuf[PROXY_MAXCONCHLEN]; |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 5913 | int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5914 | if( len<0 ){ |
| 5915 | pFile->lastErrno = errno; |
| 5916 | return SQLITE_IOERR_LOCK; |
| 5917 | } |
| 5918 | if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 5919 | /* don't break the lock if the host id doesn't match */ |
| 5920 | if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 5921 | return SQLITE_BUSY; |
| 5922 | } |
| 5923 | }else{ |
| 5924 | /* don't break the lock on short read or a version mismatch */ |
| 5925 | return SQLITE_BUSY; |
| 5926 | } |
| 5927 | usleep(10000000); /* wait 10 sec and try the lock again */ |
| 5928 | continue; |
| 5929 | } |
| 5930 | |
| 5931 | assert( nTries==3 ); |
| 5932 | if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 5933 | rc = SQLITE_OK; |
| 5934 | if( lockType==EXCLUSIVE_LOCK ){ |
| 5935 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 5936 | } |
| 5937 | if( !rc ){ |
| 5938 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 5939 | } |
| 5940 | } |
| 5941 | } |
| 5942 | } while( rc==SQLITE_BUSY && nTries<3 ); |
| 5943 | |
| 5944 | return rc; |
| 5945 | } |
| 5946 | |
| 5947 | /* 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] | 5948 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 5949 | ** lockPath means that the lockPath in the conch file will be used if the |
| 5950 | ** host IDs match, or a new lock path will be generated automatically |
| 5951 | ** and written to the conch file. |
| 5952 | */ |
| 5953 | static int proxyTakeConch(unixFile *pFile){ |
| 5954 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5955 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5956 | if( pCtx->conchHeld!=0 ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5957 | return SQLITE_OK; |
| 5958 | }else{ |
| 5959 | unixFile *conchFile = pCtx->conchFile; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5960 | uuid_t myHostID; |
| 5961 | int pError = 0; |
| 5962 | char readBuf[PROXY_MAXCONCHLEN]; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5963 | char lockPath[MAXPATHLEN]; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5964 | char *tempLockPath = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5965 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5966 | int createConch = 0; |
| 5967 | int hostIdMatch = 0; |
| 5968 | int readLen = 0; |
| 5969 | int tryOldLockPath = 0; |
| 5970 | int forceNewLockPath = 0; |
| 5971 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5972 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
| 5973 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5974 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5975 | rc = proxyGetHostID(myHostID, &pError); |
| 5976 | if( (rc&0xff)==SQLITE_IOERR ){ |
| 5977 | pFile->lastErrno = pError; |
| 5978 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5979 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5980 | rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5981 | if( rc!=SQLITE_OK ){ |
| 5982 | goto end_takeconch; |
| 5983 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5984 | /* read the existing conch file */ |
| 5985 | readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 5986 | if( readLen<0 ){ |
| 5987 | /* I/O error: lastErrno set by seekAndRead */ |
| 5988 | pFile->lastErrno = conchFile->lastErrno; |
| 5989 | rc = SQLITE_IOERR_READ; |
| 5990 | goto end_takeconch; |
| 5991 | }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 5992 | readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 5993 | /* a short read or version format mismatch means we need to create a new |
| 5994 | ** conch file. |
| 5995 | */ |
| 5996 | createConch = 1; |
| 5997 | } |
| 5998 | /* if the host id matches and the lock path already exists in the conch |
| 5999 | ** we'll try to use the path there, if we can't open that path, we'll |
| 6000 | ** retry with a new auto-generated path |
| 6001 | */ |
| 6002 | do { /* in case we need to try again for an :auto: named lock file */ |
| 6003 | |
| 6004 | if( !createConch && !forceNewLockPath ){ |
| 6005 | hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 6006 | PROXY_HOSTIDLEN); |
| 6007 | /* if the conch has data compare the contents */ |
| 6008 | if( !pCtx->lockProxyPath ){ |
| 6009 | /* for auto-named local lock file, just check the host ID and we'll |
| 6010 | ** use the local lock file path that's already in there |
| 6011 | */ |
| 6012 | if( hostIdMatch ){ |
| 6013 | size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 6014 | |
| 6015 | if( pathLen>=MAXPATHLEN ){ |
| 6016 | pathLen=MAXPATHLEN-1; |
| 6017 | } |
| 6018 | memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 6019 | lockPath[pathLen] = 0; |
| 6020 | tempLockPath = lockPath; |
| 6021 | tryOldLockPath = 1; |
| 6022 | /* create a copy of the lock path if the conch is taken */ |
| 6023 | goto end_takeconch; |
| 6024 | } |
| 6025 | }else if( hostIdMatch |
| 6026 | && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 6027 | readLen-PROXY_PATHINDEX) |
| 6028 | ){ |
| 6029 | /* conch host and lock path match */ |
| 6030 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6031 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6032 | } |
| 6033 | |
| 6034 | /* if the conch isn't writable and doesn't match, we can't take it */ |
| 6035 | if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 6036 | rc = SQLITE_BUSY; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6037 | goto end_takeconch; |
| 6038 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6039 | |
| 6040 | /* 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] | 6041 | if( !pCtx->lockProxyPath ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6042 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 6043 | tempLockPath = lockPath; |
| 6044 | /* create a copy of the lock path _only_ if the conch is taken */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6045 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6046 | |
| 6047 | /* update conch with host and path (this will fail if other process |
| 6048 | ** has a shared lock already), if the host id matches, use the big |
| 6049 | ** stick. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6050 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6051 | futimes(conchFile->h, NULL); |
| 6052 | if( hostIdMatch && !createConch ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 6053 | if( conchFile->pInode && conchFile->pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6054 | /* We are trying for an exclusive lock but another thread in this |
| 6055 | ** same process is still holding a shared lock. */ |
| 6056 | rc = SQLITE_BUSY; |
| 6057 | } else { |
| 6058 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6059 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6060 | }else{ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6061 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6062 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6063 | if( rc==SQLITE_OK ){ |
| 6064 | char writeBuffer[PROXY_MAXCONCHLEN]; |
| 6065 | int writeSize = 0; |
| 6066 | |
| 6067 | writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 6068 | memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 6069 | if( pCtx->lockProxyPath!=NULL ){ |
| 6070 | strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN); |
| 6071 | }else{ |
| 6072 | strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 6073 | } |
| 6074 | writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6075 | robust_ftruncate(conchFile->h, writeSize); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6076 | rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
| 6077 | fsync(conchFile->h); |
| 6078 | /* If we created a new conch file (not just updated the contents of a |
| 6079 | ** valid conch file), try to match the permissions of the database |
| 6080 | */ |
| 6081 | if( rc==SQLITE_OK && createConch ){ |
| 6082 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6083 | int err = osFstat(pFile->h, &buf); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6084 | if( err==0 ){ |
| 6085 | mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 6086 | S_IROTH|S_IWOTH); |
| 6087 | /* try to match the database file R/W permissions, ignore failure */ |
| 6088 | #ifndef SQLITE_PROXY_DEBUG |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6089 | osFchmod(conchFile->h, cmode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6090 | #else |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6091 | do{ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6092 | rc = osFchmod(conchFile->h, cmode); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6093 | }while( rc==(-1) && errno==EINTR ); |
| 6094 | if( rc!=0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6095 | int code = errno; |
| 6096 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 6097 | cmode, code, strerror(code)); |
| 6098 | } else { |
| 6099 | fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 6100 | } |
| 6101 | }else{ |
| 6102 | int code = errno; |
| 6103 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 6104 | err, code, strerror(code)); |
| 6105 | #endif |
| 6106 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6107 | } |
| 6108 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6109 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 6110 | |
| 6111 | end_takeconch: |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6112 | OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6113 | if( rc==SQLITE_OK && pFile->openFlags ){ |
| 6114 | if( pFile->h>=0 ){ |
drh | e84009f | 2011-03-02 17:54:32 +0000 | [diff] [blame] | 6115 | robust_close(pFile, pFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6116 | } |
| 6117 | pFile->h = -1; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 6118 | int fd = robust_open(pCtx->dbPath, pFile->openFlags, |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6119 | SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6120 | OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6121 | if( fd>=0 ){ |
| 6122 | pFile->h = fd; |
| 6123 | }else{ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6124 | rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6125 | during locking */ |
| 6126 | } |
| 6127 | } |
| 6128 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 6129 | char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 6130 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 6131 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 6132 | /* we couldn't create the proxy lock file with the old lock file path |
| 6133 | ** so try again via auto-naming |
| 6134 | */ |
| 6135 | forceNewLockPath = 1; |
| 6136 | tryOldLockPath = 0; |
dan | 2b0ef47 | 2010-02-16 12:18:47 +0000 | [diff] [blame] | 6137 | continue; /* go back to the do {} while start point, try again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6138 | } |
| 6139 | } |
| 6140 | if( rc==SQLITE_OK ){ |
| 6141 | /* Need to make a copy of path if we extracted the value |
| 6142 | ** from the conch file or the path was allocated on the stack |
| 6143 | */ |
| 6144 | if( tempLockPath ){ |
| 6145 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 6146 | if( !pCtx->lockProxyPath ){ |
| 6147 | rc = SQLITE_NOMEM; |
| 6148 | } |
| 6149 | } |
| 6150 | } |
| 6151 | if( rc==SQLITE_OK ){ |
| 6152 | pCtx->conchHeld = 1; |
| 6153 | |
| 6154 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 6155 | afpLockingContext *afpCtx; |
| 6156 | afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 6157 | afpCtx->dbPath = pCtx->lockProxyPath; |
| 6158 | } |
| 6159 | } else { |
| 6160 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 6161 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6162 | OSTRACE(("TAKECONCH %d %s\n", conchFile->h, |
| 6163 | rc==SQLITE_OK?"ok":"failed")); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6164 | return rc; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6165 | } while (1); /* in case we need to retry the :auto: lock file - |
| 6166 | ** we should never get here except via the 'continue' call. */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6167 | } |
| 6168 | } |
| 6169 | |
| 6170 | /* |
| 6171 | ** If pFile holds a lock on a conch file, then release that lock. |
| 6172 | */ |
| 6173 | static int proxyReleaseConch(unixFile *pFile){ |
drh | 1c5bb4d | 2010-05-10 17:29:28 +0000 | [diff] [blame] | 6174 | int rc = SQLITE_OK; /* Subroutine return code */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6175 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 6176 | unixFile *conchFile; /* Name of the conch file */ |
| 6177 | |
| 6178 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6179 | conchFile = pCtx->conchFile; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6180 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6181 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6182 | getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6183 | if( pCtx->conchHeld>0 ){ |
| 6184 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 6185 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6186 | pCtx->conchHeld = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6187 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| 6188 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6189 | return rc; |
| 6190 | } |
| 6191 | |
| 6192 | /* |
| 6193 | ** Given the name of a database file, compute the name of its conch file. |
| 6194 | ** Store the conch filename in memory obtained from sqlite3_malloc(). |
| 6195 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 6196 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 6197 | ** |
| 6198 | ** The caller is responsible for ensuring that the allocated memory |
| 6199 | ** space is eventually freed. |
| 6200 | ** |
| 6201 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 6202 | */ |
| 6203 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 6204 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6205 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6206 | char *conchPath; /* buffer in which to construct conch name */ |
| 6207 | |
| 6208 | /* Allocate space for the conch filename and initialize the name to |
| 6209 | ** the name of the original database file. */ |
| 6210 | *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8); |
| 6211 | if( conchPath==0 ){ |
| 6212 | return SQLITE_NOMEM; |
| 6213 | } |
| 6214 | memcpy(conchPath, dbPath, len+1); |
| 6215 | |
| 6216 | /* now insert a "." before the last / character */ |
| 6217 | for( i=(len-1); i>=0; i-- ){ |
| 6218 | if( conchPath[i]=='/' ){ |
| 6219 | i++; |
| 6220 | break; |
| 6221 | } |
| 6222 | } |
| 6223 | conchPath[i]='.'; |
| 6224 | while ( i<len ){ |
| 6225 | conchPath[i+1]=dbPath[i]; |
| 6226 | i++; |
| 6227 | } |
| 6228 | |
| 6229 | /* append the "-conch" suffix to the file */ |
| 6230 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6231 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6232 | |
| 6233 | return SQLITE_OK; |
| 6234 | } |
| 6235 | |
| 6236 | |
| 6237 | /* Takes a fully configured proxy locking-style unix file and switches |
| 6238 | ** the local lock file path |
| 6239 | */ |
| 6240 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 6241 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 6242 | char *oldPath = pCtx->lockProxyPath; |
| 6243 | int rc = SQLITE_OK; |
| 6244 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6245 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6246 | return SQLITE_BUSY; |
| 6247 | } |
| 6248 | |
| 6249 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 6250 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 6251 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 6252 | return SQLITE_OK; |
| 6253 | }else{ |
| 6254 | unixFile *lockProxy = pCtx->lockProxy; |
| 6255 | pCtx->lockProxy=NULL; |
| 6256 | pCtx->conchHeld = 0; |
| 6257 | if( lockProxy!=NULL ){ |
| 6258 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 6259 | if( rc ) return rc; |
| 6260 | sqlite3_free(lockProxy); |
| 6261 | } |
| 6262 | sqlite3_free(oldPath); |
| 6263 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 6264 | } |
| 6265 | |
| 6266 | return rc; |
| 6267 | } |
| 6268 | |
| 6269 | /* |
| 6270 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 6271 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 6272 | ** |
| 6273 | ** This routine find the filename associated with pFile and writes it |
| 6274 | ** int dbPath. |
| 6275 | */ |
| 6276 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6277 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6278 | if( pFile->pMethod == &afpIoMethods ){ |
| 6279 | /* afp style keeps a reference to the db path in the filePath field |
| 6280 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6281 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6282 | strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN); |
| 6283 | } else |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6284 | #endif |
| 6285 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 6286 | /* dot lock style uses the locking context to store the dot lock |
| 6287 | ** file path */ |
| 6288 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 6289 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 6290 | }else{ |
| 6291 | /* all other styles use the locking context to store the db file path */ |
| 6292 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6293 | strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6294 | } |
| 6295 | return SQLITE_OK; |
| 6296 | } |
| 6297 | |
| 6298 | /* |
| 6299 | ** Takes an already filled in unix file and alters it so all file locking |
| 6300 | ** will be performed on the local proxy lock file. The following fields |
| 6301 | ** are preserved in the locking context so that they can be restored and |
| 6302 | ** the unix structure properly cleaned up at close time: |
| 6303 | ** ->lockingContext |
| 6304 | ** ->pMethod |
| 6305 | */ |
| 6306 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 6307 | proxyLockingContext *pCtx; |
| 6308 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 6309 | char *lockPath=NULL; |
| 6310 | int rc = SQLITE_OK; |
| 6311 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6312 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6313 | return SQLITE_BUSY; |
| 6314 | } |
| 6315 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 6316 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 6317 | lockPath=NULL; |
| 6318 | }else{ |
| 6319 | lockPath=(char *)path; |
| 6320 | } |
| 6321 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6322 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
| 6323 | (lockPath ? lockPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6324 | |
| 6325 | pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 6326 | if( pCtx==0 ){ |
| 6327 | return SQLITE_NOMEM; |
| 6328 | } |
| 6329 | memset(pCtx, 0, sizeof(*pCtx)); |
| 6330 | |
| 6331 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 6332 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6333 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 6334 | if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 6335 | /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 6336 | ** (c) the file system is read-only, then enable no-locking access. |
| 6337 | ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 6338 | ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 6339 | */ |
| 6340 | struct statfs fsInfo; |
| 6341 | struct stat conchInfo; |
| 6342 | int goLockless = 0; |
| 6343 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6344 | if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6345 | int err = errno; |
| 6346 | if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 6347 | goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 6348 | } |
| 6349 | } |
| 6350 | if( goLockless ){ |
| 6351 | pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 6352 | rc = SQLITE_OK; |
| 6353 | } |
| 6354 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6355 | } |
| 6356 | if( rc==SQLITE_OK && lockPath ){ |
| 6357 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 6358 | } |
| 6359 | |
| 6360 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6361 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 6362 | if( pCtx->dbPath==NULL ){ |
| 6363 | rc = SQLITE_NOMEM; |
| 6364 | } |
| 6365 | } |
| 6366 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6367 | /* all memory is allocated, proxys are created and assigned, |
| 6368 | ** switch the locking context and pMethod then return. |
| 6369 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6370 | pCtx->oldLockingContext = pFile->lockingContext; |
| 6371 | pFile->lockingContext = pCtx; |
| 6372 | pCtx->pOldMethod = pFile->pMethod; |
| 6373 | pFile->pMethod = &proxyIoMethods; |
| 6374 | }else{ |
| 6375 | if( pCtx->conchFile ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6376 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6377 | sqlite3_free(pCtx->conchFile); |
| 6378 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 6379 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6380 | sqlite3_free(pCtx->conchFilePath); |
| 6381 | sqlite3_free(pCtx); |
| 6382 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6383 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 6384 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6385 | return rc; |
| 6386 | } |
| 6387 | |
| 6388 | |
| 6389 | /* |
| 6390 | ** This routine handles sqlite3_file_control() calls that are specific |
| 6391 | ** to proxy locking. |
| 6392 | */ |
| 6393 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 6394 | switch( op ){ |
| 6395 | case SQLITE_GET_LOCKPROXYFILE: { |
| 6396 | unixFile *pFile = (unixFile*)id; |
| 6397 | if( pFile->pMethod == &proxyIoMethods ){ |
| 6398 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 6399 | proxyTakeConch(pFile); |
| 6400 | if( pCtx->lockProxyPath ){ |
| 6401 | *(const char **)pArg = pCtx->lockProxyPath; |
| 6402 | }else{ |
| 6403 | *(const char **)pArg = ":auto: (not held)"; |
| 6404 | } |
| 6405 | } else { |
| 6406 | *(const char **)pArg = NULL; |
| 6407 | } |
| 6408 | return SQLITE_OK; |
| 6409 | } |
| 6410 | case SQLITE_SET_LOCKPROXYFILE: { |
| 6411 | unixFile *pFile = (unixFile*)id; |
| 6412 | int rc = SQLITE_OK; |
| 6413 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 6414 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 6415 | if( isProxyStyle ){ |
| 6416 | /* turn off proxy locking - not supported */ |
| 6417 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 6418 | }else{ |
| 6419 | /* turn off proxy locking - already off - NOOP */ |
| 6420 | rc = SQLITE_OK; |
| 6421 | } |
| 6422 | }else{ |
| 6423 | const char *proxyPath = (const char *)pArg; |
| 6424 | if( isProxyStyle ){ |
| 6425 | proxyLockingContext *pCtx = |
| 6426 | (proxyLockingContext*)pFile->lockingContext; |
| 6427 | if( !strcmp(pArg, ":auto:") |
| 6428 | || (pCtx->lockProxyPath && |
| 6429 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 6430 | ){ |
| 6431 | rc = SQLITE_OK; |
| 6432 | }else{ |
| 6433 | rc = switchLockProxyPath(pFile, proxyPath); |
| 6434 | } |
| 6435 | }else{ |
| 6436 | /* turn on proxy file locking */ |
| 6437 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 6438 | } |
| 6439 | } |
| 6440 | return rc; |
| 6441 | } |
| 6442 | default: { |
| 6443 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 6444 | } |
| 6445 | } |
| 6446 | /*NOTREACHED*/ |
| 6447 | return SQLITE_ERROR; |
| 6448 | } |
| 6449 | |
| 6450 | /* |
| 6451 | ** Within this division (the proxying locking implementation) the procedures |
| 6452 | ** above this point are all utilities. The lock-related methods of the |
| 6453 | ** proxy-locking sqlite3_io_method object follow. |
| 6454 | */ |
| 6455 | |
| 6456 | |
| 6457 | /* |
| 6458 | ** This routine checks if there is a RESERVED lock held on the specified |
| 6459 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 6460 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 6461 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 6462 | */ |
| 6463 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 6464 | unixFile *pFile = (unixFile*)id; |
| 6465 | int rc = proxyTakeConch(pFile); |
| 6466 | if( rc==SQLITE_OK ){ |
| 6467 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6468 | if( pCtx->conchHeld>0 ){ |
| 6469 | unixFile *proxy = pCtx->lockProxy; |
| 6470 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 6471 | }else{ /* conchHeld < 0 is lockless */ |
| 6472 | pResOut=0; |
| 6473 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6474 | } |
| 6475 | return rc; |
| 6476 | } |
| 6477 | |
| 6478 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6479 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6480 | ** of the following: |
| 6481 | ** |
| 6482 | ** (1) SHARED_LOCK |
| 6483 | ** (2) RESERVED_LOCK |
| 6484 | ** (3) PENDING_LOCK |
| 6485 | ** (4) EXCLUSIVE_LOCK |
| 6486 | ** |
| 6487 | ** Sometimes when requesting one lock state, additional lock states |
| 6488 | ** are inserted in between. The locking might fail on one of the later |
| 6489 | ** transitions leaving the lock state different from what it started but |
| 6490 | ** still short of its goal. The following chart shows the allowed |
| 6491 | ** transitions and the inserted intermediate states: |
| 6492 | ** |
| 6493 | ** UNLOCKED -> SHARED |
| 6494 | ** SHARED -> RESERVED |
| 6495 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 6496 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 6497 | ** PENDING -> EXCLUSIVE |
| 6498 | ** |
| 6499 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 6500 | ** routine to lower a locking level. |
| 6501 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6502 | static int proxyLock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6503 | unixFile *pFile = (unixFile*)id; |
| 6504 | int rc = proxyTakeConch(pFile); |
| 6505 | if( rc==SQLITE_OK ){ |
| 6506 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6507 | if( pCtx->conchHeld>0 ){ |
| 6508 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6509 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); |
| 6510 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6511 | }else{ |
| 6512 | /* conchHeld < 0 is lockless */ |
| 6513 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6514 | } |
| 6515 | return rc; |
| 6516 | } |
| 6517 | |
| 6518 | |
| 6519 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6520 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6521 | ** must be either NO_LOCK or SHARED_LOCK. |
| 6522 | ** |
| 6523 | ** If the locking level of the file descriptor is already at or below |
| 6524 | ** the requested locking level, this routine is a no-op. |
| 6525 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6526 | static int proxyUnlock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6527 | unixFile *pFile = (unixFile*)id; |
| 6528 | int rc = proxyTakeConch(pFile); |
| 6529 | if( rc==SQLITE_OK ){ |
| 6530 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6531 | if( pCtx->conchHeld>0 ){ |
| 6532 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6533 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); |
| 6534 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6535 | }else{ |
| 6536 | /* conchHeld < 0 is lockless */ |
| 6537 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6538 | } |
| 6539 | return rc; |
| 6540 | } |
| 6541 | |
| 6542 | /* |
| 6543 | ** Close a file that uses proxy locks. |
| 6544 | */ |
| 6545 | static int proxyClose(sqlite3_file *id) { |
| 6546 | if( id ){ |
| 6547 | unixFile *pFile = (unixFile*)id; |
| 6548 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6549 | unixFile *lockProxy = pCtx->lockProxy; |
| 6550 | unixFile *conchFile = pCtx->conchFile; |
| 6551 | int rc = SQLITE_OK; |
| 6552 | |
| 6553 | if( lockProxy ){ |
| 6554 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 6555 | if( rc ) return rc; |
| 6556 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 6557 | if( rc ) return rc; |
| 6558 | sqlite3_free(lockProxy); |
| 6559 | pCtx->lockProxy = 0; |
| 6560 | } |
| 6561 | if( conchFile ){ |
| 6562 | if( pCtx->conchHeld ){ |
| 6563 | rc = proxyReleaseConch(pFile); |
| 6564 | if( rc ) return rc; |
| 6565 | } |
| 6566 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 6567 | if( rc ) return rc; |
| 6568 | sqlite3_free(conchFile); |
| 6569 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 6570 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6571 | sqlite3_free(pCtx->conchFilePath); |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 6572 | sqlite3DbFree(0, pCtx->dbPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6573 | /* restore the original locking context and pMethod then close it */ |
| 6574 | pFile->lockingContext = pCtx->oldLockingContext; |
| 6575 | pFile->pMethod = pCtx->pOldMethod; |
| 6576 | sqlite3_free(pCtx); |
| 6577 | return pFile->pMethod->xClose(id); |
| 6578 | } |
| 6579 | return SQLITE_OK; |
| 6580 | } |
| 6581 | |
| 6582 | |
| 6583 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6584 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6585 | /* |
| 6586 | ** The proxy locking style is intended for use with AFP filesystems. |
| 6587 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 6588 | ** restricted to MacOSX. |
| 6589 | ** |
| 6590 | ** |
| 6591 | ******************* End of the proxy lock implementation ********************** |
| 6592 | ******************************************************************************/ |
| 6593 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6594 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6595 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6596 | ** |
| 6597 | ** This routine registers all VFS implementations for unix-like operating |
| 6598 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 6599 | ** should be the only routines in this file that are visible from other |
| 6600 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6601 | ** |
| 6602 | ** This routine is called once during SQLite initialization and by a |
| 6603 | ** single thread. The memory allocation and mutex subsystems have not |
| 6604 | ** necessarily been initialized when this routine is called, and so they |
| 6605 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6606 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6607 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6608 | /* |
| 6609 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6610 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 6611 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 6612 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 6613 | ** and so we have to go through the intermediate pointer to avoid problems |
| 6614 | ** when compiling with -pedantic-errors on GCC.) |
| 6615 | ** |
| 6616 | ** 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] | 6617 | ** finder-function. The finder-function returns a pointer to the |
| 6618 | ** sqlite_io_methods object that implements the desired locking |
| 6619 | ** behaviors. See the division above that contains the IOMETHODS |
| 6620 | ** macro for addition information on finder-functions. |
| 6621 | ** |
| 6622 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 6623 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 6624 | ** more than that; it looks at the filesystem type that hosts the |
| 6625 | ** database file and tries to choose an locking method appropriate for |
| 6626 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6627 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6628 | #define UNIXVFS(VFSNAME, FINDER) { \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6629 | 3, /* iVersion */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6630 | sizeof(unixFile), /* szOsFile */ \ |
| 6631 | MAX_PATHNAME, /* mxPathname */ \ |
| 6632 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6633 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6634 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6635 | unixOpen, /* xOpen */ \ |
| 6636 | unixDelete, /* xDelete */ \ |
| 6637 | unixAccess, /* xAccess */ \ |
| 6638 | unixFullPathname, /* xFullPathname */ \ |
| 6639 | unixDlOpen, /* xDlOpen */ \ |
| 6640 | unixDlError, /* xDlError */ \ |
| 6641 | unixDlSym, /* xDlSym */ \ |
| 6642 | unixDlClose, /* xDlClose */ \ |
| 6643 | unixRandomness, /* xRandomness */ \ |
| 6644 | unixSleep, /* xSleep */ \ |
| 6645 | unixCurrentTime, /* xCurrentTime */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6646 | unixGetLastError, /* xGetLastError */ \ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6647 | unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6648 | unixSetSystemCall, /* xSetSystemCall */ \ |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 6649 | unixGetSystemCall, /* xGetSystemCall */ \ |
| 6650 | unixNextSystemCall, /* xNextSystemCall */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6651 | } |
| 6652 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6653 | /* |
| 6654 | ** All default VFSes for unix are contained in the following array. |
| 6655 | ** |
| 6656 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 6657 | ** by the SQLite core when the VFS is registered. So the following |
| 6658 | ** array cannot be const. |
| 6659 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6660 | static sqlite3_vfs aVfs[] = { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6661 | #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__)) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6662 | UNIXVFS("unix", autolockIoFinder ), |
| 6663 | #else |
| 6664 | UNIXVFS("unix", posixIoFinder ), |
| 6665 | #endif |
| 6666 | UNIXVFS("unix-none", nolockIoFinder ), |
| 6667 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 6668 | UNIXVFS("unix-excl", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6669 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6670 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6671 | #endif |
| 6672 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6673 | UNIXVFS("unix-posix", posixIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6674 | #if !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6675 | UNIXVFS("unix-flock", flockIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6676 | #endif |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6677 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6678 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6679 | UNIXVFS("unix-afp", afpIoFinder ), |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6680 | UNIXVFS("unix-nfs", nfsIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6681 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6682 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6683 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6684 | unsigned int i; /* Loop counter */ |
| 6685 | |
| 6686 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6687 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6688 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6689 | } |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6690 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6691 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6692 | |
| 6693 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6694 | ** Shutdown the operating system interface. |
| 6695 | ** |
| 6696 | ** Some operating systems might need to do some cleanup in this routine, |
| 6697 | ** to release dynamically allocated objects. But not on unix. |
| 6698 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6699 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6700 | int sqlite3_os_end(void){ |
| 6701 | return SQLITE_OK; |
| 6702 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 6703 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 6704 | #endif /* SQLITE_OS_UNIX */ |