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 |
drh | 1da88f0 | 2011-12-17 16:09:16 +0000 | [diff] [blame] | 125 | #ifndef MISSING_STATVFS |
| 126 | #include <sys/statvfs.h> |
| 127 | #endif |
| 128 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 129 | |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 130 | #if SQLITE_ENABLE_LOCKING_STYLE |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 131 | # include <sys/ioctl.h> |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 132 | # if OS_VXWORKS |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 133 | # include <semaphore.h> |
| 134 | # include <limits.h> |
| 135 | # else |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 136 | # include <sys/file.h> |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 137 | # include <sys/param.h> |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 138 | # endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 139 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 140 | |
drh | f8b4d8c | 2010-03-05 13:53:22 +0000 | [diff] [blame] | 141 | #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS) |
drh | 84a2bf6 | 2010-03-05 13:41:06 +0000 | [diff] [blame] | 142 | # include <sys/mount.h> |
| 143 | #endif |
| 144 | |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 145 | #ifdef HAVE_UTIME |
| 146 | # include <utime.h> |
| 147 | #endif |
| 148 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 149 | /* |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 150 | ** Allowed values of unixFile.fsFlags |
| 151 | */ |
| 152 | #define SQLITE_FSFLAGS_IS_MSDOS 0x1 |
| 153 | |
| 154 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 155 | ** If we are to be thread-safe, include the pthreads header and define |
| 156 | ** the SQLITE_UNIX_THREADS macro. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 157 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 158 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 159 | # include <pthread.h> |
| 160 | # define SQLITE_UNIX_THREADS 1 |
| 161 | #endif |
| 162 | |
| 163 | /* |
| 164 | ** Default permissions when creating a new file |
| 165 | */ |
| 166 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 167 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 168 | #endif |
| 169 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 170 | /* |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 171 | ** Default permissions when creating auto proxy dir |
| 172 | */ |
| 173 | #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 174 | # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 |
| 175 | #endif |
| 176 | |
| 177 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 178 | ** Maximum supported path-length. |
| 179 | */ |
| 180 | #define MAX_PATHNAME 512 |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 181 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 182 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 183 | ** Only set the lastErrno if the error code is a real error and not |
| 184 | ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 185 | */ |
| 186 | #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |
| 187 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 188 | /* Forward references */ |
| 189 | typedef struct unixShm unixShm; /* Connection shared memory */ |
| 190 | typedef struct unixShmNode unixShmNode; /* Shared memory instance */ |
| 191 | typedef struct unixInodeInfo unixInodeInfo; /* An i-node */ |
| 192 | typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 193 | |
| 194 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 195 | ** Sometimes, after a file handle is closed by SQLite, the file descriptor |
| 196 | ** cannot be closed immediately. In these cases, instances of the following |
| 197 | ** structure are used to store the file descriptor while waiting for an |
| 198 | ** opportunity to either close or reuse it. |
| 199 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 200 | struct UnixUnusedFd { |
| 201 | int fd; /* File descriptor to close */ |
| 202 | int flags; /* Flags this file descriptor was opened with */ |
| 203 | UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ |
| 204 | }; |
| 205 | |
| 206 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 207 | ** The unixFile structure is subclass of sqlite3_file specific to the unix |
| 208 | ** VFS implementations. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 209 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 210 | typedef struct unixFile unixFile; |
| 211 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 212 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 213 | sqlite3_vfs *pVfs; /* The VFS that created this unixFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 214 | unixInodeInfo *pInode; /* Info about locks on this inode */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 215 | int h; /* The file descriptor */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 216 | unsigned char eFileLock; /* The type of lock held on this fd */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 217 | unsigned char ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 218 | unsigned char szSector; /* Sectorsize/512 */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 219 | int lastErrno; /* The unix errno from last I/O error */ |
| 220 | void *lockingContext; /* Locking style specific state */ |
| 221 | UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 222 | const char *zPath; /* Name of the file */ |
| 223 | unixShm *pShm; /* Shared memory segment information */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 224 | int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 225 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 226 | int openFlags; /* The flags specified at open() */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 227 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 228 | #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 229 | unsigned fsFlags; /* cached details from statfs() */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 230 | #endif |
| 231 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 232 | int isDelete; /* Delete on close if true */ |
| 233 | struct vxworksFileId *pId; /* Unique file ID */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 234 | #endif |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 235 | #ifndef NDEBUG |
| 236 | /* The next group of variables are used to track whether or not the |
| 237 | ** transaction counter in bytes 24-27 of database files are updated |
| 238 | ** whenever any part of the database changes. An assertion fault will |
| 239 | ** occur if a file is updated without also updating the transaction |
| 240 | ** counter. This test is made to avoid new problems similar to the |
| 241 | ** one described by ticket #3584. |
| 242 | */ |
| 243 | unsigned char transCntrChng; /* True if the transaction counter changed */ |
| 244 | unsigned char dbUpdate; /* True if any part of database file changed */ |
| 245 | unsigned char inNormalWrite; /* True if in a normal write operation */ |
| 246 | #endif |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 247 | #ifdef SQLITE_TEST |
| 248 | /* In test mode, increase the size of this structure a bit so that |
| 249 | ** it is larger than the struct CrashFile defined in test6.c. |
| 250 | */ |
| 251 | char aPadding[32]; |
| 252 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 253 | }; |
| 254 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 255 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 256 | ** Allowed values for the unixFile.ctrlFlags bitmask: |
| 257 | */ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 258 | #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ |
| 259 | #define UNIXFILE_RDONLY 0x02 /* Connection is read only */ |
| 260 | #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ |
dan | ee140c4 | 2011-08-25 13:46:32 +0000 | [diff] [blame] | 261 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 262 | # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ |
| 263 | #else |
| 264 | # define UNIXFILE_DIRSYNC 0x00 |
| 265 | #endif |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame^] | 266 | #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 267 | |
| 268 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 269 | ** Include code that is common to all os_*.c files |
| 270 | */ |
| 271 | #include "os_common.h" |
| 272 | |
| 273 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 274 | ** Define various macros that are missing from some systems. |
| 275 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 276 | #ifndef O_LARGEFILE |
| 277 | # define O_LARGEFILE 0 |
| 278 | #endif |
| 279 | #ifdef SQLITE_DISABLE_LFS |
| 280 | # undef O_LARGEFILE |
| 281 | # define O_LARGEFILE 0 |
| 282 | #endif |
| 283 | #ifndef O_NOFOLLOW |
| 284 | # define O_NOFOLLOW 0 |
| 285 | #endif |
| 286 | #ifndef O_BINARY |
| 287 | # define O_BINARY 0 |
| 288 | #endif |
| 289 | |
| 290 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 291 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 292 | ** testing and debugging only. |
| 293 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 294 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 295 | #define threadid pthread_self() |
| 296 | #else |
| 297 | #define threadid 0 |
| 298 | #endif |
| 299 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 300 | /* |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 301 | ** Different Unix systems declare open() in different ways. Same use |
| 302 | ** open(const char*,int,mode_t). Others use open(const char*,int,...). |
| 303 | ** The difference is important when using a pointer to the function. |
| 304 | ** |
| 305 | ** The safest way to deal with the problem is to always use this wrapper |
| 306 | ** which always has the same well-defined interface. |
| 307 | */ |
| 308 | static int posixOpen(const char *zFile, int flags, int mode){ |
| 309 | return open(zFile, flags, mode); |
| 310 | } |
| 311 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 312 | /* Forward reference */ |
| 313 | static int openDirectory(const char*, int*); |
| 314 | |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 315 | /* |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 316 | ** Many system calls are accessed through pointer-to-functions so that |
| 317 | ** they may be overridden at runtime to facilitate fault injection during |
| 318 | ** testing and sandboxing. The following array holds the names and pointers |
| 319 | ** to all overrideable system calls. |
| 320 | */ |
| 321 | static struct unix_syscall { |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 322 | const char *zName; /* Name of the sytem call */ |
| 323 | sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ |
| 324 | sqlite3_syscall_ptr pDefault; /* Default value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 325 | } aSyscall[] = { |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 326 | { "open", (sqlite3_syscall_ptr)posixOpen, 0 }, |
| 327 | #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent) |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 328 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 329 | { "close", (sqlite3_syscall_ptr)close, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 330 | #define osClose ((int(*)(int))aSyscall[1].pCurrent) |
| 331 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 332 | { "access", (sqlite3_syscall_ptr)access, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 333 | #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent) |
| 334 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 335 | { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 336 | #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent) |
| 337 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 338 | { "stat", (sqlite3_syscall_ptr)stat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 339 | #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) |
| 340 | |
| 341 | /* |
| 342 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 343 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 344 | ** that always succeeds. This means that locking does not occur under |
| 345 | ** DJGPP. But it is DOS - what did you expect? |
| 346 | */ |
| 347 | #ifdef __DJGPP__ |
| 348 | { "fstat", 0, 0 }, |
| 349 | #define osFstat(a,b,c) 0 |
| 350 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 351 | { "fstat", (sqlite3_syscall_ptr)fstat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 352 | #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent) |
| 353 | #endif |
| 354 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 355 | { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 356 | #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent) |
| 357 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 358 | { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 359 | #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 360 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 361 | { "read", (sqlite3_syscall_ptr)read, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 362 | #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) |
| 363 | |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 364 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 365 | { "pread", (sqlite3_syscall_ptr)pread, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 366 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 367 | { "pread", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 368 | #endif |
| 369 | #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) |
| 370 | |
| 371 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 372 | { "pread64", (sqlite3_syscall_ptr)pread64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 373 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 374 | { "pread64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 375 | #endif |
| 376 | #define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent) |
| 377 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 378 | { "write", (sqlite3_syscall_ptr)write, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 379 | #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) |
| 380 | |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 381 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 382 | { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 383 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 384 | { "pwrite", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 385 | #endif |
| 386 | #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 387 | aSyscall[12].pCurrent) |
| 388 | |
| 389 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 390 | { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 391 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 392 | { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 393 | #endif |
| 394 | #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 395 | aSyscall[13].pCurrent) |
| 396 | |
drh | a6c4749 | 2011-04-11 18:35:09 +0000 | [diff] [blame] | 397 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 398 | { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 399 | #else |
| 400 | { "fchmod", (sqlite3_syscall_ptr)0, 0 }, |
drh | a6c4749 | 2011-04-11 18:35:09 +0000 | [diff] [blame] | 401 | #endif |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 402 | #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 403 | |
| 404 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 405 | { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 406 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 407 | { "fallocate", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 408 | #endif |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 409 | #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 410 | |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 411 | { "unlink", (sqlite3_syscall_ptr)unlink, 0 }, |
| 412 | #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent) |
| 413 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 414 | { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 }, |
| 415 | #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent) |
| 416 | |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 417 | { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 }, |
| 418 | #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent) |
| 419 | |
| 420 | { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 }, |
| 421 | #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent) |
| 422 | |
drh | 1da88f0 | 2011-12-17 16:09:16 +0000 | [diff] [blame] | 423 | #if defined(MISSING_STATVFS) |
| 424 | { "statvfs", (sqlite3_syscall_ptr)0, 0 }, |
| 425 | #define osStatvfs ((int(*)(const char*,void*))aSyscall[20].pCurrent) |
| 426 | #else |
| 427 | { "statvfs", (sqlite3_syscall_ptr)statvfs, 0 }, |
| 428 | #define osStatvfs ((int(*)(const char*,struct statvfs*))aSyscall[20].pCurrent) |
| 429 | #endif |
| 430 | |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 431 | }; /* End of the overrideable system calls */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 432 | |
| 433 | /* |
| 434 | ** This is the xSetSystemCall() method of sqlite3_vfs for all of the |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 435 | ** "unix" VFSes. Return SQLITE_OK opon successfully updating the |
| 436 | ** system call pointer, or SQLITE_NOTFOUND if there is no configurable |
| 437 | ** system call named zName. |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 438 | */ |
| 439 | static int unixSetSystemCall( |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 440 | sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ |
| 441 | const char *zName, /* Name of system call to override */ |
| 442 | sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 443 | ){ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 444 | unsigned int i; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 445 | int rc = SQLITE_NOTFOUND; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 446 | |
| 447 | UNUSED_PARAMETER(pNotUsed); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 448 | if( zName==0 ){ |
| 449 | /* If no zName is given, restore all system calls to their default |
| 450 | ** settings and return NULL |
| 451 | */ |
dan | 51438a7 | 2011-04-02 17:00:47 +0000 | [diff] [blame] | 452 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 453 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 454 | if( aSyscall[i].pDefault ){ |
| 455 | aSyscall[i].pCurrent = aSyscall[i].pDefault; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | }else{ |
| 459 | /* If zName is specified, operate on only the one system call |
| 460 | ** specified. |
| 461 | */ |
| 462 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 463 | if( strcmp(zName, aSyscall[i].zName)==0 ){ |
| 464 | if( aSyscall[i].pDefault==0 ){ |
| 465 | aSyscall[i].pDefault = aSyscall[i].pCurrent; |
| 466 | } |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 467 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 468 | if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault; |
| 469 | aSyscall[i].pCurrent = pNewFunc; |
| 470 | break; |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | return rc; |
| 475 | } |
| 476 | |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 477 | /* |
| 478 | ** Return the value of a system call. Return NULL if zName is not a |
| 479 | ** recognized system call name. NULL is also returned if the system call |
| 480 | ** is currently undefined. |
| 481 | */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 482 | static sqlite3_syscall_ptr unixGetSystemCall( |
| 483 | sqlite3_vfs *pNotUsed, |
| 484 | const char *zName |
| 485 | ){ |
| 486 | unsigned int i; |
| 487 | |
| 488 | UNUSED_PARAMETER(pNotUsed); |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 489 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 490 | if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent; |
| 491 | } |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | ** Return the name of the first system call after zName. If zName==NULL |
| 497 | ** then return the name of the first system call. Return NULL if zName |
| 498 | ** is the last system call or if zName is not the name of a valid |
| 499 | ** system call. |
| 500 | */ |
| 501 | static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){ |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 502 | int i = -1; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 503 | |
| 504 | UNUSED_PARAMETER(p); |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 505 | if( zName ){ |
| 506 | for(i=0; i<ArraySize(aSyscall)-1; i++){ |
| 507 | if( strcmp(zName, aSyscall[i].zName)==0 ) break; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 508 | } |
| 509 | } |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 510 | for(i++; i<ArraySize(aSyscall); i++){ |
| 511 | if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 512 | } |
| 513 | return 0; |
| 514 | } |
| 515 | |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 516 | /* |
| 517 | ** Retry open() calls that fail due to EINTR |
| 518 | */ |
| 519 | static int robust_open(const char *z, int f, int m){ |
| 520 | int rc; |
| 521 | do{ rc = osOpen(z,f,m); }while( rc<0 && errno==EINTR ); |
| 522 | return rc; |
| 523 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 524 | |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 525 | /* |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 526 | ** Helper functions to obtain and relinquish the global mutex. The |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 527 | ** global mutex is used to protect the unixInodeInfo and |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 528 | ** vxworksFileId objects used by this file, all of which may be |
| 529 | ** shared by multiple threads. |
| 530 | ** |
| 531 | ** Function unixMutexHeld() is used to assert() that the global mutex |
| 532 | ** is held when required. This function is only used as part of assert() |
| 533 | ** statements. e.g. |
| 534 | ** |
| 535 | ** unixEnterMutex() |
| 536 | ** assert( unixMutexHeld() ); |
| 537 | ** unixEnterLeave() |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 538 | */ |
| 539 | static void unixEnterMutex(void){ |
| 540 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 541 | } |
| 542 | static void unixLeaveMutex(void){ |
| 543 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 544 | } |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 545 | #ifdef SQLITE_DEBUG |
| 546 | static int unixMutexHeld(void) { |
| 547 | return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 548 | } |
| 549 | #endif |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 550 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 551 | |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 552 | #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 553 | /* |
| 554 | ** Helper function for printing out trace information from debugging |
| 555 | ** binaries. This returns the string represetation of the supplied |
| 556 | ** integer lock-type. |
| 557 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 558 | static const char *azFileLock(int eFileLock){ |
| 559 | switch( eFileLock ){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 560 | case NO_LOCK: return "NONE"; |
| 561 | case SHARED_LOCK: return "SHARED"; |
| 562 | case RESERVED_LOCK: return "RESERVED"; |
| 563 | case PENDING_LOCK: return "PENDING"; |
| 564 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 565 | } |
| 566 | return "ERROR"; |
| 567 | } |
| 568 | #endif |
| 569 | |
| 570 | #ifdef SQLITE_LOCK_TRACE |
| 571 | /* |
| 572 | ** Print out information about all locking operations. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 573 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 574 | ** This routine is used for troubleshooting locks on multithreaded |
| 575 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 576 | ** command-line option on the compiler. This code is normally |
| 577 | ** turned off. |
| 578 | */ |
| 579 | static int lockTrace(int fd, int op, struct flock *p){ |
| 580 | char *zOpName, *zType; |
| 581 | int s; |
| 582 | int savedErrno; |
| 583 | if( op==F_GETLK ){ |
| 584 | zOpName = "GETLK"; |
| 585 | }else if( op==F_SETLK ){ |
| 586 | zOpName = "SETLK"; |
| 587 | }else{ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 588 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 589 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 590 | return s; |
| 591 | } |
| 592 | if( p->l_type==F_RDLCK ){ |
| 593 | zType = "RDLCK"; |
| 594 | }else if( p->l_type==F_WRLCK ){ |
| 595 | zType = "WRLCK"; |
| 596 | }else if( p->l_type==F_UNLCK ){ |
| 597 | zType = "UNLCK"; |
| 598 | }else{ |
| 599 | assert( 0 ); |
| 600 | } |
| 601 | assert( p->l_whence==SEEK_SET ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 602 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 603 | savedErrno = errno; |
| 604 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 605 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 606 | (int)p->l_pid, s); |
| 607 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 608 | struct flock l2; |
| 609 | l2 = *p; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 610 | osFcntl(fd, F_GETLK, &l2); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 611 | if( l2.l_type==F_RDLCK ){ |
| 612 | zType = "RDLCK"; |
| 613 | }else if( l2.l_type==F_WRLCK ){ |
| 614 | zType = "WRLCK"; |
| 615 | }else if( l2.l_type==F_UNLCK ){ |
| 616 | zType = "UNLCK"; |
| 617 | }else{ |
| 618 | assert( 0 ); |
| 619 | } |
| 620 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 621 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 622 | } |
| 623 | errno = savedErrno; |
| 624 | return s; |
| 625 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 626 | #undef osFcntl |
| 627 | #define osFcntl lockTrace |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 628 | #endif /* SQLITE_LOCK_TRACE */ |
| 629 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 630 | /* |
| 631 | ** Retry ftruncate() calls that fail due to EINTR |
| 632 | */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 633 | static int robust_ftruncate(int h, sqlite3_int64 sz){ |
| 634 | int rc; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 635 | do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR ); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 636 | return rc; |
| 637 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 638 | |
| 639 | /* |
| 640 | ** This routine translates a standard POSIX errno code into something |
| 641 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 642 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 643 | ** and a variety of "please close the file descriptor NOW" errors into |
| 644 | ** SQLITE_IOERR |
| 645 | ** |
| 646 | ** Errors during initialization of locks, or file system support for locks, |
| 647 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 648 | */ |
| 649 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
| 650 | switch (posixError) { |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 651 | #if 0 |
| 652 | /* At one point this code was not commented out. In theory, this branch |
| 653 | ** should never be hit, as this function should only be called after |
| 654 | ** a locking-related function (i.e. fcntl()) has returned non-zero with |
| 655 | ** the value of errno as the first argument. Since a system call has failed, |
| 656 | ** errno should be non-zero. |
| 657 | ** |
| 658 | ** Despite this, if errno really is zero, we still don't want to return |
| 659 | ** SQLITE_OK. The system call failed, and *some* SQLite error should be |
| 660 | ** propagated back to the caller. Commenting this branch out means errno==0 |
| 661 | ** will be handled by the "default:" case below. |
| 662 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 663 | case 0: |
| 664 | return SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 665 | #endif |
| 666 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 667 | case EAGAIN: |
| 668 | case ETIMEDOUT: |
| 669 | case EBUSY: |
| 670 | case EINTR: |
| 671 | case ENOLCK: |
| 672 | /* random NFS retry error, unless during file system support |
| 673 | * introspection, in which it actually means what it says */ |
| 674 | return SQLITE_BUSY; |
| 675 | |
| 676 | case EACCES: |
| 677 | /* EACCES is like EAGAIN during locking operations, but not any other time*/ |
| 678 | if( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
| 679 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 680 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 681 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){ |
| 682 | return SQLITE_BUSY; |
| 683 | } |
| 684 | /* else fall through */ |
| 685 | case EPERM: |
| 686 | return SQLITE_PERM; |
| 687 | |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 688 | /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And |
| 689 | ** this module never makes such a call. And the code in SQLite itself |
| 690 | ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons |
| 691 | ** this case is also commented out. If the system does set errno to EDEADLK, |
| 692 | ** the default SQLITE_IOERR_XXX code will be returned. */ |
| 693 | #if 0 |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 694 | case EDEADLK: |
| 695 | return SQLITE_IOERR_BLOCKED; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 696 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 697 | |
| 698 | #if EOPNOTSUPP!=ENOTSUP |
| 699 | case EOPNOTSUPP: |
| 700 | /* something went terribly awry, unless during file system support |
| 701 | * introspection, in which it actually means what it says */ |
| 702 | #endif |
| 703 | #ifdef ENOTSUP |
| 704 | case ENOTSUP: |
| 705 | /* invalid fd, unless during file system support introspection, in which |
| 706 | * it actually means what it says */ |
| 707 | #endif |
| 708 | case EIO: |
| 709 | case EBADF: |
| 710 | case EINVAL: |
| 711 | case ENOTCONN: |
| 712 | case ENODEV: |
| 713 | case ENXIO: |
| 714 | case ENOENT: |
dan | 33067e7 | 2011-07-15 13:43:34 +0000 | [diff] [blame] | 715 | #ifdef ESTALE /* ESTALE is not defined on Interix systems */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 716 | case ESTALE: |
dan | 33067e7 | 2011-07-15 13:43:34 +0000 | [diff] [blame] | 717 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 718 | case ENOSYS: |
| 719 | /* these should force the client to close the file and reconnect */ |
| 720 | |
| 721 | default: |
| 722 | return sqliteIOErr; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | |
| 727 | |
| 728 | /****************************************************************************** |
| 729 | ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 730 | ** |
| 731 | ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 732 | ** the device number and the inode number. But this does not work on VxWorks. |
| 733 | ** On VxWorks, a unique file id must be based on the canonical filename. |
| 734 | ** |
| 735 | ** A pointer to an instance of the following structure can be used as a |
| 736 | ** unique file ID in VxWorks. Each instance of this structure contains |
| 737 | ** a copy of the canonical filename. There is also a reference count. |
| 738 | ** The structure is reclaimed when the number of pointers to it drops to |
| 739 | ** zero. |
| 740 | ** |
| 741 | ** There are never very many files open at one time and lookups are not |
| 742 | ** a performance-critical path, so it is sufficient to put these |
| 743 | ** structures on a linked list. |
| 744 | */ |
| 745 | struct vxworksFileId { |
| 746 | struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 747 | int nRef; /* Number of references to this one */ |
| 748 | int nName; /* Length of the zCanonicalName[] string */ |
| 749 | char *zCanonicalName; /* Canonical filename */ |
| 750 | }; |
| 751 | |
| 752 | #if OS_VXWORKS |
| 753 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 754 | ** All unique filenames are held on a linked list headed by this |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 755 | ** variable: |
| 756 | */ |
| 757 | static struct vxworksFileId *vxworksFileList = 0; |
| 758 | |
| 759 | /* |
| 760 | ** Simplify a filename into its canonical form |
| 761 | ** by making the following changes: |
| 762 | ** |
| 763 | ** * removing any trailing and duplicate / |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 764 | ** * convert /./ into just / |
| 765 | ** * convert /A/../ where A is any simple name into just / |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 766 | ** |
| 767 | ** Changes are made in-place. Return the new name length. |
| 768 | ** |
| 769 | ** The original filename is in z[0..n-1]. Return the number of |
| 770 | ** characters in the simplified name. |
| 771 | */ |
| 772 | static int vxworksSimplifyName(char *z, int n){ |
| 773 | int i, j; |
| 774 | while( n>1 && z[n-1]=='/' ){ n--; } |
| 775 | for(i=j=0; i<n; i++){ |
| 776 | if( z[i]=='/' ){ |
| 777 | if( z[i+1]=='/' ) continue; |
| 778 | if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 779 | i += 1; |
| 780 | continue; |
| 781 | } |
| 782 | if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 783 | while( j>0 && z[j-1]!='/' ){ j--; } |
| 784 | if( j>0 ){ j--; } |
| 785 | i += 2; |
| 786 | continue; |
| 787 | } |
| 788 | } |
| 789 | z[j++] = z[i]; |
| 790 | } |
| 791 | z[j] = 0; |
| 792 | return j; |
| 793 | } |
| 794 | |
| 795 | /* |
| 796 | ** Find a unique file ID for the given absolute pathname. Return |
| 797 | ** a pointer to the vxworksFileId object. This pointer is the unique |
| 798 | ** file ID. |
| 799 | ** |
| 800 | ** The nRef field of the vxworksFileId object is incremented before |
| 801 | ** the object is returned. A new vxworksFileId object is created |
| 802 | ** and added to the global list if necessary. |
| 803 | ** |
| 804 | ** If a memory allocation error occurs, return NULL. |
| 805 | */ |
| 806 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 807 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 808 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 809 | int n; /* Length of zAbsoluteName string */ |
| 810 | |
| 811 | assert( zAbsoluteName[0]=='/' ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 812 | n = (int)strlen(zAbsoluteName); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 813 | pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) ); |
| 814 | if( pNew==0 ) return 0; |
| 815 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 816 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 817 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 818 | |
| 819 | /* Search for an existing entry that matching the canonical name. |
| 820 | ** If found, increment the reference count and return a pointer to |
| 821 | ** the existing file ID. |
| 822 | */ |
| 823 | unixEnterMutex(); |
| 824 | for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 825 | if( pCandidate->nName==n |
| 826 | && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 827 | ){ |
| 828 | sqlite3_free(pNew); |
| 829 | pCandidate->nRef++; |
| 830 | unixLeaveMutex(); |
| 831 | return pCandidate; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | /* No match was found. We will make a new file ID */ |
| 836 | pNew->nRef = 1; |
| 837 | pNew->nName = n; |
| 838 | pNew->pNext = vxworksFileList; |
| 839 | vxworksFileList = pNew; |
| 840 | unixLeaveMutex(); |
| 841 | return pNew; |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | ** Decrement the reference count on a vxworksFileId object. Free |
| 846 | ** the object when the reference count reaches zero. |
| 847 | */ |
| 848 | static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 849 | unixEnterMutex(); |
| 850 | assert( pId->nRef>0 ); |
| 851 | pId->nRef--; |
| 852 | if( pId->nRef==0 ){ |
| 853 | struct vxworksFileId **pp; |
| 854 | for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 855 | assert( *pp==pId ); |
| 856 | *pp = pId->pNext; |
| 857 | sqlite3_free(pId); |
| 858 | } |
| 859 | unixLeaveMutex(); |
| 860 | } |
| 861 | #endif /* OS_VXWORKS */ |
| 862 | /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 863 | ******************************************************************************/ |
| 864 | |
| 865 | |
| 866 | /****************************************************************************** |
| 867 | *************************** Posix Advisory Locking **************************** |
| 868 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 869 | ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 870 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 871 | ** sets or clears a lock, that operation overrides any prior locks set |
| 872 | ** by the same process. It does not explicitly say so, but this implies |
| 873 | ** that it overrides locks set by the same process using a different |
| 874 | ** file descriptor. Consider this test case: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 875 | ** |
| 876 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 877 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 878 | ** |
| 879 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 880 | ** one is a hard or symbolic link to the other) then if you set |
| 881 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 882 | ** on fd2, it works. I would have expected the second lock to |
| 883 | ** fail since there was already a lock on the file due to fd1. |
| 884 | ** But not so. Since both locks came from the same process, the |
| 885 | ** second overrides the first, even though they were on different |
| 886 | ** file descriptors opened on different file names. |
| 887 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 888 | ** This means that we cannot use POSIX locks to synchronize file access |
| 889 | ** among competing threads of the same process. POSIX locks will work fine |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 890 | ** to synchronize access for threads in separate processes, but not |
| 891 | ** threads within the same process. |
| 892 | ** |
| 893 | ** To work around the problem, SQLite has to manage file locks internally |
| 894 | ** on its own. Whenever a new database is opened, we have to find the |
| 895 | ** specific inode of the database file (the inode is determined by the |
| 896 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 897 | ** and check for locks already existing on that inode. When locks are |
| 898 | ** created or removed, we have to look at our own internal record of the |
| 899 | ** locks to see if another thread has previously set a lock on that same |
| 900 | ** inode. |
| 901 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 902 | ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 903 | ** For VxWorks, we have to use the alternative unique ID system based on |
| 904 | ** canonical filename and implemented in the previous division.) |
| 905 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 906 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 907 | ** descriptor. It is now a structure that holds the integer file |
| 908 | ** descriptor and a pointer to a structure that describes the internal |
| 909 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 910 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 911 | ** point to the same locking structure. The locking structure keeps |
| 912 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 913 | ** field that tells us its internal lock status. cnt==0 means the |
| 914 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 915 | ** cnt>0 means there are cnt shared locks on the file. |
| 916 | ** |
| 917 | ** Any attempt to lock or unlock a file first checks the locking |
| 918 | ** structure. The fcntl() system call is only invoked to set a |
| 919 | ** POSIX lock if the internal lock structure transitions between |
| 920 | ** a locked and an unlocked state. |
| 921 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 922 | ** But wait: there are yet more problems with POSIX advisory locks. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 923 | ** |
| 924 | ** If you close a file descriptor that points to a file that has locks, |
| 925 | ** all locks on that file that are owned by the current process are |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 926 | ** released. To work around this problem, each unixInodeInfo object |
| 927 | ** maintains a count of the number of pending locks on tha inode. |
| 928 | ** When an attempt is made to close an unixFile, if there are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 929 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 930 | ** to close() the file descriptor is deferred until all of the locks clear. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 931 | ** The unixInodeInfo structure keeps a list of file descriptors that need to |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 932 | ** be closed and that list is walked (and cleared) when the last lock |
| 933 | ** clears. |
| 934 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 935 | ** Yet another problem: LinuxThreads do not play well with posix locks. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 936 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 937 | ** Many older versions of linux use the LinuxThreads library which is |
| 938 | ** not posix compliant. Under LinuxThreads, a lock created by thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 939 | ** A cannot be modified or overridden by a different thread B. |
| 940 | ** Only thread A can modify the lock. Locking behavior is correct |
| 941 | ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 942 | ** on linux - with NPTL a lock created by thread A can override locks |
| 943 | ** in thread B. But there is no way to know at compile-time which |
| 944 | ** threading library is being used. So there is no way to know at |
| 945 | ** compile-time whether or not thread A can override locks on thread B. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 946 | ** 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] | 947 | ** current process. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 948 | ** |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 949 | ** SQLite used to support LinuxThreads. But support for LinuxThreads |
| 950 | ** was dropped beginning with version 3.7.0. SQLite will still work with |
| 951 | ** LinuxThreads provided that (1) there is no more than one connection |
| 952 | ** per database file in the same process and (2) database connections |
| 953 | ** do not move across threads. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 954 | */ |
| 955 | |
| 956 | /* |
| 957 | ** An instance of the following structure serves as the key used |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 958 | ** to locate a particular unixInodeInfo object. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 959 | */ |
| 960 | struct unixFileId { |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 961 | dev_t dev; /* Device number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 962 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 963 | struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 964 | #else |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 965 | ino_t ino; /* Inode number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 966 | #endif |
| 967 | }; |
| 968 | |
| 969 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 970 | ** An instance of the following structure is allocated for each open |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 971 | ** inode. Or, on LinuxThreads, there is one of these structures for |
| 972 | ** each inode opened by each thread. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 973 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 974 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 975 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 976 | ** object keeps a count of the number of unixFile pointing to it. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 977 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 978 | struct unixInodeInfo { |
| 979 | struct unixFileId fileId; /* The lookup key */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 980 | int nShared; /* Number of SHARED locks held */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 981 | unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
| 982 | unsigned char bProcessLock; /* An exclusive process lock is held */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 983 | int nRef; /* Number of pointers to this structure */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 984 | unixShmNode *pShmNode; /* Shared memory associated with this inode */ |
| 985 | int nLock; /* Number of outstanding file locks */ |
| 986 | UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
| 987 | unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ |
| 988 | unixInodeInfo *pPrev; /* .... doubly linked */ |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 989 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 990 | unsigned long long sharedByte; /* for AFP simulated shared lock */ |
| 991 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 992 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 993 | sem_t *pSem; /* Named POSIX semaphore */ |
| 994 | char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 995 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 996 | }; |
| 997 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 998 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 999 | ** A lists of all unixInodeInfo objects. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1000 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1001 | static unixInodeInfo *inodeList = 0; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1002 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1003 | /* |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1004 | ** |
| 1005 | ** This function - unixLogError_x(), is only ever called via the macro |
| 1006 | ** unixLogError(). |
| 1007 | ** |
| 1008 | ** It is invoked after an error occurs in an OS function and errno has been |
| 1009 | ** set. It logs a message using sqlite3_log() containing the current value of |
| 1010 | ** errno and, if possible, the human-readable equivalent from strerror() or |
| 1011 | ** strerror_r(). |
| 1012 | ** |
| 1013 | ** The first argument passed to the macro should be the error code that |
| 1014 | ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). |
| 1015 | ** The two subsequent arguments should be the name of the OS function that |
| 1016 | ** failed (e.g. "unlink", "open") and the the associated file-system path, |
| 1017 | ** if any. |
| 1018 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1019 | #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__) |
| 1020 | static int unixLogErrorAtLine( |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1021 | int errcode, /* SQLite error code */ |
| 1022 | const char *zFunc, /* Name of OS function that failed */ |
| 1023 | const char *zPath, /* File path associated with error */ |
| 1024 | int iLine /* Source line number where error occurred */ |
| 1025 | ){ |
| 1026 | char *zErr; /* Message from strerror() or equivalent */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1027 | int iErrno = errno; /* Saved syscall error number */ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1028 | |
| 1029 | /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use |
| 1030 | ** the strerror() function to obtain the human-readable error message |
| 1031 | ** equivalent to errno. Otherwise, use strerror_r(). |
| 1032 | */ |
| 1033 | #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) |
| 1034 | char aErr[80]; |
| 1035 | memset(aErr, 0, sizeof(aErr)); |
| 1036 | zErr = aErr; |
| 1037 | |
| 1038 | /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined, |
| 1039 | ** assume that the system provides the the GNU version of strerror_r() that |
| 1040 | ** returns a pointer to a buffer containing the error message. That pointer |
| 1041 | ** may point to aErr[], or it may point to some static storage somewhere. |
| 1042 | ** Otherwise, assume that the system provides the POSIX version of |
| 1043 | ** strerror_r(), which always writes an error message into aErr[]. |
| 1044 | ** |
| 1045 | ** If the code incorrectly assumes that it is the POSIX version that is |
| 1046 | ** available, the error message will often be an empty string. Not a |
| 1047 | ** huge problem. Incorrectly concluding that the GNU version is available |
| 1048 | ** could lead to a segfault though. |
| 1049 | */ |
| 1050 | #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) |
| 1051 | zErr = |
| 1052 | # endif |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1053 | strerror_r(iErrno, aErr, sizeof(aErr)-1); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1054 | |
| 1055 | #elif SQLITE_THREADSAFE |
| 1056 | /* This is a threadsafe build, but strerror_r() is not available. */ |
| 1057 | zErr = ""; |
| 1058 | #else |
| 1059 | /* Non-threadsafe build, use strerror(). */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1060 | zErr = strerror(iErrno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1061 | #endif |
| 1062 | |
| 1063 | assert( errcode!=SQLITE_OK ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1064 | if( zPath==0 ) zPath = ""; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1065 | sqlite3_log(errcode, |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1066 | "os_unix.c:%d: (%d) %s(%s) - %s", |
| 1067 | iLine, iErrno, zFunc, zPath, zErr |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1068 | ); |
| 1069 | |
| 1070 | return errcode; |
| 1071 | } |
| 1072 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1073 | /* |
| 1074 | ** Close a file descriptor. |
| 1075 | ** |
| 1076 | ** We assume that close() almost always works, since it is only in a |
| 1077 | ** very sick application or on a very sick platform that it might fail. |
| 1078 | ** If it does fail, simply leak the file descriptor, but do log the |
| 1079 | ** error. |
| 1080 | ** |
| 1081 | ** Note that it is not safe to retry close() after EINTR since the |
| 1082 | ** file descriptor might have already been reused by another thread. |
| 1083 | ** So we don't even try to recover from an EINTR. Just log the error |
| 1084 | ** and move on. |
| 1085 | */ |
| 1086 | static void robust_close(unixFile *pFile, int h, int lineno){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1087 | if( osClose(h) ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1088 | unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", |
| 1089 | pFile ? pFile->zPath : 0, lineno); |
| 1090 | } |
| 1091 | } |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1092 | |
| 1093 | /* |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1094 | ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1095 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1096 | static void closePendingFds(unixFile *pFile){ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1097 | unixInodeInfo *pInode = pFile->pInode; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1098 | UnixUnusedFd *p; |
| 1099 | UnixUnusedFd *pNext; |
| 1100 | for(p=pInode->pUnused; p; p=pNext){ |
| 1101 | pNext = p->pNext; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1102 | robust_close(pFile, p->fd, __LINE__); |
| 1103 | sqlite3_free(p); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1104 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1105 | pInode->pUnused = 0; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1109 | ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1110 | ** |
| 1111 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1112 | ** when this function is called. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1113 | */ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1114 | static void releaseInodeInfo(unixFile *pFile){ |
| 1115 | unixInodeInfo *pInode = pFile->pInode; |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1116 | assert( unixMutexHeld() ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1117 | if( ALWAYS(pInode) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1118 | pInode->nRef--; |
| 1119 | if( pInode->nRef==0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1120 | assert( pInode->pShmNode==0 ); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1121 | closePendingFds(pFile); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1122 | if( pInode->pPrev ){ |
| 1123 | assert( pInode->pPrev->pNext==pInode ); |
| 1124 | pInode->pPrev->pNext = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1125 | }else{ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1126 | assert( inodeList==pInode ); |
| 1127 | inodeList = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1128 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1129 | if( pInode->pNext ){ |
| 1130 | assert( pInode->pNext->pPrev==pInode ); |
| 1131 | pInode->pNext->pPrev = pInode->pPrev; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1132 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1133 | sqlite3_free(pInode); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1134 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1139 | ** Given a file descriptor, locate the unixInodeInfo object that |
| 1140 | ** describes that file descriptor. Create a new one if necessary. The |
| 1141 | ** return value might be uninitialized if an error occurs. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1142 | ** |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1143 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1144 | ** when this function is called. |
| 1145 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1146 | ** Return an appropriate error code. |
| 1147 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1148 | static int findInodeInfo( |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1149 | unixFile *pFile, /* Unix file with file desc used in the key */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1150 | unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1151 | ){ |
| 1152 | int rc; /* System call return code */ |
| 1153 | int fd; /* The file descriptor for pFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1154 | struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ |
| 1155 | struct stat statbuf; /* Low-level file information */ |
| 1156 | unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1157 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1158 | assert( unixMutexHeld() ); |
| 1159 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1160 | /* Get low-level information about the file that we can used to |
| 1161 | ** create a unique name for the file. |
| 1162 | */ |
| 1163 | fd = pFile->h; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1164 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1165 | if( rc!=0 ){ |
| 1166 | pFile->lastErrno = errno; |
| 1167 | #ifdef EOVERFLOW |
| 1168 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 1169 | #endif |
| 1170 | return SQLITE_IOERR; |
| 1171 | } |
| 1172 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1173 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1174 | /* On OS X on an msdos filesystem, the inode number is reported |
| 1175 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 1176 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 1177 | ** we always increase the file size to 1 by writing a single byte |
| 1178 | ** prior to accessing the inode number. The one byte written is |
| 1179 | ** an ASCII 'S' character which also happens to be the first byte |
| 1180 | ** in the header of every SQLite database. In this way, if there |
| 1181 | ** is a race condition such that another thread has already populated |
| 1182 | ** the first page of the database, no damage is done. |
| 1183 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1184 | if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 1185 | do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR ); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1186 | if( rc!=1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1187 | pFile->lastErrno = errno; |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1188 | return SQLITE_IOERR; |
| 1189 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1190 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1191 | if( rc!=0 ){ |
| 1192 | pFile->lastErrno = errno; |
| 1193 | return SQLITE_IOERR; |
| 1194 | } |
| 1195 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1196 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1197 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1198 | memset(&fileId, 0, sizeof(fileId)); |
| 1199 | fileId.dev = statbuf.st_dev; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1200 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1201 | fileId.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1202 | #else |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1203 | fileId.ino = statbuf.st_ino; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1204 | #endif |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1205 | pInode = inodeList; |
| 1206 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 1207 | pInode = pInode->pNext; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1208 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1209 | if( pInode==0 ){ |
| 1210 | pInode = sqlite3_malloc( sizeof(*pInode) ); |
| 1211 | if( pInode==0 ){ |
| 1212 | return SQLITE_NOMEM; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1213 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1214 | memset(pInode, 0, sizeof(*pInode)); |
| 1215 | memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
| 1216 | pInode->nRef = 1; |
| 1217 | pInode->pNext = inodeList; |
| 1218 | pInode->pPrev = 0; |
| 1219 | if( inodeList ) inodeList->pPrev = pInode; |
| 1220 | inodeList = pInode; |
| 1221 | }else{ |
| 1222 | pInode->nRef++; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1223 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1224 | *ppInode = pInode; |
| 1225 | return SQLITE_OK; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1226 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1227 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1228 | |
| 1229 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1230 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1231 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1232 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1233 | ** 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] | 1234 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1235 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1236 | int rc = SQLITE_OK; |
| 1237 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1238 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1239 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1240 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1241 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1242 | assert( pFile ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1243 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1244 | |
| 1245 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1246 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1247 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1250 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1251 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1252 | #ifndef __DJGPP__ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1253 | if( !reserved && !pFile->pInode->bProcessLock ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1254 | struct flock lock; |
| 1255 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1256 | lock.l_start = RESERVED_BYTE; |
| 1257 | lock.l_len = 1; |
| 1258 | lock.l_type = F_WRLCK; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1259 | if( osFcntl(pFile->h, F_GETLK, &lock) ){ |
| 1260 | rc = SQLITE_IOERR_CHECKRESERVEDLOCK; |
| 1261 | pFile->lastErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1262 | } else if( lock.l_type!=F_UNLCK ){ |
| 1263 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1264 | } |
| 1265 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1266 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1267 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1268 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1269 | OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1270 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1271 | *pResOut = reserved; |
| 1272 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1276 | ** Attempt to set a system-lock on the file pFile. The lock is |
| 1277 | ** described by pLock. |
| 1278 | ** |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1279 | ** If the pFile was opened read/write from unix-excl, then the only lock |
| 1280 | ** ever obtained is an exclusive lock, and it is obtained exactly once |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1281 | ** the first time any lock is attempted. All subsequent system locking |
| 1282 | ** operations become no-ops. Locking operations still happen internally, |
| 1283 | ** in order to coordinate access between separate database connections |
| 1284 | ** within this process, but all of that is handled in memory and the |
| 1285 | ** operating system does not participate. |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1286 | ** |
| 1287 | ** This function is a pass-through to fcntl(F_SETLK) if pFile is using |
| 1288 | ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl" |
| 1289 | ** and is read-only. |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1290 | ** |
| 1291 | ** Zero is returned if the call completes successfully, or -1 if a call |
| 1292 | ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()). |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1293 | */ |
| 1294 | static int unixFileLock(unixFile *pFile, struct flock *pLock){ |
| 1295 | int rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1296 | unixInodeInfo *pInode = pFile->pInode; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1297 | assert( unixMutexHeld() ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1298 | assert( pInode!=0 ); |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1299 | if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock) |
| 1300 | && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0) |
| 1301 | ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1302 | if( pInode->bProcessLock==0 ){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1303 | struct flock lock; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1304 | assert( pInode->nLock==0 ); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1305 | lock.l_whence = SEEK_SET; |
| 1306 | lock.l_start = SHARED_FIRST; |
| 1307 | lock.l_len = SHARED_SIZE; |
| 1308 | lock.l_type = F_WRLCK; |
| 1309 | rc = osFcntl(pFile->h, F_SETLK, &lock); |
| 1310 | if( rc<0 ) return rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1311 | pInode->bProcessLock = 1; |
| 1312 | pInode->nLock++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1313 | }else{ |
| 1314 | rc = 0; |
| 1315 | } |
| 1316 | }else{ |
| 1317 | rc = osFcntl(pFile->h, F_SETLK, pLock); |
| 1318 | } |
| 1319 | return rc; |
| 1320 | } |
| 1321 | |
| 1322 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1323 | ** Lock the file with the lock specified by parameter eFileLock - one |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1324 | ** of the following: |
| 1325 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1326 | ** (1) SHARED_LOCK |
| 1327 | ** (2) RESERVED_LOCK |
| 1328 | ** (3) PENDING_LOCK |
| 1329 | ** (4) EXCLUSIVE_LOCK |
| 1330 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1331 | ** Sometimes when requesting one lock state, additional lock states |
| 1332 | ** are inserted in between. The locking might fail on one of the later |
| 1333 | ** transitions leaving the lock state different from what it started but |
| 1334 | ** still short of its goal. The following chart shows the allowed |
| 1335 | ** transitions and the inserted intermediate states: |
| 1336 | ** |
| 1337 | ** UNLOCKED -> SHARED |
| 1338 | ** SHARED -> RESERVED |
| 1339 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1340 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1341 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1342 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1343 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1344 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1345 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1346 | static int unixLock(sqlite3_file *id, int eFileLock){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1347 | /* The following describes the implementation of the various locks and |
| 1348 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1349 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1350 | ** confusion with SQLite lock names). The algorithms are complicated |
| 1351 | ** slightly in order to be compatible with windows systems simultaneously |
| 1352 | ** accessing the same database file, in case that is ever required. |
| 1353 | ** |
| 1354 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1355 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1356 | ** range', a range of 510 bytes at a well known offset. |
| 1357 | ** |
| 1358 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 1359 | ** byte'. If this is successful, a random byte from the 'shared byte |
| 1360 | ** range' is read-locked and the lock on the 'pending byte' released. |
| 1361 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1362 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1363 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1364 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1365 | ** |
| 1366 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1367 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1368 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1369 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1370 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1371 | ** This property is used by the algorithm for rolling back a journal file |
| 1372 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1373 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1374 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1375 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1376 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1377 | ** within this range, this ensures that no other locks are held on the |
| 1378 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1379 | ** |
| 1380 | ** The reason a single byte cannot be used instead of the 'shared byte |
| 1381 | ** range' is that some versions of windows do not support read-locks. By |
| 1382 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 1383 | ** even if the locking primitive used is always a write-lock. |
| 1384 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1385 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1386 | unixFile *pFile = (unixFile*)id; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 1387 | unixInodeInfo *pInode; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1388 | struct flock lock; |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 1389 | int tErrno = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1390 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1391 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1392 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 1393 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 1394 | azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid())); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1395 | |
| 1396 | /* 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] | 1397 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1398 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1399 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1400 | if( pFile->eFileLock>=eFileLock ){ |
| 1401 | OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
| 1402 | azFileLock(eFileLock))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1403 | return SQLITE_OK; |
| 1404 | } |
| 1405 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1406 | /* Make sure the locking sequence is correct. |
| 1407 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 1408 | ** (2) SQLite never explicitly requests a pendig lock. |
| 1409 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1410 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1411 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 1412 | assert( eFileLock!=PENDING_LOCK ); |
| 1413 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1414 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1415 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1416 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1417 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1418 | pInode = pFile->pInode; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1419 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1420 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1421 | ** handle that precludes the requested lock, return BUSY. |
| 1422 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1423 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 1424 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1425 | ){ |
| 1426 | rc = SQLITE_BUSY; |
| 1427 | goto end_lock; |
| 1428 | } |
| 1429 | |
| 1430 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1431 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1432 | ** return SQLITE_OK. |
| 1433 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1434 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1435 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1436 | assert( eFileLock==SHARED_LOCK ); |
| 1437 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1438 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1439 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1440 | pInode->nShared++; |
| 1441 | pInode->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1442 | goto end_lock; |
| 1443 | } |
| 1444 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1445 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1446 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1447 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1448 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1449 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1450 | lock.l_len = 1L; |
| 1451 | lock.l_whence = SEEK_SET; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1452 | if( eFileLock==SHARED_LOCK |
| 1453 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1454 | ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1455 | lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1456 | lock.l_start = PENDING_BYTE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1457 | if( unixFileLock(pFile, &lock) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1458 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1459 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1460 | if( rc!=SQLITE_BUSY ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1461 | pFile->lastErrno = tErrno; |
| 1462 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1463 | goto end_lock; |
| 1464 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | |
| 1468 | /* If control gets to this point, then actually go ahead and make |
| 1469 | ** operating system calls for the specified lock. |
| 1470 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1471 | if( eFileLock==SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1472 | assert( pInode->nShared==0 ); |
| 1473 | assert( pInode->eFileLock==0 ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1474 | assert( rc==SQLITE_OK ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1475 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1476 | /* Now get the read-lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1477 | lock.l_start = SHARED_FIRST; |
| 1478 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1479 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1480 | tErrno = errno; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1481 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1482 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1483 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1484 | /* Drop the temporary PENDING lock */ |
| 1485 | lock.l_start = PENDING_BYTE; |
| 1486 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1487 | lock.l_type = F_UNLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1488 | if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){ |
| 1489 | /* This could happen with a network mount */ |
| 1490 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1491 | rc = SQLITE_IOERR_UNLOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1492 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1493 | |
| 1494 | if( rc ){ |
| 1495 | if( rc!=SQLITE_BUSY ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1496 | pFile->lastErrno = tErrno; |
| 1497 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1498 | goto end_lock; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1499 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1500 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1501 | pInode->nLock++; |
| 1502 | pInode->nShared = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1503 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1504 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1505 | /* We are trying for an exclusive lock but another thread in this |
| 1506 | ** same process is still holding a shared lock. */ |
| 1507 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1508 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1509 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1510 | ** assumed that there is a SHARED or greater lock on the file |
| 1511 | ** already. |
| 1512 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1513 | assert( 0!=pFile->eFileLock ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1514 | lock.l_type = F_WRLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1515 | |
| 1516 | assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK ); |
| 1517 | if( eFileLock==RESERVED_LOCK ){ |
| 1518 | lock.l_start = RESERVED_BYTE; |
| 1519 | lock.l_len = 1L; |
| 1520 | }else{ |
| 1521 | lock.l_start = SHARED_FIRST; |
| 1522 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1523 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1524 | |
| 1525 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1526 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1527 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1528 | if( rc!=SQLITE_BUSY ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1529 | pFile->lastErrno = tErrno; |
| 1530 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1531 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1532 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1533 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1534 | |
| 1535 | #ifndef NDEBUG |
| 1536 | /* Set up the transaction-counter change checking flags when |
| 1537 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1538 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1539 | ** write operation (not a hot journal rollback). |
| 1540 | */ |
| 1541 | if( rc==SQLITE_OK |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1542 | && pFile->eFileLock<=SHARED_LOCK |
| 1543 | && eFileLock==RESERVED_LOCK |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1544 | ){ |
| 1545 | pFile->transCntrChng = 0; |
| 1546 | pFile->dbUpdate = 0; |
| 1547 | pFile->inNormalWrite = 1; |
| 1548 | } |
| 1549 | #endif |
| 1550 | |
| 1551 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1552 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1553 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1554 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1555 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 1556 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1557 | pInode->eFileLock = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1558 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1559 | |
| 1560 | end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1561 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1562 | OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), |
| 1563 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1564 | return rc; |
| 1565 | } |
| 1566 | |
| 1567 | /* |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1568 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1569 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1570 | */ |
| 1571 | static void setPendingFd(unixFile *pFile){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1572 | unixInodeInfo *pInode = pFile->pInode; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1573 | UnixUnusedFd *p = pFile->pUnused; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1574 | p->pNext = pInode->pUnused; |
| 1575 | pInode->pUnused = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1576 | pFile->h = -1; |
| 1577 | pFile->pUnused = 0; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1581 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1582 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1583 | ** |
| 1584 | ** If the locking level of the file descriptor is already at or below |
| 1585 | ** the requested locking level, this routine is a no-op. |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1586 | ** |
| 1587 | ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 1588 | ** the byte range is divided into 2 parts and the first part is unlocked then |
| 1589 | ** set to a read lock, then the other part is simply unlocked. This works |
| 1590 | ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 1591 | ** remove the write lock on a region when a read lock is set. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1592 | */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1593 | static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1594 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1595 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1596 | struct flock lock; |
| 1597 | int rc = SQLITE_OK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1598 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1599 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1600 | 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] | 1601 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1602 | getpid())); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1603 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1604 | assert( eFileLock<=SHARED_LOCK ); |
| 1605 | if( pFile->eFileLock<=eFileLock ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1606 | return SQLITE_OK; |
| 1607 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1608 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1609 | pInode = pFile->pInode; |
| 1610 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1611 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1612 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1613 | |
| 1614 | #ifndef NDEBUG |
| 1615 | /* When reducing a lock such that other processes can start |
| 1616 | ** reading the database file again, make sure that the |
| 1617 | ** transaction counter was updated if any part of the database |
| 1618 | ** file changed. If the transaction counter is not updated, |
| 1619 | ** other connections to the same file might not realize that |
| 1620 | ** the file has changed and hence might not know to flush their |
| 1621 | ** cache. The use of a stale cache can lead to database corruption. |
| 1622 | */ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1623 | pFile->inNormalWrite = 0; |
| 1624 | #endif |
| 1625 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1626 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 1627 | ** before establishing the readlock - to avoid a race condition we downgrade |
| 1628 | ** the lock in 2 blocks, so that part of the range will be covered by a |
| 1629 | ** write lock until the rest is covered by a read lock: |
| 1630 | ** 1: [WWWWW] |
| 1631 | ** 2: [....W] |
| 1632 | ** 3: [RRRRW] |
| 1633 | ** 4: [RRRR.] |
| 1634 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1635 | if( eFileLock==SHARED_LOCK ){ |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1636 | |
| 1637 | #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE |
drh | 87e79ae | 2011-03-08 13:06:41 +0000 | [diff] [blame] | 1638 | (void)handleNFSUnlock; |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1639 | assert( handleNFSUnlock==0 ); |
| 1640 | #endif |
| 1641 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1642 | if( handleNFSUnlock ){ |
drh | 026663d | 2011-04-01 13:29:29 +0000 | [diff] [blame] | 1643 | int tErrno; /* Error code from system call errors */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1644 | off_t divSize = SHARED_SIZE - 1; |
| 1645 | |
| 1646 | lock.l_type = F_UNLCK; |
| 1647 | lock.l_whence = SEEK_SET; |
| 1648 | lock.l_start = SHARED_FIRST; |
| 1649 | lock.l_len = divSize; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 1650 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1651 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1652 | rc = SQLITE_IOERR_UNLOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1653 | if( IS_LOCK_ERROR(rc) ){ |
| 1654 | pFile->lastErrno = tErrno; |
| 1655 | } |
| 1656 | goto end_unlock; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1657 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1658 | lock.l_type = F_RDLCK; |
| 1659 | lock.l_whence = SEEK_SET; |
| 1660 | lock.l_start = SHARED_FIRST; |
| 1661 | lock.l_len = divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1662 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1663 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1664 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1665 | if( IS_LOCK_ERROR(rc) ){ |
| 1666 | pFile->lastErrno = tErrno; |
| 1667 | } |
| 1668 | goto end_unlock; |
| 1669 | } |
| 1670 | lock.l_type = F_UNLCK; |
| 1671 | lock.l_whence = SEEK_SET; |
| 1672 | lock.l_start = SHARED_FIRST+divSize; |
| 1673 | lock.l_len = SHARED_SIZE-divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1674 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1675 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1676 | rc = SQLITE_IOERR_UNLOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1677 | if( IS_LOCK_ERROR(rc) ){ |
| 1678 | pFile->lastErrno = tErrno; |
| 1679 | } |
| 1680 | goto end_unlock; |
| 1681 | } |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1682 | }else |
| 1683 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 1684 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1685 | lock.l_type = F_RDLCK; |
| 1686 | lock.l_whence = SEEK_SET; |
| 1687 | lock.l_start = SHARED_FIRST; |
| 1688 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1689 | if( unixFileLock(pFile, &lock) ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1690 | /* In theory, the call to unixFileLock() cannot fail because another |
| 1691 | ** process is holding an incompatible lock. If it does, this |
| 1692 | ** indicates that the other process is not following the locking |
| 1693 | ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning |
| 1694 | ** SQLITE_BUSY would confuse the upper layer (in practice it causes |
| 1695 | ** an assert to fail). */ |
| 1696 | rc = SQLITE_IOERR_RDLOCK; |
| 1697 | pFile->lastErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1698 | goto end_unlock; |
| 1699 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1700 | } |
| 1701 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1702 | lock.l_type = F_UNLCK; |
| 1703 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1704 | lock.l_start = PENDING_BYTE; |
| 1705 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1706 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1707 | pInode->eFileLock = SHARED_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1708 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1709 | rc = SQLITE_IOERR_UNLOCK; |
| 1710 | pFile->lastErrno = errno; |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 1711 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1712 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1713 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1714 | if( eFileLock==NO_LOCK ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1715 | /* Decrement the shared lock counter. Release the lock using an |
| 1716 | ** OS call only when all threads in this same process have released |
| 1717 | ** the lock. |
| 1718 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1719 | pInode->nShared--; |
| 1720 | if( pInode->nShared==0 ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1721 | lock.l_type = F_UNLCK; |
| 1722 | lock.l_whence = SEEK_SET; |
| 1723 | lock.l_start = lock.l_len = 0L; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1724 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1725 | pInode->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1726 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1727 | rc = SQLITE_IOERR_UNLOCK; |
| 1728 | pFile->lastErrno = errno; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1729 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1730 | pFile->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1731 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1734 | /* Decrement the count of locks against this same file. When the |
| 1735 | ** count reaches zero, close any other file descriptors whose close |
| 1736 | ** was deferred because of outstanding locks. |
| 1737 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1738 | pInode->nLock--; |
| 1739 | assert( pInode->nLock>=0 ); |
| 1740 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1741 | closePendingFds(pFile); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1742 | } |
| 1743 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1744 | |
| 1745 | end_unlock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1746 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1747 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1748 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1752 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1753 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1754 | ** |
| 1755 | ** If the locking level of the file descriptor is already at or below |
| 1756 | ** the requested locking level, this routine is a no-op. |
| 1757 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1758 | static int unixUnlock(sqlite3_file *id, int eFileLock){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1759 | return posixUnlock(id, eFileLock, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1763 | ** This function performs the parts of the "close file" operation |
| 1764 | ** common to all locking schemes. It closes the directory and file |
| 1765 | ** handles, if they are valid, and sets all fields of the unixFile |
| 1766 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1767 | ** |
| 1768 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 1769 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 1770 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1771 | */ |
| 1772 | static int closeUnixFile(sqlite3_file *id){ |
| 1773 | unixFile *pFile = (unixFile*)id; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1774 | if( pFile->h>=0 ){ |
| 1775 | robust_close(pFile, pFile->h, __LINE__); |
| 1776 | pFile->h = -1; |
| 1777 | } |
| 1778 | #if OS_VXWORKS |
| 1779 | if( pFile->pId ){ |
| 1780 | if( pFile->isDelete ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 1781 | osUnlink(pFile->pId->zCanonicalName); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1782 | } |
| 1783 | vxworksReleaseFileId(pFile->pId); |
| 1784 | pFile->pId = 0; |
| 1785 | } |
| 1786 | #endif |
| 1787 | OSTRACE(("CLOSE %-3d\n", pFile->h)); |
| 1788 | OpenCounter(-1); |
| 1789 | sqlite3_free(pFile->pUnused); |
| 1790 | memset(pFile, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1791 | return SQLITE_OK; |
| 1792 | } |
| 1793 | |
| 1794 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1795 | ** Close a file. |
| 1796 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1797 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1798 | int rc = SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1799 | unixFile *pFile = (unixFile *)id; |
| 1800 | unixUnlock(id, NO_LOCK); |
| 1801 | unixEnterMutex(); |
| 1802 | |
| 1803 | /* unixFile.pInode is always valid here. Otherwise, a different close |
| 1804 | ** routine (e.g. nolockClose()) would be called instead. |
| 1805 | */ |
| 1806 | assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); |
| 1807 | if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){ |
| 1808 | /* If there are outstanding locks, do not actually close the file just |
| 1809 | ** yet because that would clear those locks. Instead, add the file |
| 1810 | ** descriptor to pInode->pUnused list. It will be automatically closed |
| 1811 | ** when the last lock is cleared. |
| 1812 | */ |
| 1813 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1814 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1815 | releaseInodeInfo(pFile); |
| 1816 | rc = closeUnixFile(id); |
| 1817 | unixLeaveMutex(); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1818 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1821 | /************** End of the posix advisory lock implementation ***************** |
| 1822 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1823 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1824 | /****************************************************************************** |
| 1825 | ****************************** No-op Locking ********************************** |
| 1826 | ** |
| 1827 | ** Of the various locking implementations available, this is by far the |
| 1828 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 1829 | ** file for reading or writing. |
| 1830 | ** |
| 1831 | ** This locking mode is appropriate for use on read-only databases |
| 1832 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 1833 | ** also be used if the application employs some external mechanism to |
| 1834 | ** prevent simultaneous access of the same database by two or more |
| 1835 | ** database connections. But there is a serious risk of database |
| 1836 | ** corruption if this locking mode is used in situations where multiple |
| 1837 | ** database connections are accessing the same database file at the same |
| 1838 | ** time and one or more of those connections are writing. |
| 1839 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1840 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1841 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 1842 | UNUSED_PARAMETER(NotUsed); |
| 1843 | *pResOut = 0; |
| 1844 | return SQLITE_OK; |
| 1845 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1846 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1847 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1848 | return SQLITE_OK; |
| 1849 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1850 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1851 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1852 | return SQLITE_OK; |
| 1853 | } |
| 1854 | |
| 1855 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1856 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1857 | */ |
| 1858 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1859 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | /******************* End of the no-op lock implementation ********************* |
| 1863 | ******************************************************************************/ |
| 1864 | |
| 1865 | /****************************************************************************** |
| 1866 | ************************* Begin dot-file Locking ****************************** |
| 1867 | ** |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1868 | ** The dotfile locking implementation uses the existance of separate lock |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 1869 | ** files (really a directory) to control access to the database. This works |
| 1870 | ** on just about every filesystem imaginable. But there are serious downsides: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1871 | ** |
| 1872 | ** (1) There is zero concurrency. A single reader blocks all other |
| 1873 | ** connections from reading or writing the database. |
| 1874 | ** |
| 1875 | ** (2) An application crash or power loss can leave stale lock files |
| 1876 | ** sitting around that need to be cleared manually. |
| 1877 | ** |
| 1878 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 1879 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1880 | ** |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 1881 | ** Dotfile locking works by creating a subdirectory in the same directory as |
| 1882 | ** the database and with the same name but with a ".lock" extension added. |
| 1883 | ** The existance of a lock directory implies an EXCLUSIVE lock. All other |
| 1884 | ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1885 | */ |
| 1886 | |
| 1887 | /* |
| 1888 | ** The file suffix added to the data base filename in order to create the |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 1889 | ** lock directory. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1890 | */ |
| 1891 | #define DOTLOCK_SUFFIX ".lock" |
| 1892 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1893 | /* |
| 1894 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1895 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1896 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1897 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1898 | ** |
| 1899 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 1900 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 1901 | ** is held on the file and false if the file is unlocked. |
| 1902 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1903 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 1904 | int rc = SQLITE_OK; |
| 1905 | int reserved = 0; |
| 1906 | unixFile *pFile = (unixFile*)id; |
| 1907 | |
| 1908 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1909 | |
| 1910 | assert( pFile ); |
| 1911 | |
| 1912 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1913 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1914 | /* Either this connection or some other connection in the same process |
| 1915 | ** holds a lock on the file. No need to check further. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1916 | reserved = 1; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1917 | }else{ |
| 1918 | /* The lock is held if and only if the lockfile exists */ |
| 1919 | const char *zLockFile = (const char*)pFile->lockingContext; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1920 | reserved = osAccess(zLockFile, 0)==0; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1921 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1922 | OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1923 | *pResOut = reserved; |
| 1924 | return rc; |
| 1925 | } |
| 1926 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1927 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1928 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1929 | ** of the following: |
| 1930 | ** |
| 1931 | ** (1) SHARED_LOCK |
| 1932 | ** (2) RESERVED_LOCK |
| 1933 | ** (3) PENDING_LOCK |
| 1934 | ** (4) EXCLUSIVE_LOCK |
| 1935 | ** |
| 1936 | ** Sometimes when requesting one lock state, additional lock states |
| 1937 | ** are inserted in between. The locking might fail on one of the later |
| 1938 | ** transitions leaving the lock state different from what it started but |
| 1939 | ** still short of its goal. The following chart shows the allowed |
| 1940 | ** transitions and the inserted intermediate states: |
| 1941 | ** |
| 1942 | ** UNLOCKED -> SHARED |
| 1943 | ** SHARED -> RESERVED |
| 1944 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1945 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1946 | ** PENDING -> EXCLUSIVE |
| 1947 | ** |
| 1948 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1949 | ** routine to lower a locking level. |
| 1950 | ** |
| 1951 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 1952 | ** But we track the other locking levels internally. |
| 1953 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1954 | static int dotlockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1955 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1956 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1957 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1958 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1959 | |
| 1960 | /* If we have any lock, then the lock file already exists. All we have |
| 1961 | ** to do is adjust our internal record of the lock level. |
| 1962 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1963 | if( pFile->eFileLock > NO_LOCK ){ |
| 1964 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1965 | /* Always update the timestamp on the old file */ |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 1966 | #ifdef HAVE_UTIME |
| 1967 | utime(zLockFile, NULL); |
| 1968 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1969 | utimes(zLockFile, NULL); |
| 1970 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1971 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | /* grab an exclusive lock */ |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 1975 | rc = osMkdir(zLockFile, 0777); |
| 1976 | if( rc<0 ){ |
| 1977 | /* failed to open/create the lock directory */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1978 | int tErrno = errno; |
| 1979 | if( EEXIST == tErrno ){ |
| 1980 | rc = SQLITE_BUSY; |
| 1981 | } else { |
| 1982 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1983 | if( IS_LOCK_ERROR(rc) ){ |
| 1984 | pFile->lastErrno = tErrno; |
| 1985 | } |
| 1986 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1987 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1988 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1989 | |
| 1990 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1991 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1992 | return rc; |
| 1993 | } |
| 1994 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1995 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1996 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1997 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1998 | ** |
| 1999 | ** If the locking level of the file descriptor is already at or below |
| 2000 | ** the requested locking level, this routine is a no-op. |
| 2001 | ** |
| 2002 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 2003 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2004 | static int dotlockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2005 | unixFile *pFile = (unixFile*)id; |
| 2006 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2007 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2008 | |
| 2009 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2010 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
| 2011 | pFile->eFileLock, getpid())); |
| 2012 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2013 | |
| 2014 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2015 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2016 | return SQLITE_OK; |
| 2017 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2018 | |
| 2019 | /* To downgrade to shared, simply update our internal notion of the |
| 2020 | ** lock state. No need to mess with the file on disk. |
| 2021 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2022 | if( eFileLock==SHARED_LOCK ){ |
| 2023 | pFile->eFileLock = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2024 | return SQLITE_OK; |
| 2025 | } |
| 2026 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2027 | /* To fully unlock the database, delete the lock file */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2028 | assert( eFileLock==NO_LOCK ); |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2029 | rc = osRmdir(zLockFile); |
| 2030 | if( rc<0 && errno==ENOTDIR ) rc = osUnlink(zLockFile); |
| 2031 | if( rc<0 ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 2032 | int tErrno = errno; |
drh | 13e0ea9 | 2011-12-11 02:29:25 +0000 | [diff] [blame] | 2033 | rc = 0; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2034 | if( ENOENT != tErrno ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2035 | rc = SQLITE_IOERR_UNLOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2036 | } |
| 2037 | if( IS_LOCK_ERROR(rc) ){ |
| 2038 | pFile->lastErrno = tErrno; |
| 2039 | } |
| 2040 | return rc; |
| 2041 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2042 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2043 | return SQLITE_OK; |
| 2044 | } |
| 2045 | |
| 2046 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2047 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2048 | */ |
| 2049 | static int dotlockClose(sqlite3_file *id) { |
| 2050 | int rc; |
| 2051 | if( id ){ |
| 2052 | unixFile *pFile = (unixFile*)id; |
| 2053 | dotlockUnlock(id, NO_LOCK); |
| 2054 | sqlite3_free(pFile->lockingContext); |
| 2055 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2056 | rc = closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2057 | return rc; |
| 2058 | } |
| 2059 | /****************** End of the dot-file lock implementation ******************* |
| 2060 | ******************************************************************************/ |
| 2061 | |
| 2062 | /****************************************************************************** |
| 2063 | ************************** Begin flock Locking ******************************** |
| 2064 | ** |
| 2065 | ** Use the flock() system call to do file locking. |
| 2066 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2067 | ** flock() locking is like dot-file locking in that the various |
| 2068 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 2069 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 2070 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 2071 | ** still works when you do this, but concurrency is reduced since |
| 2072 | ** only a single process can be reading the database at a time. |
| 2073 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2074 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if |
| 2075 | ** compiling for VXWORKS. |
| 2076 | */ |
| 2077 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2078 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2079 | /* |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2080 | ** Retry flock() calls that fail with EINTR |
| 2081 | */ |
| 2082 | #ifdef EINTR |
| 2083 | static int robust_flock(int fd, int op){ |
| 2084 | int rc; |
| 2085 | do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR ); |
| 2086 | return rc; |
| 2087 | } |
| 2088 | #else |
drh | 5c81927 | 2011-02-23 14:00:12 +0000 | [diff] [blame] | 2089 | # define robust_flock(a,b) flock(a,b) |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2090 | #endif |
| 2091 | |
| 2092 | |
| 2093 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2094 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2095 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2096 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2097 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2098 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2099 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 2100 | int rc = SQLITE_OK; |
| 2101 | int reserved = 0; |
| 2102 | unixFile *pFile = (unixFile*)id; |
| 2103 | |
| 2104 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2105 | |
| 2106 | assert( pFile ); |
| 2107 | |
| 2108 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2109 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2110 | reserved = 1; |
| 2111 | } |
| 2112 | |
| 2113 | /* Otherwise see if some other process holds it. */ |
| 2114 | if( !reserved ){ |
| 2115 | /* attempt to get the lock */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2116 | int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2117 | if( !lrc ){ |
| 2118 | /* got the lock, unlock it */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2119 | lrc = robust_flock(pFile->h, LOCK_UN); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2120 | if ( lrc ) { |
| 2121 | int tErrno = errno; |
| 2122 | /* unlock failed with an error */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2123 | lrc = SQLITE_IOERR_UNLOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2124 | if( IS_LOCK_ERROR(lrc) ){ |
| 2125 | pFile->lastErrno = tErrno; |
| 2126 | rc = lrc; |
| 2127 | } |
| 2128 | } |
| 2129 | } else { |
| 2130 | int tErrno = errno; |
| 2131 | reserved = 1; |
| 2132 | /* someone else might have it reserved */ |
| 2133 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2134 | if( IS_LOCK_ERROR(lrc) ){ |
| 2135 | pFile->lastErrno = tErrno; |
| 2136 | rc = lrc; |
| 2137 | } |
| 2138 | } |
| 2139 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2140 | OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2141 | |
| 2142 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2143 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2144 | rc = SQLITE_OK; |
| 2145 | reserved=1; |
| 2146 | } |
| 2147 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2148 | *pResOut = reserved; |
| 2149 | return rc; |
| 2150 | } |
| 2151 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2152 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2153 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2154 | ** of the following: |
| 2155 | ** |
| 2156 | ** (1) SHARED_LOCK |
| 2157 | ** (2) RESERVED_LOCK |
| 2158 | ** (3) PENDING_LOCK |
| 2159 | ** (4) EXCLUSIVE_LOCK |
| 2160 | ** |
| 2161 | ** Sometimes when requesting one lock state, additional lock states |
| 2162 | ** are inserted in between. The locking might fail on one of the later |
| 2163 | ** transitions leaving the lock state different from what it started but |
| 2164 | ** still short of its goal. The following chart shows the allowed |
| 2165 | ** transitions and the inserted intermediate states: |
| 2166 | ** |
| 2167 | ** UNLOCKED -> SHARED |
| 2168 | ** SHARED -> RESERVED |
| 2169 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2170 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2171 | ** PENDING -> EXCLUSIVE |
| 2172 | ** |
| 2173 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 2174 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2175 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2176 | ** access the file. |
| 2177 | ** |
| 2178 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2179 | ** routine to lower a locking level. |
| 2180 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2181 | static int flockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2182 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2183 | unixFile *pFile = (unixFile*)id; |
| 2184 | |
| 2185 | assert( pFile ); |
| 2186 | |
| 2187 | /* if we already have a lock, it is exclusive. |
| 2188 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2189 | if (pFile->eFileLock > NO_LOCK) { |
| 2190 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2191 | return SQLITE_OK; |
| 2192 | } |
| 2193 | |
| 2194 | /* grab an exclusive lock */ |
| 2195 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2196 | if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2197 | int tErrno = errno; |
| 2198 | /* didn't get, must be busy */ |
| 2199 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2200 | if( IS_LOCK_ERROR(rc) ){ |
| 2201 | pFile->lastErrno = tErrno; |
| 2202 | } |
| 2203 | } else { |
| 2204 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2205 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2206 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2207 | OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), |
| 2208 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2209 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2210 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2211 | rc = SQLITE_BUSY; |
| 2212 | } |
| 2213 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2214 | return rc; |
| 2215 | } |
| 2216 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2217 | |
| 2218 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2219 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2220 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2221 | ** |
| 2222 | ** If the locking level of the file descriptor is already at or below |
| 2223 | ** the requested locking level, this routine is a no-op. |
| 2224 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2225 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2226 | unixFile *pFile = (unixFile*)id; |
| 2227 | |
| 2228 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2229 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
| 2230 | pFile->eFileLock, getpid())); |
| 2231 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2232 | |
| 2233 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2234 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2235 | return SQLITE_OK; |
| 2236 | } |
| 2237 | |
| 2238 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2239 | if (eFileLock==SHARED_LOCK) { |
| 2240 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2241 | return SQLITE_OK; |
| 2242 | } |
| 2243 | |
| 2244 | /* no, really, unlock. */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2245 | if( robust_flock(pFile->h, LOCK_UN) ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2246 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2247 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2248 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2249 | return SQLITE_IOERR_UNLOCK; |
| 2250 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2251 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2252 | return SQLITE_OK; |
| 2253 | } |
| 2254 | } |
| 2255 | |
| 2256 | /* |
| 2257 | ** Close a file. |
| 2258 | */ |
| 2259 | static int flockClose(sqlite3_file *id) { |
| 2260 | if( id ){ |
| 2261 | flockUnlock(id, NO_LOCK); |
| 2262 | } |
| 2263 | return closeUnixFile(id); |
| 2264 | } |
| 2265 | |
| 2266 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 2267 | |
| 2268 | /******************* End of the flock lock implementation ********************* |
| 2269 | ******************************************************************************/ |
| 2270 | |
| 2271 | /****************************************************************************** |
| 2272 | ************************ Begin Named Semaphore Locking ************************ |
| 2273 | ** |
| 2274 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2275 | ** |
| 2276 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 2277 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 2278 | ** the database file at a time. This reduces potential concurrency, but |
| 2279 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2280 | */ |
| 2281 | #if OS_VXWORKS |
| 2282 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2283 | /* |
| 2284 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2285 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2286 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2287 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2288 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2289 | static int semCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 2290 | int rc = SQLITE_OK; |
| 2291 | int reserved = 0; |
| 2292 | unixFile *pFile = (unixFile*)id; |
| 2293 | |
| 2294 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2295 | |
| 2296 | assert( pFile ); |
| 2297 | |
| 2298 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2299 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2300 | reserved = 1; |
| 2301 | } |
| 2302 | |
| 2303 | /* Otherwise see if some other process holds it. */ |
| 2304 | if( !reserved ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2305 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2306 | struct stat statBuf; |
| 2307 | |
| 2308 | if( sem_trywait(pSem)==-1 ){ |
| 2309 | int tErrno = errno; |
| 2310 | if( EAGAIN != tErrno ){ |
| 2311 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 2312 | pFile->lastErrno = tErrno; |
| 2313 | } else { |
| 2314 | /* someone else has the lock when we are in NO_LOCK */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2315 | reserved = (pFile->eFileLock < SHARED_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2316 | } |
| 2317 | }else{ |
| 2318 | /* we could have it if we want it */ |
| 2319 | sem_post(pSem); |
| 2320 | } |
| 2321 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2322 | OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2323 | |
| 2324 | *pResOut = reserved; |
| 2325 | return rc; |
| 2326 | } |
| 2327 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2328 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2329 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2330 | ** of the following: |
| 2331 | ** |
| 2332 | ** (1) SHARED_LOCK |
| 2333 | ** (2) RESERVED_LOCK |
| 2334 | ** (3) PENDING_LOCK |
| 2335 | ** (4) EXCLUSIVE_LOCK |
| 2336 | ** |
| 2337 | ** Sometimes when requesting one lock state, additional lock states |
| 2338 | ** are inserted in between. The locking might fail on one of the later |
| 2339 | ** transitions leaving the lock state different from what it started but |
| 2340 | ** still short of its goal. The following chart shows the allowed |
| 2341 | ** transitions and the inserted intermediate states: |
| 2342 | ** |
| 2343 | ** UNLOCKED -> SHARED |
| 2344 | ** SHARED -> RESERVED |
| 2345 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2346 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2347 | ** PENDING -> EXCLUSIVE |
| 2348 | ** |
| 2349 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 2350 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2351 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2352 | ** access the file. |
| 2353 | ** |
| 2354 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2355 | ** routine to lower a locking level. |
| 2356 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2357 | static int semLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2358 | unixFile *pFile = (unixFile*)id; |
| 2359 | int fd; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2360 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2361 | int rc = SQLITE_OK; |
| 2362 | |
| 2363 | /* if we already have a lock, it is exclusive. |
| 2364 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2365 | if (pFile->eFileLock > NO_LOCK) { |
| 2366 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2367 | rc = SQLITE_OK; |
| 2368 | goto sem_end_lock; |
| 2369 | } |
| 2370 | |
| 2371 | /* lock semaphore now but bail out when already locked. */ |
| 2372 | if( sem_trywait(pSem)==-1 ){ |
| 2373 | rc = SQLITE_BUSY; |
| 2374 | goto sem_end_lock; |
| 2375 | } |
| 2376 | |
| 2377 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2378 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2379 | |
| 2380 | sem_end_lock: |
| 2381 | return rc; |
| 2382 | } |
| 2383 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2384 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2385 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2386 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2387 | ** |
| 2388 | ** If the locking level of the file descriptor is already at or below |
| 2389 | ** the requested locking level, this routine is a no-op. |
| 2390 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2391 | static int semUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2392 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2393 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2394 | |
| 2395 | assert( pFile ); |
| 2396 | assert( pSem ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2397 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
| 2398 | pFile->eFileLock, getpid())); |
| 2399 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2400 | |
| 2401 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2402 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2403 | return SQLITE_OK; |
| 2404 | } |
| 2405 | |
| 2406 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2407 | if (eFileLock==SHARED_LOCK) { |
| 2408 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2409 | return SQLITE_OK; |
| 2410 | } |
| 2411 | |
| 2412 | /* no, really unlock. */ |
| 2413 | if ( sem_post(pSem)==-1 ) { |
| 2414 | int rc, tErrno = errno; |
| 2415 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2416 | if( IS_LOCK_ERROR(rc) ){ |
| 2417 | pFile->lastErrno = tErrno; |
| 2418 | } |
| 2419 | return rc; |
| 2420 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2421 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2422 | return SQLITE_OK; |
| 2423 | } |
| 2424 | |
| 2425 | /* |
| 2426 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2427 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2428 | static int semClose(sqlite3_file *id) { |
| 2429 | if( id ){ |
| 2430 | unixFile *pFile = (unixFile*)id; |
| 2431 | semUnlock(id, NO_LOCK); |
| 2432 | assert( pFile ); |
| 2433 | unixEnterMutex(); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2434 | releaseInodeInfo(pFile); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2435 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2436 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2437 | } |
| 2438 | return SQLITE_OK; |
| 2439 | } |
| 2440 | |
| 2441 | #endif /* OS_VXWORKS */ |
| 2442 | /* |
| 2443 | ** Named semaphore locking is only available on VxWorks. |
| 2444 | ** |
| 2445 | *************** End of the named semaphore lock implementation **************** |
| 2446 | ******************************************************************************/ |
| 2447 | |
| 2448 | |
| 2449 | /****************************************************************************** |
| 2450 | *************************** Begin AFP Locking ********************************* |
| 2451 | ** |
| 2452 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2453 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2454 | ** |
| 2455 | ** Third-party implementations of AFP are available. But this code here |
| 2456 | ** only works on OSX. |
| 2457 | */ |
| 2458 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2459 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2460 | /* |
| 2461 | ** The afpLockingContext structure contains all afp lock specific state |
| 2462 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2463 | typedef struct afpLockingContext afpLockingContext; |
| 2464 | struct afpLockingContext { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2465 | int reserved; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2466 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2467 | }; |
| 2468 | |
| 2469 | struct ByteRangeLockPB2 |
| 2470 | { |
| 2471 | unsigned long long offset; /* offset to first byte to lock */ |
| 2472 | unsigned long long length; /* nbr of bytes to lock */ |
| 2473 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2474 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2475 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2476 | int fd; /* file desc to assoc this lock with */ |
| 2477 | }; |
| 2478 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2479 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2480 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2481 | /* |
| 2482 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2483 | ** AFP filesystem. |
| 2484 | ** |
| 2485 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2486 | */ |
| 2487 | static int afpSetLock( |
| 2488 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2489 | unixFile *pFile, /* Open file descriptor on path */ |
| 2490 | unsigned long long offset, /* First byte to be locked */ |
| 2491 | unsigned long long length, /* Number of bytes to lock */ |
| 2492 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2493 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2494 | struct ByteRangeLockPB2 pb; |
| 2495 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2496 | |
| 2497 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2498 | pb.startEndFlag = 0; |
| 2499 | pb.offset = offset; |
| 2500 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2501 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2502 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2503 | OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2504 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2505 | offset, length)); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2506 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2507 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2508 | int rc; |
| 2509 | int tErrno = errno; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2510 | OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2511 | path, tErrno, strerror(tErrno))); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2512 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2513 | rc = SQLITE_BUSY; |
| 2514 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2515 | rc = sqliteErrorFromPosixError(tErrno, |
| 2516 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2517 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2518 | if( IS_LOCK_ERROR(rc) ){ |
| 2519 | pFile->lastErrno = tErrno; |
| 2520 | } |
| 2521 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2522 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2523 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2524 | } |
| 2525 | } |
| 2526 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2527 | /* |
| 2528 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2529 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2530 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2531 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2532 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2533 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2534 | int rc = SQLITE_OK; |
| 2535 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2536 | unixFile *pFile = (unixFile*)id; |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2537 | afpLockingContext *context; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2538 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2539 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2540 | |
| 2541 | assert( pFile ); |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2542 | context = (afpLockingContext *) pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2543 | if( context->reserved ){ |
| 2544 | *pResOut = 1; |
| 2545 | return SQLITE_OK; |
| 2546 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2547 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2548 | |
| 2549 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2550 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2551 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2552 | } |
| 2553 | |
| 2554 | /* Otherwise see if some other process holds it. |
| 2555 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2556 | if( !reserved ){ |
| 2557 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2558 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2559 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2560 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2561 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2562 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2563 | } else { |
| 2564 | /* if we failed to get the lock then someone else must have it */ |
| 2565 | reserved = 1; |
| 2566 | } |
| 2567 | if( IS_LOCK_ERROR(lrc) ){ |
| 2568 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2569 | } |
| 2570 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2571 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2572 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2573 | OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2574 | |
| 2575 | *pResOut = reserved; |
| 2576 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2579 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2580 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2581 | ** of the following: |
| 2582 | ** |
| 2583 | ** (1) SHARED_LOCK |
| 2584 | ** (2) RESERVED_LOCK |
| 2585 | ** (3) PENDING_LOCK |
| 2586 | ** (4) EXCLUSIVE_LOCK |
| 2587 | ** |
| 2588 | ** Sometimes when requesting one lock state, additional lock states |
| 2589 | ** are inserted in between. The locking might fail on one of the later |
| 2590 | ** transitions leaving the lock state different from what it started but |
| 2591 | ** still short of its goal. The following chart shows the allowed |
| 2592 | ** transitions and the inserted intermediate states: |
| 2593 | ** |
| 2594 | ** UNLOCKED -> SHARED |
| 2595 | ** SHARED -> RESERVED |
| 2596 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2597 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2598 | ** PENDING -> EXCLUSIVE |
| 2599 | ** |
| 2600 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2601 | ** routine to lower a locking level. |
| 2602 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2603 | static int afpLock(sqlite3_file *id, int eFileLock){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2604 | int rc = SQLITE_OK; |
| 2605 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2606 | unixInodeInfo *pInode = pFile->pInode; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2607 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2608 | |
| 2609 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2610 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 2611 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2612 | azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2613 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2614 | /* 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] | 2615 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2616 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2617 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2618 | if( pFile->eFileLock>=eFileLock ){ |
| 2619 | OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
| 2620 | azFileLock(eFileLock))); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2621 | return SQLITE_OK; |
| 2622 | } |
| 2623 | |
| 2624 | /* Make sure the locking sequence is correct |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2625 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 2626 | ** (2) SQLite never explicitly requests a pendig lock. |
| 2627 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2628 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2629 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 2630 | assert( eFileLock!=PENDING_LOCK ); |
| 2631 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2632 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2633 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2634 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2635 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2636 | pInode = pFile->pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2637 | |
| 2638 | /* If some thread using this PID has a lock via a different unixFile* |
| 2639 | ** handle that precludes the requested lock, return BUSY. |
| 2640 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2641 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 2642 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2643 | ){ |
| 2644 | rc = SQLITE_BUSY; |
| 2645 | goto afp_end_lock; |
| 2646 | } |
| 2647 | |
| 2648 | /* If a SHARED lock is requested, and some thread using this PID already |
| 2649 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 2650 | ** return SQLITE_OK. |
| 2651 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2652 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2653 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2654 | assert( eFileLock==SHARED_LOCK ); |
| 2655 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2656 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2657 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2658 | pInode->nShared++; |
| 2659 | pInode->nLock++; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2660 | goto afp_end_lock; |
| 2661 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2662 | |
| 2663 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2664 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2665 | ** be released. |
| 2666 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2667 | if( eFileLock==SHARED_LOCK |
| 2668 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2669 | ){ |
| 2670 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2671 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2672 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2673 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2674 | goto afp_end_lock; |
| 2675 | } |
| 2676 | } |
| 2677 | |
| 2678 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2679 | ** operating system calls for the specified lock. |
| 2680 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2681 | if( eFileLock==SHARED_LOCK ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2682 | int lrc1, lrc2, lrc1Errno = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2683 | long lk, mask; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2684 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2685 | assert( pInode->nShared==0 ); |
| 2686 | assert( pInode->eFileLock==0 ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2687 | |
| 2688 | mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2689 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2690 | /* note that the quality of the randomness doesn't matter that much */ |
| 2691 | lk = random(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2692 | pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2693 | lrc1 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2694 | SHARED_FIRST+pInode->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2695 | if( IS_LOCK_ERROR(lrc1) ){ |
| 2696 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2697 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2698 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2699 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2700 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2701 | if( IS_LOCK_ERROR(lrc1) ) { |
| 2702 | pFile->lastErrno = lrc1Errno; |
| 2703 | rc = lrc1; |
| 2704 | goto afp_end_lock; |
| 2705 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 2706 | rc = lrc2; |
| 2707 | goto afp_end_lock; |
| 2708 | } else if( lrc1 != SQLITE_OK ) { |
| 2709 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2710 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2711 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2712 | pInode->nLock++; |
| 2713 | pInode->nShared = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2714 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2715 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2716 | /* We are trying for an exclusive lock but another thread in this |
| 2717 | ** same process is still holding a shared lock. */ |
| 2718 | rc = SQLITE_BUSY; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2719 | }else{ |
| 2720 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 2721 | ** assumed that there is a SHARED or greater lock on the file |
| 2722 | ** already. |
| 2723 | */ |
| 2724 | int failed = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2725 | assert( 0!=pFile->eFileLock ); |
| 2726 | if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2727 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2728 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2729 | if( !failed ){ |
| 2730 | context->reserved = 1; |
| 2731 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2732 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2733 | if (!failed && eFileLock == EXCLUSIVE_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2734 | /* Acquire an EXCLUSIVE lock */ |
| 2735 | |
| 2736 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2737 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2738 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2739 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2740 | pInode->sharedByte, 1, 0)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2741 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2742 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2743 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2744 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2745 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2746 | SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2747 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 2748 | ** a critical I/O error |
| 2749 | */ |
| 2750 | rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : |
| 2751 | SQLITE_IOERR_LOCK; |
| 2752 | goto afp_end_lock; |
| 2753 | } |
| 2754 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2755 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2756 | } |
| 2757 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2758 | if( failed ){ |
| 2759 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2760 | } |
| 2761 | } |
| 2762 | |
| 2763 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2764 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2765 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2766 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 2767 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2768 | pInode->eFileLock = PENDING_LOCK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
| 2771 | afp_end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2772 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2773 | OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), |
| 2774 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2775 | return rc; |
| 2776 | } |
| 2777 | |
| 2778 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2779 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2780 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2781 | ** |
| 2782 | ** If the locking level of the file descriptor is already at or below |
| 2783 | ** the requested locking level, this routine is a no-op. |
| 2784 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2785 | static int afpUnlock(sqlite3_file *id, int eFileLock) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2786 | int rc = SQLITE_OK; |
| 2787 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2788 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2789 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 2790 | int skipShared = 0; |
| 2791 | #ifdef SQLITE_TEST |
| 2792 | int h = pFile->h; |
| 2793 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2794 | |
| 2795 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2796 | 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] | 2797 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2798 | getpid())); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2799 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2800 | assert( eFileLock<=SHARED_LOCK ); |
| 2801 | if( pFile->eFileLock<=eFileLock ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2802 | return SQLITE_OK; |
| 2803 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2804 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2805 | pInode = pFile->pInode; |
| 2806 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2807 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2808 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2809 | SimulateIOErrorBenign(1); |
| 2810 | SimulateIOError( h=(-1) ) |
| 2811 | SimulateIOErrorBenign(0); |
| 2812 | |
| 2813 | #ifndef NDEBUG |
| 2814 | /* When reducing a lock such that other processes can start |
| 2815 | ** reading the database file again, make sure that the |
| 2816 | ** transaction counter was updated if any part of the database |
| 2817 | ** file changed. If the transaction counter is not updated, |
| 2818 | ** other connections to the same file might not realize that |
| 2819 | ** the file has changed and hence might not know to flush their |
| 2820 | ** cache. The use of a stale cache can lead to database corruption. |
| 2821 | */ |
| 2822 | assert( pFile->inNormalWrite==0 |
| 2823 | || pFile->dbUpdate==0 |
| 2824 | || pFile->transCntrChng==1 ); |
| 2825 | pFile->inNormalWrite = 0; |
| 2826 | #endif |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2827 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2828 | if( pFile->eFileLock==EXCLUSIVE_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2829 | rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2830 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2831 | /* only re-establish the shared lock if necessary */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2832 | int sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2833 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 2834 | } else { |
| 2835 | skipShared = 1; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2836 | } |
| 2837 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2838 | if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2839 | rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2840 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2841 | if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2842 | rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 2843 | if( !rc ){ |
| 2844 | context->reserved = 0; |
| 2845 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2846 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2847 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ |
| 2848 | pInode->eFileLock = SHARED_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2849 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2850 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2851 | if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2852 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2853 | /* Decrement the shared lock counter. Release the lock using an |
| 2854 | ** OS call only when all threads in this same process have released |
| 2855 | ** the lock. |
| 2856 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2857 | unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 2858 | pInode->nShared--; |
| 2859 | if( pInode->nShared==0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2860 | SimulateIOErrorBenign(1); |
| 2861 | SimulateIOError( h=(-1) ) |
| 2862 | SimulateIOErrorBenign(0); |
| 2863 | if( !skipShared ){ |
| 2864 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 2865 | } |
| 2866 | if( !rc ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2867 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2868 | pFile->eFileLock = NO_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2869 | } |
| 2870 | } |
| 2871 | if( rc==SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2872 | pInode->nLock--; |
| 2873 | assert( pInode->nLock>=0 ); |
| 2874 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 2875 | closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2876 | } |
| 2877 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2878 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2879 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2880 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2881 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2882 | return rc; |
| 2883 | } |
| 2884 | |
| 2885 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2886 | ** Close a file & cleanup AFP specific locking context |
| 2887 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2888 | static int afpClose(sqlite3_file *id) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2889 | int rc = SQLITE_OK; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2890 | if( id ){ |
| 2891 | unixFile *pFile = (unixFile*)id; |
| 2892 | afpUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2893 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2894 | if( pFile->pInode && pFile->pInode->nLock ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2895 | /* If there are outstanding locks, do not actually close the file just |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2896 | ** yet because that would clear those locks. Instead, add the file |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2897 | ** descriptor to pInode->aPending. It will be automatically closed when |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2898 | ** the last lock is cleared. |
| 2899 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2900 | setPendingFd(pFile); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2901 | } |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2902 | releaseInodeInfo(pFile); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2903 | sqlite3_free(pFile->lockingContext); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2904 | rc = closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2905 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2906 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2907 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2910 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2911 | /* |
| 2912 | ** The code above is the AFP lock implementation. The code is specific |
| 2913 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2914 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 2915 | ** VFS is not available. |
| 2916 | ** |
| 2917 | ********************* End of the AFP lock implementation ********************** |
| 2918 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2919 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2920 | /****************************************************************************** |
| 2921 | *************************** Begin NFS Locking ********************************/ |
| 2922 | |
| 2923 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 2924 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2925 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2926 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2927 | ** |
| 2928 | ** If the locking level of the file descriptor is already at or below |
| 2929 | ** the requested locking level, this routine is a no-op. |
| 2930 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2931 | static int nfsUnlock(sqlite3_file *id, int eFileLock){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 2932 | return posixUnlock(id, eFileLock, 1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
| 2935 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 2936 | /* |
| 2937 | ** The code above is the NFS lock implementation. The code is specific |
| 2938 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2939 | ** is available. |
| 2940 | ** |
| 2941 | ********************* End of the NFS lock implementation ********************** |
| 2942 | ******************************************************************************/ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2943 | |
| 2944 | /****************************************************************************** |
| 2945 | **************** Non-locking sqlite3_file methods ***************************** |
| 2946 | ** |
| 2947 | ** The next division contains implementations for all methods of the |
| 2948 | ** sqlite3_file object other than the locking methods. The locking |
| 2949 | ** methods were defined in divisions above (one locking method per |
| 2950 | ** division). Those methods that are common to all locking modes |
| 2951 | ** are gather together into this division. |
| 2952 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2953 | |
| 2954 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2955 | ** Seek to the offset passed as the second argument, then read cnt |
| 2956 | ** bytes into pBuf. Return the number of bytes actually read. |
| 2957 | ** |
| 2958 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 2959 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 2960 | ** one system to another. Since SQLite does not define USE_PREAD |
| 2961 | ** any any form by default, we will not attempt to define _XOPEN_SOURCE. |
| 2962 | ** See tickets #2741 and #2681. |
| 2963 | ** |
| 2964 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 2965 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2966 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2967 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 2968 | int got; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 2969 | int prior = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2970 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2971 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2972 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2973 | TIMER_START; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 2974 | do{ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2975 | #if defined(USE_PREAD) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 2976 | got = osPread(id->h, pBuf, cnt, offset); |
| 2977 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2978 | #elif defined(USE_PREAD64) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 2979 | got = osPread64(id->h, pBuf, cnt, offset); |
| 2980 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2981 | #else |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 2982 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 2983 | SimulateIOError( newOffset-- ); |
| 2984 | if( newOffset!=offset ){ |
| 2985 | if( newOffset == -1 ){ |
| 2986 | ((unixFile*)id)->lastErrno = errno; |
| 2987 | }else{ |
| 2988 | ((unixFile*)id)->lastErrno = 0; |
| 2989 | } |
| 2990 | return -1; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2991 | } |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 2992 | got = osRead(id->h, pBuf, cnt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2993 | #endif |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 2994 | if( got==cnt ) break; |
| 2995 | if( got<0 ){ |
| 2996 | if( errno==EINTR ){ got = 1; continue; } |
| 2997 | prior = 0; |
| 2998 | ((unixFile*)id)->lastErrno = errno; |
| 2999 | break; |
| 3000 | }else if( got>0 ){ |
| 3001 | cnt -= got; |
| 3002 | offset += got; |
| 3003 | prior += got; |
| 3004 | pBuf = (void*)(got + (char*)pBuf); |
| 3005 | } |
| 3006 | }while( got>0 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3007 | TIMER_END; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3008 | OSTRACE(("READ %-3d %5d %7lld %llu\n", |
| 3009 | id->h, got+prior, offset-prior, TIMER_ELAPSED)); |
| 3010 | return got+prior; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3014 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 3015 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 3016 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3017 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3018 | static int unixRead( |
| 3019 | sqlite3_file *id, |
| 3020 | void *pBuf, |
| 3021 | int amt, |
| 3022 | sqlite3_int64 offset |
| 3023 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3024 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3025 | int got; |
| 3026 | assert( id ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3027 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3028 | /* If this is a database file (not a journal, master-journal or temp |
| 3029 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3030 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3031 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3032 | || offset>=PENDING_BYTE+512 |
| 3033 | || offset+amt<=PENDING_BYTE |
| 3034 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3035 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3036 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3037 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3038 | if( got==amt ){ |
| 3039 | return SQLITE_OK; |
| 3040 | }else if( got<0 ){ |
| 3041 | /* lastErrno set by seekAndRead */ |
| 3042 | return SQLITE_IOERR_READ; |
| 3043 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3044 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3045 | /* Unread parts of the buffer must be zero-filled */ |
| 3046 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 3047 | return SQLITE_IOERR_SHORT_READ; |
| 3048 | } |
| 3049 | } |
| 3050 | |
| 3051 | /* |
| 3052 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 3053 | ** Return the number of bytes actually read. Update the offset. |
| 3054 | ** |
| 3055 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 3056 | ** is set before returning. |
| 3057 | */ |
| 3058 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
| 3059 | int got; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3060 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3061 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3062 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3063 | TIMER_START; |
| 3064 | #if defined(USE_PREAD) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 3065 | do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3066 | #elif defined(USE_PREAD64) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 3067 | do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3068 | #else |
drh | bd1e50c | 2011-08-19 14:54:12 +0000 | [diff] [blame] | 3069 | do{ |
| 3070 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 3071 | SimulateIOError( newOffset-- ); |
| 3072 | if( newOffset!=offset ){ |
| 3073 | if( newOffset == -1 ){ |
| 3074 | ((unixFile*)id)->lastErrno = errno; |
| 3075 | }else{ |
| 3076 | ((unixFile*)id)->lastErrno = 0; |
| 3077 | } |
| 3078 | return -1; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3079 | } |
drh | bd1e50c | 2011-08-19 14:54:12 +0000 | [diff] [blame] | 3080 | got = osWrite(id->h, pBuf, cnt); |
| 3081 | }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3082 | #endif |
| 3083 | TIMER_END; |
| 3084 | if( got<0 ){ |
| 3085 | ((unixFile*)id)->lastErrno = errno; |
| 3086 | } |
| 3087 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3088 | OSTRACE(("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3089 | return got; |
| 3090 | } |
| 3091 | |
| 3092 | |
| 3093 | /* |
| 3094 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 3095 | ** or some other error code on failure. |
| 3096 | */ |
| 3097 | static int unixWrite( |
| 3098 | sqlite3_file *id, |
| 3099 | const void *pBuf, |
| 3100 | int amt, |
| 3101 | sqlite3_int64 offset |
| 3102 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3103 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3104 | int wrote = 0; |
| 3105 | assert( id ); |
| 3106 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3107 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3108 | /* If this is a database file (not a journal, master-journal or temp |
| 3109 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3110 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3111 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3112 | || offset>=PENDING_BYTE+512 |
| 3113 | || offset+amt<=PENDING_BYTE |
| 3114 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3115 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3116 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3117 | #ifndef NDEBUG |
| 3118 | /* If we are doing a normal write to a database file (as opposed to |
| 3119 | ** doing a hot-journal rollback or a write to some file other than a |
| 3120 | ** normal database file) then record the fact that the database |
| 3121 | ** has changed. If the transaction counter is modified, record that |
| 3122 | ** fact too. |
| 3123 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3124 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3125 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 3126 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3127 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3128 | char oldCntr[4]; |
| 3129 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3130 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3131 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3132 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3133 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 3134 | } |
| 3135 | } |
| 3136 | } |
| 3137 | #endif |
| 3138 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3139 | while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3140 | amt -= wrote; |
| 3141 | offset += wrote; |
| 3142 | pBuf = &((char*)pBuf)[wrote]; |
| 3143 | } |
| 3144 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 3145 | SimulateDiskfullError(( wrote=0, amt=1 )); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3146 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3147 | if( amt>0 ){ |
drh | a21b83b | 2011-04-15 12:36:10 +0000 | [diff] [blame] | 3148 | if( wrote<0 && pFile->lastErrno!=ENOSPC ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3149 | /* lastErrno set by seekAndWrite */ |
| 3150 | return SQLITE_IOERR_WRITE; |
| 3151 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3152 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3153 | return SQLITE_FULL; |
| 3154 | } |
| 3155 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3156 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3157 | return SQLITE_OK; |
| 3158 | } |
| 3159 | |
| 3160 | #ifdef SQLITE_TEST |
| 3161 | /* |
| 3162 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3163 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3164 | */ |
| 3165 | int sqlite3_sync_count = 0; |
| 3166 | int sqlite3_fullsync_count = 0; |
| 3167 | #endif |
| 3168 | |
| 3169 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3170 | ** We do not trust systems to provide a working fdatasync(). Some do. |
drh | 20f8e13 | 2011-08-31 21:01:55 +0000 | [diff] [blame] | 3171 | ** Others do no. To be safe, we will stick with the (slightly slower) |
| 3172 | ** fsync(). If you know that your system does support fdatasync() correctly, |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3173 | ** then simply compile with -Dfdatasync=fdatasync |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3174 | */ |
drh | 20f8e13 | 2011-08-31 21:01:55 +0000 | [diff] [blame] | 3175 | #if !defined(fdatasync) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3176 | # define fdatasync fsync |
| 3177 | #endif |
| 3178 | |
| 3179 | /* |
| 3180 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 3181 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 3182 | ** only available on Mac OS X. But that could change. |
| 3183 | */ |
| 3184 | #ifdef F_FULLFSYNC |
| 3185 | # define HAVE_FULLFSYNC 1 |
| 3186 | #else |
| 3187 | # define HAVE_FULLFSYNC 0 |
| 3188 | #endif |
| 3189 | |
| 3190 | |
| 3191 | /* |
| 3192 | ** The fsync() system call does not work as advertised on many |
| 3193 | ** unix systems. The following procedure is an attempt to make |
| 3194 | ** it work better. |
| 3195 | ** |
| 3196 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 3197 | ** for testing when we want to run through the test suite quickly. |
| 3198 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 3199 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 3200 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3201 | ** |
| 3202 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 3203 | ** The idea behind dataOnly is that it should only write the file content |
| 3204 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 3205 | ** unchanged since the file size is part of the inode. However, |
| 3206 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 3207 | ** file size has changed. The only real difference between fdatasync() |
| 3208 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 3209 | ** inode if the mtime or owner or other inode attributes have changed. |
| 3210 | ** We only care about the file size, not the other file attributes, so |
| 3211 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 3212 | ** So, we always use fdatasync() if it is available, regardless of |
| 3213 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3214 | */ |
| 3215 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3216 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3217 | |
| 3218 | /* The following "ifdef/elif/else/" block has the same structure as |
| 3219 | ** the one below. It is replicated here solely to avoid cluttering |
| 3220 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 3221 | */ |
| 3222 | #ifdef SQLITE_NO_SYNC |
| 3223 | UNUSED_PARAMETER(fd); |
| 3224 | UNUSED_PARAMETER(fullSync); |
| 3225 | UNUSED_PARAMETER(dataOnly); |
| 3226 | #elif HAVE_FULLFSYNC |
| 3227 | UNUSED_PARAMETER(dataOnly); |
| 3228 | #else |
| 3229 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3230 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3231 | #endif |
| 3232 | |
| 3233 | /* Record the number of times that we do a normal fsync() and |
| 3234 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 3235 | ** gets called with the correct arguments. |
| 3236 | */ |
| 3237 | #ifdef SQLITE_TEST |
| 3238 | if( fullSync ) sqlite3_fullsync_count++; |
| 3239 | sqlite3_sync_count++; |
| 3240 | #endif |
| 3241 | |
| 3242 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 3243 | ** no-op |
| 3244 | */ |
| 3245 | #ifdef SQLITE_NO_SYNC |
| 3246 | rc = SQLITE_OK; |
| 3247 | #elif HAVE_FULLFSYNC |
| 3248 | if( fullSync ){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3249 | rc = osFcntl(fd, F_FULLFSYNC, 0); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3250 | }else{ |
| 3251 | rc = 1; |
| 3252 | } |
| 3253 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3254 | ** It shouldn't be possible for fullfsync to fail on the local |
| 3255 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 3256 | ** isn't supported for this file system. So, attempt an fsync |
| 3257 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 3258 | ** It'd be better to detect fullfsync support once and avoid |
| 3259 | ** the fcntl call every time sync is called. |
| 3260 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3261 | if( rc ) rc = fsync(fd); |
| 3262 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3263 | #elif defined(__APPLE__) |
| 3264 | /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 3265 | ** so currently we default to the macro that redefines fdatasync to fsync |
| 3266 | */ |
| 3267 | rc = fsync(fd); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3268 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3269 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 3270 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3271 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3272 | rc = fsync(fd); |
| 3273 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3274 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3275 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 3276 | |
| 3277 | if( OS_VXWORKS && rc!= -1 ){ |
| 3278 | rc = 0; |
| 3279 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3280 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3283 | /* |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3284 | ** Open a file descriptor to the directory containing file zFilename. |
| 3285 | ** If successful, *pFd is set to the opened file descriptor and |
| 3286 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 3287 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 3288 | ** value. |
| 3289 | ** |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3290 | ** The directory file descriptor is used for only one thing - to |
| 3291 | ** fsync() a directory to make sure file creation and deletion events |
| 3292 | ** are flushed to disk. Such fsyncs are not needed on newer |
| 3293 | ** journaling filesystems, but are required on older filesystems. |
| 3294 | ** |
| 3295 | ** This routine can be overridden using the xSetSysCall interface. |
| 3296 | ** The ability to override this routine was added in support of the |
| 3297 | ** chromium sandbox. Opening a directory is a security risk (we are |
| 3298 | ** told) so making it overrideable allows the chromium sandbox to |
| 3299 | ** replace this routine with a harmless no-op. To make this routine |
| 3300 | ** a no-op, replace it with a stub that returns SQLITE_OK but leaves |
| 3301 | ** *pFd set to a negative number. |
| 3302 | ** |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3303 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 3304 | ** the file descriptor *pFd using close(). |
| 3305 | */ |
| 3306 | static int openDirectory(const char *zFilename, int *pFd){ |
| 3307 | int ii; |
| 3308 | int fd = -1; |
| 3309 | char zDirname[MAX_PATHNAME+1]; |
| 3310 | |
| 3311 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
| 3312 | for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--); |
| 3313 | if( ii>0 ){ |
| 3314 | zDirname[ii] = '\0'; |
| 3315 | fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0); |
| 3316 | if( fd>=0 ){ |
| 3317 | #ifdef FD_CLOEXEC |
| 3318 | osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 3319 | #endif |
| 3320 | OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); |
| 3321 | } |
| 3322 | } |
| 3323 | *pFd = fd; |
| 3324 | return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname)); |
| 3325 | } |
| 3326 | |
| 3327 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3328 | ** Make sure all writes to a particular file are committed to disk. |
| 3329 | ** |
| 3330 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 3331 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 3332 | ** file data is synced. |
| 3333 | ** |
| 3334 | ** Under Unix, also make sure that the directory entry for the file |
| 3335 | ** has been created by fsync-ing the directory that contains the file. |
| 3336 | ** If we do not do this and we encounter a power failure, the directory |
| 3337 | ** entry for the journal might not exist after we reboot. The next |
| 3338 | ** SQLite to access the file will not know that the journal exists (because |
| 3339 | ** the directory entry for the journal was never created) and the transaction |
| 3340 | ** will not roll back - possibly leading to database corruption. |
| 3341 | */ |
| 3342 | static int unixSync(sqlite3_file *id, int flags){ |
| 3343 | int rc; |
| 3344 | unixFile *pFile = (unixFile*)id; |
| 3345 | |
| 3346 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 3347 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 3348 | |
| 3349 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 3350 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 3351 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 3352 | ); |
| 3353 | |
| 3354 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 3355 | ** line is to test that doing so does not cause any problems. |
| 3356 | */ |
| 3357 | SimulateDiskfullError( return SQLITE_FULL ); |
| 3358 | |
| 3359 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3360 | OSTRACE(("SYNC %-3d\n", pFile->h)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3361 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 3362 | SimulateIOError( rc=1 ); |
| 3363 | if( rc ){ |
| 3364 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3365 | return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3366 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3367 | |
| 3368 | /* Also fsync the directory containing the file if the DIRSYNC flag |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3369 | ** is set. This is a one-time occurrance. Many systems (examples: AIX) |
| 3370 | ** are unable to fsync a directory, so ignore errors on the fsync. |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3371 | */ |
| 3372 | if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){ |
| 3373 | int dirfd; |
| 3374 | OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3375 | HAVE_FULLFSYNC, isFullsync)); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3376 | rc = osOpenDirectory(pFile->zPath, &dirfd); |
| 3377 | if( rc==SQLITE_OK && dirfd>=0 ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3378 | full_fsync(dirfd, 0, 0); |
| 3379 | robust_close(pFile, dirfd, __LINE__); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 3380 | }else if( rc==SQLITE_CANTOPEN ){ |
| 3381 | rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3382 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3383 | pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3384 | } |
| 3385 | return rc; |
| 3386 | } |
| 3387 | |
| 3388 | /* |
| 3389 | ** Truncate an open file to a specified size |
| 3390 | */ |
| 3391 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3392 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3393 | int rc; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3394 | assert( pFile ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3395 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3396 | |
| 3397 | /* If the user has configured a chunk-size for this file, truncate the |
| 3398 | ** file so that it consists of an integer number of chunks (i.e. the |
| 3399 | ** actual file size after the operation may be larger than the requested |
| 3400 | ** size). |
| 3401 | */ |
| 3402 | if( pFile->szChunk ){ |
| 3403 | nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 3404 | } |
| 3405 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3406 | rc = robust_ftruncate(pFile->h, (off_t)nByte); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3407 | if( rc ){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3408 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3409 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3410 | }else{ |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3411 | #ifndef NDEBUG |
| 3412 | /* If we are doing a normal write to a database file (as opposed to |
| 3413 | ** doing a hot-journal rollback or a write to some file other than a |
| 3414 | ** normal database file) and we truncate the file to zero length, |
| 3415 | ** that effectively updates the change counter. This might happen |
| 3416 | ** when restoring a database using the backup API from a zero-length |
| 3417 | ** source. |
| 3418 | */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3419 | if( pFile->inNormalWrite && nByte==0 ){ |
| 3420 | pFile->transCntrChng = 1; |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3421 | } |
| 3422 | #endif |
| 3423 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3424 | return SQLITE_OK; |
| 3425 | } |
| 3426 | } |
| 3427 | |
| 3428 | /* |
| 3429 | ** Determine the current size of a file in bytes |
| 3430 | */ |
| 3431 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 3432 | int rc; |
| 3433 | struct stat buf; |
| 3434 | assert( id ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3435 | rc = osFstat(((unixFile*)id)->h, &buf); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3436 | SimulateIOError( rc=1 ); |
| 3437 | if( rc!=0 ){ |
| 3438 | ((unixFile*)id)->lastErrno = errno; |
| 3439 | return SQLITE_IOERR_FSTAT; |
| 3440 | } |
| 3441 | *pSize = buf.st_size; |
| 3442 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3443 | /* When opening a zero-size database, the findInodeInfo() procedure |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3444 | ** writes a single byte into that file in order to work around a bug |
| 3445 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 3446 | ** layers, we need to report this file size as zero even though it is |
| 3447 | ** really 1. Ticket #3260. |
| 3448 | */ |
| 3449 | if( *pSize==1 ) *pSize = 0; |
| 3450 | |
| 3451 | |
| 3452 | return SQLITE_OK; |
| 3453 | } |
| 3454 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3455 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3456 | /* |
| 3457 | ** Handler for proxy-locking file-control verbs. Defined below in the |
| 3458 | ** proxying locking division. |
| 3459 | */ |
| 3460 | static int proxyFileControl(sqlite3_file*,int,void*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3461 | #endif |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3462 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3463 | /* |
| 3464 | ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 3465 | ** file-control operation. Enlarge the database to nBytes in size |
| 3466 | ** (rounded up to the next chunk-size). If the database is already |
| 3467 | ** nBytes or larger, this routine is a no-op. |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3468 | */ |
| 3469 | static int fcntlSizeHint(unixFile *pFile, i64 nByte){ |
mistachkin | d589a54 | 2011-08-30 01:23:34 +0000 | [diff] [blame] | 3470 | if( pFile->szChunk>0 ){ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3471 | i64 nSize; /* Required file size */ |
| 3472 | struct stat buf; /* Used to hold return values of fstat() */ |
| 3473 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3474 | if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3475 | |
| 3476 | nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; |
| 3477 | if( nSize>(i64)buf.st_size ){ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3478 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3479 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3480 | /* The code below is handling the return value of osFallocate() |
| 3481 | ** correctly. posix_fallocate() is defined to "returns zero on success, |
| 3482 | ** or an error number on failure". See the manpage for details. */ |
| 3483 | int err; |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3484 | do{ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3485 | err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size); |
| 3486 | }while( err==EINTR ); |
| 3487 | if( err ) return SQLITE_IOERR_WRITE; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3488 | #else |
| 3489 | /* If the OS does not have posix_fallocate(), fake it. First use |
| 3490 | ** ftruncate() to set the file size, then write a single byte to |
| 3491 | ** the last byte in each block within the extended region. This |
| 3492 | ** is the same technique used by glibc to implement posix_fallocate() |
| 3493 | ** on systems that do not have a real fallocate() system call. |
| 3494 | */ |
| 3495 | int nBlk = buf.st_blksize; /* File-system block size */ |
| 3496 | i64 iWrite; /* Next offset to write to */ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3497 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3498 | if( robust_ftruncate(pFile->h, nSize) ){ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3499 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3500 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3501 | } |
| 3502 | iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1; |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3503 | while( iWrite<nSize ){ |
| 3504 | int nWrite = seekAndWrite(pFile, iWrite, "", 1); |
| 3505 | if( nWrite!=1 ) return SQLITE_IOERR_WRITE; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3506 | iWrite += nBlk; |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3507 | } |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3508 | #endif |
| 3509 | } |
| 3510 | } |
| 3511 | |
| 3512 | return SQLITE_OK; |
| 3513 | } |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3514 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3515 | /* |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3516 | ** If *pArg is inititially negative then this is a query. Set *pArg to |
| 3517 | ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set. |
| 3518 | ** |
| 3519 | ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags. |
| 3520 | */ |
| 3521 | static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){ |
| 3522 | if( *pArg<0 ){ |
| 3523 | *pArg = (pFile->ctrlFlags & mask)!=0; |
| 3524 | }else if( (*pArg)==0 ){ |
| 3525 | pFile->ctrlFlags &= ~mask; |
| 3526 | }else{ |
| 3527 | pFile->ctrlFlags |= mask; |
| 3528 | } |
| 3529 | } |
| 3530 | |
| 3531 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3532 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3533 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3534 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3535 | unixFile *pFile = (unixFile*)id; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3536 | switch( op ){ |
| 3537 | case SQLITE_FCNTL_LOCKSTATE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3538 | *(int*)pArg = pFile->eFileLock; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3539 | return SQLITE_OK; |
| 3540 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3541 | case SQLITE_LAST_ERRNO: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3542 | *(int*)pArg = pFile->lastErrno; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3543 | return SQLITE_OK; |
| 3544 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3545 | case SQLITE_FCNTL_CHUNK_SIZE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3546 | pFile->szChunk = *(int *)pArg; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3547 | return SQLITE_OK; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3548 | } |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3549 | case SQLITE_FCNTL_SIZE_HINT: { |
dan | da04ea4 | 2011-08-23 05:10:39 +0000 | [diff] [blame] | 3550 | int rc; |
| 3551 | SimulateIOErrorBenign(1); |
| 3552 | rc = fcntlSizeHint(pFile, *(i64 *)pArg); |
| 3553 | SimulateIOErrorBenign(0); |
| 3554 | return rc; |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3555 | } |
| 3556 | case SQLITE_FCNTL_PERSIST_WAL: { |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3557 | unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg); |
| 3558 | return SQLITE_OK; |
| 3559 | } |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame^] | 3560 | case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { |
| 3561 | unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg); |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3562 | return SQLITE_OK; |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3563 | } |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 3564 | case SQLITE_FCNTL_VFSNAME: { |
| 3565 | *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName); |
| 3566 | return SQLITE_OK; |
| 3567 | } |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3568 | #ifndef NDEBUG |
| 3569 | /* The pager calls this method to signal that it has done |
| 3570 | ** a rollback and that the database is therefore unchanged and |
| 3571 | ** it hence it is OK for the transaction change counter to be |
| 3572 | ** unchanged. |
| 3573 | */ |
| 3574 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 3575 | ((unixFile*)id)->dbUpdate = 0; |
| 3576 | return SQLITE_OK; |
| 3577 | } |
| 3578 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3579 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3580 | case SQLITE_SET_LOCKPROXYFILE: |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3581 | case SQLITE_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3582 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3583 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3584 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 3585 | case SQLITE_FCNTL_SYNC_OMITTED: { |
| 3586 | return SQLITE_OK; /* A no-op */ |
| 3587 | } |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3588 | } |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 3589 | return SQLITE_NOTFOUND; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 3590 | } |
| 3591 | |
| 3592 | /* |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3593 | ** Return the sector size in bytes of the underlying block device for |
| 3594 | ** the specified file. This is almost always 512 bytes, but may be |
| 3595 | ** larger for some devices. |
| 3596 | ** |
| 3597 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 3598 | ** if two files are created in the same file-system directory (i.e. |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3599 | ** a database and its journal file) that the sector size will be the |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3600 | ** same for both. |
| 3601 | */ |
drh | 1da88f0 | 2011-12-17 16:09:16 +0000 | [diff] [blame] | 3602 | static int unixSectorSize(sqlite3_file *pFile){ |
| 3603 | unixFile *p = (unixFile*)pFile; |
| 3604 | if( p->szSector==0 ){ |
| 3605 | #ifdef MISSING_STATVFS |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3606 | p->szSector = SQLITE_DEFAULT_SECTOR_SIZE/512; |
drh | 1da88f0 | 2011-12-17 16:09:16 +0000 | [diff] [blame] | 3607 | #else |
| 3608 | struct statvfs x; |
| 3609 | int sz; |
| 3610 | memset(&x, 0, sizeof(x)); |
| 3611 | osStatvfs(p->zPath, &x); |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3612 | sz = (int)x.f_frsize; |
drh | 1da88f0 | 2011-12-17 16:09:16 +0000 | [diff] [blame] | 3613 | if( sz<512 || sz>65536 || (sz&(sz-1))!=0 ){ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3614 | sz = SQLITE_DEFAULT_SECTOR_SIZE; |
drh | 1da88f0 | 2011-12-17 16:09:16 +0000 | [diff] [blame] | 3615 | } |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3616 | p->szSector = sz/512; |
drh | 1da88f0 | 2011-12-17 16:09:16 +0000 | [diff] [blame] | 3617 | #endif |
drh | 9c0e293 | 2011-12-17 16:25:17 +0000 | [diff] [blame] | 3618 | } |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3619 | return p->szSector*512; |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3620 | } |
| 3621 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3622 | /* |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3623 | ** Return the device characteristics for the file. |
| 3624 | ** |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame^] | 3625 | ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default. |
| 3626 | ** However, that choice is contraversial since technically the underlying |
| 3627 | ** file system does not always provide powersafe overwrites. (In other |
| 3628 | ** words, after a power-loss event, parts of the file that were never |
| 3629 | ** written might end up being altered.) However, non-PSOW behavior is very, |
| 3630 | ** very rare. And asserting PSOW makes a large reduction in the amount |
| 3631 | ** of required I/O for journaling, since a lot of padding is eliminated. |
| 3632 | ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control |
| 3633 | ** available to turn it off and URI query parameter available to turn it off. |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3634 | */ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3635 | static int unixDeviceCharacteristics(sqlite3_file *id){ |
| 3636 | unixFile *p = (unixFile*)id; |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame^] | 3637 | if( p->ctrlFlags & UNIXFILE_PSOW ){ |
| 3638 | return SQLITE_IOCAP_POWERSAFE_OVERWRITE; |
| 3639 | }else{ |
| 3640 | return 0; |
| 3641 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3644 | #ifndef SQLITE_OMIT_WAL |
| 3645 | |
| 3646 | |
| 3647 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3648 | ** Object used to represent an shared memory buffer. |
| 3649 | ** |
| 3650 | ** When multiple threads all reference the same wal-index, each thread |
| 3651 | ** has its own unixShm object, but they all point to a single instance |
| 3652 | ** of this unixShmNode object. In other words, each wal-index is opened |
| 3653 | ** only once per process. |
| 3654 | ** |
| 3655 | ** Each unixShmNode object is connected to a single unixInodeInfo object. |
| 3656 | ** We could coalesce this object into unixInodeInfo, but that would mean |
| 3657 | ** every open file that does not use shared memory (in other words, most |
| 3658 | ** open files) would have to carry around this extra information. So |
| 3659 | ** the unixInodeInfo object contains a pointer to this unixShmNode object |
| 3660 | ** and the unixShmNode object is created only when needed. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3661 | ** |
| 3662 | ** unixMutexHeld() must be true when creating or destroying |
| 3663 | ** this object or while reading or writing the following fields: |
| 3664 | ** |
| 3665 | ** nRef |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3666 | ** |
| 3667 | ** The following fields are read-only after the object is created: |
| 3668 | ** |
| 3669 | ** fid |
| 3670 | ** zFilename |
| 3671 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3672 | ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3673 | ** unixMutexHeld() is true when reading or writing any other field |
| 3674 | ** in this structure. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3675 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3676 | struct unixShmNode { |
| 3677 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3678 | sqlite3_mutex *mutex; /* Mutex to access this object */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3679 | char *zFilename; /* Name of the mmapped file */ |
| 3680 | int h; /* Open file descriptor */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3681 | int szRegion; /* Size of shared-memory regions */ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 3682 | u16 nRegion; /* Size of array apRegion */ |
| 3683 | u8 isReadonly; /* True if read-only */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3684 | char **apRegion; /* Array of mapped shared-memory regions */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3685 | int nRef; /* Number of unixShm objects pointing to this */ |
| 3686 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3687 | #ifdef SQLITE_DEBUG |
| 3688 | u8 exclMask; /* Mask of exclusive locks held */ |
| 3689 | u8 sharedMask; /* Mask of shared locks held */ |
| 3690 | u8 nextShmId; /* Next available unixShm.id value */ |
| 3691 | #endif |
| 3692 | }; |
| 3693 | |
| 3694 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3695 | ** Structure used internally by this VFS to record the state of an |
| 3696 | ** open shared memory connection. |
| 3697 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3698 | ** The following fields are initialized when this object is created and |
| 3699 | ** are read-only thereafter: |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3700 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3701 | ** unixShm.pFile |
| 3702 | ** unixShm.id |
| 3703 | ** |
| 3704 | ** All other fields are read/write. The unixShm.pFile->mutex must be held |
| 3705 | ** while accessing any read/write fields. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3706 | */ |
| 3707 | struct unixShm { |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3708 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 3709 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3710 | u8 hasMutex; /* True if holding the unixShmNode mutex */ |
drh | fd53231 | 2011-08-31 18:35:34 +0000 | [diff] [blame] | 3711 | u8 id; /* Id of this connection within its unixShmNode */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3712 | u16 sharedMask; /* Mask of shared locks held */ |
| 3713 | u16 exclMask; /* Mask of exclusive locks held */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3714 | }; |
| 3715 | |
| 3716 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3717 | ** Constants used for locking |
| 3718 | */ |
drh | bd9676c | 2010-06-23 17:58:38 +0000 | [diff] [blame] | 3719 | #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
drh | 4222441 | 2010-05-31 14:28:25 +0000 | [diff] [blame] | 3720 | #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3721 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3722 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3723 | ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3724 | ** |
| 3725 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 3726 | ** otherwise. |
| 3727 | */ |
| 3728 | static int unixShmSystemLock( |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3729 | unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */ |
| 3730 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3731 | int ofst, /* First byte of the locking range */ |
| 3732 | int n /* Number of bytes to lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3733 | ){ |
| 3734 | struct flock f; /* The posix advisory locking structure */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3735 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3736 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3737 | /* Access to the unixShmNode object is serialized by the caller */ |
| 3738 | assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3739 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3740 | /* Shared locks never span more than one byte */ |
| 3741 | assert( n==1 || lockType!=F_RDLCK ); |
| 3742 | |
| 3743 | /* Locks are within range */ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3744 | assert( n>=1 && n<SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3745 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3746 | if( pShmNode->h>=0 ){ |
| 3747 | /* Initialize the locking parameters */ |
| 3748 | memset(&f, 0, sizeof(f)); |
| 3749 | f.l_type = lockType; |
| 3750 | f.l_whence = SEEK_SET; |
| 3751 | f.l_start = ofst; |
| 3752 | f.l_len = n; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3753 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3754 | rc = osFcntl(pShmNode->h, F_SETLK, &f); |
| 3755 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 3756 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3757 | |
| 3758 | /* Update the global lock state and do debug tracing */ |
| 3759 | #ifdef SQLITE_DEBUG |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3760 | { u16 mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3761 | OSTRACE(("SHM-LOCK ")); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3762 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3763 | if( rc==SQLITE_OK ){ |
| 3764 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3765 | OSTRACE(("unlock %d ok", ofst)); |
| 3766 | pShmNode->exclMask &= ~mask; |
| 3767 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3768 | }else if( lockType==F_RDLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3769 | OSTRACE(("read-lock %d ok", ofst)); |
| 3770 | pShmNode->exclMask &= ~mask; |
| 3771 | pShmNode->sharedMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3772 | }else{ |
| 3773 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3774 | OSTRACE(("write-lock %d ok", ofst)); |
| 3775 | pShmNode->exclMask |= mask; |
| 3776 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3777 | } |
| 3778 | }else{ |
| 3779 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3780 | OSTRACE(("unlock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3781 | }else if( lockType==F_RDLCK ){ |
| 3782 | OSTRACE(("read-lock failed")); |
| 3783 | }else{ |
| 3784 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3785 | OSTRACE(("write-lock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3786 | } |
| 3787 | } |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 3788 | OSTRACE((" - afterwards %03x,%03x\n", |
| 3789 | pShmNode->sharedMask, pShmNode->exclMask)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3790 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3791 | #endif |
| 3792 | |
| 3793 | return rc; |
| 3794 | } |
| 3795 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3796 | |
| 3797 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3798 | ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3799 | ** |
| 3800 | ** This is not a VFS shared-memory method; it is a utility function called |
| 3801 | ** by VFS shared-memory methods. |
| 3802 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3803 | static void unixShmPurge(unixFile *pFd){ |
| 3804 | unixShmNode *p = pFd->pInode->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3805 | assert( unixMutexHeld() ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3806 | if( p && p->nRef==0 ){ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3807 | int i; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3808 | assert( p->pInode==pFd->pInode ); |
drh | df3aa16 | 2011-06-24 11:29:51 +0000 | [diff] [blame] | 3809 | sqlite3_mutex_free(p->mutex); |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3810 | for(i=0; i<p->nRegion; i++){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3811 | if( p->h>=0 ){ |
| 3812 | munmap(p->apRegion[i], p->szRegion); |
| 3813 | }else{ |
| 3814 | sqlite3_free(p->apRegion[i]); |
| 3815 | } |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3816 | } |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3817 | sqlite3_free(p->apRegion); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 3818 | if( p->h>=0 ){ |
| 3819 | robust_close(pFd, p->h, __LINE__); |
| 3820 | p->h = -1; |
| 3821 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3822 | p->pInode->pShmNode = 0; |
| 3823 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3824 | } |
| 3825 | } |
| 3826 | |
| 3827 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3828 | ** Open a shared-memory area associated with open database file pDbFd. |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3829 | ** This particular implementation uses mmapped files. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3830 | ** |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3831 | ** The file used to implement shared-memory is in the same directory |
| 3832 | ** as the open database file and has the same name as the open database |
| 3833 | ** file with the "-shm" suffix added. For example, if the database file |
| 3834 | ** is "/home/user1/config.db" then the file that is created and mmapped |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3835 | ** for shared memory will be called "/home/user1/config.db-shm". |
| 3836 | ** |
| 3837 | ** Another approach to is to use files in /dev/shm or /dev/tmp or an |
| 3838 | ** some other tmpfs mount. But if a file in a different directory |
| 3839 | ** from the database file is used, then differing access permissions |
| 3840 | ** or a chroot() might cause two different processes on the same |
| 3841 | ** database to end up using different files for shared memory - |
| 3842 | ** meaning that their memory would not really be shared - resulting |
| 3843 | ** in database corruption. Nevertheless, this tmpfs file usage |
| 3844 | ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" |
| 3845 | ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time |
| 3846 | ** option results in an incompatible build of SQLite; builds of SQLite |
| 3847 | ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the |
| 3848 | ** same database file at the same time, database corruption will likely |
| 3849 | ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered |
| 3850 | ** "unsupported" and may go away in a future SQLite release. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3851 | ** |
| 3852 | ** When opening a new shared-memory file, if no other instances of that |
| 3853 | ** file are currently open, in this process or in other processes, then |
| 3854 | ** the file must be truncated to zero length or have its header cleared. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3855 | ** |
| 3856 | ** If the original database file (pDbFd) is using the "unix-excl" VFS |
| 3857 | ** that means that an exclusive lock is held on the database file and |
| 3858 | ** that no other processes are able to read or write the database. In |
| 3859 | ** that case, we do not really need shared memory. No shared memory |
| 3860 | ** file is created. The shared memory will be simulated with heap memory. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3861 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3862 | static int unixOpenSharedMemory(unixFile *pDbFd){ |
| 3863 | struct unixShm *p = 0; /* The connection to be opened */ |
| 3864 | struct unixShmNode *pShmNode; /* The underlying mmapped file */ |
| 3865 | int rc; /* Result code */ |
| 3866 | unixInodeInfo *pInode; /* The inode of fd */ |
| 3867 | char *zShmFilename; /* Name of the file used for SHM */ |
| 3868 | int nShmFilename; /* Size of the SHM filename in bytes */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3869 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3870 | /* Allocate space for the new unixShm object. */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3871 | p = sqlite3_malloc( sizeof(*p) ); |
| 3872 | if( p==0 ) return SQLITE_NOMEM; |
| 3873 | memset(p, 0, sizeof(*p)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3874 | assert( pDbFd->pShm==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3875 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3876 | /* Check to see if a unixShmNode object already exists. Reuse an existing |
| 3877 | ** one if present. Create a new one if necessary. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3878 | */ |
| 3879 | unixEnterMutex(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 3880 | pInode = pDbFd->pInode; |
| 3881 | pShmNode = pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3882 | if( pShmNode==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 3883 | struct stat sStat; /* fstat() info for database file */ |
| 3884 | |
| 3885 | /* Call fstat() to figure out the permissions on the database file. If |
| 3886 | ** a new *-shm file is created, an attempt will be made to create it |
| 3887 | ** with the same permissions. The actual permissions the file is created |
| 3888 | ** with are subject to the current umask setting. |
| 3889 | */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3890 | if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 3891 | rc = SQLITE_IOERR_FSTAT; |
| 3892 | goto shm_open_err; |
| 3893 | } |
| 3894 | |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3895 | #ifdef SQLITE_SHM_DIRECTORY |
| 3896 | nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 30; |
| 3897 | #else |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3898 | nShmFilename = 5 + (int)strlen(pDbFd->zPath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3899 | #endif |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3900 | pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3901 | if( pShmNode==0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3902 | rc = SQLITE_NOMEM; |
| 3903 | goto shm_open_err; |
| 3904 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3905 | memset(pShmNode, 0, sizeof(*pShmNode)); |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3906 | zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1]; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3907 | #ifdef SQLITE_SHM_DIRECTORY |
| 3908 | sqlite3_snprintf(nShmFilename, zShmFilename, |
| 3909 | SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", |
| 3910 | (u32)sStat.st_ino, (u32)sStat.st_dev); |
| 3911 | #else |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3912 | sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath); |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 3913 | sqlite3FileSuffix3(pDbFd->zPath, zShmFilename); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3914 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3915 | pShmNode->h = -1; |
| 3916 | pDbFd->pInode->pShmNode = pShmNode; |
| 3917 | pShmNode->pInode = pDbFd->pInode; |
| 3918 | pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 3919 | if( pShmNode->mutex==0 ){ |
| 3920 | rc = SQLITE_NOMEM; |
| 3921 | goto shm_open_err; |
| 3922 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3923 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3924 | if( pInode->bProcessLock==0 ){ |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 3925 | int openFlags = O_RDWR | O_CREAT; |
drh | 9291372 | 2011-12-23 00:07:33 +0000 | [diff] [blame] | 3926 | if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 3927 | openFlags = O_RDONLY; |
| 3928 | pShmNode->isReadonly = 1; |
| 3929 | } |
| 3930 | pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777)); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3931 | if( pShmNode->h<0 ){ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 3932 | if( pShmNode->h<0 ){ |
| 3933 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename); |
| 3934 | goto shm_open_err; |
| 3935 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3936 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3937 | |
| 3938 | /* Check to see if another process is holding the dead-man switch. |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 3939 | ** If not, truncate the file to zero length. |
| 3940 | */ |
| 3941 | rc = SQLITE_OK; |
| 3942 | if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ |
| 3943 | if( robust_ftruncate(pShmNode->h, 0) ){ |
| 3944 | rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3945 | } |
| 3946 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 3947 | if( rc==SQLITE_OK ){ |
| 3948 | rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1); |
| 3949 | } |
| 3950 | if( rc ) goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3951 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3952 | } |
| 3953 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3954 | /* Make the new connection a child of the unixShmNode */ |
| 3955 | p->pShmNode = pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3956 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3957 | p->id = pShmNode->nextShmId++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3958 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3959 | pShmNode->nRef++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3960 | pDbFd->pShm = p; |
| 3961 | unixLeaveMutex(); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 3962 | |
| 3963 | /* The reference count on pShmNode has already been incremented under |
| 3964 | ** the cover of the unixEnterMutex() mutex and the pointer from the |
| 3965 | ** new (struct unixShm) object to the pShmNode has been set. All that is |
| 3966 | ** left to do is to link the new object into the linked list starting |
| 3967 | ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex |
| 3968 | ** mutex. |
| 3969 | */ |
| 3970 | sqlite3_mutex_enter(pShmNode->mutex); |
| 3971 | p->pNext = pShmNode->pFirst; |
| 3972 | pShmNode->pFirst = p; |
| 3973 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3974 | return SQLITE_OK; |
| 3975 | |
| 3976 | /* Jump here on any error */ |
| 3977 | shm_open_err: |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3978 | unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3979 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3980 | unixLeaveMutex(); |
| 3981 | return rc; |
| 3982 | } |
| 3983 | |
| 3984 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3985 | ** This function is called to obtain a pointer to region iRegion of the |
| 3986 | ** shared-memory associated with the database file fd. Shared-memory regions |
| 3987 | ** are numbered starting from zero. Each shared-memory region is szRegion |
| 3988 | ** bytes in size. |
| 3989 | ** |
| 3990 | ** If an error occurs, an error code is returned and *pp is set to NULL. |
| 3991 | ** |
| 3992 | ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory |
| 3993 | ** region has not been allocated (by any client, including one running in a |
| 3994 | ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 3995 | ** bExtend is non-zero and the requested shared-memory region has not yet |
| 3996 | ** been allocated, it is allocated by this function. |
| 3997 | ** |
| 3998 | ** If the shared-memory region has already been allocated or is allocated by |
| 3999 | ** this call as described above, then it is mapped into this processes |
| 4000 | ** address space (if it is not already), *pp is set to point to the mapped |
| 4001 | ** memory and SQLITE_OK returned. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4002 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4003 | static int unixShmMap( |
| 4004 | sqlite3_file *fd, /* Handle open on database file */ |
| 4005 | int iRegion, /* Region to retrieve */ |
| 4006 | int szRegion, /* Size of regions */ |
| 4007 | int bExtend, /* True to extend file if necessary */ |
| 4008 | void volatile **pp /* OUT: Mapped memory */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4009 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4010 | unixFile *pDbFd = (unixFile*)fd; |
| 4011 | unixShm *p; |
| 4012 | unixShmNode *pShmNode; |
| 4013 | int rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4014 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4015 | /* If the shared-memory file has not yet been opened, open it now. */ |
| 4016 | if( pDbFd->pShm==0 ){ |
| 4017 | rc = unixOpenSharedMemory(pDbFd); |
| 4018 | if( rc!=SQLITE_OK ) return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4019 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4020 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4021 | p = pDbFd->pShm; |
| 4022 | pShmNode = p->pShmNode; |
| 4023 | sqlite3_mutex_enter(pShmNode->mutex); |
| 4024 | assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4025 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4026 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4027 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4028 | |
| 4029 | if( pShmNode->nRegion<=iRegion ){ |
| 4030 | char **apNew; /* New apRegion[] array */ |
| 4031 | int nByte = (iRegion+1)*szRegion; /* Minimum required file size */ |
| 4032 | struct stat sStat; /* Used by fstat() */ |
| 4033 | |
| 4034 | pShmNode->szRegion = szRegion; |
| 4035 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4036 | if( pShmNode->h>=0 ){ |
| 4037 | /* The requested region is not mapped into this processes address space. |
| 4038 | ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 4039 | ** large enough to contain the requested region). |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4040 | */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4041 | if( osFstat(pShmNode->h, &sStat) ){ |
| 4042 | rc = SQLITE_IOERR_SHMSIZE; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4043 | goto shmpage_out; |
| 4044 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4045 | |
| 4046 | if( sStat.st_size<nByte ){ |
| 4047 | /* The requested memory region does not exist. If bExtend is set to |
| 4048 | ** false, exit early. *pp will be set to NULL and SQLITE_OK returned. |
| 4049 | ** |
| 4050 | ** Alternatively, if bExtend is true, use ftruncate() to allocate |
| 4051 | ** the requested memory region. |
| 4052 | */ |
| 4053 | if( !bExtend ) goto shmpage_out; |
| 4054 | if( robust_ftruncate(pShmNode->h, nByte) ){ |
| 4055 | rc = unixLogError(SQLITE_IOERR_SHMSIZE, "ftruncate", |
| 4056 | pShmNode->zFilename); |
| 4057 | goto shmpage_out; |
| 4058 | } |
| 4059 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4060 | } |
| 4061 | |
| 4062 | /* Map the requested memory region into this processes address space. */ |
| 4063 | apNew = (char **)sqlite3_realloc( |
| 4064 | pShmNode->apRegion, (iRegion+1)*sizeof(char *) |
| 4065 | ); |
| 4066 | if( !apNew ){ |
| 4067 | rc = SQLITE_IOERR_NOMEM; |
| 4068 | goto shmpage_out; |
| 4069 | } |
| 4070 | pShmNode->apRegion = apNew; |
| 4071 | while(pShmNode->nRegion<=iRegion){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4072 | void *pMem; |
| 4073 | if( pShmNode->h>=0 ){ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4074 | pMem = mmap(0, szRegion, |
| 4075 | pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4076 | MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion |
| 4077 | ); |
| 4078 | if( pMem==MAP_FAILED ){ |
drh | 50990db | 2011-04-13 20:26:13 +0000 | [diff] [blame] | 4079 | rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4080 | goto shmpage_out; |
| 4081 | } |
| 4082 | }else{ |
| 4083 | pMem = sqlite3_malloc(szRegion); |
| 4084 | if( pMem==0 ){ |
| 4085 | rc = SQLITE_NOMEM; |
| 4086 | goto shmpage_out; |
| 4087 | } |
| 4088 | memset(pMem, 0, szRegion); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4089 | } |
| 4090 | pShmNode->apRegion[pShmNode->nRegion] = pMem; |
| 4091 | pShmNode->nRegion++; |
| 4092 | } |
| 4093 | } |
| 4094 | |
| 4095 | shmpage_out: |
| 4096 | if( pShmNode->nRegion>iRegion ){ |
| 4097 | *pp = pShmNode->apRegion[iRegion]; |
| 4098 | }else{ |
| 4099 | *pp = 0; |
| 4100 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4101 | if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4102 | sqlite3_mutex_leave(pShmNode->mutex); |
| 4103 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4104 | } |
| 4105 | |
| 4106 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4107 | ** Change the lock state for a shared-memory segment. |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4108 | ** |
| 4109 | ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little |
| 4110 | ** different here than in posix. In xShmLock(), one can go from unlocked |
| 4111 | ** to shared and back or from unlocked to exclusive and back. But one may |
| 4112 | ** not go from shared to exclusive or from exclusive to shared. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4113 | */ |
| 4114 | static int unixShmLock( |
| 4115 | sqlite3_file *fd, /* Database file holding the shared memory */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4116 | int ofst, /* First lock to acquire or release */ |
| 4117 | int n, /* Number of locks to acquire or release */ |
| 4118 | int flags /* What to do with the lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4119 | ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4120 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 4121 | unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 4122 | unixShm *pX; /* For looping over all siblings */ |
| 4123 | unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 4124 | int rc = SQLITE_OK; /* Result code */ |
| 4125 | u16 mask; /* Mask of locks to take or release */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4126 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4127 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4128 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4129 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4130 | assert( n>=1 ); |
| 4131 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 4132 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 4133 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 4134 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 4135 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4136 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4137 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4138 | |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4139 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4140 | assert( n>1 || mask==(1<<ofst) ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4141 | sqlite3_mutex_enter(pShmNode->mutex); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4142 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 4143 | u16 allMask = 0; /* Mask of locks held by siblings */ |
| 4144 | |
| 4145 | /* See if any siblings hold this same lock */ |
| 4146 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 4147 | if( pX==p ) continue; |
| 4148 | assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
| 4149 | allMask |= pX->sharedMask; |
| 4150 | } |
| 4151 | |
| 4152 | /* Unlock the system-level locks */ |
| 4153 | if( (mask & allMask)==0 ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4154 | rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4155 | }else{ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4156 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4157 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4158 | |
| 4159 | /* Undo the local locks */ |
| 4160 | if( rc==SQLITE_OK ){ |
| 4161 | p->exclMask &= ~mask; |
| 4162 | p->sharedMask &= ~mask; |
| 4163 | } |
| 4164 | }else if( flags & SQLITE_SHM_SHARED ){ |
| 4165 | u16 allShared = 0; /* Union of locks held by connections other than "p" */ |
| 4166 | |
| 4167 | /* Find out which shared locks are already held by sibling connections. |
| 4168 | ** If any sibling already holds an exclusive lock, go ahead and return |
| 4169 | ** SQLITE_BUSY. |
| 4170 | */ |
| 4171 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4172 | if( (pX->exclMask & mask)!=0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4173 | rc = SQLITE_BUSY; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4174 | break; |
| 4175 | } |
| 4176 | allShared |= pX->sharedMask; |
| 4177 | } |
| 4178 | |
| 4179 | /* Get shared locks at the system level, if necessary */ |
| 4180 | if( rc==SQLITE_OK ){ |
| 4181 | if( (allShared & mask)==0 ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4182 | rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4183 | }else{ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4184 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4185 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4186 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4187 | |
| 4188 | /* Get the local shared locks */ |
| 4189 | if( rc==SQLITE_OK ){ |
| 4190 | p->sharedMask |= mask; |
| 4191 | } |
| 4192 | }else{ |
| 4193 | /* Make sure no sibling connections hold locks that will block this |
| 4194 | ** lock. If any do, return SQLITE_BUSY right away. |
| 4195 | */ |
| 4196 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4197 | if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ |
| 4198 | rc = SQLITE_BUSY; |
| 4199 | break; |
| 4200 | } |
| 4201 | } |
| 4202 | |
| 4203 | /* Get the exclusive locks at the system level. Then if successful |
| 4204 | ** also mark the local connection as being locked. |
| 4205 | */ |
| 4206 | if( rc==SQLITE_OK ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4207 | rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4208 | if( rc==SQLITE_OK ){ |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4209 | assert( (p->sharedMask & mask)==0 ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4210 | p->exclMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4211 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4212 | } |
| 4213 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4214 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 4215 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
| 4216 | p->id, getpid(), p->sharedMask, p->exclMask)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4217 | return rc; |
| 4218 | } |
| 4219 | |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4220 | /* |
| 4221 | ** Implement a memory barrier or memory fence on shared memory. |
| 4222 | ** |
| 4223 | ** All loads and stores begun before the barrier must complete before |
| 4224 | ** any load or store begun after the barrier. |
| 4225 | */ |
| 4226 | static void unixShmBarrier( |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4227 | sqlite3_file *fd /* Database file holding the shared memory */ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4228 | ){ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 4229 | UNUSED_PARAMETER(fd); |
drh | b29ad85 | 2010-06-01 00:03:57 +0000 | [diff] [blame] | 4230 | unixEnterMutex(); |
| 4231 | unixLeaveMutex(); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4232 | } |
| 4233 | |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4234 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4235 | ** Close a connection to shared-memory. Delete the underlying |
| 4236 | ** storage if deleteFlag is true. |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 4237 | ** |
| 4238 | ** If there is no shared memory associated with the connection then this |
| 4239 | ** routine is a harmless no-op. |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4240 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4241 | static int unixShmUnmap( |
| 4242 | sqlite3_file *fd, /* The underlying database file */ |
| 4243 | int deleteFlag /* Delete shared-memory if true */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4244 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4245 | unixShm *p; /* The connection to be closed */ |
| 4246 | unixShmNode *pShmNode; /* The underlying shared-memory file */ |
| 4247 | unixShm **pp; /* For looping over sibling connections */ |
| 4248 | unixFile *pDbFd; /* The underlying database file */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4249 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4250 | pDbFd = (unixFile*)fd; |
| 4251 | p = pDbFd->pShm; |
| 4252 | if( p==0 ) return SQLITE_OK; |
| 4253 | pShmNode = p->pShmNode; |
| 4254 | |
| 4255 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4256 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4257 | |
| 4258 | /* Remove connection p from the set of connections associated |
| 4259 | ** with pShmNode */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4260 | sqlite3_mutex_enter(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4261 | for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 4262 | *pp = p->pNext; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4263 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4264 | /* Free the connection p */ |
| 4265 | sqlite3_free(p); |
| 4266 | pDbFd->pShm = 0; |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4267 | sqlite3_mutex_leave(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4268 | |
| 4269 | /* If pShmNode->nRef has reached 0, then close the underlying |
| 4270 | ** shared-memory file, too */ |
| 4271 | unixEnterMutex(); |
| 4272 | assert( pShmNode->nRef>0 ); |
| 4273 | pShmNode->nRef--; |
| 4274 | if( pShmNode->nRef==0 ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 4275 | if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4276 | unixShmPurge(pDbFd); |
| 4277 | } |
| 4278 | unixLeaveMutex(); |
| 4279 | |
| 4280 | return SQLITE_OK; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4281 | } |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4282 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4283 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4284 | #else |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 4285 | # define unixShmMap 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4286 | # define unixShmLock 0 |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4287 | # define unixShmBarrier 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4288 | # define unixShmUnmap 0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4289 | #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 4290 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4291 | /* |
| 4292 | ** Here ends the implementation of all sqlite3_file methods. |
| 4293 | ** |
| 4294 | ********************** End sqlite3_file Methods ******************************* |
| 4295 | ******************************************************************************/ |
| 4296 | |
| 4297 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4298 | ** This division contains definitions of sqlite3_io_methods objects that |
| 4299 | ** implement various file locking strategies. It also contains definitions |
| 4300 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 4301 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 4302 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 4303 | ** the correct finder-function for that VFS. |
| 4304 | ** |
| 4305 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 4306 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 4307 | ** looks at the filesystem type and tries to guess the best locking |
| 4308 | ** strategy from that. |
| 4309 | ** |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4310 | ** For finder-funtion F, two objects are created: |
| 4311 | ** |
| 4312 | ** (1) The real finder-function named "FImpt()". |
| 4313 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4314 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4315 | ** |
| 4316 | ** |
| 4317 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 4318 | ** objects. We have to do this instead of letting pAppData point |
| 4319 | ** directly at the finder-function since C90 rules prevent a void* |
| 4320 | ** from be cast into a function pointer. |
| 4321 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4322 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4323 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4324 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4325 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 4326 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 4327 | ** |
| 4328 | ** * An I/O method finder function called FINDER that returns a pointer |
| 4329 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4330 | */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4331 | #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4332 | static const sqlite3_io_methods METHOD = { \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4333 | VERSION, /* iVersion */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4334 | CLOSE, /* xClose */ \ |
| 4335 | unixRead, /* xRead */ \ |
| 4336 | unixWrite, /* xWrite */ \ |
| 4337 | unixTruncate, /* xTruncate */ \ |
| 4338 | unixSync, /* xSync */ \ |
| 4339 | unixFileSize, /* xFileSize */ \ |
| 4340 | LOCK, /* xLock */ \ |
| 4341 | UNLOCK, /* xUnlock */ \ |
| 4342 | CKLOCK, /* xCheckReservedLock */ \ |
| 4343 | unixFileControl, /* xFileControl */ \ |
| 4344 | unixSectorSize, /* xSectorSize */ \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4345 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 4346 | unixShmMap, /* xShmMap */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4347 | unixShmLock, /* xShmLock */ \ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4348 | unixShmBarrier, /* xShmBarrier */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4349 | unixShmUnmap /* xShmUnmap */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4350 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4351 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 4352 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4353 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4354 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4355 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4356 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4357 | |
| 4358 | /* |
| 4359 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 4360 | ** locking strategies. Functions that return pointers to these methods |
| 4361 | ** are also created. |
| 4362 | */ |
| 4363 | IOMETHODS( |
| 4364 | posixIoFinder, /* Finder function name */ |
| 4365 | posixIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4366 | 2, /* shared memory is enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4367 | unixClose, /* xClose method */ |
| 4368 | unixLock, /* xLock method */ |
| 4369 | unixUnlock, /* xUnlock method */ |
| 4370 | unixCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4371 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4372 | IOMETHODS( |
| 4373 | nolockIoFinder, /* Finder function name */ |
| 4374 | nolockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4375 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4376 | nolockClose, /* xClose method */ |
| 4377 | nolockLock, /* xLock method */ |
| 4378 | nolockUnlock, /* xUnlock method */ |
| 4379 | nolockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4380 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4381 | IOMETHODS( |
| 4382 | dotlockIoFinder, /* Finder function name */ |
| 4383 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4384 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4385 | dotlockClose, /* xClose method */ |
| 4386 | dotlockLock, /* xLock method */ |
| 4387 | dotlockUnlock, /* xUnlock method */ |
| 4388 | dotlockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4389 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4390 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4391 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4392 | IOMETHODS( |
| 4393 | flockIoFinder, /* Finder function name */ |
| 4394 | flockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4395 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4396 | flockClose, /* xClose method */ |
| 4397 | flockLock, /* xLock method */ |
| 4398 | flockUnlock, /* xUnlock method */ |
| 4399 | flockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4400 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4401 | #endif |
| 4402 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4403 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4404 | IOMETHODS( |
| 4405 | semIoFinder, /* Finder function name */ |
| 4406 | semIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4407 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4408 | semClose, /* xClose method */ |
| 4409 | semLock, /* xLock method */ |
| 4410 | semUnlock, /* xUnlock method */ |
| 4411 | semCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4412 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4413 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4414 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4415 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4416 | IOMETHODS( |
| 4417 | afpIoFinder, /* Finder function name */ |
| 4418 | afpIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4419 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4420 | afpClose, /* xClose method */ |
| 4421 | afpLock, /* xLock method */ |
| 4422 | afpUnlock, /* xUnlock method */ |
| 4423 | afpCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4424 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4425 | #endif |
| 4426 | |
| 4427 | /* |
| 4428 | ** The proxy locking method is a "super-method" in the sense that it |
| 4429 | ** opens secondary file descriptors for the conch and lock files and |
| 4430 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 4431 | ** secondary files. For this reason, the division that implements |
| 4432 | ** proxy locking is located much further down in the file. But we need |
| 4433 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 4434 | ** for proxy locking here. So we forward declare the I/O methods. |
| 4435 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4436 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4437 | static int proxyClose(sqlite3_file*); |
| 4438 | static int proxyLock(sqlite3_file*, int); |
| 4439 | static int proxyUnlock(sqlite3_file*, int); |
| 4440 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4441 | IOMETHODS( |
| 4442 | proxyIoFinder, /* Finder function name */ |
| 4443 | proxyIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4444 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4445 | proxyClose, /* xClose method */ |
| 4446 | proxyLock, /* xLock method */ |
| 4447 | proxyUnlock, /* xUnlock method */ |
| 4448 | proxyCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4449 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4450 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4451 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4452 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 4453 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 4454 | IOMETHODS( |
| 4455 | nfsIoFinder, /* Finder function name */ |
| 4456 | nfsIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4457 | 1, /* shared memory is disabled */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4458 | unixClose, /* xClose method */ |
| 4459 | unixLock, /* xLock method */ |
| 4460 | nfsUnlock, /* xUnlock method */ |
| 4461 | unixCheckReservedLock /* xCheckReservedLock method */ |
| 4462 | ) |
| 4463 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4464 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4465 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4466 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4467 | ** This "finder" function attempts to determine the best locking strategy |
| 4468 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4469 | ** object that implements that strategy. |
| 4470 | ** |
| 4471 | ** This is for MacOSX only. |
| 4472 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4473 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4474 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4475 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4476 | ){ |
| 4477 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4478 | const char *zFilesystem; /* Filesystem type name */ |
| 4479 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4480 | } aMap[] = { |
| 4481 | { "hfs", &posixIoMethods }, |
| 4482 | { "ufs", &posixIoMethods }, |
| 4483 | { "afpfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4484 | { "smbfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4485 | { "webdav", &nolockIoMethods }, |
| 4486 | { 0, 0 } |
| 4487 | }; |
| 4488 | int i; |
| 4489 | struct statfs fsInfo; |
| 4490 | struct flock lockInfo; |
| 4491 | |
| 4492 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4493 | /* If filePath==NULL that means we are dealing with a transient file |
| 4494 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4495 | return &nolockIoMethods; |
| 4496 | } |
| 4497 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 4498 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 4499 | return &nolockIoMethods; |
| 4500 | } |
| 4501 | for(i=0; aMap[i].zFilesystem; i++){ |
| 4502 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 4503 | return aMap[i].pMethods; |
| 4504 | } |
| 4505 | } |
| 4506 | } |
| 4507 | |
| 4508 | /* Default case. Handles, amongst others, "nfs". |
| 4509 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 4510 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4511 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4512 | lockInfo.l_len = 1; |
| 4513 | lockInfo.l_start = 0; |
| 4514 | lockInfo.l_whence = SEEK_SET; |
| 4515 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4516 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4517 | if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 4518 | return &nfsIoMethods; |
| 4519 | } else { |
| 4520 | return &posixIoMethods; |
| 4521 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4522 | }else{ |
| 4523 | return &dotlockIoMethods; |
| 4524 | } |
| 4525 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4526 | static const sqlite3_io_methods |
| 4527 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4528 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4529 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4530 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4531 | #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE |
| 4532 | /* |
| 4533 | ** This "finder" function attempts to determine the best locking strategy |
| 4534 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
| 4535 | ** object that implements that strategy. |
| 4536 | ** |
| 4537 | ** This is for VXWorks only. |
| 4538 | */ |
| 4539 | static const sqlite3_io_methods *autolockIoFinderImpl( |
| 4540 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4541 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4542 | ){ |
| 4543 | struct flock lockInfo; |
| 4544 | |
| 4545 | if( !filePath ){ |
| 4546 | /* If filePath==NULL that means we are dealing with a transient file |
| 4547 | ** that does not need to be locked. */ |
| 4548 | return &nolockIoMethods; |
| 4549 | } |
| 4550 | |
| 4551 | /* Test if fcntl() is supported and use POSIX style locks. |
| 4552 | ** Otherwise fall back to the named semaphore method. |
| 4553 | */ |
| 4554 | lockInfo.l_len = 1; |
| 4555 | lockInfo.l_start = 0; |
| 4556 | lockInfo.l_whence = SEEK_SET; |
| 4557 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4558 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4559 | return &posixIoMethods; |
| 4560 | }else{ |
| 4561 | return &semIoMethods; |
| 4562 | } |
| 4563 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4564 | static const sqlite3_io_methods |
| 4565 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4566 | |
| 4567 | #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */ |
| 4568 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4569 | /* |
| 4570 | ** An abstract type for a pointer to a IO method finder function: |
| 4571 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4572 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4573 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4574 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4575 | /**************************************************************************** |
| 4576 | **************************** sqlite3_vfs methods **************************** |
| 4577 | ** |
| 4578 | ** This division contains the implementation of methods on the |
| 4579 | ** sqlite3_vfs object. |
| 4580 | */ |
| 4581 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4582 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4583 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4584 | */ |
| 4585 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4586 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4587 | int h, /* Open file descriptor of file being opened */ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 4588 | int syncDir, /* True to sync directory on first sync */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4589 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4590 | const char *zFilename, /* Name of the file being opened */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4591 | int noLock, /* Omit locking if true */ |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 4592 | int isDelete, /* Delete on close if true */ |
| 4593 | int isReadOnly /* True if the file is opened read-only */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4594 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4595 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4596 | unixFile *pNew = (unixFile *)pId; |
| 4597 | int rc = SQLITE_OK; |
| 4598 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4599 | assert( pNew->pInode==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4600 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4601 | /* Parameter isDelete is only used on vxworks. Express this explicitly |
| 4602 | ** here to prevent compiler warnings about unused parameters. |
danielk1977 | a03396a | 2008-11-19 14:35:46 +0000 | [diff] [blame] | 4603 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4604 | UNUSED_PARAMETER(isDelete); |
danielk1977 | a03396a | 2008-11-19 14:35:46 +0000 | [diff] [blame] | 4605 | |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4606 | /* Usually the path zFilename should not be a relative pathname. The |
| 4607 | ** exception is when opening the proxy "conch" file in builds that |
| 4608 | ** include the special Apple locking styles. |
| 4609 | */ |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4610 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | f7f55ed | 2010-10-05 18:22:47 +0000 | [diff] [blame] | 4611 | assert( zFilename==0 || zFilename[0]=='/' |
| 4612 | || pVfs->pAppData==(void*)&autolockIoFinder ); |
| 4613 | #else |
| 4614 | assert( zFilename==0 || zFilename[0]=='/' ); |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4615 | #endif |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4616 | |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 4617 | /* No locking occurs in temporary files */ |
| 4618 | assert( zFilename!=0 || noLock ); |
| 4619 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4620 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4621 | pNew->h = h; |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 4622 | pNew->pVfs = pVfs; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4623 | pNew->zPath = zFilename; |
drh | bec7c97 | 2011-12-23 00:25:02 +0000 | [diff] [blame] | 4624 | pNew->ctrlFlags = 0; |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame^] | 4625 | if( sqlite3_uri_boolean(zFilename, "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
| 4626 | pNew->ctrlFlags |= UNIXFILE_PSOW; |
drh | bec7c97 | 2011-12-23 00:25:02 +0000 | [diff] [blame] | 4627 | } |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 4628 | if( memcmp(pVfs->zName,"unix-excl",10)==0 ){ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 4629 | pNew->ctrlFlags |= UNIXFILE_EXCL; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 4630 | } |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 4631 | if( isReadOnly ){ |
| 4632 | pNew->ctrlFlags |= UNIXFILE_RDONLY; |
| 4633 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 4634 | if( syncDir ){ |
| 4635 | pNew->ctrlFlags |= UNIXFILE_DIRSYNC; |
| 4636 | } |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 4637 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4638 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 4639 | pNew->pId = vxworksFindFileId(zFilename); |
| 4640 | if( pNew->pId==0 ){ |
| 4641 | noLock = 1; |
| 4642 | rc = SQLITE_NOMEM; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4643 | } |
| 4644 | #endif |
| 4645 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4646 | if( noLock ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4647 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4648 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4649 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4650 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4651 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 4652 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 4653 | ** zFilename remains valid until file is closed, to support */ |
| 4654 | pNew->lockingContext = (void*)zFilename; |
| 4655 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4656 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4657 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4658 | if( pLockingStyle == &posixIoMethods |
| 4659 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 4660 | || pLockingStyle == &nfsIoMethods |
| 4661 | #endif |
| 4662 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4663 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4664 | rc = findInodeInfo(pNew, &pNew->pInode); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4665 | if( rc!=SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4666 | /* If an error occured in findInodeInfo(), close the file descriptor |
| 4667 | ** immediately, before releasing the mutex. findInodeInfo() may fail |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4668 | ** in two scenarios: |
| 4669 | ** |
| 4670 | ** (a) A call to fstat() failed. |
| 4671 | ** (b) A malloc failed. |
| 4672 | ** |
| 4673 | ** Scenario (b) may only occur if the process is holding no other |
| 4674 | ** file descriptors open on the same file. If there were other file |
| 4675 | ** descriptors on this file, then no malloc would be required by |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4676 | ** findInodeInfo(). If this is the case, it is quite safe to close |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4677 | ** handle h - as it is guaranteed that no posix locks will be released |
| 4678 | ** by doing so. |
| 4679 | ** |
| 4680 | ** If scenario (a) caused the error then things are not so safe. The |
| 4681 | ** implicit assumption here is that if fstat() fails, things are in |
| 4682 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 4683 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4684 | robust_close(pNew, h, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4685 | h = -1; |
| 4686 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4687 | unixLeaveMutex(); |
| 4688 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4689 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4690 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 4691 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4692 | /* AFP locking uses the file path so it needs to be included in |
| 4693 | ** the afpLockingContext. |
| 4694 | */ |
| 4695 | afpLockingContext *pCtx; |
| 4696 | pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 4697 | if( pCtx==0 ){ |
| 4698 | rc = SQLITE_NOMEM; |
| 4699 | }else{ |
| 4700 | /* NB: zFilename exists and remains valid until the file is closed |
| 4701 | ** according to requirement F11141. So we do not need to make a |
| 4702 | ** copy of the filename. */ |
| 4703 | pCtx->dbPath = zFilename; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4704 | pCtx->reserved = 0; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4705 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4706 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4707 | rc = findInodeInfo(pNew, &pNew->pInode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4708 | if( rc!=SQLITE_OK ){ |
| 4709 | sqlite3_free(pNew->lockingContext); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4710 | robust_close(pNew, h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4711 | h = -1; |
| 4712 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4713 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4714 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4715 | } |
| 4716 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4717 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4718 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 4719 | /* Dotfile locking uses the file path so it needs to be included in |
| 4720 | ** the dotlockLockingContext |
| 4721 | */ |
| 4722 | char *zLockFile; |
| 4723 | int nFilename; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 4724 | assert( zFilename!=0 ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4725 | nFilename = (int)strlen(zFilename) + 6; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4726 | zLockFile = (char *)sqlite3_malloc(nFilename); |
| 4727 | if( zLockFile==0 ){ |
| 4728 | rc = SQLITE_NOMEM; |
| 4729 | }else{ |
| 4730 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4731 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4732 | pNew->lockingContext = zLockFile; |
| 4733 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4734 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4735 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4736 | else if( pLockingStyle == &semIoMethods ){ |
| 4737 | /* Named semaphore locking uses the file path so it needs to be |
| 4738 | ** included in the semLockingContext |
| 4739 | */ |
| 4740 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4741 | rc = findInodeInfo(pNew, &pNew->pInode); |
| 4742 | if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 4743 | char *zSemName = pNew->pInode->aSemName; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4744 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 4745 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4746 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 4747 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4748 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4749 | pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 4750 | if( pNew->pInode->pSem == SEM_FAILED ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4751 | rc = SQLITE_NOMEM; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4752 | pNew->pInode->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4753 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4754 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4755 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4756 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4757 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 4758 | |
| 4759 | pNew->lastErrno = 0; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4760 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4761 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4762 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
drh | 309e655 | 2010-02-05 18:00:26 +0000 | [diff] [blame] | 4763 | h = -1; |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 4764 | osUnlink(zFilename); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4765 | isDelete = 0; |
| 4766 | } |
| 4767 | pNew->isDelete = isDelete; |
| 4768 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4769 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4770 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4771 | }else{ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4772 | pNew->pMethod = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4773 | OpenCounter(+1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4774 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4775 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 4776 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 4777 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4778 | /* |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4779 | ** Return the name of a directory in which to put temporary files. |
| 4780 | ** If no suitable temporary file directory can be found, return NULL. |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4781 | */ |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4782 | static const char *unixTempFileDir(void){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4783 | static const char *azDirs[] = { |
| 4784 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4785 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4786 | "/var/tmp", |
| 4787 | "/usr/tmp", |
| 4788 | "/tmp", |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4789 | 0 /* List terminator */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4790 | }; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4791 | unsigned int i; |
| 4792 | struct stat buf; |
| 4793 | const char *zDir = 0; |
| 4794 | |
| 4795 | azDirs[0] = sqlite3_temp_directory; |
| 4796 | if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); |
drh | 19515c8 | 2010-06-19 23:53:11 +0000 | [diff] [blame] | 4797 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){ |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4798 | if( zDir==0 ) continue; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4799 | if( osStat(zDir, &buf) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4800 | if( !S_ISDIR(buf.st_mode) ) continue; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4801 | if( osAccess(zDir, 07) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4802 | break; |
| 4803 | } |
| 4804 | return zDir; |
| 4805 | } |
| 4806 | |
| 4807 | /* |
| 4808 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 4809 | ** by the calling process and must be big enough to hold at least |
| 4810 | ** pVfs->mxPathname bytes. |
| 4811 | */ |
| 4812 | static int unixGetTempname(int nBuf, char *zBuf){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4813 | static const unsigned char zChars[] = |
| 4814 | "abcdefghijklmnopqrstuvwxyz" |
| 4815 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 4816 | "0123456789"; |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 4817 | unsigned int i, j; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4818 | const char *zDir; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4819 | |
| 4820 | /* It's odd to simulate an io-error here, but really this is just |
| 4821 | ** using the io-error infrastructure to test that SQLite handles this |
| 4822 | ** function failing. |
| 4823 | */ |
| 4824 | SimulateIOError( return SQLITE_IOERR ); |
| 4825 | |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4826 | zDir = unixTempFileDir(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4827 | if( zDir==0 ) zDir = "."; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4828 | |
| 4829 | /* Check that the output buffer is large enough for the temporary file |
| 4830 | ** name. If it is not, return SQLITE_ERROR. |
| 4831 | */ |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 4832 | if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4833 | return SQLITE_ERROR; |
| 4834 | } |
| 4835 | |
| 4836 | do{ |
| 4837 | sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4838 | j = (int)strlen(zBuf); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4839 | sqlite3_randomness(15, &zBuf[j]); |
| 4840 | for(i=0; i<15; i++, j++){ |
| 4841 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 4842 | } |
| 4843 | zBuf[j] = 0; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4844 | }while( osAccess(zBuf,0)==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4845 | return SQLITE_OK; |
| 4846 | } |
| 4847 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4848 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 4849 | /* |
| 4850 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 4851 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 4852 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 4853 | */ |
| 4854 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 4855 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 4856 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4857 | /* |
| 4858 | ** Search for an unused file descriptor that was opened on the database |
| 4859 | ** file (not a journal or master-journal file) identified by pathname |
| 4860 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 4861 | ** argument to this function. |
| 4862 | ** |
| 4863 | ** Such a file descriptor may exist if a database connection was closed |
| 4864 | ** but the associated file descriptor could not be closed because some |
| 4865 | ** other file descriptor open on the same file is holding a file-lock. |
| 4866 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 4867 | ** describing "Posix Advisory Locking" at the start of this file for |
| 4868 | ** further details. Also, ticket #4018. |
| 4869 | ** |
| 4870 | ** If a suitable file descriptor is found, then it is returned. If no |
| 4871 | ** such file descriptor is located, -1 is returned. |
| 4872 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4873 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 4874 | UnixUnusedFd *pUnused = 0; |
| 4875 | |
| 4876 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 4877 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 4878 | ** but because no way to test it is currently available. It is better |
| 4879 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 4880 | ** feature. */ |
| 4881 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4882 | struct stat sStat; /* Results of stat() call */ |
| 4883 | |
| 4884 | /* A stat() call may fail for various reasons. If this happens, it is |
| 4885 | ** almost certain that an open() call on the same path will also fail. |
| 4886 | ** For this reason, if an error occurs in the stat() call here, it is |
| 4887 | ** ignored and -1 is returned. The caller will try to open a new file |
| 4888 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 4889 | ** |
| 4890 | ** Even if a subsequent open() call does succeed, the consequences of |
| 4891 | ** not searching for a resusable file descriptor are not dire. */ |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 4892 | if( 0==osStat(zPath, &sStat) ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4893 | unixInodeInfo *pInode; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4894 | |
| 4895 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4896 | pInode = inodeList; |
| 4897 | while( pInode && (pInode->fileId.dev!=sStat.st_dev |
| 4898 | || pInode->fileId.ino!=sStat.st_ino) ){ |
| 4899 | pInode = pInode->pNext; |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 4900 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4901 | if( pInode ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4902 | UnixUnusedFd **pp; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4903 | for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4904 | pUnused = *pp; |
| 4905 | if( pUnused ){ |
| 4906 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4907 | } |
| 4908 | } |
| 4909 | unixLeaveMutex(); |
| 4910 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4911 | #endif /* if !OS_VXWORKS */ |
| 4912 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4913 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4914 | |
| 4915 | /* |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4916 | ** This function is called by unixOpen() to determine the unix permissions |
drh | f65bc91 | 2010-07-14 20:51:34 +0000 | [diff] [blame] | 4917 | ** 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] | 4918 | ** and a value suitable for passing as the third argument to open(2) is |
| 4919 | ** written to *pMode. If an IO error occurs, an SQLite error code is |
| 4920 | ** returned and the value of *pMode is not modified. |
| 4921 | ** |
| 4922 | ** If the file being opened is a temporary file, it is always created with |
| 4923 | ** the octal permissions 0600 (read/writable by owner only). If the file |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4924 | ** is a database or master journal file, it is created with the permissions |
| 4925 | ** mask SQLITE_DEFAULT_FILE_PERMISSIONS. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4926 | ** |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4927 | ** Finally, if the file being opened is a WAL or regular journal file, then |
| 4928 | ** this function queries the file-system for the permissions on the |
| 4929 | ** corresponding database file and sets *pMode to this value. Whenever |
| 4930 | ** possible, WAL and journal files are created using the same permissions |
| 4931 | ** as the associated database file. |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 4932 | ** |
| 4933 | ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the |
| 4934 | ** original filename is unavailable. But 8_3_NAMES is only used for |
| 4935 | ** FAT filesystems and permissions do not matter there, so just use |
| 4936 | ** the default permissions. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4937 | */ |
| 4938 | static int findCreateFileMode( |
| 4939 | const char *zPath, /* Path of file (possibly) being created */ |
| 4940 | int flags, /* Flags passed as 4th argument to xOpen() */ |
| 4941 | mode_t *pMode /* OUT: Permissions to open file with */ |
| 4942 | ){ |
| 4943 | int rc = SQLITE_OK; /* Return Code */ |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 4944 | *pMode = SQLITE_DEFAULT_FILE_PERMISSIONS; |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4945 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4946 | char zDb[MAX_PATHNAME+1]; /* Database file path */ |
| 4947 | int nDb; /* Number of valid bytes in zDb */ |
| 4948 | struct stat sStat; /* Output of stat() on database file */ |
| 4949 | |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 4950 | /* zPath is a path to a WAL or journal file. The following block derives |
| 4951 | ** the path to the associated database file from zPath. This block handles |
| 4952 | ** the following naming conventions: |
| 4953 | ** |
| 4954 | ** "<path to db>-journal" |
| 4955 | ** "<path to db>-wal" |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 4956 | ** "<path to db>-journalNN" |
| 4957 | ** "<path to db>-walNN" |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 4958 | ** |
drh | d337c5b | 2011-10-20 18:23:35 +0000 | [diff] [blame] | 4959 | ** where NN is a decimal number. The NN naming schemes are |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 4960 | ** used by the test_multiplex.c module. |
| 4961 | */ |
| 4962 | nDb = sqlite3Strlen30(zPath) - 1; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 4963 | #ifdef SQLITE_ENABLE_8_3_NAMES |
dan | 28a67fd | 2011-12-12 19:48:43 +0000 | [diff] [blame] | 4964 | while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--; |
drh | d337c5b | 2011-10-20 18:23:35 +0000 | [diff] [blame] | 4965 | if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 4966 | #else |
| 4967 | while( zPath[nDb]!='-' ){ |
| 4968 | assert( nDb>0 ); |
| 4969 | assert( zPath[nDb]!='\n' ); |
| 4970 | nDb--; |
| 4971 | } |
| 4972 | #endif |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4973 | memcpy(zDb, zPath, nDb); |
| 4974 | zDb[nDb] = '\0'; |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 4975 | |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 4976 | if( 0==osStat(zDb, &sStat) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4977 | *pMode = sStat.st_mode & 0777; |
| 4978 | }else{ |
| 4979 | rc = SQLITE_IOERR_FSTAT; |
| 4980 | } |
| 4981 | }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 4982 | *pMode = 0600; |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4983 | } |
| 4984 | return rc; |
| 4985 | } |
| 4986 | |
| 4987 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4988 | ** Open the file zPath. |
| 4989 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4990 | ** Previously, the SQLite OS layer used three functions in place of this |
| 4991 | ** one: |
| 4992 | ** |
| 4993 | ** sqlite3OsOpenReadWrite(); |
| 4994 | ** sqlite3OsOpenReadOnly(); |
| 4995 | ** sqlite3OsOpenExclusive(); |
| 4996 | ** |
| 4997 | ** These calls correspond to the following combinations of flags: |
| 4998 | ** |
| 4999 | ** ReadWrite() -> (READWRITE | CREATE) |
| 5000 | ** ReadOnly() -> (READONLY) |
| 5001 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 5002 | ** |
| 5003 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 5004 | ** true, the file was configured to be automatically deleted when the |
| 5005 | ** file handle closed. To achieve the same effect using this new |
| 5006 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 5007 | ** OpenExclusive(). |
| 5008 | */ |
| 5009 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5010 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 5011 | const char *zPath, /* Pathname of file to be opened */ |
| 5012 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 5013 | int flags, /* Input flags to control the opening */ |
| 5014 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5015 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5016 | unixFile *p = (unixFile *)pFile; |
| 5017 | int fd = -1; /* File descriptor returned by open() */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5018 | int openFlags = 0; /* Flags to pass to open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5019 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5020 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5021 | int rc = SQLITE_OK; /* Function Return Code */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5022 | |
| 5023 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 5024 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 5025 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 5026 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 5027 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5028 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5029 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 5030 | #endif |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 5031 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 5032 | struct statfs fsInfo; |
| 5033 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5034 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5035 | /* If creating a master or main-file journal, this function will open |
| 5036 | ** a file-descriptor on the directory too. The first time unixSync() |
| 5037 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 5038 | */ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5039 | int syncDir = (isCreate && ( |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5040 | eType==SQLITE_OPEN_MASTER_JOURNAL |
| 5041 | || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 5042 | || eType==SQLITE_OPEN_WAL |
| 5043 | )); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5044 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5045 | /* If argument zPath is a NULL pointer, this function is required to open |
| 5046 | ** a temporary file. Use this buffer to store the file name in. |
| 5047 | */ |
| 5048 | char zTmpname[MAX_PATHNAME+1]; |
| 5049 | const char *zName = zPath; |
| 5050 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5051 | /* Check the following statements are true: |
| 5052 | ** |
| 5053 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 5054 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 5055 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5056 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5057 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5058 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5059 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5060 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5061 | assert(isDelete==0 || isCreate); |
| 5062 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5063 | /* The main DB, main journal, WAL file and master journal are never |
| 5064 | ** automatically deleted. Nor are they ever temporary files. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5065 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 5066 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 5067 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5068 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5069 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5070 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 5071 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 5072 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 5073 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5074 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5075 | ); |
| 5076 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5077 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5078 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5079 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5080 | UnixUnusedFd *pUnused; |
| 5081 | pUnused = findReusableFd(zName, flags); |
| 5082 | if( pUnused ){ |
| 5083 | fd = pUnused->fd; |
| 5084 | }else{ |
dan | 6aa657f | 2009-08-24 18:57:58 +0000 | [diff] [blame] | 5085 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5086 | if( !pUnused ){ |
| 5087 | return SQLITE_NOMEM; |
| 5088 | } |
| 5089 | } |
| 5090 | p->pUnused = pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5091 | }else if( !zName ){ |
| 5092 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5093 | assert(isDelete && !syncDir); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5094 | rc = unixGetTempname(MAX_PATHNAME+1, zTmpname); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5095 | if( rc!=SQLITE_OK ){ |
| 5096 | return rc; |
| 5097 | } |
| 5098 | zName = zTmpname; |
| 5099 | } |
| 5100 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5101 | /* Determine the value of the flags parameter passed to POSIX function |
| 5102 | ** open(). These must be calculated even if open() is not called, as |
| 5103 | ** they may be stored as part of the file handle and used by the |
| 5104 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5105 | if( isReadonly ) openFlags |= O_RDONLY; |
| 5106 | if( isReadWrite ) openFlags |= O_RDWR; |
| 5107 | if( isCreate ) openFlags |= O_CREAT; |
| 5108 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 5109 | openFlags |= (O_LARGEFILE|O_BINARY); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5110 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5111 | if( fd<0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5112 | mode_t openMode; /* Permissions to create file with */ |
| 5113 | rc = findCreateFileMode(zName, flags, &openMode); |
| 5114 | if( rc!=SQLITE_OK ){ |
| 5115 | assert( !p->pUnused ); |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5116 | assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5117 | return rc; |
| 5118 | } |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5119 | fd = robust_open(zName, openFlags, openMode); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5120 | OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5121 | if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ |
| 5122 | /* Failed to open the file for read/write access. Try read-only. */ |
| 5123 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5124 | openFlags &= ~(O_RDWR|O_CREAT); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5125 | flags |= SQLITE_OPEN_READONLY; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5126 | openFlags |= O_RDONLY; |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 5127 | isReadonly = 1; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5128 | fd = robust_open(zName, openFlags, openMode); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5129 | } |
| 5130 | if( fd<0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5131 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5132 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5133 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5134 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5135 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5136 | if( pOutFlags ){ |
| 5137 | *pOutFlags = flags; |
| 5138 | } |
| 5139 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5140 | if( p->pUnused ){ |
| 5141 | p->pUnused->fd = fd; |
| 5142 | p->pUnused->flags = flags; |
| 5143 | } |
| 5144 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5145 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5146 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5147 | zPath = zName; |
| 5148 | #else |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5149 | osUnlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5150 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5151 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 5152 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5153 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5154 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 5155 | } |
| 5156 | #endif |
| 5157 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5158 | #ifdef FD_CLOEXEC |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5159 | osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5160 | #endif |
| 5161 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5162 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5163 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5164 | |
| 5165 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5166 | if( fstatfs(fd, &fsInfo) == -1 ){ |
| 5167 | ((unixFile*)pFile)->lastErrno = errno; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5168 | robust_close(p, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5169 | return SQLITE_IOERR_ACCESS; |
| 5170 | } |
| 5171 | if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 5172 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 5173 | } |
| 5174 | #endif |
| 5175 | |
| 5176 | #if SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5177 | #if SQLITE_PREFER_PROXY_LOCKING |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5178 | isAutoProxy = 1; |
| 5179 | #endif |
| 5180 | if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5181 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 5182 | int useProxy = 0; |
| 5183 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5184 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 5185 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5186 | if( envforce!=NULL ){ |
| 5187 | useProxy = atoi(envforce)>0; |
| 5188 | }else{ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5189 | if( statfs(zPath, &fsInfo) == -1 ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5190 | /* In theory, the close(fd) call is sub-optimal. If the file opened |
| 5191 | ** with fd is a database file, and there are other connections open |
| 5192 | ** on that file that are currently holding advisory locks on it, |
| 5193 | ** then the call to close() will cancel those locks. In practice, |
| 5194 | ** we're assuming that statfs() doesn't fail very often. At least |
| 5195 | ** not while other file descriptors opened by the same process on |
| 5196 | ** the same file are working. */ |
| 5197 | p->lastErrno = errno; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5198 | robust_close(p, fd, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5199 | rc = SQLITE_IOERR_ACCESS; |
| 5200 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5201 | } |
| 5202 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 5203 | } |
| 5204 | if( useProxy ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5205 | rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock, |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 5206 | isDelete, isReadonly); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5207 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5208 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5209 | if( rc!=SQLITE_OK ){ |
| 5210 | /* Use unixClose to clean up the resources added in fillInUnixFile |
| 5211 | ** and clear all the structure's references. Specifically, |
| 5212 | ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 5213 | */ |
| 5214 | unixClose(pFile); |
| 5215 | return rc; |
| 5216 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5217 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5218 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5219 | } |
| 5220 | } |
| 5221 | #endif |
| 5222 | |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5223 | rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock, |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 5224 | isDelete, isReadonly); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5225 | open_finished: |
| 5226 | if( rc!=SQLITE_OK ){ |
| 5227 | sqlite3_free(p->pUnused); |
| 5228 | } |
| 5229 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5230 | } |
| 5231 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5232 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5233 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5234 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 5235 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5236 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5237 | static int unixDelete( |
| 5238 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 5239 | const char *zPath, /* Name of file to be deleted */ |
| 5240 | int dirSync /* If true, fsync() directory after deleting file */ |
| 5241 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5242 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5243 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5244 | SimulateIOError(return SQLITE_IOERR_DELETE); |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5245 | if( osUnlink(zPath)==(-1) && errno!=ENOENT ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5246 | return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath); |
drh | 5d4feff | 2010-07-14 01:45:22 +0000 | [diff] [blame] | 5247 | } |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 5248 | #ifndef SQLITE_DISABLE_DIRSYNC |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5249 | if( dirSync ){ |
| 5250 | int fd; |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 5251 | rc = osOpenDirectory(zPath, &fd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5252 | if( rc==SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5253 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5254 | if( fsync(fd)==-1 ) |
| 5255 | #else |
| 5256 | if( fsync(fd) ) |
| 5257 | #endif |
| 5258 | { |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5259 | rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5260 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5261 | robust_close(0, fd, __LINE__); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 5262 | }else if( rc==SQLITE_CANTOPEN ){ |
| 5263 | rc = SQLITE_OK; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5264 | } |
| 5265 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 5266 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5267 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5268 | } |
| 5269 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5270 | /* |
| 5271 | ** Test the existance of or access permissions of file zPath. The |
| 5272 | ** test performed depends on the value of flags: |
| 5273 | ** |
| 5274 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 5275 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 5276 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 5277 | ** |
| 5278 | ** Otherwise return 0. |
| 5279 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5280 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5281 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 5282 | const char *zPath, /* Path of the file to examine */ |
| 5283 | int flags, /* What do we want to learn about the zPath file? */ |
| 5284 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5285 | ){ |
rse | 25c0d1a | 2007-09-20 08:38:14 +0000 | [diff] [blame] | 5286 | int amode = 0; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5287 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5288 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5289 | switch( flags ){ |
| 5290 | case SQLITE_ACCESS_EXISTS: |
| 5291 | amode = F_OK; |
| 5292 | break; |
| 5293 | case SQLITE_ACCESS_READWRITE: |
| 5294 | amode = W_OK|R_OK; |
| 5295 | break; |
drh | 50d3f90 | 2007-08-27 21:10:36 +0000 | [diff] [blame] | 5296 | case SQLITE_ACCESS_READ: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5297 | amode = R_OK; |
| 5298 | break; |
| 5299 | |
| 5300 | default: |
| 5301 | assert(!"Invalid flags argument"); |
| 5302 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5303 | *pResOut = (osAccess(zPath, amode)==0); |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 5304 | if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){ |
| 5305 | struct stat buf; |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 5306 | if( 0==osStat(zPath, &buf) && buf.st_size==0 ){ |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 5307 | *pResOut = 0; |
| 5308 | } |
| 5309 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5310 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5311 | } |
| 5312 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5313 | |
| 5314 | /* |
| 5315 | ** Turn a relative pathname into a full pathname. The relative path |
| 5316 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 5317 | ** zPath. |
| 5318 | ** |
| 5319 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 5320 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 5321 | ** this buffer before returning. |
| 5322 | */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 5323 | static int unixFullPathname( |
| 5324 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 5325 | const char *zPath, /* Possibly relative input path */ |
| 5326 | int nOut, /* Size of output buffer in bytes */ |
| 5327 | char *zOut /* Output buffer */ |
| 5328 | ){ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 5329 | |
| 5330 | /* It's odd to simulate an io-error here, but really this is just |
| 5331 | ** using the io-error infrastructure to test that SQLite handles this |
| 5332 | ** function failing. This function could fail if, for example, the |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5333 | ** current working directory has been unlinked. |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 5334 | */ |
| 5335 | SimulateIOError( return SQLITE_ERROR ); |
| 5336 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 5337 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 5338 | UNUSED_PARAMETER(pVfs); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5339 | |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 5340 | zOut[nOut-1] = '\0'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5341 | if( zPath[0]=='/' ){ |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 5342 | sqlite3_snprintf(nOut, zOut, "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5343 | }else{ |
| 5344 | int nCwd; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5345 | if( osGetcwd(zOut, nOut-1)==0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5346 | return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5347 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5348 | nCwd = (int)strlen(zOut); |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 5349 | sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5350 | } |
| 5351 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5352 | } |
| 5353 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 5354 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5355 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 5356 | /* |
| 5357 | ** Interfaces for opening a shared library, finding entry points |
| 5358 | ** within the shared library, and closing the shared library. |
| 5359 | */ |
| 5360 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5361 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 5362 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5363 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 5364 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 5365 | |
| 5366 | /* |
| 5367 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 5368 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 5369 | ** message is available, it is written to zBufOut. If no error message |
| 5370 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 5371 | ** error message. |
| 5372 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5373 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
dan | 3239053 | 2010-11-29 18:36:22 +0000 | [diff] [blame] | 5374 | const char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5375 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5376 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5377 | zErr = dlerror(); |
| 5378 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 5379 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5380 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5381 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5382 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5383 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 5384 | /* |
| 5385 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 5386 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 5387 | ** returns a void* which is really a pointer to a function. So how do we |
| 5388 | ** use dlsym() with -pedantic-errors? |
| 5389 | ** |
| 5390 | ** Variable x below is defined to be a pointer to a function taking |
| 5391 | ** parameters void* and const char* and returning a pointer to a function. |
| 5392 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 5393 | ** (That assignment requires a cast.) Then we call the function that |
| 5394 | ** x points to. |
| 5395 | ** |
| 5396 | ** This work-around is unlikely to work correctly on any system where |
| 5397 | ** you really cannot cast a function pointer into void*. But then, on the |
| 5398 | ** other hand, dlsym() will not work on such a system either, so we have |
| 5399 | ** not really lost anything. |
| 5400 | */ |
| 5401 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5402 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5403 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 5404 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5405 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5406 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 5407 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5408 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5409 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5410 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 5411 | #define unixDlOpen 0 |
| 5412 | #define unixDlError 0 |
| 5413 | #define unixDlSym 0 |
| 5414 | #define unixDlClose 0 |
| 5415 | #endif |
| 5416 | |
| 5417 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5418 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5419 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5420 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 5421 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 5422 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5423 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5424 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 5425 | ** errors. The reports issued by valgrind are incorrect - we would |
| 5426 | ** prefer that the randomness be increased by making use of the |
| 5427 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 5428 | ** some users. Rather than argue, it seems easier just to initialize |
| 5429 | ** the whole array and silence valgrind, even if that means less randomness |
| 5430 | ** in the random seed. |
| 5431 | ** |
| 5432 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 5433 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5434 | ** tests repeatable. |
| 5435 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5436 | memset(zBuf, 0, nBuf); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5437 | #if !defined(SQLITE_TEST) |
| 5438 | { |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5439 | int pid, fd; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5440 | fd = robust_open("/dev/urandom", O_RDONLY, 0); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5441 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 5442 | time_t t; |
| 5443 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5444 | memcpy(zBuf, &t, sizeof(t)); |
| 5445 | pid = getpid(); |
| 5446 | memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid)); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 5447 | assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf ); |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 5448 | nBuf = sizeof(t) + sizeof(pid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5449 | }else{ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 5450 | do{ nBuf = osRead(fd, zBuf, nBuf); }while( nBuf<0 && errno==EINTR ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5451 | robust_close(0, fd, __LINE__); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5452 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5453 | } |
| 5454 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 5455 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5456 | } |
| 5457 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5458 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5459 | /* |
| 5460 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5461 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 5462 | ** The return value is the number of microseconds of sleep actually |
| 5463 | ** requested from the underlying operating system, a number which |
| 5464 | ** might be greater than or equal to the argument, but not less |
| 5465 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5466 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5467 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5468 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5469 | struct timespec sp; |
| 5470 | |
| 5471 | sp.tv_sec = microseconds / 1000000; |
| 5472 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 5473 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 5474 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5475 | return microseconds; |
| 5476 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5477 | usleep(microseconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 5478 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5479 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5480 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5481 | int seconds = (microseconds+999999)/1000000; |
| 5482 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 5483 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 5484 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 5485 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 5486 | } |
| 5487 | |
| 5488 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5489 | ** The following variable, if set to a non-zero value, is interpreted as |
| 5490 | ** the number of seconds since 1970 and is used to set the result of |
| 5491 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5492 | */ |
| 5493 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5494 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5495 | #endif |
| 5496 | |
| 5497 | /* |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5498 | ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 5499 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 5500 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 5501 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 5502 | ** proleptic Gregorian calendar. |
| 5503 | ** |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5504 | ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
| 5505 | ** cannot be found. |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5506 | */ |
| 5507 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 5508 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5509 | int rc = SQLITE_OK; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5510 | #if defined(NO_GETTOD) |
| 5511 | time_t t; |
| 5512 | time(&t); |
dan | 15eac4e | 2010-11-22 17:26:07 +0000 | [diff] [blame] | 5513 | *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5514 | #elif OS_VXWORKS |
| 5515 | struct timespec sNow; |
| 5516 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 5517 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 5518 | #else |
| 5519 | struct timeval sNow; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5520 | if( gettimeofday(&sNow, 0)==0 ){ |
| 5521 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
| 5522 | }else{ |
| 5523 | rc = SQLITE_ERROR; |
| 5524 | } |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5525 | #endif |
| 5526 | |
| 5527 | #ifdef SQLITE_TEST |
| 5528 | if( sqlite3_current_time ){ |
| 5529 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 5530 | } |
| 5531 | #endif |
| 5532 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5533 | return rc; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5534 | } |
| 5535 | |
| 5536 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5537 | ** Find the current time (in Universal Coordinated Time). Write the |
| 5538 | ** current time and date as a Julian Day number into *prNow and |
| 5539 | ** return 0. Return 1 if the time and date cannot be found. |
| 5540 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5541 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | b87a666 | 2011-10-13 01:01:14 +0000 | [diff] [blame] | 5542 | sqlite3_int64 i = 0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5543 | int rc; |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 5544 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5545 | rc = unixCurrentTimeInt64(0, &i); |
drh | 0dcb0a7 | 2010-05-03 18:22:52 +0000 | [diff] [blame] | 5546 | *prNow = i/86400000.0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5547 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5548 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5549 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5550 | /* |
| 5551 | ** We added the xGetLastError() method with the intention of providing |
| 5552 | ** better low-level error messages when operating-system problems come up |
| 5553 | ** during SQLite operation. But so far, none of that has been implemented |
| 5554 | ** in the core. So this routine is never called. For now, it is merely |
| 5555 | ** a place-holder. |
| 5556 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5557 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 5558 | UNUSED_PARAMETER(NotUsed); |
| 5559 | UNUSED_PARAMETER(NotUsed2); |
| 5560 | UNUSED_PARAMETER(NotUsed3); |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 5561 | return 0; |
| 5562 | } |
| 5563 | |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5564 | |
| 5565 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5566 | ************************ End of sqlite3_vfs methods *************************** |
| 5567 | ******************************************************************************/ |
| 5568 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5569 | /****************************************************************************** |
| 5570 | ************************** Begin Proxy Locking ******************************** |
| 5571 | ** |
| 5572 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 5573 | ** other locking methods on secondary lock files. Proxy locking is a |
| 5574 | ** meta-layer over top of the primitive locking implemented above. For |
| 5575 | ** this reason, the division that implements of proxy locking is deferred |
| 5576 | ** until late in the file (here) after all of the other I/O methods have |
| 5577 | ** been defined - so that the primitive locking methods are available |
| 5578 | ** as services to help with the implementation of proxy locking. |
| 5579 | ** |
| 5580 | **** |
| 5581 | ** |
| 5582 | ** The default locking schemes in SQLite use byte-range locks on the |
| 5583 | ** database file to coordinate safe, concurrent access by multiple readers |
| 5584 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 5585 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 5586 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 5587 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 5588 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 5589 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 5590 | ** address in the shared range is taken for a SHARED lock, the entire |
| 5591 | ** shared range is taken for an EXCLUSIVE lock): |
| 5592 | ** |
| 5593 | ** PENDING_BYTE 0x40000000 |
| 5594 | ** RESERVED_BYTE 0x40000001 |
| 5595 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 5596 | ** |
| 5597 | ** This works well on the local file system, but shows a nearly 100x |
| 5598 | ** slowdown in read performance on AFP because the AFP client disables |
| 5599 | ** the read cache when byte-range locks are present. Enabling the read |
| 5600 | ** cache exposes a cache coherency problem that is present on all OS X |
| 5601 | ** supported network file systems. NFS and AFP both observe the |
| 5602 | ** close-to-open semantics for ensuring cache coherency |
| 5603 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 5604 | ** address the requirements for concurrent database access by multiple |
| 5605 | ** readers and writers |
| 5606 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 5607 | ** |
| 5608 | ** To address the performance and cache coherency issues, proxy file locking |
| 5609 | ** changes the way database access is controlled by limiting access to a |
| 5610 | ** single host at a time and moving file locks off of the database file |
| 5611 | ** and onto a proxy file on the local file system. |
| 5612 | ** |
| 5613 | ** |
| 5614 | ** Using proxy locks |
| 5615 | ** ----------------- |
| 5616 | ** |
| 5617 | ** C APIs |
| 5618 | ** |
| 5619 | ** sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE, |
| 5620 | ** <proxy_path> | ":auto:"); |
| 5621 | ** sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>); |
| 5622 | ** |
| 5623 | ** |
| 5624 | ** SQL pragmas |
| 5625 | ** |
| 5626 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 5627 | ** PRAGMA [database.]lock_proxy_file |
| 5628 | ** |
| 5629 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 5630 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 5631 | ** a proxy path based on the user's temp dir |
| 5632 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 5633 | ** actual proxy file name is generated from the name and path of the |
| 5634 | ** database file. For example: |
| 5635 | ** |
| 5636 | ** For database path "/Users/me/foo.db" |
| 5637 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 5638 | ** |
| 5639 | ** Once a lock proxy is configured for a database connection, it can not |
| 5640 | ** be removed, however it may be switched to a different proxy path via |
| 5641 | ** the above APIs (assuming the conch file is not being held by another |
| 5642 | ** connection or process). |
| 5643 | ** |
| 5644 | ** |
| 5645 | ** How proxy locking works |
| 5646 | ** ----------------------- |
| 5647 | ** |
| 5648 | ** Proxy file locking relies primarily on two new supporting files: |
| 5649 | ** |
| 5650 | ** * conch file to limit access to the database file to a single host |
| 5651 | ** at a time |
| 5652 | ** |
| 5653 | ** * proxy file to act as a proxy for the advisory locks normally |
| 5654 | ** taken on the database |
| 5655 | ** |
| 5656 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 5657 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 5658 | ** contents and comparing the host's unique host ID (see below) and lock |
| 5659 | ** proxy path against the values stored in the conch. The conch file is |
| 5660 | ** stored in the same directory as the database file and the file name |
| 5661 | ** is patterned after the database file name as ".<databasename>-conch". |
| 5662 | ** If the conch file does not exist, or it's contents do not match the |
| 5663 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 5664 | ** lock and the conch file contents is updated with the host ID and proxy |
| 5665 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 5666 | ** is held by another process (with a shared lock), the exclusive lock |
| 5667 | ** will fail and SQLITE_BUSY is returned. |
| 5668 | ** |
| 5669 | ** The proxy file - a single-byte file used for all advisory file locks |
| 5670 | ** normally taken on the database file. This allows for safe sharing |
| 5671 | ** of the database file for multiple readers and writers on the same |
| 5672 | ** host (the conch ensures that they all use the same local lock file). |
| 5673 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5674 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 5675 | ** only taken when the first request to lock database file is made. |
| 5676 | ** This matches the semantics of the traditional locking behavior, where |
| 5677 | ** opening a connection to a database file does not take a lock on it. |
| 5678 | ** The shared lock and an open file descriptor are maintained until |
| 5679 | ** the connection to the database is closed. |
| 5680 | ** |
| 5681 | ** The proxy file and the lock file are never deleted so they only need |
| 5682 | ** to be created the first time they are used. |
| 5683 | ** |
| 5684 | ** Configuration options |
| 5685 | ** --------------------- |
| 5686 | ** |
| 5687 | ** SQLITE_PREFER_PROXY_LOCKING |
| 5688 | ** |
| 5689 | ** Database files accessed on non-local file systems are |
| 5690 | ** automatically configured for proxy locking, lock files are |
| 5691 | ** named automatically using the same logic as |
| 5692 | ** PRAGMA lock_proxy_file=":auto:" |
| 5693 | ** |
| 5694 | ** SQLITE_PROXY_DEBUG |
| 5695 | ** |
| 5696 | ** Enables the logging of error messages during host id file |
| 5697 | ** retrieval and creation |
| 5698 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5699 | ** LOCKPROXYDIR |
| 5700 | ** |
| 5701 | ** Overrides the default directory used for lock proxy files that |
| 5702 | ** are named automatically via the ":auto:" setting |
| 5703 | ** |
| 5704 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 5705 | ** |
| 5706 | ** Permissions to use when creating a directory for storing the |
| 5707 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 5708 | ** |
| 5709 | ** |
| 5710 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 5711 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 5712 | ** force proxy locking to be used for every database file opened, and 0 |
| 5713 | ** will force automatic proxy locking to be disabled for all database |
| 5714 | ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or |
| 5715 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 5716 | */ |
| 5717 | |
| 5718 | /* |
| 5719 | ** Proxy locking is only available on MacOSX |
| 5720 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5721 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5722 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5723 | /* |
| 5724 | ** The proxyLockingContext has the path and file structures for the remote |
| 5725 | ** and local proxy files in it |
| 5726 | */ |
| 5727 | typedef struct proxyLockingContext proxyLockingContext; |
| 5728 | struct proxyLockingContext { |
| 5729 | unixFile *conchFile; /* Open conch file */ |
| 5730 | char *conchFilePath; /* Name of the conch file */ |
| 5731 | unixFile *lockProxy; /* Open proxy lock file */ |
| 5732 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 5733 | char *dbPath; /* Name of the open file */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5734 | int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5735 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 5736 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 5737 | }; |
| 5738 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5739 | /* |
| 5740 | ** The proxy lock file path for the database at dbPath is written into lPath, |
| 5741 | ** which must point to valid, writable memory large enough for a maxLen length |
| 5742 | ** file path. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5743 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5744 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 5745 | int len; |
| 5746 | int dbLen; |
| 5747 | int i; |
| 5748 | |
| 5749 | #ifdef LOCKPROXYDIR |
| 5750 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 5751 | #else |
| 5752 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 5753 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5754 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5755 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
| 5756 | lPath, errno, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5757 | return SQLITE_IOERR_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5758 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5759 | len = strlcat(lPath, "sqliteplocks", maxLen); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5760 | } |
| 5761 | # else |
| 5762 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 5763 | # endif |
| 5764 | #endif |
| 5765 | |
| 5766 | if( lPath[len-1]!='/' ){ |
| 5767 | len = strlcat(lPath, "/", maxLen); |
| 5768 | } |
| 5769 | |
| 5770 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5771 | dbLen = (int)strlen(dbPath); |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5772 | for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5773 | char c = dbPath[i]; |
| 5774 | lPath[i+len] = (c=='/')?'_':c; |
| 5775 | } |
| 5776 | lPath[i+len]='\0'; |
| 5777 | strlcat(lPath, ":auto:", maxLen); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5778 | OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5779 | return SQLITE_OK; |
| 5780 | } |
| 5781 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5782 | /* |
| 5783 | ** Creates the lock file and any missing directories in lockPath |
| 5784 | */ |
| 5785 | static int proxyCreateLockPath(const char *lockPath){ |
| 5786 | int i, len; |
| 5787 | char buf[MAXPATHLEN]; |
| 5788 | int start = 0; |
| 5789 | |
| 5790 | assert(lockPath!=NULL); |
| 5791 | /* try to create all the intermediate directories */ |
| 5792 | len = (int)strlen(lockPath); |
| 5793 | buf[0] = lockPath[0]; |
| 5794 | for( i=1; i<len; i++ ){ |
| 5795 | if( lockPath[i] == '/' && (i - start > 0) ){ |
| 5796 | /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 5797 | if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 5798 | || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 5799 | buf[i]='\0'; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 5800 | if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5801 | int err=errno; |
| 5802 | if( err!=EEXIST ) { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5803 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5804 | "'%s' proxy lock path=%s pid=%d\n", |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5805 | buf, strerror(err), lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5806 | return err; |
| 5807 | } |
| 5808 | } |
| 5809 | } |
| 5810 | start=i+1; |
| 5811 | } |
| 5812 | buf[i] = lockPath[i]; |
| 5813 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5814 | OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5815 | return 0; |
| 5816 | } |
| 5817 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5818 | /* |
| 5819 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 5820 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 5821 | ** |
| 5822 | ** The caller is responsible not only for closing the file descriptor |
| 5823 | ** but also for freeing the memory associated with the file descriptor. |
| 5824 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5825 | static int proxyCreateUnixFile( |
| 5826 | const char *path, /* path for the new unixFile */ |
| 5827 | unixFile **ppFile, /* unixFile created and returned by ref */ |
| 5828 | int islockfile /* if non zero missing dirs will be created */ |
| 5829 | ) { |
| 5830 | int fd = -1; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5831 | unixFile *pNew; |
| 5832 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5833 | int openFlags = O_RDWR | O_CREAT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5834 | sqlite3_vfs dummyVfs; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5835 | int terrno = 0; |
| 5836 | UnixUnusedFd *pUnused = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5837 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5838 | /* 1. first try to open/create the file |
| 5839 | ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 5840 | ** the parent directories and then try again. |
| 5841 | ** 3. if that fails, try to open the file read-only |
| 5842 | ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 5843 | */ |
| 5844 | pUnused = findReusableFd(path, openFlags); |
| 5845 | if( pUnused ){ |
| 5846 | fd = pUnused->fd; |
| 5847 | }else{ |
| 5848 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
| 5849 | if( !pUnused ){ |
| 5850 | return SQLITE_NOMEM; |
| 5851 | } |
| 5852 | } |
| 5853 | if( fd<0 ){ |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5854 | fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5855 | terrno = errno; |
| 5856 | if( fd<0 && errno==ENOENT && islockfile ){ |
| 5857 | if( proxyCreateLockPath(path) == SQLITE_OK ){ |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5858 | fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5859 | } |
| 5860 | } |
| 5861 | } |
| 5862 | if( fd<0 ){ |
| 5863 | openFlags = O_RDONLY; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5864 | fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5865 | terrno = errno; |
| 5866 | } |
| 5867 | if( fd<0 ){ |
| 5868 | if( islockfile ){ |
| 5869 | return SQLITE_BUSY; |
| 5870 | } |
| 5871 | switch (terrno) { |
| 5872 | case EACCES: |
| 5873 | return SQLITE_PERM; |
| 5874 | case EIO: |
| 5875 | return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 5876 | default: |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 5877 | return SQLITE_CANTOPEN_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5878 | } |
| 5879 | } |
| 5880 | |
| 5881 | pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew)); |
| 5882 | if( pNew==NULL ){ |
| 5883 | rc = SQLITE_NOMEM; |
| 5884 | goto end_create_proxy; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5885 | } |
| 5886 | memset(pNew, 0, sizeof(unixFile)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5887 | pNew->openFlags = openFlags; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 5888 | memset(&dummyVfs, 0, sizeof(dummyVfs)); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5889 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 5890 | dummyVfs.zName = "dummy"; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5891 | pUnused->fd = fd; |
| 5892 | pUnused->flags = openFlags; |
| 5893 | pNew->pUnused = pUnused; |
| 5894 | |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5895 | rc = fillInUnixFile(&dummyVfs, fd, 0, (sqlite3_file*)pNew, path, 0, 0, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5896 | if( rc==SQLITE_OK ){ |
| 5897 | *ppFile = pNew; |
| 5898 | return SQLITE_OK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5899 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5900 | end_create_proxy: |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5901 | robust_close(pNew, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5902 | sqlite3_free(pNew); |
| 5903 | sqlite3_free(pUnused); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5904 | return rc; |
| 5905 | } |
| 5906 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5907 | #ifdef SQLITE_TEST |
| 5908 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 5909 | int sqlite3_hostid_num = 0; |
| 5910 | #endif |
| 5911 | |
| 5912 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 5913 | |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5914 | /* Not always defined in the headers as it ought to be */ |
| 5915 | extern int gethostuuid(uuid_t id, const struct timespec *wait); |
| 5916 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5917 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 5918 | ** bytes of writable memory. |
| 5919 | */ |
| 5920 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5921 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 5922 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 5923 | #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\ |
| 5924 | && __MAC_OS_X_VERSION_MIN_REQUIRED<1050 |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 5925 | { |
| 5926 | static const struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
| 5927 | if( gethostuuid(pHostID, &timeout) ){ |
| 5928 | int err = errno; |
| 5929 | if( pError ){ |
| 5930 | *pError = err; |
| 5931 | } |
| 5932 | return SQLITE_IOERR; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5933 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5934 | } |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 5935 | #else |
| 5936 | UNUSED_PARAMETER(pError); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 5937 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5938 | #ifdef SQLITE_TEST |
| 5939 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 5940 | if( sqlite3_hostid_num != 0){ |
| 5941 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 5942 | } |
| 5943 | #endif |
| 5944 | |
| 5945 | return SQLITE_OK; |
| 5946 | } |
| 5947 | |
| 5948 | /* The conch file contains the header, host id and lock file path |
| 5949 | */ |
| 5950 | #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 5951 | #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 5952 | #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 5953 | #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 5954 | |
| 5955 | /* |
| 5956 | ** Takes an open conch file, copies the contents to a new path and then moves |
| 5957 | ** it back. The newly created file's file descriptor is assigned to the |
| 5958 | ** conch file structure and finally the original conch file descriptor is |
| 5959 | ** closed. Returns zero if successful. |
| 5960 | */ |
| 5961 | static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 5962 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5963 | unixFile *conchFile = pCtx->conchFile; |
| 5964 | char tPath[MAXPATHLEN]; |
| 5965 | char buf[PROXY_MAXCONCHLEN]; |
| 5966 | char *cPath = pCtx->conchFilePath; |
| 5967 | size_t readLen = 0; |
| 5968 | size_t pathLen = 0; |
| 5969 | char errmsg[64] = ""; |
| 5970 | int fd = -1; |
| 5971 | int rc = -1; |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5972 | UNUSED_PARAMETER(myHostID); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5973 | |
| 5974 | /* create a new path by replace the trailing '-conch' with '-break' */ |
| 5975 | pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 5976 | if( pathLen>MAXPATHLEN || pathLen<6 || |
| 5977 | (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5978 | sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5979 | goto end_breaklock; |
| 5980 | } |
| 5981 | /* read the conch content */ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 5982 | readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5983 | if( readLen<PROXY_PATHINDEX ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5984 | sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5985 | goto end_breaklock; |
| 5986 | } |
| 5987 | /* write it out to the temporary break file */ |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5988 | fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), |
| 5989 | SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5990 | if( fd<0 ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5991 | sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5992 | goto end_breaklock; |
| 5993 | } |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 5994 | if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5995 | sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5996 | goto end_breaklock; |
| 5997 | } |
| 5998 | if( rename(tPath, cPath) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5999 | sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6000 | goto end_breaklock; |
| 6001 | } |
| 6002 | rc = 0; |
| 6003 | fprintf(stderr, "broke stale lock on %s\n", cPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6004 | robust_close(pFile, conchFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6005 | conchFile->h = fd; |
| 6006 | conchFile->openFlags = O_RDWR | O_CREAT; |
| 6007 | |
| 6008 | end_breaklock: |
| 6009 | if( rc ){ |
| 6010 | if( fd>=0 ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 6011 | osUnlink(tPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6012 | robust_close(pFile, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6013 | } |
| 6014 | fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 6015 | } |
| 6016 | return rc; |
| 6017 | } |
| 6018 | |
| 6019 | /* Take the requested lock on the conch file and break a stale lock if the |
| 6020 | ** host id matches. |
| 6021 | */ |
| 6022 | static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 6023 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6024 | unixFile *conchFile = pCtx->conchFile; |
| 6025 | int rc = SQLITE_OK; |
| 6026 | int nTries = 0; |
| 6027 | struct timespec conchModTime; |
| 6028 | |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6029 | memset(&conchModTime, 0, sizeof(conchModTime)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6030 | do { |
| 6031 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 6032 | nTries ++; |
| 6033 | if( rc==SQLITE_BUSY ){ |
| 6034 | /* If the lock failed (busy): |
| 6035 | * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 6036 | * 2nd try: fail if the mod time changed or host id is different, wait |
| 6037 | * 10 sec and try again |
| 6038 | * 3rd try: break the lock unless the mod time has changed. |
| 6039 | */ |
| 6040 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6041 | if( osFstat(conchFile->h, &buf) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6042 | pFile->lastErrno = errno; |
| 6043 | return SQLITE_IOERR_LOCK; |
| 6044 | } |
| 6045 | |
| 6046 | if( nTries==1 ){ |
| 6047 | conchModTime = buf.st_mtimespec; |
| 6048 | usleep(500000); /* wait 0.5 sec and try the lock again*/ |
| 6049 | continue; |
| 6050 | } |
| 6051 | |
| 6052 | assert( nTries>1 ); |
| 6053 | if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 6054 | conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 6055 | return SQLITE_BUSY; |
| 6056 | } |
| 6057 | |
| 6058 | if( nTries==2 ){ |
| 6059 | char tBuf[PROXY_MAXCONCHLEN]; |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6060 | int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6061 | if( len<0 ){ |
| 6062 | pFile->lastErrno = errno; |
| 6063 | return SQLITE_IOERR_LOCK; |
| 6064 | } |
| 6065 | if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 6066 | /* don't break the lock if the host id doesn't match */ |
| 6067 | if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 6068 | return SQLITE_BUSY; |
| 6069 | } |
| 6070 | }else{ |
| 6071 | /* don't break the lock on short read or a version mismatch */ |
| 6072 | return SQLITE_BUSY; |
| 6073 | } |
| 6074 | usleep(10000000); /* wait 10 sec and try the lock again */ |
| 6075 | continue; |
| 6076 | } |
| 6077 | |
| 6078 | assert( nTries==3 ); |
| 6079 | if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 6080 | rc = SQLITE_OK; |
| 6081 | if( lockType==EXCLUSIVE_LOCK ){ |
| 6082 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 6083 | } |
| 6084 | if( !rc ){ |
| 6085 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 6086 | } |
| 6087 | } |
| 6088 | } |
| 6089 | } while( rc==SQLITE_BUSY && nTries<3 ); |
| 6090 | |
| 6091 | return rc; |
| 6092 | } |
| 6093 | |
| 6094 | /* 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] | 6095 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 6096 | ** lockPath means that the lockPath in the conch file will be used if the |
| 6097 | ** host IDs match, or a new lock path will be generated automatically |
| 6098 | ** and written to the conch file. |
| 6099 | */ |
| 6100 | static int proxyTakeConch(unixFile *pFile){ |
| 6101 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6102 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6103 | if( pCtx->conchHeld!=0 ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6104 | return SQLITE_OK; |
| 6105 | }else{ |
| 6106 | unixFile *conchFile = pCtx->conchFile; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6107 | uuid_t myHostID; |
| 6108 | int pError = 0; |
| 6109 | char readBuf[PROXY_MAXCONCHLEN]; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6110 | char lockPath[MAXPATHLEN]; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6111 | char *tempLockPath = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6112 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6113 | int createConch = 0; |
| 6114 | int hostIdMatch = 0; |
| 6115 | int readLen = 0; |
| 6116 | int tryOldLockPath = 0; |
| 6117 | int forceNewLockPath = 0; |
| 6118 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6119 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
| 6120 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6121 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6122 | rc = proxyGetHostID(myHostID, &pError); |
| 6123 | if( (rc&0xff)==SQLITE_IOERR ){ |
| 6124 | pFile->lastErrno = pError; |
| 6125 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6126 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6127 | rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6128 | if( rc!=SQLITE_OK ){ |
| 6129 | goto end_takeconch; |
| 6130 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6131 | /* read the existing conch file */ |
| 6132 | readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 6133 | if( readLen<0 ){ |
| 6134 | /* I/O error: lastErrno set by seekAndRead */ |
| 6135 | pFile->lastErrno = conchFile->lastErrno; |
| 6136 | rc = SQLITE_IOERR_READ; |
| 6137 | goto end_takeconch; |
| 6138 | }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 6139 | readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 6140 | /* a short read or version format mismatch means we need to create a new |
| 6141 | ** conch file. |
| 6142 | */ |
| 6143 | createConch = 1; |
| 6144 | } |
| 6145 | /* if the host id matches and the lock path already exists in the conch |
| 6146 | ** we'll try to use the path there, if we can't open that path, we'll |
| 6147 | ** retry with a new auto-generated path |
| 6148 | */ |
| 6149 | do { /* in case we need to try again for an :auto: named lock file */ |
| 6150 | |
| 6151 | if( !createConch && !forceNewLockPath ){ |
| 6152 | hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 6153 | PROXY_HOSTIDLEN); |
| 6154 | /* if the conch has data compare the contents */ |
| 6155 | if( !pCtx->lockProxyPath ){ |
| 6156 | /* for auto-named local lock file, just check the host ID and we'll |
| 6157 | ** use the local lock file path that's already in there |
| 6158 | */ |
| 6159 | if( hostIdMatch ){ |
| 6160 | size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 6161 | |
| 6162 | if( pathLen>=MAXPATHLEN ){ |
| 6163 | pathLen=MAXPATHLEN-1; |
| 6164 | } |
| 6165 | memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 6166 | lockPath[pathLen] = 0; |
| 6167 | tempLockPath = lockPath; |
| 6168 | tryOldLockPath = 1; |
| 6169 | /* create a copy of the lock path if the conch is taken */ |
| 6170 | goto end_takeconch; |
| 6171 | } |
| 6172 | }else if( hostIdMatch |
| 6173 | && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 6174 | readLen-PROXY_PATHINDEX) |
| 6175 | ){ |
| 6176 | /* conch host and lock path match */ |
| 6177 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6178 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6179 | } |
| 6180 | |
| 6181 | /* if the conch isn't writable and doesn't match, we can't take it */ |
| 6182 | if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 6183 | rc = SQLITE_BUSY; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6184 | goto end_takeconch; |
| 6185 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6186 | |
| 6187 | /* 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] | 6188 | if( !pCtx->lockProxyPath ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6189 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 6190 | tempLockPath = lockPath; |
| 6191 | /* create a copy of the lock path _only_ if the conch is taken */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6192 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6193 | |
| 6194 | /* update conch with host and path (this will fail if other process |
| 6195 | ** has a shared lock already), if the host id matches, use the big |
| 6196 | ** stick. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6197 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6198 | futimes(conchFile->h, NULL); |
| 6199 | if( hostIdMatch && !createConch ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 6200 | if( conchFile->pInode && conchFile->pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6201 | /* We are trying for an exclusive lock but another thread in this |
| 6202 | ** same process is still holding a shared lock. */ |
| 6203 | rc = SQLITE_BUSY; |
| 6204 | } else { |
| 6205 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6206 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6207 | }else{ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6208 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6209 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6210 | if( rc==SQLITE_OK ){ |
| 6211 | char writeBuffer[PROXY_MAXCONCHLEN]; |
| 6212 | int writeSize = 0; |
| 6213 | |
| 6214 | writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 6215 | memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 6216 | if( pCtx->lockProxyPath!=NULL ){ |
| 6217 | strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN); |
| 6218 | }else{ |
| 6219 | strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 6220 | } |
| 6221 | writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6222 | robust_ftruncate(conchFile->h, writeSize); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6223 | rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
| 6224 | fsync(conchFile->h); |
| 6225 | /* If we created a new conch file (not just updated the contents of a |
| 6226 | ** valid conch file), try to match the permissions of the database |
| 6227 | */ |
| 6228 | if( rc==SQLITE_OK && createConch ){ |
| 6229 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6230 | int err = osFstat(pFile->h, &buf); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6231 | if( err==0 ){ |
| 6232 | mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 6233 | S_IROTH|S_IWOTH); |
| 6234 | /* try to match the database file R/W permissions, ignore failure */ |
| 6235 | #ifndef SQLITE_PROXY_DEBUG |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6236 | osFchmod(conchFile->h, cmode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6237 | #else |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6238 | do{ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6239 | rc = osFchmod(conchFile->h, cmode); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6240 | }while( rc==(-1) && errno==EINTR ); |
| 6241 | if( rc!=0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6242 | int code = errno; |
| 6243 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 6244 | cmode, code, strerror(code)); |
| 6245 | } else { |
| 6246 | fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 6247 | } |
| 6248 | }else{ |
| 6249 | int code = errno; |
| 6250 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 6251 | err, code, strerror(code)); |
| 6252 | #endif |
| 6253 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6254 | } |
| 6255 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6256 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 6257 | |
| 6258 | end_takeconch: |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6259 | OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6260 | if( rc==SQLITE_OK && pFile->openFlags ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6261 | int fd; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6262 | if( pFile->h>=0 ){ |
drh | e84009f | 2011-03-02 17:54:32 +0000 | [diff] [blame] | 6263 | robust_close(pFile, pFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6264 | } |
| 6265 | pFile->h = -1; |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6266 | fd = robust_open(pCtx->dbPath, pFile->openFlags, |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6267 | SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6268 | OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6269 | if( fd>=0 ){ |
| 6270 | pFile->h = fd; |
| 6271 | }else{ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6272 | rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6273 | during locking */ |
| 6274 | } |
| 6275 | } |
| 6276 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 6277 | char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 6278 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 6279 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 6280 | /* we couldn't create the proxy lock file with the old lock file path |
| 6281 | ** so try again via auto-naming |
| 6282 | */ |
| 6283 | forceNewLockPath = 1; |
| 6284 | tryOldLockPath = 0; |
dan | 2b0ef47 | 2010-02-16 12:18:47 +0000 | [diff] [blame] | 6285 | continue; /* go back to the do {} while start point, try again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6286 | } |
| 6287 | } |
| 6288 | if( rc==SQLITE_OK ){ |
| 6289 | /* Need to make a copy of path if we extracted the value |
| 6290 | ** from the conch file or the path was allocated on the stack |
| 6291 | */ |
| 6292 | if( tempLockPath ){ |
| 6293 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 6294 | if( !pCtx->lockProxyPath ){ |
| 6295 | rc = SQLITE_NOMEM; |
| 6296 | } |
| 6297 | } |
| 6298 | } |
| 6299 | if( rc==SQLITE_OK ){ |
| 6300 | pCtx->conchHeld = 1; |
| 6301 | |
| 6302 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 6303 | afpLockingContext *afpCtx; |
| 6304 | afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 6305 | afpCtx->dbPath = pCtx->lockProxyPath; |
| 6306 | } |
| 6307 | } else { |
| 6308 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 6309 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6310 | OSTRACE(("TAKECONCH %d %s\n", conchFile->h, |
| 6311 | rc==SQLITE_OK?"ok":"failed")); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6312 | return rc; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6313 | } while (1); /* in case we need to retry the :auto: lock file - |
| 6314 | ** we should never get here except via the 'continue' call. */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6315 | } |
| 6316 | } |
| 6317 | |
| 6318 | /* |
| 6319 | ** If pFile holds a lock on a conch file, then release that lock. |
| 6320 | */ |
| 6321 | static int proxyReleaseConch(unixFile *pFile){ |
drh | 1c5bb4d | 2010-05-10 17:29:28 +0000 | [diff] [blame] | 6322 | int rc = SQLITE_OK; /* Subroutine return code */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6323 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 6324 | unixFile *conchFile; /* Name of the conch file */ |
| 6325 | |
| 6326 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6327 | conchFile = pCtx->conchFile; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6328 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6329 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6330 | getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6331 | if( pCtx->conchHeld>0 ){ |
| 6332 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 6333 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6334 | pCtx->conchHeld = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6335 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| 6336 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6337 | return rc; |
| 6338 | } |
| 6339 | |
| 6340 | /* |
| 6341 | ** Given the name of a database file, compute the name of its conch file. |
| 6342 | ** Store the conch filename in memory obtained from sqlite3_malloc(). |
| 6343 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 6344 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 6345 | ** |
| 6346 | ** The caller is responsible for ensuring that the allocated memory |
| 6347 | ** space is eventually freed. |
| 6348 | ** |
| 6349 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 6350 | */ |
| 6351 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 6352 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6353 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6354 | char *conchPath; /* buffer in which to construct conch name */ |
| 6355 | |
| 6356 | /* Allocate space for the conch filename and initialize the name to |
| 6357 | ** the name of the original database file. */ |
| 6358 | *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8); |
| 6359 | if( conchPath==0 ){ |
| 6360 | return SQLITE_NOMEM; |
| 6361 | } |
| 6362 | memcpy(conchPath, dbPath, len+1); |
| 6363 | |
| 6364 | /* now insert a "." before the last / character */ |
| 6365 | for( i=(len-1); i>=0; i-- ){ |
| 6366 | if( conchPath[i]=='/' ){ |
| 6367 | i++; |
| 6368 | break; |
| 6369 | } |
| 6370 | } |
| 6371 | conchPath[i]='.'; |
| 6372 | while ( i<len ){ |
| 6373 | conchPath[i+1]=dbPath[i]; |
| 6374 | i++; |
| 6375 | } |
| 6376 | |
| 6377 | /* append the "-conch" suffix to the file */ |
| 6378 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6379 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6380 | |
| 6381 | return SQLITE_OK; |
| 6382 | } |
| 6383 | |
| 6384 | |
| 6385 | /* Takes a fully configured proxy locking-style unix file and switches |
| 6386 | ** the local lock file path |
| 6387 | */ |
| 6388 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 6389 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 6390 | char *oldPath = pCtx->lockProxyPath; |
| 6391 | int rc = SQLITE_OK; |
| 6392 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6393 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6394 | return SQLITE_BUSY; |
| 6395 | } |
| 6396 | |
| 6397 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 6398 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 6399 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 6400 | return SQLITE_OK; |
| 6401 | }else{ |
| 6402 | unixFile *lockProxy = pCtx->lockProxy; |
| 6403 | pCtx->lockProxy=NULL; |
| 6404 | pCtx->conchHeld = 0; |
| 6405 | if( lockProxy!=NULL ){ |
| 6406 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 6407 | if( rc ) return rc; |
| 6408 | sqlite3_free(lockProxy); |
| 6409 | } |
| 6410 | sqlite3_free(oldPath); |
| 6411 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 6412 | } |
| 6413 | |
| 6414 | return rc; |
| 6415 | } |
| 6416 | |
| 6417 | /* |
| 6418 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 6419 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 6420 | ** |
| 6421 | ** This routine find the filename associated with pFile and writes it |
| 6422 | ** int dbPath. |
| 6423 | */ |
| 6424 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6425 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6426 | if( pFile->pMethod == &afpIoMethods ){ |
| 6427 | /* afp style keeps a reference to the db path in the filePath field |
| 6428 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6429 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6430 | strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN); |
| 6431 | } else |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6432 | #endif |
| 6433 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 6434 | /* dot lock style uses the locking context to store the dot lock |
| 6435 | ** file path */ |
| 6436 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 6437 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 6438 | }else{ |
| 6439 | /* all other styles use the locking context to store the db file path */ |
| 6440 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6441 | strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6442 | } |
| 6443 | return SQLITE_OK; |
| 6444 | } |
| 6445 | |
| 6446 | /* |
| 6447 | ** Takes an already filled in unix file and alters it so all file locking |
| 6448 | ** will be performed on the local proxy lock file. The following fields |
| 6449 | ** are preserved in the locking context so that they can be restored and |
| 6450 | ** the unix structure properly cleaned up at close time: |
| 6451 | ** ->lockingContext |
| 6452 | ** ->pMethod |
| 6453 | */ |
| 6454 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 6455 | proxyLockingContext *pCtx; |
| 6456 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 6457 | char *lockPath=NULL; |
| 6458 | int rc = SQLITE_OK; |
| 6459 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6460 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6461 | return SQLITE_BUSY; |
| 6462 | } |
| 6463 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 6464 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 6465 | lockPath=NULL; |
| 6466 | }else{ |
| 6467 | lockPath=(char *)path; |
| 6468 | } |
| 6469 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6470 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
| 6471 | (lockPath ? lockPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6472 | |
| 6473 | pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 6474 | if( pCtx==0 ){ |
| 6475 | return SQLITE_NOMEM; |
| 6476 | } |
| 6477 | memset(pCtx, 0, sizeof(*pCtx)); |
| 6478 | |
| 6479 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 6480 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6481 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 6482 | if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 6483 | /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 6484 | ** (c) the file system is read-only, then enable no-locking access. |
| 6485 | ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 6486 | ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 6487 | */ |
| 6488 | struct statfs fsInfo; |
| 6489 | struct stat conchInfo; |
| 6490 | int goLockless = 0; |
| 6491 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6492 | if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6493 | int err = errno; |
| 6494 | if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 6495 | goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 6496 | } |
| 6497 | } |
| 6498 | if( goLockless ){ |
| 6499 | pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 6500 | rc = SQLITE_OK; |
| 6501 | } |
| 6502 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6503 | } |
| 6504 | if( rc==SQLITE_OK && lockPath ){ |
| 6505 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 6506 | } |
| 6507 | |
| 6508 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6509 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 6510 | if( pCtx->dbPath==NULL ){ |
| 6511 | rc = SQLITE_NOMEM; |
| 6512 | } |
| 6513 | } |
| 6514 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6515 | /* all memory is allocated, proxys are created and assigned, |
| 6516 | ** switch the locking context and pMethod then return. |
| 6517 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6518 | pCtx->oldLockingContext = pFile->lockingContext; |
| 6519 | pFile->lockingContext = pCtx; |
| 6520 | pCtx->pOldMethod = pFile->pMethod; |
| 6521 | pFile->pMethod = &proxyIoMethods; |
| 6522 | }else{ |
| 6523 | if( pCtx->conchFile ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6524 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6525 | sqlite3_free(pCtx->conchFile); |
| 6526 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 6527 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6528 | sqlite3_free(pCtx->conchFilePath); |
| 6529 | sqlite3_free(pCtx); |
| 6530 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6531 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 6532 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6533 | return rc; |
| 6534 | } |
| 6535 | |
| 6536 | |
| 6537 | /* |
| 6538 | ** This routine handles sqlite3_file_control() calls that are specific |
| 6539 | ** to proxy locking. |
| 6540 | */ |
| 6541 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 6542 | switch( op ){ |
| 6543 | case SQLITE_GET_LOCKPROXYFILE: { |
| 6544 | unixFile *pFile = (unixFile*)id; |
| 6545 | if( pFile->pMethod == &proxyIoMethods ){ |
| 6546 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 6547 | proxyTakeConch(pFile); |
| 6548 | if( pCtx->lockProxyPath ){ |
| 6549 | *(const char **)pArg = pCtx->lockProxyPath; |
| 6550 | }else{ |
| 6551 | *(const char **)pArg = ":auto: (not held)"; |
| 6552 | } |
| 6553 | } else { |
| 6554 | *(const char **)pArg = NULL; |
| 6555 | } |
| 6556 | return SQLITE_OK; |
| 6557 | } |
| 6558 | case SQLITE_SET_LOCKPROXYFILE: { |
| 6559 | unixFile *pFile = (unixFile*)id; |
| 6560 | int rc = SQLITE_OK; |
| 6561 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 6562 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 6563 | if( isProxyStyle ){ |
| 6564 | /* turn off proxy locking - not supported */ |
| 6565 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 6566 | }else{ |
| 6567 | /* turn off proxy locking - already off - NOOP */ |
| 6568 | rc = SQLITE_OK; |
| 6569 | } |
| 6570 | }else{ |
| 6571 | const char *proxyPath = (const char *)pArg; |
| 6572 | if( isProxyStyle ){ |
| 6573 | proxyLockingContext *pCtx = |
| 6574 | (proxyLockingContext*)pFile->lockingContext; |
| 6575 | if( !strcmp(pArg, ":auto:") |
| 6576 | || (pCtx->lockProxyPath && |
| 6577 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 6578 | ){ |
| 6579 | rc = SQLITE_OK; |
| 6580 | }else{ |
| 6581 | rc = switchLockProxyPath(pFile, proxyPath); |
| 6582 | } |
| 6583 | }else{ |
| 6584 | /* turn on proxy file locking */ |
| 6585 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 6586 | } |
| 6587 | } |
| 6588 | return rc; |
| 6589 | } |
| 6590 | default: { |
| 6591 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 6592 | } |
| 6593 | } |
| 6594 | /*NOTREACHED*/ |
| 6595 | return SQLITE_ERROR; |
| 6596 | } |
| 6597 | |
| 6598 | /* |
| 6599 | ** Within this division (the proxying locking implementation) the procedures |
| 6600 | ** above this point are all utilities. The lock-related methods of the |
| 6601 | ** proxy-locking sqlite3_io_method object follow. |
| 6602 | */ |
| 6603 | |
| 6604 | |
| 6605 | /* |
| 6606 | ** This routine checks if there is a RESERVED lock held on the specified |
| 6607 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 6608 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 6609 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 6610 | */ |
| 6611 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 6612 | unixFile *pFile = (unixFile*)id; |
| 6613 | int rc = proxyTakeConch(pFile); |
| 6614 | if( rc==SQLITE_OK ){ |
| 6615 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6616 | if( pCtx->conchHeld>0 ){ |
| 6617 | unixFile *proxy = pCtx->lockProxy; |
| 6618 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 6619 | }else{ /* conchHeld < 0 is lockless */ |
| 6620 | pResOut=0; |
| 6621 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6622 | } |
| 6623 | return rc; |
| 6624 | } |
| 6625 | |
| 6626 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6627 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6628 | ** of the following: |
| 6629 | ** |
| 6630 | ** (1) SHARED_LOCK |
| 6631 | ** (2) RESERVED_LOCK |
| 6632 | ** (3) PENDING_LOCK |
| 6633 | ** (4) EXCLUSIVE_LOCK |
| 6634 | ** |
| 6635 | ** Sometimes when requesting one lock state, additional lock states |
| 6636 | ** are inserted in between. The locking might fail on one of the later |
| 6637 | ** transitions leaving the lock state different from what it started but |
| 6638 | ** still short of its goal. The following chart shows the allowed |
| 6639 | ** transitions and the inserted intermediate states: |
| 6640 | ** |
| 6641 | ** UNLOCKED -> SHARED |
| 6642 | ** SHARED -> RESERVED |
| 6643 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 6644 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 6645 | ** PENDING -> EXCLUSIVE |
| 6646 | ** |
| 6647 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 6648 | ** routine to lower a locking level. |
| 6649 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6650 | static int proxyLock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6651 | unixFile *pFile = (unixFile*)id; |
| 6652 | int rc = proxyTakeConch(pFile); |
| 6653 | if( rc==SQLITE_OK ){ |
| 6654 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6655 | if( pCtx->conchHeld>0 ){ |
| 6656 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6657 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); |
| 6658 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6659 | }else{ |
| 6660 | /* conchHeld < 0 is lockless */ |
| 6661 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6662 | } |
| 6663 | return rc; |
| 6664 | } |
| 6665 | |
| 6666 | |
| 6667 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6668 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6669 | ** must be either NO_LOCK or SHARED_LOCK. |
| 6670 | ** |
| 6671 | ** If the locking level of the file descriptor is already at or below |
| 6672 | ** the requested locking level, this routine is a no-op. |
| 6673 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6674 | static int proxyUnlock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6675 | unixFile *pFile = (unixFile*)id; |
| 6676 | int rc = proxyTakeConch(pFile); |
| 6677 | if( rc==SQLITE_OK ){ |
| 6678 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6679 | if( pCtx->conchHeld>0 ){ |
| 6680 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6681 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); |
| 6682 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6683 | }else{ |
| 6684 | /* conchHeld < 0 is lockless */ |
| 6685 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6686 | } |
| 6687 | return rc; |
| 6688 | } |
| 6689 | |
| 6690 | /* |
| 6691 | ** Close a file that uses proxy locks. |
| 6692 | */ |
| 6693 | static int proxyClose(sqlite3_file *id) { |
| 6694 | if( id ){ |
| 6695 | unixFile *pFile = (unixFile*)id; |
| 6696 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6697 | unixFile *lockProxy = pCtx->lockProxy; |
| 6698 | unixFile *conchFile = pCtx->conchFile; |
| 6699 | int rc = SQLITE_OK; |
| 6700 | |
| 6701 | if( lockProxy ){ |
| 6702 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 6703 | if( rc ) return rc; |
| 6704 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 6705 | if( rc ) return rc; |
| 6706 | sqlite3_free(lockProxy); |
| 6707 | pCtx->lockProxy = 0; |
| 6708 | } |
| 6709 | if( conchFile ){ |
| 6710 | if( pCtx->conchHeld ){ |
| 6711 | rc = proxyReleaseConch(pFile); |
| 6712 | if( rc ) return rc; |
| 6713 | } |
| 6714 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 6715 | if( rc ) return rc; |
| 6716 | sqlite3_free(conchFile); |
| 6717 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 6718 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6719 | sqlite3_free(pCtx->conchFilePath); |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 6720 | sqlite3DbFree(0, pCtx->dbPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6721 | /* restore the original locking context and pMethod then close it */ |
| 6722 | pFile->lockingContext = pCtx->oldLockingContext; |
| 6723 | pFile->pMethod = pCtx->pOldMethod; |
| 6724 | sqlite3_free(pCtx); |
| 6725 | return pFile->pMethod->xClose(id); |
| 6726 | } |
| 6727 | return SQLITE_OK; |
| 6728 | } |
| 6729 | |
| 6730 | |
| 6731 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6732 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6733 | /* |
| 6734 | ** The proxy locking style is intended for use with AFP filesystems. |
| 6735 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 6736 | ** restricted to MacOSX. |
| 6737 | ** |
| 6738 | ** |
| 6739 | ******************* End of the proxy lock implementation ********************** |
| 6740 | ******************************************************************************/ |
| 6741 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6742 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6743 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6744 | ** |
| 6745 | ** This routine registers all VFS implementations for unix-like operating |
| 6746 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 6747 | ** should be the only routines in this file that are visible from other |
| 6748 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6749 | ** |
| 6750 | ** This routine is called once during SQLite initialization and by a |
| 6751 | ** single thread. The memory allocation and mutex subsystems have not |
| 6752 | ** necessarily been initialized when this routine is called, and so they |
| 6753 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6754 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6755 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6756 | /* |
| 6757 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6758 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 6759 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 6760 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 6761 | ** and so we have to go through the intermediate pointer to avoid problems |
| 6762 | ** when compiling with -pedantic-errors on GCC.) |
| 6763 | ** |
| 6764 | ** 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] | 6765 | ** finder-function. The finder-function returns a pointer to the |
| 6766 | ** sqlite_io_methods object that implements the desired locking |
| 6767 | ** behaviors. See the division above that contains the IOMETHODS |
| 6768 | ** macro for addition information on finder-functions. |
| 6769 | ** |
| 6770 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 6771 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 6772 | ** more than that; it looks at the filesystem type that hosts the |
| 6773 | ** database file and tries to choose an locking method appropriate for |
| 6774 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6775 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6776 | #define UNIXVFS(VFSNAME, FINDER) { \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6777 | 3, /* iVersion */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6778 | sizeof(unixFile), /* szOsFile */ \ |
| 6779 | MAX_PATHNAME, /* mxPathname */ \ |
| 6780 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6781 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6782 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6783 | unixOpen, /* xOpen */ \ |
| 6784 | unixDelete, /* xDelete */ \ |
| 6785 | unixAccess, /* xAccess */ \ |
| 6786 | unixFullPathname, /* xFullPathname */ \ |
| 6787 | unixDlOpen, /* xDlOpen */ \ |
| 6788 | unixDlError, /* xDlError */ \ |
| 6789 | unixDlSym, /* xDlSym */ \ |
| 6790 | unixDlClose, /* xDlClose */ \ |
| 6791 | unixRandomness, /* xRandomness */ \ |
| 6792 | unixSleep, /* xSleep */ \ |
| 6793 | unixCurrentTime, /* xCurrentTime */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6794 | unixGetLastError, /* xGetLastError */ \ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6795 | unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6796 | unixSetSystemCall, /* xSetSystemCall */ \ |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 6797 | unixGetSystemCall, /* xGetSystemCall */ \ |
| 6798 | unixNextSystemCall, /* xNextSystemCall */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6799 | } |
| 6800 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6801 | /* |
| 6802 | ** All default VFSes for unix are contained in the following array. |
| 6803 | ** |
| 6804 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 6805 | ** by the SQLite core when the VFS is registered. So the following |
| 6806 | ** array cannot be const. |
| 6807 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6808 | static sqlite3_vfs aVfs[] = { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6809 | #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__)) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6810 | UNIXVFS("unix", autolockIoFinder ), |
| 6811 | #else |
| 6812 | UNIXVFS("unix", posixIoFinder ), |
| 6813 | #endif |
| 6814 | UNIXVFS("unix-none", nolockIoFinder ), |
| 6815 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 6816 | UNIXVFS("unix-excl", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6817 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6818 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6819 | #endif |
| 6820 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6821 | UNIXVFS("unix-posix", posixIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6822 | #if !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6823 | UNIXVFS("unix-flock", flockIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6824 | #endif |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6825 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6826 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6827 | UNIXVFS("unix-afp", afpIoFinder ), |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6828 | UNIXVFS("unix-nfs", nfsIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6829 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6830 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6831 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6832 | unsigned int i; /* Loop counter */ |
| 6833 | |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 6834 | /* Double-check that the aSyscall[] array has been constructed |
| 6835 | ** correctly. See ticket [bb3a86e890c8e96ab] */ |
drh | 1da88f0 | 2011-12-17 16:09:16 +0000 | [diff] [blame] | 6836 | assert( ArraySize(aSyscall)==21 ); |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 6837 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6838 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6839 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6840 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6841 | } |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6842 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6843 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6844 | |
| 6845 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6846 | ** Shutdown the operating system interface. |
| 6847 | ** |
| 6848 | ** Some operating systems might need to do some cleanup in this routine, |
| 6849 | ** to release dynamically allocated objects. But not on unix. |
| 6850 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6851 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6852 | int sqlite3_os_end(void){ |
| 6853 | return SQLITE_OK; |
| 6854 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 6855 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 6856 | #endif /* SQLITE_OS_UNIX */ |