drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2004 May 22 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 13 | ** This file contains the VFS implementation for unix-like operating systems |
| 14 | ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others. |
danielk1977 | 822a516 | 2008-05-16 04:51:54 +0000 | [diff] [blame] | 15 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 16 | ** There are actually several different VFS implementations in this file. |
| 17 | ** The differences are in the way that file locking is done. The default |
| 18 | ** implementation uses Posix Advisory Locks. Alternative implementations |
| 19 | ** use flock(), dot-files, various proprietary locking schemas, or simply |
| 20 | ** skip locking all together. |
| 21 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 22 | ** This source file is organized into divisions where the logic for various |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 23 | ** subfunctions is contained within the appropriate division. PLEASE |
| 24 | ** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed |
| 25 | ** in the correct division and should be clearly labeled. |
| 26 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 27 | ** The layout of divisions is as follows: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 28 | ** |
| 29 | ** * General-purpose declarations and utility functions. |
| 30 | ** * Unique file ID logic used by VxWorks. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 31 | ** * Various locking primitive implementations (all except proxy locking): |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 32 | ** + for Posix Advisory Locks |
| 33 | ** + for no-op locks |
| 34 | ** + for dot-file locks |
| 35 | ** + for flock() locking |
| 36 | ** + for named semaphore locks (VxWorks only) |
| 37 | ** + for AFP filesystem locks (MacOSX only) |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 38 | ** * sqlite3_file methods not associated with locking. |
| 39 | ** * Definitions of sqlite3_io_methods objects for all locking |
| 40 | ** methods plus "finder" functions for each locking method. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 41 | ** * sqlite3_vfs method implementations. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 42 | ** * Locking primitives for the proxy uber-locking-method. (MacOSX only) |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 43 | ** * Definitions of sqlite3_vfs objects for all locking methods |
| 44 | ** plus implementations of sqlite3_os_init() and sqlite3_os_end(). |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 45 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 46 | #include "sqliteInt.h" |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 47 | #if SQLITE_OS_UNIX /* This file is used on unix only */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 48 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 49 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 50 | ** There are various methods for file locking used for concurrency |
| 51 | ** control: |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 52 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 53 | ** 1. POSIX locking (the default), |
| 54 | ** 2. No locking, |
| 55 | ** 3. Dot-file locking, |
| 56 | ** 4. flock() locking, |
| 57 | ** 5. AFP locking (OSX only), |
| 58 | ** 6. Named POSIX semaphores (VXWorks only), |
| 59 | ** 7. proxy locking. (OSX only) |
| 60 | ** |
| 61 | ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE |
| 62 | ** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic |
| 63 | ** selection of the appropriate locking style based on the filesystem |
| 64 | ** where the database is located. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 65 | */ |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 66 | #if !defined(SQLITE_ENABLE_LOCKING_STYLE) |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 67 | # if defined(__APPLE__) |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 68 | # define SQLITE_ENABLE_LOCKING_STYLE 1 |
| 69 | # else |
| 70 | # define SQLITE_ENABLE_LOCKING_STYLE 0 |
| 71 | # endif |
| 72 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 73 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 74 | /* |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 75 | ** Define the OS_VXWORKS pre-processor macro to 1 if building on |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 76 | ** vxworks, or 0 otherwise. |
| 77 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 78 | #ifndef OS_VXWORKS |
| 79 | # if defined(__RTP__) || defined(_WRS_KERNEL) |
| 80 | # define OS_VXWORKS 1 |
| 81 | # else |
| 82 | # define OS_VXWORKS 0 |
| 83 | # endif |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 84 | #endif |
| 85 | |
| 86 | /* |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 87 | ** These #defines should enable >2GB file support on Posix if the |
| 88 | ** underlying operating system supports it. If the OS lacks |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 89 | ** large file support, these should be no-ops. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 90 | ** |
| 91 | ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch |
| 92 | ** on the compiler command line. This is necessary if you are compiling |
| 93 | ** on a recent machine (ex: RedHat 7.2) but you want your code to work |
| 94 | ** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2 |
| 95 | ** without this option, LFS is enable. But LFS does not exist in the kernel |
| 96 | ** in RedHat 6.0, so the code won't work. Hence, for maximum binary |
| 97 | ** portability you should omit LFS. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 98 | ** |
| 99 | ** The previous paragraph was written in 2005. (This paragraph is written |
| 100 | ** on 2008-11-28.) These days, all Linux kernels support large files, so |
| 101 | ** you should probably leave LFS enabled. But some embedded platforms might |
| 102 | ** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 103 | */ |
| 104 | #ifndef SQLITE_DISABLE_LFS |
| 105 | # define _LARGE_FILE 1 |
| 106 | # ifndef _FILE_OFFSET_BITS |
| 107 | # define _FILE_OFFSET_BITS 64 |
| 108 | # endif |
| 109 | # define _LARGEFILE_SOURCE 1 |
| 110 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 111 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 112 | /* |
| 113 | ** standard include files. |
| 114 | */ |
| 115 | #include <sys/types.h> |
| 116 | #include <sys/stat.h> |
| 117 | #include <fcntl.h> |
| 118 | #include <unistd.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 119 | #include <time.h> |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 120 | #include <sys/time.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 121 | #include <errno.h> |
drh | b469f46 | 2010-12-22 21:48:50 +0000 | [diff] [blame] | 122 | #ifndef SQLITE_OMIT_WAL |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 123 | #include <sys/mman.h> |
drh | b469f46 | 2010-12-22 21:48:50 +0000 | [diff] [blame] | 124 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 125 | |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 126 | #if SQLITE_ENABLE_LOCKING_STYLE |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 127 | # include <sys/ioctl.h> |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 128 | # if OS_VXWORKS |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 129 | # include <semaphore.h> |
| 130 | # include <limits.h> |
| 131 | # else |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 132 | # include <sys/file.h> |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 133 | # include <sys/param.h> |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 134 | # endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 135 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 136 | |
drh | f8b4d8c | 2010-03-05 13:53:22 +0000 | [diff] [blame] | 137 | #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS) |
drh | 84a2bf6 | 2010-03-05 13:41:06 +0000 | [diff] [blame] | 138 | # include <sys/mount.h> |
| 139 | #endif |
| 140 | |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 141 | #ifdef HAVE_UTIME |
| 142 | # include <utime.h> |
| 143 | #endif |
| 144 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 145 | /* |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 146 | ** Allowed values of unixFile.fsFlags |
| 147 | */ |
| 148 | #define SQLITE_FSFLAGS_IS_MSDOS 0x1 |
| 149 | |
| 150 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 151 | ** If we are to be thread-safe, include the pthreads header and define |
| 152 | ** the SQLITE_UNIX_THREADS macro. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 153 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 154 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 155 | # include <pthread.h> |
| 156 | # define SQLITE_UNIX_THREADS 1 |
| 157 | #endif |
| 158 | |
| 159 | /* |
| 160 | ** Default permissions when creating a new file |
| 161 | */ |
| 162 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 163 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 164 | #endif |
| 165 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 166 | /* |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 167 | ** Default permissions when creating auto proxy dir |
| 168 | */ |
| 169 | #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 170 | # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 |
| 171 | #endif |
| 172 | |
| 173 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 174 | ** Maximum supported path-length. |
| 175 | */ |
| 176 | #define MAX_PATHNAME 512 |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 177 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 178 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 179 | ** Only set the lastErrno if the error code is a real error and not |
| 180 | ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 181 | */ |
| 182 | #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |
| 183 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 184 | /* Forward references */ |
| 185 | typedef struct unixShm unixShm; /* Connection shared memory */ |
| 186 | typedef struct unixShmNode unixShmNode; /* Shared memory instance */ |
| 187 | typedef struct unixInodeInfo unixInodeInfo; /* An i-node */ |
| 188 | typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 189 | |
| 190 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 191 | ** Sometimes, after a file handle is closed by SQLite, the file descriptor |
| 192 | ** cannot be closed immediately. In these cases, instances of the following |
| 193 | ** structure are used to store the file descriptor while waiting for an |
| 194 | ** opportunity to either close or reuse it. |
| 195 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 196 | struct UnixUnusedFd { |
| 197 | int fd; /* File descriptor to close */ |
| 198 | int flags; /* Flags this file descriptor was opened with */ |
| 199 | UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ |
| 200 | }; |
| 201 | |
| 202 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 203 | ** The unixFile structure is subclass of sqlite3_file specific to the unix |
| 204 | ** VFS implementations. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 205 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 206 | typedef struct unixFile unixFile; |
| 207 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 208 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 209 | unixInodeInfo *pInode; /* Info about locks on this inode */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 210 | int h; /* The file descriptor */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 211 | unsigned char eFileLock; /* The type of lock held on this fd */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 212 | unsigned char ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 213 | int lastErrno; /* The unix errno from last I/O error */ |
| 214 | void *lockingContext; /* Locking style specific state */ |
| 215 | UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 216 | const char *zPath; /* Name of the file */ |
| 217 | unixShm *pShm; /* Shared memory segment information */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 218 | int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 219 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 220 | int openFlags; /* The flags specified at open() */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 221 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 222 | #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 223 | unsigned fsFlags; /* cached details from statfs() */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 224 | #endif |
| 225 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 226 | int isDelete; /* Delete on close if true */ |
| 227 | struct vxworksFileId *pId; /* Unique file ID */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 228 | #endif |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 229 | #ifndef NDEBUG |
| 230 | /* The next group of variables are used to track whether or not the |
| 231 | ** transaction counter in bytes 24-27 of database files are updated |
| 232 | ** whenever any part of the database changes. An assertion fault will |
| 233 | ** occur if a file is updated without also updating the transaction |
| 234 | ** counter. This test is made to avoid new problems similar to the |
| 235 | ** one described by ticket #3584. |
| 236 | */ |
| 237 | unsigned char transCntrChng; /* True if the transaction counter changed */ |
| 238 | unsigned char dbUpdate; /* True if any part of database file changed */ |
| 239 | unsigned char inNormalWrite; /* True if in a normal write operation */ |
| 240 | #endif |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 241 | #ifdef SQLITE_TEST |
| 242 | /* In test mode, increase the size of this structure a bit so that |
| 243 | ** it is larger than the struct CrashFile defined in test6.c. |
| 244 | */ |
| 245 | char aPadding[32]; |
| 246 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 247 | }; |
| 248 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 249 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 250 | ** Allowed values for the unixFile.ctrlFlags bitmask: |
| 251 | */ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 252 | #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ |
| 253 | #define UNIXFILE_RDONLY 0x02 /* Connection is read only */ |
| 254 | #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ |
dan | ee140c4 | 2011-08-25 13:46:32 +0000 | [diff] [blame] | 255 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 256 | # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ |
| 257 | #else |
| 258 | # define UNIXFILE_DIRSYNC 0x00 |
| 259 | #endif |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 260 | |
| 261 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 262 | ** Include code that is common to all os_*.c files |
| 263 | */ |
| 264 | #include "os_common.h" |
| 265 | |
| 266 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 267 | ** Define various macros that are missing from some systems. |
| 268 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 269 | #ifndef O_LARGEFILE |
| 270 | # define O_LARGEFILE 0 |
| 271 | #endif |
| 272 | #ifdef SQLITE_DISABLE_LFS |
| 273 | # undef O_LARGEFILE |
| 274 | # define O_LARGEFILE 0 |
| 275 | #endif |
| 276 | #ifndef O_NOFOLLOW |
| 277 | # define O_NOFOLLOW 0 |
| 278 | #endif |
| 279 | #ifndef O_BINARY |
| 280 | # define O_BINARY 0 |
| 281 | #endif |
| 282 | |
| 283 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 284 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 285 | ** testing and debugging only. |
| 286 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 287 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 288 | #define threadid pthread_self() |
| 289 | #else |
| 290 | #define threadid 0 |
| 291 | #endif |
| 292 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 293 | /* |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 294 | ** Different Unix systems declare open() in different ways. Same use |
| 295 | ** open(const char*,int,mode_t). Others use open(const char*,int,...). |
| 296 | ** The difference is important when using a pointer to the function. |
| 297 | ** |
| 298 | ** The safest way to deal with the problem is to always use this wrapper |
| 299 | ** which always has the same well-defined interface. |
| 300 | */ |
| 301 | static int posixOpen(const char *zFile, int flags, int mode){ |
| 302 | return open(zFile, flags, mode); |
| 303 | } |
| 304 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 305 | /* Forward reference */ |
| 306 | static int openDirectory(const char*, int*); |
| 307 | |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 308 | /* |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 309 | ** Many system calls are accessed through pointer-to-functions so that |
| 310 | ** they may be overridden at runtime to facilitate fault injection during |
| 311 | ** testing and sandboxing. The following array holds the names and pointers |
| 312 | ** to all overrideable system calls. |
| 313 | */ |
| 314 | static struct unix_syscall { |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 315 | const char *zName; /* Name of the sytem call */ |
| 316 | sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ |
| 317 | sqlite3_syscall_ptr pDefault; /* Default value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 318 | } aSyscall[] = { |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 319 | { "open", (sqlite3_syscall_ptr)posixOpen, 0 }, |
| 320 | #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent) |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 321 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 322 | { "close", (sqlite3_syscall_ptr)close, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 323 | #define osClose ((int(*)(int))aSyscall[1].pCurrent) |
| 324 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 325 | { "access", (sqlite3_syscall_ptr)access, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 326 | #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent) |
| 327 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 328 | { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 329 | #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent) |
| 330 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 331 | { "stat", (sqlite3_syscall_ptr)stat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 332 | #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) |
| 333 | |
| 334 | /* |
| 335 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 336 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 337 | ** that always succeeds. This means that locking does not occur under |
| 338 | ** DJGPP. But it is DOS - what did you expect? |
| 339 | */ |
| 340 | #ifdef __DJGPP__ |
| 341 | { "fstat", 0, 0 }, |
| 342 | #define osFstat(a,b,c) 0 |
| 343 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 344 | { "fstat", (sqlite3_syscall_ptr)fstat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 345 | #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent) |
| 346 | #endif |
| 347 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 348 | { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 349 | #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent) |
| 350 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 351 | { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 352 | #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 353 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 354 | { "read", (sqlite3_syscall_ptr)read, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 355 | #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) |
| 356 | |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 357 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 358 | { "pread", (sqlite3_syscall_ptr)pread, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 359 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 360 | { "pread", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 361 | #endif |
| 362 | #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) |
| 363 | |
| 364 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 365 | { "pread64", (sqlite3_syscall_ptr)pread64, 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 | { "pread64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 368 | #endif |
| 369 | #define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent) |
| 370 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 371 | { "write", (sqlite3_syscall_ptr)write, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 372 | #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) |
| 373 | |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 374 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 375 | { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 376 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 377 | { "pwrite", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 378 | #endif |
| 379 | #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 380 | aSyscall[12].pCurrent) |
| 381 | |
| 382 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 383 | { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 384 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 385 | { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 386 | #endif |
| 387 | #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 388 | aSyscall[13].pCurrent) |
| 389 | |
drh | a6c4749 | 2011-04-11 18:35:09 +0000 | [diff] [blame] | 390 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 391 | { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 392 | #else |
| 393 | { "fchmod", (sqlite3_syscall_ptr)0, 0 }, |
drh | a6c4749 | 2011-04-11 18:35:09 +0000 | [diff] [blame] | 394 | #endif |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 395 | #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 396 | |
| 397 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 398 | { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 399 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 400 | { "fallocate", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 401 | #endif |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 402 | #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 403 | |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 404 | { "unlink", (sqlite3_syscall_ptr)unlink, 0 }, |
| 405 | #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent) |
| 406 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 407 | { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 }, |
| 408 | #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent) |
| 409 | |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 410 | }; /* End of the overrideable system calls */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 411 | |
| 412 | /* |
| 413 | ** This is the xSetSystemCall() method of sqlite3_vfs for all of the |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 414 | ** "unix" VFSes. Return SQLITE_OK opon successfully updating the |
| 415 | ** system call pointer, or SQLITE_NOTFOUND if there is no configurable |
| 416 | ** system call named zName. |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 417 | */ |
| 418 | static int unixSetSystemCall( |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 419 | sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ |
| 420 | const char *zName, /* Name of system call to override */ |
| 421 | sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 422 | ){ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 423 | unsigned int i; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 424 | int rc = SQLITE_NOTFOUND; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 425 | |
| 426 | UNUSED_PARAMETER(pNotUsed); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 427 | if( zName==0 ){ |
| 428 | /* If no zName is given, restore all system calls to their default |
| 429 | ** settings and return NULL |
| 430 | */ |
dan | 51438a7 | 2011-04-02 17:00:47 +0000 | [diff] [blame] | 431 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 432 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 433 | if( aSyscall[i].pDefault ){ |
| 434 | aSyscall[i].pCurrent = aSyscall[i].pDefault; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | }else{ |
| 438 | /* If zName is specified, operate on only the one system call |
| 439 | ** specified. |
| 440 | */ |
| 441 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 442 | if( strcmp(zName, aSyscall[i].zName)==0 ){ |
| 443 | if( aSyscall[i].pDefault==0 ){ |
| 444 | aSyscall[i].pDefault = aSyscall[i].pCurrent; |
| 445 | } |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 446 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 447 | if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault; |
| 448 | aSyscall[i].pCurrent = pNewFunc; |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | return rc; |
| 454 | } |
| 455 | |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 456 | /* |
| 457 | ** Return the value of a system call. Return NULL if zName is not a |
| 458 | ** recognized system call name. NULL is also returned if the system call |
| 459 | ** is currently undefined. |
| 460 | */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 461 | static sqlite3_syscall_ptr unixGetSystemCall( |
| 462 | sqlite3_vfs *pNotUsed, |
| 463 | const char *zName |
| 464 | ){ |
| 465 | unsigned int i; |
| 466 | |
| 467 | UNUSED_PARAMETER(pNotUsed); |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 468 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 469 | if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent; |
| 470 | } |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | ** Return the name of the first system call after zName. If zName==NULL |
| 476 | ** then return the name of the first system call. Return NULL if zName |
| 477 | ** is the last system call or if zName is not the name of a valid |
| 478 | ** system call. |
| 479 | */ |
| 480 | static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){ |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 481 | int i = -1; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 482 | |
| 483 | UNUSED_PARAMETER(p); |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 484 | if( zName ){ |
| 485 | for(i=0; i<ArraySize(aSyscall)-1; i++){ |
| 486 | if( strcmp(zName, aSyscall[i].zName)==0 ) break; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 487 | } |
| 488 | } |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 489 | for(i++; i<ArraySize(aSyscall); i++){ |
| 490 | if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 491 | } |
| 492 | return 0; |
| 493 | } |
| 494 | |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 495 | /* |
| 496 | ** Retry open() calls that fail due to EINTR |
| 497 | */ |
| 498 | static int robust_open(const char *z, int f, int m){ |
| 499 | int rc; |
| 500 | do{ rc = osOpen(z,f,m); }while( rc<0 && errno==EINTR ); |
| 501 | return rc; |
| 502 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 503 | |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 504 | /* |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 505 | ** Helper functions to obtain and relinquish the global mutex. The |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 506 | ** global mutex is used to protect the unixInodeInfo and |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 507 | ** vxworksFileId objects used by this file, all of which may be |
| 508 | ** shared by multiple threads. |
| 509 | ** |
| 510 | ** Function unixMutexHeld() is used to assert() that the global mutex |
| 511 | ** is held when required. This function is only used as part of assert() |
| 512 | ** statements. e.g. |
| 513 | ** |
| 514 | ** unixEnterMutex() |
| 515 | ** assert( unixMutexHeld() ); |
| 516 | ** unixEnterLeave() |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 517 | */ |
| 518 | static void unixEnterMutex(void){ |
| 519 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 520 | } |
| 521 | static void unixLeaveMutex(void){ |
| 522 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 523 | } |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 524 | #ifdef SQLITE_DEBUG |
| 525 | static int unixMutexHeld(void) { |
| 526 | return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 527 | } |
| 528 | #endif |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 529 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 530 | |
| 531 | #ifdef SQLITE_DEBUG |
| 532 | /* |
| 533 | ** Helper function for printing out trace information from debugging |
| 534 | ** binaries. This returns the string represetation of the supplied |
| 535 | ** integer lock-type. |
| 536 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 537 | static const char *azFileLock(int eFileLock){ |
| 538 | switch( eFileLock ){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 539 | case NO_LOCK: return "NONE"; |
| 540 | case SHARED_LOCK: return "SHARED"; |
| 541 | case RESERVED_LOCK: return "RESERVED"; |
| 542 | case PENDING_LOCK: return "PENDING"; |
| 543 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 544 | } |
| 545 | return "ERROR"; |
| 546 | } |
| 547 | #endif |
| 548 | |
| 549 | #ifdef SQLITE_LOCK_TRACE |
| 550 | /* |
| 551 | ** Print out information about all locking operations. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 552 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 553 | ** This routine is used for troubleshooting locks on multithreaded |
| 554 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 555 | ** command-line option on the compiler. This code is normally |
| 556 | ** turned off. |
| 557 | */ |
| 558 | static int lockTrace(int fd, int op, struct flock *p){ |
| 559 | char *zOpName, *zType; |
| 560 | int s; |
| 561 | int savedErrno; |
| 562 | if( op==F_GETLK ){ |
| 563 | zOpName = "GETLK"; |
| 564 | }else if( op==F_SETLK ){ |
| 565 | zOpName = "SETLK"; |
| 566 | }else{ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 567 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 568 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 569 | return s; |
| 570 | } |
| 571 | if( p->l_type==F_RDLCK ){ |
| 572 | zType = "RDLCK"; |
| 573 | }else if( p->l_type==F_WRLCK ){ |
| 574 | zType = "WRLCK"; |
| 575 | }else if( p->l_type==F_UNLCK ){ |
| 576 | zType = "UNLCK"; |
| 577 | }else{ |
| 578 | assert( 0 ); |
| 579 | } |
| 580 | assert( p->l_whence==SEEK_SET ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 581 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 582 | savedErrno = errno; |
| 583 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 584 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 585 | (int)p->l_pid, s); |
| 586 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 587 | struct flock l2; |
| 588 | l2 = *p; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 589 | osFcntl(fd, F_GETLK, &l2); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 590 | if( l2.l_type==F_RDLCK ){ |
| 591 | zType = "RDLCK"; |
| 592 | }else if( l2.l_type==F_WRLCK ){ |
| 593 | zType = "WRLCK"; |
| 594 | }else if( l2.l_type==F_UNLCK ){ |
| 595 | zType = "UNLCK"; |
| 596 | }else{ |
| 597 | assert( 0 ); |
| 598 | } |
| 599 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 600 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 601 | } |
| 602 | errno = savedErrno; |
| 603 | return s; |
| 604 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 605 | #undef osFcntl |
| 606 | #define osFcntl lockTrace |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 607 | #endif /* SQLITE_LOCK_TRACE */ |
| 608 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 609 | /* |
| 610 | ** Retry ftruncate() calls that fail due to EINTR |
| 611 | */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 612 | static int robust_ftruncate(int h, sqlite3_int64 sz){ |
| 613 | int rc; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 614 | do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR ); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 615 | return rc; |
| 616 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 617 | |
| 618 | /* |
| 619 | ** This routine translates a standard POSIX errno code into something |
| 620 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 621 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 622 | ** and a variety of "please close the file descriptor NOW" errors into |
| 623 | ** SQLITE_IOERR |
| 624 | ** |
| 625 | ** Errors during initialization of locks, or file system support for locks, |
| 626 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 627 | */ |
| 628 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
| 629 | switch (posixError) { |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 630 | #if 0 |
| 631 | /* At one point this code was not commented out. In theory, this branch |
| 632 | ** should never be hit, as this function should only be called after |
| 633 | ** a locking-related function (i.e. fcntl()) has returned non-zero with |
| 634 | ** the value of errno as the first argument. Since a system call has failed, |
| 635 | ** errno should be non-zero. |
| 636 | ** |
| 637 | ** Despite this, if errno really is zero, we still don't want to return |
| 638 | ** SQLITE_OK. The system call failed, and *some* SQLite error should be |
| 639 | ** propagated back to the caller. Commenting this branch out means errno==0 |
| 640 | ** will be handled by the "default:" case below. |
| 641 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 642 | case 0: |
| 643 | return SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 644 | #endif |
| 645 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 646 | case EAGAIN: |
| 647 | case ETIMEDOUT: |
| 648 | case EBUSY: |
| 649 | case EINTR: |
| 650 | case ENOLCK: |
| 651 | /* random NFS retry error, unless during file system support |
| 652 | * introspection, in which it actually means what it says */ |
| 653 | return SQLITE_BUSY; |
| 654 | |
| 655 | case EACCES: |
| 656 | /* EACCES is like EAGAIN during locking operations, but not any other time*/ |
| 657 | if( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
| 658 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 659 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 660 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){ |
| 661 | return SQLITE_BUSY; |
| 662 | } |
| 663 | /* else fall through */ |
| 664 | case EPERM: |
| 665 | return SQLITE_PERM; |
| 666 | |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 667 | /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And |
| 668 | ** this module never makes such a call. And the code in SQLite itself |
| 669 | ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons |
| 670 | ** this case is also commented out. If the system does set errno to EDEADLK, |
| 671 | ** the default SQLITE_IOERR_XXX code will be returned. */ |
| 672 | #if 0 |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 673 | case EDEADLK: |
| 674 | return SQLITE_IOERR_BLOCKED; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 675 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 676 | |
| 677 | #if EOPNOTSUPP!=ENOTSUP |
| 678 | case EOPNOTSUPP: |
| 679 | /* something went terribly awry, unless during file system support |
| 680 | * introspection, in which it actually means what it says */ |
| 681 | #endif |
| 682 | #ifdef ENOTSUP |
| 683 | case ENOTSUP: |
| 684 | /* invalid fd, unless during file system support introspection, in which |
| 685 | * it actually means what it says */ |
| 686 | #endif |
| 687 | case EIO: |
| 688 | case EBADF: |
| 689 | case EINVAL: |
| 690 | case ENOTCONN: |
| 691 | case ENODEV: |
| 692 | case ENXIO: |
| 693 | case ENOENT: |
dan | 33067e7 | 2011-07-15 13:43:34 +0000 | [diff] [blame] | 694 | #ifdef ESTALE /* ESTALE is not defined on Interix systems */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 695 | case ESTALE: |
dan | 33067e7 | 2011-07-15 13:43:34 +0000 | [diff] [blame] | 696 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 697 | case ENOSYS: |
| 698 | /* these should force the client to close the file and reconnect */ |
| 699 | |
| 700 | default: |
| 701 | return sqliteIOErr; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | |
| 706 | |
| 707 | /****************************************************************************** |
| 708 | ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 709 | ** |
| 710 | ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 711 | ** the device number and the inode number. But this does not work on VxWorks. |
| 712 | ** On VxWorks, a unique file id must be based on the canonical filename. |
| 713 | ** |
| 714 | ** A pointer to an instance of the following structure can be used as a |
| 715 | ** unique file ID in VxWorks. Each instance of this structure contains |
| 716 | ** a copy of the canonical filename. There is also a reference count. |
| 717 | ** The structure is reclaimed when the number of pointers to it drops to |
| 718 | ** zero. |
| 719 | ** |
| 720 | ** There are never very many files open at one time and lookups are not |
| 721 | ** a performance-critical path, so it is sufficient to put these |
| 722 | ** structures on a linked list. |
| 723 | */ |
| 724 | struct vxworksFileId { |
| 725 | struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 726 | int nRef; /* Number of references to this one */ |
| 727 | int nName; /* Length of the zCanonicalName[] string */ |
| 728 | char *zCanonicalName; /* Canonical filename */ |
| 729 | }; |
| 730 | |
| 731 | #if OS_VXWORKS |
| 732 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 733 | ** All unique filenames are held on a linked list headed by this |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 734 | ** variable: |
| 735 | */ |
| 736 | static struct vxworksFileId *vxworksFileList = 0; |
| 737 | |
| 738 | /* |
| 739 | ** Simplify a filename into its canonical form |
| 740 | ** by making the following changes: |
| 741 | ** |
| 742 | ** * removing any trailing and duplicate / |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 743 | ** * convert /./ into just / |
| 744 | ** * convert /A/../ where A is any simple name into just / |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 745 | ** |
| 746 | ** Changes are made in-place. Return the new name length. |
| 747 | ** |
| 748 | ** The original filename is in z[0..n-1]. Return the number of |
| 749 | ** characters in the simplified name. |
| 750 | */ |
| 751 | static int vxworksSimplifyName(char *z, int n){ |
| 752 | int i, j; |
| 753 | while( n>1 && z[n-1]=='/' ){ n--; } |
| 754 | for(i=j=0; i<n; i++){ |
| 755 | if( z[i]=='/' ){ |
| 756 | if( z[i+1]=='/' ) continue; |
| 757 | if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 758 | i += 1; |
| 759 | continue; |
| 760 | } |
| 761 | if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 762 | while( j>0 && z[j-1]!='/' ){ j--; } |
| 763 | if( j>0 ){ j--; } |
| 764 | i += 2; |
| 765 | continue; |
| 766 | } |
| 767 | } |
| 768 | z[j++] = z[i]; |
| 769 | } |
| 770 | z[j] = 0; |
| 771 | return j; |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | ** Find a unique file ID for the given absolute pathname. Return |
| 776 | ** a pointer to the vxworksFileId object. This pointer is the unique |
| 777 | ** file ID. |
| 778 | ** |
| 779 | ** The nRef field of the vxworksFileId object is incremented before |
| 780 | ** the object is returned. A new vxworksFileId object is created |
| 781 | ** and added to the global list if necessary. |
| 782 | ** |
| 783 | ** If a memory allocation error occurs, return NULL. |
| 784 | */ |
| 785 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 786 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 787 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 788 | int n; /* Length of zAbsoluteName string */ |
| 789 | |
| 790 | assert( zAbsoluteName[0]=='/' ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 791 | n = (int)strlen(zAbsoluteName); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 792 | pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) ); |
| 793 | if( pNew==0 ) return 0; |
| 794 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 795 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 796 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 797 | |
| 798 | /* Search for an existing entry that matching the canonical name. |
| 799 | ** If found, increment the reference count and return a pointer to |
| 800 | ** the existing file ID. |
| 801 | */ |
| 802 | unixEnterMutex(); |
| 803 | for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 804 | if( pCandidate->nName==n |
| 805 | && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 806 | ){ |
| 807 | sqlite3_free(pNew); |
| 808 | pCandidate->nRef++; |
| 809 | unixLeaveMutex(); |
| 810 | return pCandidate; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | /* No match was found. We will make a new file ID */ |
| 815 | pNew->nRef = 1; |
| 816 | pNew->nName = n; |
| 817 | pNew->pNext = vxworksFileList; |
| 818 | vxworksFileList = pNew; |
| 819 | unixLeaveMutex(); |
| 820 | return pNew; |
| 821 | } |
| 822 | |
| 823 | /* |
| 824 | ** Decrement the reference count on a vxworksFileId object. Free |
| 825 | ** the object when the reference count reaches zero. |
| 826 | */ |
| 827 | static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 828 | unixEnterMutex(); |
| 829 | assert( pId->nRef>0 ); |
| 830 | pId->nRef--; |
| 831 | if( pId->nRef==0 ){ |
| 832 | struct vxworksFileId **pp; |
| 833 | for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 834 | assert( *pp==pId ); |
| 835 | *pp = pId->pNext; |
| 836 | sqlite3_free(pId); |
| 837 | } |
| 838 | unixLeaveMutex(); |
| 839 | } |
| 840 | #endif /* OS_VXWORKS */ |
| 841 | /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 842 | ******************************************************************************/ |
| 843 | |
| 844 | |
| 845 | /****************************************************************************** |
| 846 | *************************** Posix Advisory Locking **************************** |
| 847 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 848 | ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 849 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 850 | ** sets or clears a lock, that operation overrides any prior locks set |
| 851 | ** by the same process. It does not explicitly say so, but this implies |
| 852 | ** that it overrides locks set by the same process using a different |
| 853 | ** file descriptor. Consider this test case: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 854 | ** |
| 855 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 856 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 857 | ** |
| 858 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 859 | ** one is a hard or symbolic link to the other) then if you set |
| 860 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 861 | ** on fd2, it works. I would have expected the second lock to |
| 862 | ** fail since there was already a lock on the file due to fd1. |
| 863 | ** But not so. Since both locks came from the same process, the |
| 864 | ** second overrides the first, even though they were on different |
| 865 | ** file descriptors opened on different file names. |
| 866 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 867 | ** This means that we cannot use POSIX locks to synchronize file access |
| 868 | ** among competing threads of the same process. POSIX locks will work fine |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 869 | ** to synchronize access for threads in separate processes, but not |
| 870 | ** threads within the same process. |
| 871 | ** |
| 872 | ** To work around the problem, SQLite has to manage file locks internally |
| 873 | ** on its own. Whenever a new database is opened, we have to find the |
| 874 | ** specific inode of the database file (the inode is determined by the |
| 875 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 876 | ** and check for locks already existing on that inode. When locks are |
| 877 | ** created or removed, we have to look at our own internal record of the |
| 878 | ** locks to see if another thread has previously set a lock on that same |
| 879 | ** inode. |
| 880 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 881 | ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 882 | ** For VxWorks, we have to use the alternative unique ID system based on |
| 883 | ** canonical filename and implemented in the previous division.) |
| 884 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 885 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 886 | ** descriptor. It is now a structure that holds the integer file |
| 887 | ** descriptor and a pointer to a structure that describes the internal |
| 888 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 889 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 890 | ** point to the same locking structure. The locking structure keeps |
| 891 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 892 | ** field that tells us its internal lock status. cnt==0 means the |
| 893 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 894 | ** cnt>0 means there are cnt shared locks on the file. |
| 895 | ** |
| 896 | ** Any attempt to lock or unlock a file first checks the locking |
| 897 | ** structure. The fcntl() system call is only invoked to set a |
| 898 | ** POSIX lock if the internal lock structure transitions between |
| 899 | ** a locked and an unlocked state. |
| 900 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 901 | ** But wait: there are yet more problems with POSIX advisory locks. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 902 | ** |
| 903 | ** If you close a file descriptor that points to a file that has locks, |
| 904 | ** all locks on that file that are owned by the current process are |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 905 | ** released. To work around this problem, each unixInodeInfo object |
| 906 | ** maintains a count of the number of pending locks on tha inode. |
| 907 | ** When an attempt is made to close an unixFile, if there are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 908 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 909 | ** to close() the file descriptor is deferred until all of the locks clear. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 910 | ** The unixInodeInfo structure keeps a list of file descriptors that need to |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 911 | ** be closed and that list is walked (and cleared) when the last lock |
| 912 | ** clears. |
| 913 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 914 | ** Yet another problem: LinuxThreads do not play well with posix locks. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 915 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 916 | ** Many older versions of linux use the LinuxThreads library which is |
| 917 | ** not posix compliant. Under LinuxThreads, a lock created by thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 918 | ** A cannot be modified or overridden by a different thread B. |
| 919 | ** Only thread A can modify the lock. Locking behavior is correct |
| 920 | ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 921 | ** on linux - with NPTL a lock created by thread A can override locks |
| 922 | ** in thread B. But there is no way to know at compile-time which |
| 923 | ** threading library is being used. So there is no way to know at |
| 924 | ** compile-time whether or not thread A can override locks on thread B. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 925 | ** 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] | 926 | ** current process. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 927 | ** |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 928 | ** SQLite used to support LinuxThreads. But support for LinuxThreads |
| 929 | ** was dropped beginning with version 3.7.0. SQLite will still work with |
| 930 | ** LinuxThreads provided that (1) there is no more than one connection |
| 931 | ** per database file in the same process and (2) database connections |
| 932 | ** do not move across threads. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 933 | */ |
| 934 | |
| 935 | /* |
| 936 | ** An instance of the following structure serves as the key used |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 937 | ** to locate a particular unixInodeInfo object. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 938 | */ |
| 939 | struct unixFileId { |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 940 | dev_t dev; /* Device number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 941 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 942 | struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 943 | #else |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 944 | ino_t ino; /* Inode number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 945 | #endif |
| 946 | }; |
| 947 | |
| 948 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 949 | ** An instance of the following structure is allocated for each open |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 950 | ** inode. Or, on LinuxThreads, there is one of these structures for |
| 951 | ** each inode opened by each thread. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 952 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 953 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 954 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 955 | ** object keeps a count of the number of unixFile pointing to it. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 956 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 957 | struct unixInodeInfo { |
| 958 | struct unixFileId fileId; /* The lookup key */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 959 | int nShared; /* Number of SHARED locks held */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 960 | unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
| 961 | unsigned char bProcessLock; /* An exclusive process lock is held */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 962 | int nRef; /* Number of pointers to this structure */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 963 | unixShmNode *pShmNode; /* Shared memory associated with this inode */ |
| 964 | int nLock; /* Number of outstanding file locks */ |
| 965 | UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
| 966 | unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ |
| 967 | unixInodeInfo *pPrev; /* .... doubly linked */ |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 968 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 969 | unsigned long long sharedByte; /* for AFP simulated shared lock */ |
| 970 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 971 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 972 | sem_t *pSem; /* Named POSIX semaphore */ |
| 973 | char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 974 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 975 | }; |
| 976 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 977 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 978 | ** A lists of all unixInodeInfo objects. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 979 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 980 | static unixInodeInfo *inodeList = 0; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 981 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 982 | /* |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 983 | ** |
| 984 | ** This function - unixLogError_x(), is only ever called via the macro |
| 985 | ** unixLogError(). |
| 986 | ** |
| 987 | ** It is invoked after an error occurs in an OS function and errno has been |
| 988 | ** set. It logs a message using sqlite3_log() containing the current value of |
| 989 | ** errno and, if possible, the human-readable equivalent from strerror() or |
| 990 | ** strerror_r(). |
| 991 | ** |
| 992 | ** The first argument passed to the macro should be the error code that |
| 993 | ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). |
| 994 | ** The two subsequent arguments should be the name of the OS function that |
| 995 | ** failed (e.g. "unlink", "open") and the the associated file-system path, |
| 996 | ** if any. |
| 997 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 998 | #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__) |
| 999 | static int unixLogErrorAtLine( |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1000 | int errcode, /* SQLite error code */ |
| 1001 | const char *zFunc, /* Name of OS function that failed */ |
| 1002 | const char *zPath, /* File path associated with error */ |
| 1003 | int iLine /* Source line number where error occurred */ |
| 1004 | ){ |
| 1005 | char *zErr; /* Message from strerror() or equivalent */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1006 | int iErrno = errno; /* Saved syscall error number */ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1007 | |
| 1008 | /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use |
| 1009 | ** the strerror() function to obtain the human-readable error message |
| 1010 | ** equivalent to errno. Otherwise, use strerror_r(). |
| 1011 | */ |
| 1012 | #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) |
| 1013 | char aErr[80]; |
| 1014 | memset(aErr, 0, sizeof(aErr)); |
| 1015 | zErr = aErr; |
| 1016 | |
| 1017 | /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined, |
| 1018 | ** assume that the system provides the the GNU version of strerror_r() that |
| 1019 | ** returns a pointer to a buffer containing the error message. That pointer |
| 1020 | ** may point to aErr[], or it may point to some static storage somewhere. |
| 1021 | ** Otherwise, assume that the system provides the POSIX version of |
| 1022 | ** strerror_r(), which always writes an error message into aErr[]. |
| 1023 | ** |
| 1024 | ** If the code incorrectly assumes that it is the POSIX version that is |
| 1025 | ** available, the error message will often be an empty string. Not a |
| 1026 | ** huge problem. Incorrectly concluding that the GNU version is available |
| 1027 | ** could lead to a segfault though. |
| 1028 | */ |
| 1029 | #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) |
| 1030 | zErr = |
| 1031 | # endif |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1032 | strerror_r(iErrno, aErr, sizeof(aErr)-1); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1033 | |
| 1034 | #elif SQLITE_THREADSAFE |
| 1035 | /* This is a threadsafe build, but strerror_r() is not available. */ |
| 1036 | zErr = ""; |
| 1037 | #else |
| 1038 | /* Non-threadsafe build, use strerror(). */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1039 | zErr = strerror(iErrno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1040 | #endif |
| 1041 | |
| 1042 | assert( errcode!=SQLITE_OK ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1043 | if( zPath==0 ) zPath = ""; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1044 | sqlite3_log(errcode, |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1045 | "os_unix.c:%d: (%d) %s(%s) - %s", |
| 1046 | iLine, iErrno, zFunc, zPath, zErr |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1047 | ); |
| 1048 | |
| 1049 | return errcode; |
| 1050 | } |
| 1051 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1052 | /* |
| 1053 | ** Close a file descriptor. |
| 1054 | ** |
| 1055 | ** We assume that close() almost always works, since it is only in a |
| 1056 | ** very sick application or on a very sick platform that it might fail. |
| 1057 | ** If it does fail, simply leak the file descriptor, but do log the |
| 1058 | ** error. |
| 1059 | ** |
| 1060 | ** Note that it is not safe to retry close() after EINTR since the |
| 1061 | ** file descriptor might have already been reused by another thread. |
| 1062 | ** So we don't even try to recover from an EINTR. Just log the error |
| 1063 | ** and move on. |
| 1064 | */ |
| 1065 | static void robust_close(unixFile *pFile, int h, int lineno){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1066 | if( osClose(h) ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1067 | unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", |
| 1068 | pFile ? pFile->zPath : 0, lineno); |
| 1069 | } |
| 1070 | } |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1071 | |
| 1072 | /* |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1073 | ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1074 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1075 | static void closePendingFds(unixFile *pFile){ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1076 | unixInodeInfo *pInode = pFile->pInode; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1077 | UnixUnusedFd *p; |
| 1078 | UnixUnusedFd *pNext; |
| 1079 | for(p=pInode->pUnused; p; p=pNext){ |
| 1080 | pNext = p->pNext; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1081 | robust_close(pFile, p->fd, __LINE__); |
| 1082 | sqlite3_free(p); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1083 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1084 | pInode->pUnused = 0; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1088 | ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1089 | ** |
| 1090 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1091 | ** when this function is called. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1092 | */ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1093 | static void releaseInodeInfo(unixFile *pFile){ |
| 1094 | unixInodeInfo *pInode = pFile->pInode; |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1095 | assert( unixMutexHeld() ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1096 | if( ALWAYS(pInode) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1097 | pInode->nRef--; |
| 1098 | if( pInode->nRef==0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1099 | assert( pInode->pShmNode==0 ); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1100 | closePendingFds(pFile); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1101 | if( pInode->pPrev ){ |
| 1102 | assert( pInode->pPrev->pNext==pInode ); |
| 1103 | pInode->pPrev->pNext = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1104 | }else{ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1105 | assert( inodeList==pInode ); |
| 1106 | inodeList = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1107 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1108 | if( pInode->pNext ){ |
| 1109 | assert( pInode->pNext->pPrev==pInode ); |
| 1110 | pInode->pNext->pPrev = pInode->pPrev; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1111 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1112 | sqlite3_free(pInode); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1113 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1118 | ** Given a file descriptor, locate the unixInodeInfo object that |
| 1119 | ** describes that file descriptor. Create a new one if necessary. The |
| 1120 | ** return value might be uninitialized if an error occurs. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1121 | ** |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1122 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1123 | ** when this function is called. |
| 1124 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1125 | ** Return an appropriate error code. |
| 1126 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1127 | static int findInodeInfo( |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1128 | unixFile *pFile, /* Unix file with file desc used in the key */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1129 | unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1130 | ){ |
| 1131 | int rc; /* System call return code */ |
| 1132 | int fd; /* The file descriptor for pFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1133 | struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ |
| 1134 | struct stat statbuf; /* Low-level file information */ |
| 1135 | unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1136 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1137 | assert( unixMutexHeld() ); |
| 1138 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1139 | /* Get low-level information about the file that we can used to |
| 1140 | ** create a unique name for the file. |
| 1141 | */ |
| 1142 | fd = pFile->h; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1143 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1144 | if( rc!=0 ){ |
| 1145 | pFile->lastErrno = errno; |
| 1146 | #ifdef EOVERFLOW |
| 1147 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 1148 | #endif |
| 1149 | return SQLITE_IOERR; |
| 1150 | } |
| 1151 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1152 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1153 | /* On OS X on an msdos filesystem, the inode number is reported |
| 1154 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 1155 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 1156 | ** we always increase the file size to 1 by writing a single byte |
| 1157 | ** prior to accessing the inode number. The one byte written is |
| 1158 | ** an ASCII 'S' character which also happens to be the first byte |
| 1159 | ** in the header of every SQLite database. In this way, if there |
| 1160 | ** is a race condition such that another thread has already populated |
| 1161 | ** the first page of the database, no damage is done. |
| 1162 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1163 | if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 1164 | do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR ); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1165 | if( rc!=1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1166 | pFile->lastErrno = errno; |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1167 | return SQLITE_IOERR; |
| 1168 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1169 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1170 | if( rc!=0 ){ |
| 1171 | pFile->lastErrno = errno; |
| 1172 | return SQLITE_IOERR; |
| 1173 | } |
| 1174 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1175 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1176 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1177 | memset(&fileId, 0, sizeof(fileId)); |
| 1178 | fileId.dev = statbuf.st_dev; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1179 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1180 | fileId.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1181 | #else |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1182 | fileId.ino = statbuf.st_ino; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1183 | #endif |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1184 | pInode = inodeList; |
| 1185 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 1186 | pInode = pInode->pNext; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1187 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1188 | if( pInode==0 ){ |
| 1189 | pInode = sqlite3_malloc( sizeof(*pInode) ); |
| 1190 | if( pInode==0 ){ |
| 1191 | return SQLITE_NOMEM; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1192 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1193 | memset(pInode, 0, sizeof(*pInode)); |
| 1194 | memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
| 1195 | pInode->nRef = 1; |
| 1196 | pInode->pNext = inodeList; |
| 1197 | pInode->pPrev = 0; |
| 1198 | if( inodeList ) inodeList->pPrev = pInode; |
| 1199 | inodeList = pInode; |
| 1200 | }else{ |
| 1201 | pInode->nRef++; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1202 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1203 | *ppInode = pInode; |
| 1204 | return SQLITE_OK; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1205 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1206 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1207 | |
| 1208 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1209 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1210 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1211 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1212 | ** 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] | 1213 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1214 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1215 | int rc = SQLITE_OK; |
| 1216 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1217 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1218 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1219 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1220 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1221 | assert( pFile ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1222 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1223 | |
| 1224 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1225 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1226 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1229 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1230 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1231 | #ifndef __DJGPP__ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1232 | if( !reserved && !pFile->pInode->bProcessLock ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1233 | struct flock lock; |
| 1234 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1235 | lock.l_start = RESERVED_BYTE; |
| 1236 | lock.l_len = 1; |
| 1237 | lock.l_type = F_WRLCK; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1238 | if( osFcntl(pFile->h, F_GETLK, &lock) ){ |
| 1239 | rc = SQLITE_IOERR_CHECKRESERVEDLOCK; |
| 1240 | pFile->lastErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1241 | } else if( lock.l_type!=F_UNLCK ){ |
| 1242 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1243 | } |
| 1244 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1245 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1246 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1247 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1248 | OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1249 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1250 | *pResOut = reserved; |
| 1251 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1255 | ** Attempt to set a system-lock on the file pFile. The lock is |
| 1256 | ** described by pLock. |
| 1257 | ** |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1258 | ** If the pFile was opened read/write from unix-excl, then the only lock |
| 1259 | ** ever obtained is an exclusive lock, and it is obtained exactly once |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1260 | ** the first time any lock is attempted. All subsequent system locking |
| 1261 | ** operations become no-ops. Locking operations still happen internally, |
| 1262 | ** in order to coordinate access between separate database connections |
| 1263 | ** within this process, but all of that is handled in memory and the |
| 1264 | ** operating system does not participate. |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1265 | ** |
| 1266 | ** This function is a pass-through to fcntl(F_SETLK) if pFile is using |
| 1267 | ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl" |
| 1268 | ** and is read-only. |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1269 | ** |
| 1270 | ** Zero is returned if the call completes successfully, or -1 if a call |
| 1271 | ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()). |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1272 | */ |
| 1273 | static int unixFileLock(unixFile *pFile, struct flock *pLock){ |
| 1274 | int rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1275 | unixInodeInfo *pInode = pFile->pInode; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1276 | assert( unixMutexHeld() ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1277 | assert( pInode!=0 ); |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1278 | if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock) |
| 1279 | && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0) |
| 1280 | ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1281 | if( pInode->bProcessLock==0 ){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1282 | struct flock lock; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1283 | assert( pInode->nLock==0 ); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1284 | lock.l_whence = SEEK_SET; |
| 1285 | lock.l_start = SHARED_FIRST; |
| 1286 | lock.l_len = SHARED_SIZE; |
| 1287 | lock.l_type = F_WRLCK; |
| 1288 | rc = osFcntl(pFile->h, F_SETLK, &lock); |
| 1289 | if( rc<0 ) return rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1290 | pInode->bProcessLock = 1; |
| 1291 | pInode->nLock++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1292 | }else{ |
| 1293 | rc = 0; |
| 1294 | } |
| 1295 | }else{ |
| 1296 | rc = osFcntl(pFile->h, F_SETLK, pLock); |
| 1297 | } |
| 1298 | return rc; |
| 1299 | } |
| 1300 | |
| 1301 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1302 | ** Lock the file with the lock specified by parameter eFileLock - one |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1303 | ** of the following: |
| 1304 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1305 | ** (1) SHARED_LOCK |
| 1306 | ** (2) RESERVED_LOCK |
| 1307 | ** (3) PENDING_LOCK |
| 1308 | ** (4) EXCLUSIVE_LOCK |
| 1309 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1310 | ** Sometimes when requesting one lock state, additional lock states |
| 1311 | ** are inserted in between. The locking might fail on one of the later |
| 1312 | ** transitions leaving the lock state different from what it started but |
| 1313 | ** still short of its goal. The following chart shows the allowed |
| 1314 | ** transitions and the inserted intermediate states: |
| 1315 | ** |
| 1316 | ** UNLOCKED -> SHARED |
| 1317 | ** SHARED -> RESERVED |
| 1318 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1319 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1320 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1321 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1322 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1323 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1324 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1325 | static int unixLock(sqlite3_file *id, int eFileLock){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1326 | /* The following describes the implementation of the various locks and |
| 1327 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1328 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1329 | ** confusion with SQLite lock names). The algorithms are complicated |
| 1330 | ** slightly in order to be compatible with windows systems simultaneously |
| 1331 | ** accessing the same database file, in case that is ever required. |
| 1332 | ** |
| 1333 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1334 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1335 | ** range', a range of 510 bytes at a well known offset. |
| 1336 | ** |
| 1337 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 1338 | ** byte'. If this is successful, a random byte from the 'shared byte |
| 1339 | ** range' is read-locked and the lock on the 'pending byte' released. |
| 1340 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1341 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1342 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1343 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1344 | ** |
| 1345 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1346 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1347 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1348 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1349 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1350 | ** This property is used by the algorithm for rolling back a journal file |
| 1351 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1352 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1353 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1354 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1355 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1356 | ** within this range, this ensures that no other locks are held on the |
| 1357 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1358 | ** |
| 1359 | ** The reason a single byte cannot be used instead of the 'shared byte |
| 1360 | ** range' is that some versions of windows do not support read-locks. By |
| 1361 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 1362 | ** even if the locking primitive used is always a write-lock. |
| 1363 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1364 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1365 | unixFile *pFile = (unixFile*)id; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame^] | 1366 | unixInodeInfo *pInode; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1367 | struct flock lock; |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 1368 | int tErrno = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1369 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1370 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1371 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 1372 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame^] | 1373 | azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid())); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1374 | |
| 1375 | /* 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] | 1376 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1377 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1378 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1379 | if( pFile->eFileLock>=eFileLock ){ |
| 1380 | OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
| 1381 | azFileLock(eFileLock))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1382 | return SQLITE_OK; |
| 1383 | } |
| 1384 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1385 | /* Make sure the locking sequence is correct. |
| 1386 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 1387 | ** (2) SQLite never explicitly requests a pendig lock. |
| 1388 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1389 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1390 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 1391 | assert( eFileLock!=PENDING_LOCK ); |
| 1392 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1393 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1394 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1395 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1396 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1397 | pInode = pFile->pInode; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1398 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1399 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1400 | ** handle that precludes the requested lock, return BUSY. |
| 1401 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1402 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 1403 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1404 | ){ |
| 1405 | rc = SQLITE_BUSY; |
| 1406 | goto end_lock; |
| 1407 | } |
| 1408 | |
| 1409 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1410 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1411 | ** return SQLITE_OK. |
| 1412 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1413 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1414 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1415 | assert( eFileLock==SHARED_LOCK ); |
| 1416 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1417 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1418 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1419 | pInode->nShared++; |
| 1420 | pInode->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1421 | goto end_lock; |
| 1422 | } |
| 1423 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1424 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1425 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1426 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1427 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1428 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1429 | lock.l_len = 1L; |
| 1430 | lock.l_whence = SEEK_SET; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1431 | if( eFileLock==SHARED_LOCK |
| 1432 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1433 | ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1434 | lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1435 | lock.l_start = PENDING_BYTE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1436 | if( unixFileLock(pFile, &lock) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1437 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1438 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1439 | if( rc!=SQLITE_BUSY ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1440 | pFile->lastErrno = tErrno; |
| 1441 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1442 | goto end_lock; |
| 1443 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | |
| 1447 | /* If control gets to this point, then actually go ahead and make |
| 1448 | ** operating system calls for the specified lock. |
| 1449 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1450 | if( eFileLock==SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1451 | assert( pInode->nShared==0 ); |
| 1452 | assert( pInode->eFileLock==0 ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1453 | assert( rc==SQLITE_OK ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1454 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1455 | /* Now get the read-lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1456 | lock.l_start = SHARED_FIRST; |
| 1457 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1458 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1459 | tErrno = errno; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1460 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1461 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1462 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1463 | /* Drop the temporary PENDING lock */ |
| 1464 | lock.l_start = PENDING_BYTE; |
| 1465 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1466 | lock.l_type = F_UNLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1467 | if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){ |
| 1468 | /* This could happen with a network mount */ |
| 1469 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1470 | rc = SQLITE_IOERR_UNLOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1471 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1472 | |
| 1473 | if( rc ){ |
| 1474 | if( rc!=SQLITE_BUSY ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1475 | pFile->lastErrno = tErrno; |
| 1476 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1477 | goto end_lock; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1478 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1479 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1480 | pInode->nLock++; |
| 1481 | pInode->nShared = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1482 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1483 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1484 | /* We are trying for an exclusive lock but another thread in this |
| 1485 | ** same process is still holding a shared lock. */ |
| 1486 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1487 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1488 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1489 | ** assumed that there is a SHARED or greater lock on the file |
| 1490 | ** already. |
| 1491 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1492 | assert( 0!=pFile->eFileLock ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1493 | lock.l_type = F_WRLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1494 | |
| 1495 | assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK ); |
| 1496 | if( eFileLock==RESERVED_LOCK ){ |
| 1497 | lock.l_start = RESERVED_BYTE; |
| 1498 | lock.l_len = 1L; |
| 1499 | }else{ |
| 1500 | lock.l_start = SHARED_FIRST; |
| 1501 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1502 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1503 | |
| 1504 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1505 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1506 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1507 | if( rc!=SQLITE_BUSY ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1508 | pFile->lastErrno = tErrno; |
| 1509 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1510 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1511 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1512 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1513 | |
| 1514 | #ifndef NDEBUG |
| 1515 | /* Set up the transaction-counter change checking flags when |
| 1516 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1517 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1518 | ** write operation (not a hot journal rollback). |
| 1519 | */ |
| 1520 | if( rc==SQLITE_OK |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1521 | && pFile->eFileLock<=SHARED_LOCK |
| 1522 | && eFileLock==RESERVED_LOCK |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1523 | ){ |
| 1524 | pFile->transCntrChng = 0; |
| 1525 | pFile->dbUpdate = 0; |
| 1526 | pFile->inNormalWrite = 1; |
| 1527 | } |
| 1528 | #endif |
| 1529 | |
| 1530 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1531 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1532 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1533 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1534 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 1535 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1536 | pInode->eFileLock = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1537 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1538 | |
| 1539 | end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1540 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1541 | OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), |
| 1542 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1543 | return rc; |
| 1544 | } |
| 1545 | |
| 1546 | /* |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1547 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1548 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1549 | */ |
| 1550 | static void setPendingFd(unixFile *pFile){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1551 | unixInodeInfo *pInode = pFile->pInode; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1552 | UnixUnusedFd *p = pFile->pUnused; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1553 | p->pNext = pInode->pUnused; |
| 1554 | pInode->pUnused = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1555 | pFile->h = -1; |
| 1556 | pFile->pUnused = 0; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1560 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1561 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1562 | ** |
| 1563 | ** If the locking level of the file descriptor is already at or below |
| 1564 | ** the requested locking level, this routine is a no-op. |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1565 | ** |
| 1566 | ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 1567 | ** the byte range is divided into 2 parts and the first part is unlocked then |
| 1568 | ** set to a read lock, then the other part is simply unlocked. This works |
| 1569 | ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 1570 | ** remove the write lock on a region when a read lock is set. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1571 | */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1572 | static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1573 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1574 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1575 | struct flock lock; |
| 1576 | int rc = SQLITE_OK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1577 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1578 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1579 | 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] | 1580 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1581 | getpid())); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1582 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1583 | assert( eFileLock<=SHARED_LOCK ); |
| 1584 | if( pFile->eFileLock<=eFileLock ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1585 | return SQLITE_OK; |
| 1586 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1587 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1588 | pInode = pFile->pInode; |
| 1589 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1590 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1591 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1592 | |
| 1593 | #ifndef NDEBUG |
| 1594 | /* When reducing a lock such that other processes can start |
| 1595 | ** reading the database file again, make sure that the |
| 1596 | ** transaction counter was updated if any part of the database |
| 1597 | ** file changed. If the transaction counter is not updated, |
| 1598 | ** other connections to the same file might not realize that |
| 1599 | ** the file has changed and hence might not know to flush their |
| 1600 | ** cache. The use of a stale cache can lead to database corruption. |
| 1601 | */ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1602 | pFile->inNormalWrite = 0; |
| 1603 | #endif |
| 1604 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1605 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 1606 | ** before establishing the readlock - to avoid a race condition we downgrade |
| 1607 | ** the lock in 2 blocks, so that part of the range will be covered by a |
| 1608 | ** write lock until the rest is covered by a read lock: |
| 1609 | ** 1: [WWWWW] |
| 1610 | ** 2: [....W] |
| 1611 | ** 3: [RRRRW] |
| 1612 | ** 4: [RRRR.] |
| 1613 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1614 | if( eFileLock==SHARED_LOCK ){ |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1615 | |
| 1616 | #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE |
drh | 87e79ae | 2011-03-08 13:06:41 +0000 | [diff] [blame] | 1617 | (void)handleNFSUnlock; |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1618 | assert( handleNFSUnlock==0 ); |
| 1619 | #endif |
| 1620 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1621 | if( handleNFSUnlock ){ |
drh | 026663d | 2011-04-01 13:29:29 +0000 | [diff] [blame] | 1622 | int tErrno; /* Error code from system call errors */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1623 | off_t divSize = SHARED_SIZE - 1; |
| 1624 | |
| 1625 | lock.l_type = F_UNLCK; |
| 1626 | lock.l_whence = SEEK_SET; |
| 1627 | lock.l_start = SHARED_FIRST; |
| 1628 | lock.l_len = divSize; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 1629 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1630 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1631 | rc = SQLITE_IOERR_UNLOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1632 | if( IS_LOCK_ERROR(rc) ){ |
| 1633 | pFile->lastErrno = tErrno; |
| 1634 | } |
| 1635 | goto end_unlock; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1636 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1637 | lock.l_type = F_RDLCK; |
| 1638 | lock.l_whence = SEEK_SET; |
| 1639 | lock.l_start = SHARED_FIRST; |
| 1640 | lock.l_len = divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1641 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1642 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1643 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1644 | if( IS_LOCK_ERROR(rc) ){ |
| 1645 | pFile->lastErrno = tErrno; |
| 1646 | } |
| 1647 | goto end_unlock; |
| 1648 | } |
| 1649 | lock.l_type = F_UNLCK; |
| 1650 | lock.l_whence = SEEK_SET; |
| 1651 | lock.l_start = SHARED_FIRST+divSize; |
| 1652 | lock.l_len = SHARED_SIZE-divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1653 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1654 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1655 | rc = SQLITE_IOERR_UNLOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1656 | if( IS_LOCK_ERROR(rc) ){ |
| 1657 | pFile->lastErrno = tErrno; |
| 1658 | } |
| 1659 | goto end_unlock; |
| 1660 | } |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1661 | }else |
| 1662 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 1663 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1664 | lock.l_type = F_RDLCK; |
| 1665 | lock.l_whence = SEEK_SET; |
| 1666 | lock.l_start = SHARED_FIRST; |
| 1667 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1668 | if( unixFileLock(pFile, &lock) ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1669 | /* In theory, the call to unixFileLock() cannot fail because another |
| 1670 | ** process is holding an incompatible lock. If it does, this |
| 1671 | ** indicates that the other process is not following the locking |
| 1672 | ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning |
| 1673 | ** SQLITE_BUSY would confuse the upper layer (in practice it causes |
| 1674 | ** an assert to fail). */ |
| 1675 | rc = SQLITE_IOERR_RDLOCK; |
| 1676 | pFile->lastErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1677 | goto end_unlock; |
| 1678 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1679 | } |
| 1680 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1681 | lock.l_type = F_UNLCK; |
| 1682 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1683 | lock.l_start = PENDING_BYTE; |
| 1684 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1685 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1686 | pInode->eFileLock = SHARED_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1687 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1688 | rc = SQLITE_IOERR_UNLOCK; |
| 1689 | pFile->lastErrno = errno; |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 1690 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1691 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1692 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1693 | if( eFileLock==NO_LOCK ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1694 | /* Decrement the shared lock counter. Release the lock using an |
| 1695 | ** OS call only when all threads in this same process have released |
| 1696 | ** the lock. |
| 1697 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1698 | pInode->nShared--; |
| 1699 | if( pInode->nShared==0 ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1700 | lock.l_type = F_UNLCK; |
| 1701 | lock.l_whence = SEEK_SET; |
| 1702 | lock.l_start = lock.l_len = 0L; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1703 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1704 | pInode->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1705 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1706 | rc = SQLITE_IOERR_UNLOCK; |
| 1707 | pFile->lastErrno = errno; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1708 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1709 | pFile->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1710 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1713 | /* Decrement the count of locks against this same file. When the |
| 1714 | ** count reaches zero, close any other file descriptors whose close |
| 1715 | ** was deferred because of outstanding locks. |
| 1716 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1717 | pInode->nLock--; |
| 1718 | assert( pInode->nLock>=0 ); |
| 1719 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1720 | closePendingFds(pFile); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1721 | } |
| 1722 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1723 | |
| 1724 | end_unlock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1725 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1726 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1727 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1731 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1732 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1733 | ** |
| 1734 | ** If the locking level of the file descriptor is already at or below |
| 1735 | ** the requested locking level, this routine is a no-op. |
| 1736 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1737 | static int unixUnlock(sqlite3_file *id, int eFileLock){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1738 | return posixUnlock(id, eFileLock, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1742 | ** This function performs the parts of the "close file" operation |
| 1743 | ** common to all locking schemes. It closes the directory and file |
| 1744 | ** handles, if they are valid, and sets all fields of the unixFile |
| 1745 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1746 | ** |
| 1747 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 1748 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 1749 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1750 | */ |
| 1751 | static int closeUnixFile(sqlite3_file *id){ |
| 1752 | unixFile *pFile = (unixFile*)id; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1753 | if( pFile->h>=0 ){ |
| 1754 | robust_close(pFile, pFile->h, __LINE__); |
| 1755 | pFile->h = -1; |
| 1756 | } |
| 1757 | #if OS_VXWORKS |
| 1758 | if( pFile->pId ){ |
| 1759 | if( pFile->isDelete ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 1760 | osUnlink(pFile->pId->zCanonicalName); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1761 | } |
| 1762 | vxworksReleaseFileId(pFile->pId); |
| 1763 | pFile->pId = 0; |
| 1764 | } |
| 1765 | #endif |
| 1766 | OSTRACE(("CLOSE %-3d\n", pFile->h)); |
| 1767 | OpenCounter(-1); |
| 1768 | sqlite3_free(pFile->pUnused); |
| 1769 | memset(pFile, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1770 | return SQLITE_OK; |
| 1771 | } |
| 1772 | |
| 1773 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1774 | ** Close a file. |
| 1775 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1776 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1777 | int rc = SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1778 | unixFile *pFile = (unixFile *)id; |
| 1779 | unixUnlock(id, NO_LOCK); |
| 1780 | unixEnterMutex(); |
| 1781 | |
| 1782 | /* unixFile.pInode is always valid here. Otherwise, a different close |
| 1783 | ** routine (e.g. nolockClose()) would be called instead. |
| 1784 | */ |
| 1785 | assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); |
| 1786 | if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){ |
| 1787 | /* If there are outstanding locks, do not actually close the file just |
| 1788 | ** yet because that would clear those locks. Instead, add the file |
| 1789 | ** descriptor to pInode->pUnused list. It will be automatically closed |
| 1790 | ** when the last lock is cleared. |
| 1791 | */ |
| 1792 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1793 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1794 | releaseInodeInfo(pFile); |
| 1795 | rc = closeUnixFile(id); |
| 1796 | unixLeaveMutex(); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1797 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1800 | /************** End of the posix advisory lock implementation ***************** |
| 1801 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1802 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1803 | /****************************************************************************** |
| 1804 | ****************************** No-op Locking ********************************** |
| 1805 | ** |
| 1806 | ** Of the various locking implementations available, this is by far the |
| 1807 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 1808 | ** file for reading or writing. |
| 1809 | ** |
| 1810 | ** This locking mode is appropriate for use on read-only databases |
| 1811 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 1812 | ** also be used if the application employs some external mechanism to |
| 1813 | ** prevent simultaneous access of the same database by two or more |
| 1814 | ** database connections. But there is a serious risk of database |
| 1815 | ** corruption if this locking mode is used in situations where multiple |
| 1816 | ** database connections are accessing the same database file at the same |
| 1817 | ** time and one or more of those connections are writing. |
| 1818 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1819 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1820 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 1821 | UNUSED_PARAMETER(NotUsed); |
| 1822 | *pResOut = 0; |
| 1823 | return SQLITE_OK; |
| 1824 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1825 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1826 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1827 | return SQLITE_OK; |
| 1828 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1829 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1830 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1831 | return SQLITE_OK; |
| 1832 | } |
| 1833 | |
| 1834 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1835 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1836 | */ |
| 1837 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1838 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | /******************* End of the no-op lock implementation ********************* |
| 1842 | ******************************************************************************/ |
| 1843 | |
| 1844 | /****************************************************************************** |
| 1845 | ************************* Begin dot-file Locking ****************************** |
| 1846 | ** |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1847 | ** The dotfile locking implementation uses the existance of separate lock |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1848 | ** files in order to control access to the database. This works on just |
| 1849 | ** about every filesystem imaginable. But there are serious downsides: |
| 1850 | ** |
| 1851 | ** (1) There is zero concurrency. A single reader blocks all other |
| 1852 | ** connections from reading or writing the database. |
| 1853 | ** |
| 1854 | ** (2) An application crash or power loss can leave stale lock files |
| 1855 | ** sitting around that need to be cleared manually. |
| 1856 | ** |
| 1857 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 1858 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1859 | ** |
| 1860 | ** Dotfile locking works by creating a file in the same directory as the |
| 1861 | ** database and with the same name but with a ".lock" extension added. |
| 1862 | ** The existance of a lock file implies an EXCLUSIVE lock. All other lock |
| 1863 | ** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1864 | */ |
| 1865 | |
| 1866 | /* |
| 1867 | ** The file suffix added to the data base filename in order to create the |
| 1868 | ** lock file. |
| 1869 | */ |
| 1870 | #define DOTLOCK_SUFFIX ".lock" |
| 1871 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1872 | /* |
| 1873 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1874 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1875 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1876 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1877 | ** |
| 1878 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 1879 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 1880 | ** is held on the file and false if the file is unlocked. |
| 1881 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1882 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 1883 | int rc = SQLITE_OK; |
| 1884 | int reserved = 0; |
| 1885 | unixFile *pFile = (unixFile*)id; |
| 1886 | |
| 1887 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1888 | |
| 1889 | assert( pFile ); |
| 1890 | |
| 1891 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1892 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1893 | /* Either this connection or some other connection in the same process |
| 1894 | ** holds a lock on the file. No need to check further. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1895 | reserved = 1; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1896 | }else{ |
| 1897 | /* The lock is held if and only if the lockfile exists */ |
| 1898 | const char *zLockFile = (const char*)pFile->lockingContext; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1899 | reserved = osAccess(zLockFile, 0)==0; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1900 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1901 | OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1902 | *pResOut = reserved; |
| 1903 | return rc; |
| 1904 | } |
| 1905 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1906 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1907 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1908 | ** of the following: |
| 1909 | ** |
| 1910 | ** (1) SHARED_LOCK |
| 1911 | ** (2) RESERVED_LOCK |
| 1912 | ** (3) PENDING_LOCK |
| 1913 | ** (4) EXCLUSIVE_LOCK |
| 1914 | ** |
| 1915 | ** Sometimes when requesting one lock state, additional lock states |
| 1916 | ** are inserted in between. The locking might fail on one of the later |
| 1917 | ** transitions leaving the lock state different from what it started but |
| 1918 | ** still short of its goal. The following chart shows the allowed |
| 1919 | ** transitions and the inserted intermediate states: |
| 1920 | ** |
| 1921 | ** UNLOCKED -> SHARED |
| 1922 | ** SHARED -> RESERVED |
| 1923 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1924 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1925 | ** PENDING -> EXCLUSIVE |
| 1926 | ** |
| 1927 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1928 | ** routine to lower a locking level. |
| 1929 | ** |
| 1930 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 1931 | ** But we track the other locking levels internally. |
| 1932 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1933 | static int dotlockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1934 | unixFile *pFile = (unixFile*)id; |
| 1935 | int fd; |
| 1936 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1937 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1938 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1939 | |
| 1940 | /* If we have any lock, then the lock file already exists. All we have |
| 1941 | ** to do is adjust our internal record of the lock level. |
| 1942 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1943 | if( pFile->eFileLock > NO_LOCK ){ |
| 1944 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1945 | /* Always update the timestamp on the old file */ |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 1946 | #ifdef HAVE_UTIME |
| 1947 | utime(zLockFile, NULL); |
| 1948 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1949 | utimes(zLockFile, NULL); |
| 1950 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1951 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | /* grab an exclusive lock */ |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 1955 | fd = robust_open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1956 | if( fd<0 ){ |
| 1957 | /* failed to open/create the file, someone else may have stolen the lock */ |
| 1958 | int tErrno = errno; |
| 1959 | if( EEXIST == tErrno ){ |
| 1960 | rc = SQLITE_BUSY; |
| 1961 | } else { |
| 1962 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1963 | if( IS_LOCK_ERROR(rc) ){ |
| 1964 | pFile->lastErrno = tErrno; |
| 1965 | } |
| 1966 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1967 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1968 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1969 | robust_close(pFile, fd, __LINE__); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1970 | |
| 1971 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1972 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1973 | return rc; |
| 1974 | } |
| 1975 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1976 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1977 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1978 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1979 | ** |
| 1980 | ** If the locking level of the file descriptor is already at or below |
| 1981 | ** the requested locking level, this routine is a no-op. |
| 1982 | ** |
| 1983 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 1984 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1985 | static int dotlockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1986 | unixFile *pFile = (unixFile*)id; |
| 1987 | char *zLockFile = (char *)pFile->lockingContext; |
| 1988 | |
| 1989 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1990 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
| 1991 | pFile->eFileLock, getpid())); |
| 1992 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1993 | |
| 1994 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1995 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1996 | return SQLITE_OK; |
| 1997 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1998 | |
| 1999 | /* To downgrade to shared, simply update our internal notion of the |
| 2000 | ** lock state. No need to mess with the file on disk. |
| 2001 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2002 | if( eFileLock==SHARED_LOCK ){ |
| 2003 | pFile->eFileLock = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2004 | return SQLITE_OK; |
| 2005 | } |
| 2006 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2007 | /* To fully unlock the database, delete the lock file */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2008 | assert( eFileLock==NO_LOCK ); |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 2009 | if( osUnlink(zLockFile) ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 2010 | int rc = 0; |
| 2011 | int tErrno = errno; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2012 | if( ENOENT != tErrno ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2013 | rc = SQLITE_IOERR_UNLOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2014 | } |
| 2015 | if( IS_LOCK_ERROR(rc) ){ |
| 2016 | pFile->lastErrno = tErrno; |
| 2017 | } |
| 2018 | return rc; |
| 2019 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2020 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2021 | return SQLITE_OK; |
| 2022 | } |
| 2023 | |
| 2024 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2025 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2026 | */ |
| 2027 | static int dotlockClose(sqlite3_file *id) { |
| 2028 | int rc; |
| 2029 | if( id ){ |
| 2030 | unixFile *pFile = (unixFile*)id; |
| 2031 | dotlockUnlock(id, NO_LOCK); |
| 2032 | sqlite3_free(pFile->lockingContext); |
| 2033 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2034 | rc = closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2035 | return rc; |
| 2036 | } |
| 2037 | /****************** End of the dot-file lock implementation ******************* |
| 2038 | ******************************************************************************/ |
| 2039 | |
| 2040 | /****************************************************************************** |
| 2041 | ************************** Begin flock Locking ******************************** |
| 2042 | ** |
| 2043 | ** Use the flock() system call to do file locking. |
| 2044 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2045 | ** flock() locking is like dot-file locking in that the various |
| 2046 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 2047 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 2048 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 2049 | ** still works when you do this, but concurrency is reduced since |
| 2050 | ** only a single process can be reading the database at a time. |
| 2051 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2052 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if |
| 2053 | ** compiling for VXWORKS. |
| 2054 | */ |
| 2055 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2056 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2057 | /* |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2058 | ** Retry flock() calls that fail with EINTR |
| 2059 | */ |
| 2060 | #ifdef EINTR |
| 2061 | static int robust_flock(int fd, int op){ |
| 2062 | int rc; |
| 2063 | do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR ); |
| 2064 | return rc; |
| 2065 | } |
| 2066 | #else |
drh | 5c81927 | 2011-02-23 14:00:12 +0000 | [diff] [blame] | 2067 | # define robust_flock(a,b) flock(a,b) |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2068 | #endif |
| 2069 | |
| 2070 | |
| 2071 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2072 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2073 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2074 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2075 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2076 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2077 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 2078 | int rc = SQLITE_OK; |
| 2079 | int reserved = 0; |
| 2080 | unixFile *pFile = (unixFile*)id; |
| 2081 | |
| 2082 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2083 | |
| 2084 | assert( pFile ); |
| 2085 | |
| 2086 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2087 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2088 | reserved = 1; |
| 2089 | } |
| 2090 | |
| 2091 | /* Otherwise see if some other process holds it. */ |
| 2092 | if( !reserved ){ |
| 2093 | /* attempt to get the lock */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2094 | int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2095 | if( !lrc ){ |
| 2096 | /* got the lock, unlock it */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2097 | lrc = robust_flock(pFile->h, LOCK_UN); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2098 | if ( lrc ) { |
| 2099 | int tErrno = errno; |
| 2100 | /* unlock failed with an error */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2101 | lrc = SQLITE_IOERR_UNLOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2102 | if( IS_LOCK_ERROR(lrc) ){ |
| 2103 | pFile->lastErrno = tErrno; |
| 2104 | rc = lrc; |
| 2105 | } |
| 2106 | } |
| 2107 | } else { |
| 2108 | int tErrno = errno; |
| 2109 | reserved = 1; |
| 2110 | /* someone else might have it reserved */ |
| 2111 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2112 | if( IS_LOCK_ERROR(lrc) ){ |
| 2113 | pFile->lastErrno = tErrno; |
| 2114 | rc = lrc; |
| 2115 | } |
| 2116 | } |
| 2117 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2118 | OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2119 | |
| 2120 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2121 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2122 | rc = SQLITE_OK; |
| 2123 | reserved=1; |
| 2124 | } |
| 2125 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2126 | *pResOut = reserved; |
| 2127 | return rc; |
| 2128 | } |
| 2129 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2130 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2131 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2132 | ** of the following: |
| 2133 | ** |
| 2134 | ** (1) SHARED_LOCK |
| 2135 | ** (2) RESERVED_LOCK |
| 2136 | ** (3) PENDING_LOCK |
| 2137 | ** (4) EXCLUSIVE_LOCK |
| 2138 | ** |
| 2139 | ** Sometimes when requesting one lock state, additional lock states |
| 2140 | ** are inserted in between. The locking might fail on one of the later |
| 2141 | ** transitions leaving the lock state different from what it started but |
| 2142 | ** still short of its goal. The following chart shows the allowed |
| 2143 | ** transitions and the inserted intermediate states: |
| 2144 | ** |
| 2145 | ** UNLOCKED -> SHARED |
| 2146 | ** SHARED -> RESERVED |
| 2147 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2148 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2149 | ** PENDING -> EXCLUSIVE |
| 2150 | ** |
| 2151 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 2152 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2153 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2154 | ** access the file. |
| 2155 | ** |
| 2156 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2157 | ** routine to lower a locking level. |
| 2158 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2159 | static int flockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2160 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2161 | unixFile *pFile = (unixFile*)id; |
| 2162 | |
| 2163 | assert( pFile ); |
| 2164 | |
| 2165 | /* if we already have a lock, it is exclusive. |
| 2166 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2167 | if (pFile->eFileLock > NO_LOCK) { |
| 2168 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2169 | return SQLITE_OK; |
| 2170 | } |
| 2171 | |
| 2172 | /* grab an exclusive lock */ |
| 2173 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2174 | if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2175 | int tErrno = errno; |
| 2176 | /* didn't get, must be busy */ |
| 2177 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2178 | if( IS_LOCK_ERROR(rc) ){ |
| 2179 | pFile->lastErrno = tErrno; |
| 2180 | } |
| 2181 | } else { |
| 2182 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2183 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2184 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2185 | OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), |
| 2186 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2187 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2188 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2189 | rc = SQLITE_BUSY; |
| 2190 | } |
| 2191 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2192 | return rc; |
| 2193 | } |
| 2194 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2195 | |
| 2196 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2197 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2198 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2199 | ** |
| 2200 | ** If the locking level of the file descriptor is already at or below |
| 2201 | ** the requested locking level, this routine is a no-op. |
| 2202 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2203 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2204 | unixFile *pFile = (unixFile*)id; |
| 2205 | |
| 2206 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2207 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
| 2208 | pFile->eFileLock, getpid())); |
| 2209 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2210 | |
| 2211 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2212 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2213 | return SQLITE_OK; |
| 2214 | } |
| 2215 | |
| 2216 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2217 | if (eFileLock==SHARED_LOCK) { |
| 2218 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2219 | return SQLITE_OK; |
| 2220 | } |
| 2221 | |
| 2222 | /* no, really, unlock. */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2223 | if( robust_flock(pFile->h, LOCK_UN) ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2224 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2225 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2226 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2227 | return SQLITE_IOERR_UNLOCK; |
| 2228 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2229 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2230 | return SQLITE_OK; |
| 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | /* |
| 2235 | ** Close a file. |
| 2236 | */ |
| 2237 | static int flockClose(sqlite3_file *id) { |
| 2238 | if( id ){ |
| 2239 | flockUnlock(id, NO_LOCK); |
| 2240 | } |
| 2241 | return closeUnixFile(id); |
| 2242 | } |
| 2243 | |
| 2244 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 2245 | |
| 2246 | /******************* End of the flock lock implementation ********************* |
| 2247 | ******************************************************************************/ |
| 2248 | |
| 2249 | /****************************************************************************** |
| 2250 | ************************ Begin Named Semaphore Locking ************************ |
| 2251 | ** |
| 2252 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2253 | ** |
| 2254 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 2255 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 2256 | ** the database file at a time. This reduces potential concurrency, but |
| 2257 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2258 | */ |
| 2259 | #if OS_VXWORKS |
| 2260 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2261 | /* |
| 2262 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2263 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2264 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2265 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2266 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2267 | static int semCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 2268 | int rc = SQLITE_OK; |
| 2269 | int reserved = 0; |
| 2270 | unixFile *pFile = (unixFile*)id; |
| 2271 | |
| 2272 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2273 | |
| 2274 | assert( pFile ); |
| 2275 | |
| 2276 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2277 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2278 | reserved = 1; |
| 2279 | } |
| 2280 | |
| 2281 | /* Otherwise see if some other process holds it. */ |
| 2282 | if( !reserved ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2283 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2284 | struct stat statBuf; |
| 2285 | |
| 2286 | if( sem_trywait(pSem)==-1 ){ |
| 2287 | int tErrno = errno; |
| 2288 | if( EAGAIN != tErrno ){ |
| 2289 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 2290 | pFile->lastErrno = tErrno; |
| 2291 | } else { |
| 2292 | /* someone else has the lock when we are in NO_LOCK */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2293 | reserved = (pFile->eFileLock < SHARED_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2294 | } |
| 2295 | }else{ |
| 2296 | /* we could have it if we want it */ |
| 2297 | sem_post(pSem); |
| 2298 | } |
| 2299 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2300 | OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2301 | |
| 2302 | *pResOut = reserved; |
| 2303 | return rc; |
| 2304 | } |
| 2305 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2306 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2307 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2308 | ** of the following: |
| 2309 | ** |
| 2310 | ** (1) SHARED_LOCK |
| 2311 | ** (2) RESERVED_LOCK |
| 2312 | ** (3) PENDING_LOCK |
| 2313 | ** (4) EXCLUSIVE_LOCK |
| 2314 | ** |
| 2315 | ** Sometimes when requesting one lock state, additional lock states |
| 2316 | ** are inserted in between. The locking might fail on one of the later |
| 2317 | ** transitions leaving the lock state different from what it started but |
| 2318 | ** still short of its goal. The following chart shows the allowed |
| 2319 | ** transitions and the inserted intermediate states: |
| 2320 | ** |
| 2321 | ** UNLOCKED -> SHARED |
| 2322 | ** SHARED -> RESERVED |
| 2323 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2324 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2325 | ** PENDING -> EXCLUSIVE |
| 2326 | ** |
| 2327 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 2328 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2329 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2330 | ** access the file. |
| 2331 | ** |
| 2332 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2333 | ** routine to lower a locking level. |
| 2334 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2335 | static int semLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2336 | unixFile *pFile = (unixFile*)id; |
| 2337 | int fd; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2338 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2339 | int rc = SQLITE_OK; |
| 2340 | |
| 2341 | /* if we already have a lock, it is exclusive. |
| 2342 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2343 | if (pFile->eFileLock > NO_LOCK) { |
| 2344 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2345 | rc = SQLITE_OK; |
| 2346 | goto sem_end_lock; |
| 2347 | } |
| 2348 | |
| 2349 | /* lock semaphore now but bail out when already locked. */ |
| 2350 | if( sem_trywait(pSem)==-1 ){ |
| 2351 | rc = SQLITE_BUSY; |
| 2352 | goto sem_end_lock; |
| 2353 | } |
| 2354 | |
| 2355 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2356 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2357 | |
| 2358 | sem_end_lock: |
| 2359 | return rc; |
| 2360 | } |
| 2361 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2362 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2363 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2364 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2365 | ** |
| 2366 | ** If the locking level of the file descriptor is already at or below |
| 2367 | ** the requested locking level, this routine is a no-op. |
| 2368 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2369 | static int semUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2370 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2371 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2372 | |
| 2373 | assert( pFile ); |
| 2374 | assert( pSem ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2375 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
| 2376 | pFile->eFileLock, getpid())); |
| 2377 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2378 | |
| 2379 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2380 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2381 | return SQLITE_OK; |
| 2382 | } |
| 2383 | |
| 2384 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2385 | if (eFileLock==SHARED_LOCK) { |
| 2386 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2387 | return SQLITE_OK; |
| 2388 | } |
| 2389 | |
| 2390 | /* no, really unlock. */ |
| 2391 | if ( sem_post(pSem)==-1 ) { |
| 2392 | int rc, tErrno = errno; |
| 2393 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2394 | if( IS_LOCK_ERROR(rc) ){ |
| 2395 | pFile->lastErrno = tErrno; |
| 2396 | } |
| 2397 | return rc; |
| 2398 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2399 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2400 | return SQLITE_OK; |
| 2401 | } |
| 2402 | |
| 2403 | /* |
| 2404 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2405 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2406 | static int semClose(sqlite3_file *id) { |
| 2407 | if( id ){ |
| 2408 | unixFile *pFile = (unixFile*)id; |
| 2409 | semUnlock(id, NO_LOCK); |
| 2410 | assert( pFile ); |
| 2411 | unixEnterMutex(); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2412 | releaseInodeInfo(pFile); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2413 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2414 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2415 | } |
| 2416 | return SQLITE_OK; |
| 2417 | } |
| 2418 | |
| 2419 | #endif /* OS_VXWORKS */ |
| 2420 | /* |
| 2421 | ** Named semaphore locking is only available on VxWorks. |
| 2422 | ** |
| 2423 | *************** End of the named semaphore lock implementation **************** |
| 2424 | ******************************************************************************/ |
| 2425 | |
| 2426 | |
| 2427 | /****************************************************************************** |
| 2428 | *************************** Begin AFP Locking ********************************* |
| 2429 | ** |
| 2430 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2431 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2432 | ** |
| 2433 | ** Third-party implementations of AFP are available. But this code here |
| 2434 | ** only works on OSX. |
| 2435 | */ |
| 2436 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2437 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2438 | /* |
| 2439 | ** The afpLockingContext structure contains all afp lock specific state |
| 2440 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2441 | typedef struct afpLockingContext afpLockingContext; |
| 2442 | struct afpLockingContext { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2443 | int reserved; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2444 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2445 | }; |
| 2446 | |
| 2447 | struct ByteRangeLockPB2 |
| 2448 | { |
| 2449 | unsigned long long offset; /* offset to first byte to lock */ |
| 2450 | unsigned long long length; /* nbr of bytes to lock */ |
| 2451 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2452 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2453 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2454 | int fd; /* file desc to assoc this lock with */ |
| 2455 | }; |
| 2456 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2457 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2458 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2459 | /* |
| 2460 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2461 | ** AFP filesystem. |
| 2462 | ** |
| 2463 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2464 | */ |
| 2465 | static int afpSetLock( |
| 2466 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2467 | unixFile *pFile, /* Open file descriptor on path */ |
| 2468 | unsigned long long offset, /* First byte to be locked */ |
| 2469 | unsigned long long length, /* Number of bytes to lock */ |
| 2470 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2471 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2472 | struct ByteRangeLockPB2 pb; |
| 2473 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2474 | |
| 2475 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2476 | pb.startEndFlag = 0; |
| 2477 | pb.offset = offset; |
| 2478 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2479 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2480 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2481 | OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2482 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2483 | offset, length)); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2484 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2485 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2486 | int rc; |
| 2487 | int tErrno = errno; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2488 | OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2489 | path, tErrno, strerror(tErrno))); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2490 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2491 | rc = SQLITE_BUSY; |
| 2492 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2493 | rc = sqliteErrorFromPosixError(tErrno, |
| 2494 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2495 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2496 | if( IS_LOCK_ERROR(rc) ){ |
| 2497 | pFile->lastErrno = tErrno; |
| 2498 | } |
| 2499 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2500 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2501 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2502 | } |
| 2503 | } |
| 2504 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2505 | /* |
| 2506 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2507 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2508 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2509 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2510 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2511 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2512 | int rc = SQLITE_OK; |
| 2513 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2514 | unixFile *pFile = (unixFile*)id; |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2515 | afpLockingContext *context; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2516 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2517 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2518 | |
| 2519 | assert( pFile ); |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2520 | context = (afpLockingContext *) pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2521 | if( context->reserved ){ |
| 2522 | *pResOut = 1; |
| 2523 | return SQLITE_OK; |
| 2524 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2525 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2526 | |
| 2527 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2528 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2529 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | /* Otherwise see if some other process holds it. |
| 2533 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2534 | if( !reserved ){ |
| 2535 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2536 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2537 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2538 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2539 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2540 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2541 | } else { |
| 2542 | /* if we failed to get the lock then someone else must have it */ |
| 2543 | reserved = 1; |
| 2544 | } |
| 2545 | if( IS_LOCK_ERROR(lrc) ){ |
| 2546 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2547 | } |
| 2548 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2549 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2550 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2551 | OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2552 | |
| 2553 | *pResOut = reserved; |
| 2554 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2557 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2558 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2559 | ** of the following: |
| 2560 | ** |
| 2561 | ** (1) SHARED_LOCK |
| 2562 | ** (2) RESERVED_LOCK |
| 2563 | ** (3) PENDING_LOCK |
| 2564 | ** (4) EXCLUSIVE_LOCK |
| 2565 | ** |
| 2566 | ** Sometimes when requesting one lock state, additional lock states |
| 2567 | ** are inserted in between. The locking might fail on one of the later |
| 2568 | ** transitions leaving the lock state different from what it started but |
| 2569 | ** still short of its goal. The following chart shows the allowed |
| 2570 | ** transitions and the inserted intermediate states: |
| 2571 | ** |
| 2572 | ** UNLOCKED -> SHARED |
| 2573 | ** SHARED -> RESERVED |
| 2574 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2575 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2576 | ** PENDING -> EXCLUSIVE |
| 2577 | ** |
| 2578 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2579 | ** routine to lower a locking level. |
| 2580 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2581 | static int afpLock(sqlite3_file *id, int eFileLock){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2582 | int rc = SQLITE_OK; |
| 2583 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2584 | unixInodeInfo *pInode = pFile->pInode; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2585 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2586 | |
| 2587 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2588 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 2589 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2590 | azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2591 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2592 | /* 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] | 2593 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2594 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2595 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2596 | if( pFile->eFileLock>=eFileLock ){ |
| 2597 | OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
| 2598 | azFileLock(eFileLock))); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2599 | return SQLITE_OK; |
| 2600 | } |
| 2601 | |
| 2602 | /* Make sure the locking sequence is correct |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2603 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 2604 | ** (2) SQLite never explicitly requests a pendig lock. |
| 2605 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2606 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2607 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 2608 | assert( eFileLock!=PENDING_LOCK ); |
| 2609 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2610 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2611 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2612 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2613 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2614 | pInode = pFile->pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2615 | |
| 2616 | /* If some thread using this PID has a lock via a different unixFile* |
| 2617 | ** handle that precludes the requested lock, return BUSY. |
| 2618 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2619 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 2620 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2621 | ){ |
| 2622 | rc = SQLITE_BUSY; |
| 2623 | goto afp_end_lock; |
| 2624 | } |
| 2625 | |
| 2626 | /* If a SHARED lock is requested, and some thread using this PID already |
| 2627 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 2628 | ** return SQLITE_OK. |
| 2629 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2630 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2631 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2632 | assert( eFileLock==SHARED_LOCK ); |
| 2633 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2634 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2635 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2636 | pInode->nShared++; |
| 2637 | pInode->nLock++; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2638 | goto afp_end_lock; |
| 2639 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2640 | |
| 2641 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2642 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2643 | ** be released. |
| 2644 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2645 | if( eFileLock==SHARED_LOCK |
| 2646 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2647 | ){ |
| 2648 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2649 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2650 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2651 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2652 | goto afp_end_lock; |
| 2653 | } |
| 2654 | } |
| 2655 | |
| 2656 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2657 | ** operating system calls for the specified lock. |
| 2658 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2659 | if( eFileLock==SHARED_LOCK ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2660 | int lrc1, lrc2, lrc1Errno = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2661 | long lk, mask; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2662 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2663 | assert( pInode->nShared==0 ); |
| 2664 | assert( pInode->eFileLock==0 ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2665 | |
| 2666 | mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2667 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2668 | /* note that the quality of the randomness doesn't matter that much */ |
| 2669 | lk = random(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2670 | pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2671 | lrc1 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2672 | SHARED_FIRST+pInode->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2673 | if( IS_LOCK_ERROR(lrc1) ){ |
| 2674 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2675 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2676 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2677 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2678 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2679 | if( IS_LOCK_ERROR(lrc1) ) { |
| 2680 | pFile->lastErrno = lrc1Errno; |
| 2681 | rc = lrc1; |
| 2682 | goto afp_end_lock; |
| 2683 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 2684 | rc = lrc2; |
| 2685 | goto afp_end_lock; |
| 2686 | } else if( lrc1 != SQLITE_OK ) { |
| 2687 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2688 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2689 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2690 | pInode->nLock++; |
| 2691 | pInode->nShared = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2692 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2693 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2694 | /* We are trying for an exclusive lock but another thread in this |
| 2695 | ** same process is still holding a shared lock. */ |
| 2696 | rc = SQLITE_BUSY; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2697 | }else{ |
| 2698 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 2699 | ** assumed that there is a SHARED or greater lock on the file |
| 2700 | ** already. |
| 2701 | */ |
| 2702 | int failed = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2703 | assert( 0!=pFile->eFileLock ); |
| 2704 | if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2705 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2706 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2707 | if( !failed ){ |
| 2708 | context->reserved = 1; |
| 2709 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2710 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2711 | if (!failed && eFileLock == EXCLUSIVE_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2712 | /* Acquire an EXCLUSIVE lock */ |
| 2713 | |
| 2714 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2715 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2716 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2717 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2718 | pInode->sharedByte, 1, 0)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2719 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2720 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2721 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2722 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2723 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2724 | SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2725 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 2726 | ** a critical I/O error |
| 2727 | */ |
| 2728 | rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : |
| 2729 | SQLITE_IOERR_LOCK; |
| 2730 | goto afp_end_lock; |
| 2731 | } |
| 2732 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2733 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2734 | } |
| 2735 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2736 | if( failed ){ |
| 2737 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2738 | } |
| 2739 | } |
| 2740 | |
| 2741 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2742 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2743 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2744 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 2745 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2746 | pInode->eFileLock = PENDING_LOCK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
| 2749 | afp_end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2750 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2751 | OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), |
| 2752 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2753 | return rc; |
| 2754 | } |
| 2755 | |
| 2756 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2757 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2758 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2759 | ** |
| 2760 | ** If the locking level of the file descriptor is already at or below |
| 2761 | ** the requested locking level, this routine is a no-op. |
| 2762 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2763 | static int afpUnlock(sqlite3_file *id, int eFileLock) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2764 | int rc = SQLITE_OK; |
| 2765 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2766 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2767 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 2768 | int skipShared = 0; |
| 2769 | #ifdef SQLITE_TEST |
| 2770 | int h = pFile->h; |
| 2771 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2772 | |
| 2773 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2774 | 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] | 2775 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2776 | getpid())); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2777 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2778 | assert( eFileLock<=SHARED_LOCK ); |
| 2779 | if( pFile->eFileLock<=eFileLock ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2780 | return SQLITE_OK; |
| 2781 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2782 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2783 | pInode = pFile->pInode; |
| 2784 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2785 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2786 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2787 | SimulateIOErrorBenign(1); |
| 2788 | SimulateIOError( h=(-1) ) |
| 2789 | SimulateIOErrorBenign(0); |
| 2790 | |
| 2791 | #ifndef NDEBUG |
| 2792 | /* When reducing a lock such that other processes can start |
| 2793 | ** reading the database file again, make sure that the |
| 2794 | ** transaction counter was updated if any part of the database |
| 2795 | ** file changed. If the transaction counter is not updated, |
| 2796 | ** other connections to the same file might not realize that |
| 2797 | ** the file has changed and hence might not know to flush their |
| 2798 | ** cache. The use of a stale cache can lead to database corruption. |
| 2799 | */ |
| 2800 | assert( pFile->inNormalWrite==0 |
| 2801 | || pFile->dbUpdate==0 |
| 2802 | || pFile->transCntrChng==1 ); |
| 2803 | pFile->inNormalWrite = 0; |
| 2804 | #endif |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2805 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2806 | if( pFile->eFileLock==EXCLUSIVE_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2807 | rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2808 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2809 | /* only re-establish the shared lock if necessary */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2810 | int sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2811 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 2812 | } else { |
| 2813 | skipShared = 1; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2814 | } |
| 2815 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2816 | if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2817 | rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2818 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2819 | if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2820 | rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 2821 | if( !rc ){ |
| 2822 | context->reserved = 0; |
| 2823 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2824 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2825 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ |
| 2826 | pInode->eFileLock = SHARED_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2827 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2828 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2829 | if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2830 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2831 | /* Decrement the shared lock counter. Release the lock using an |
| 2832 | ** OS call only when all threads in this same process have released |
| 2833 | ** the lock. |
| 2834 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2835 | unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 2836 | pInode->nShared--; |
| 2837 | if( pInode->nShared==0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2838 | SimulateIOErrorBenign(1); |
| 2839 | SimulateIOError( h=(-1) ) |
| 2840 | SimulateIOErrorBenign(0); |
| 2841 | if( !skipShared ){ |
| 2842 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 2843 | } |
| 2844 | if( !rc ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2845 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2846 | pFile->eFileLock = NO_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2847 | } |
| 2848 | } |
| 2849 | if( rc==SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2850 | pInode->nLock--; |
| 2851 | assert( pInode->nLock>=0 ); |
| 2852 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 2853 | closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2854 | } |
| 2855 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2856 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2857 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2858 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2859 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2860 | return rc; |
| 2861 | } |
| 2862 | |
| 2863 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2864 | ** Close a file & cleanup AFP specific locking context |
| 2865 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2866 | static int afpClose(sqlite3_file *id) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2867 | int rc = SQLITE_OK; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2868 | if( id ){ |
| 2869 | unixFile *pFile = (unixFile*)id; |
| 2870 | afpUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2871 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2872 | if( pFile->pInode && pFile->pInode->nLock ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2873 | /* If there are outstanding locks, do not actually close the file just |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2874 | ** yet because that would clear those locks. Instead, add the file |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2875 | ** descriptor to pInode->aPending. It will be automatically closed when |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2876 | ** the last lock is cleared. |
| 2877 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2878 | setPendingFd(pFile); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2879 | } |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2880 | releaseInodeInfo(pFile); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2881 | sqlite3_free(pFile->lockingContext); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2882 | rc = closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2883 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2884 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2885 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2888 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2889 | /* |
| 2890 | ** The code above is the AFP lock implementation. The code is specific |
| 2891 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2892 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 2893 | ** VFS is not available. |
| 2894 | ** |
| 2895 | ********************* End of the AFP lock implementation ********************** |
| 2896 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2897 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2898 | /****************************************************************************** |
| 2899 | *************************** Begin NFS Locking ********************************/ |
| 2900 | |
| 2901 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 2902 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2903 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2904 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2905 | ** |
| 2906 | ** If the locking level of the file descriptor is already at or below |
| 2907 | ** the requested locking level, this routine is a no-op. |
| 2908 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2909 | static int nfsUnlock(sqlite3_file *id, int eFileLock){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 2910 | return posixUnlock(id, eFileLock, 1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
| 2913 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 2914 | /* |
| 2915 | ** The code above is the NFS lock implementation. The code is specific |
| 2916 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2917 | ** is available. |
| 2918 | ** |
| 2919 | ********************* End of the NFS lock implementation ********************** |
| 2920 | ******************************************************************************/ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2921 | |
| 2922 | /****************************************************************************** |
| 2923 | **************** Non-locking sqlite3_file methods ***************************** |
| 2924 | ** |
| 2925 | ** The next division contains implementations for all methods of the |
| 2926 | ** sqlite3_file object other than the locking methods. The locking |
| 2927 | ** methods were defined in divisions above (one locking method per |
| 2928 | ** division). Those methods that are common to all locking modes |
| 2929 | ** are gather together into this division. |
| 2930 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2931 | |
| 2932 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2933 | ** Seek to the offset passed as the second argument, then read cnt |
| 2934 | ** bytes into pBuf. Return the number of bytes actually read. |
| 2935 | ** |
| 2936 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 2937 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 2938 | ** one system to another. Since SQLite does not define USE_PREAD |
| 2939 | ** any any form by default, we will not attempt to define _XOPEN_SOURCE. |
| 2940 | ** See tickets #2741 and #2681. |
| 2941 | ** |
| 2942 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 2943 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2944 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2945 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 2946 | int got; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2947 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2948 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2949 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2950 | TIMER_START; |
| 2951 | #if defined(USE_PREAD) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 2952 | do{ got = osPread(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2953 | SimulateIOError( got = -1 ); |
| 2954 | #elif defined(USE_PREAD64) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 2955 | do{ got = osPread64(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2956 | SimulateIOError( got = -1 ); |
| 2957 | #else |
| 2958 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 2959 | SimulateIOError( newOffset-- ); |
| 2960 | if( newOffset!=offset ){ |
| 2961 | if( newOffset == -1 ){ |
| 2962 | ((unixFile*)id)->lastErrno = errno; |
| 2963 | }else{ |
| 2964 | ((unixFile*)id)->lastErrno = 0; |
| 2965 | } |
| 2966 | return -1; |
| 2967 | } |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 2968 | do{ got = osRead(id->h, pBuf, cnt); }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2969 | #endif |
| 2970 | TIMER_END; |
| 2971 | if( got<0 ){ |
| 2972 | ((unixFile*)id)->lastErrno = errno; |
| 2973 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2974 | OSTRACE(("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2975 | return got; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2979 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 2980 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 2981 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2982 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2983 | static int unixRead( |
| 2984 | sqlite3_file *id, |
| 2985 | void *pBuf, |
| 2986 | int amt, |
| 2987 | sqlite3_int64 offset |
| 2988 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2989 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2990 | int got; |
| 2991 | assert( id ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2992 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2993 | /* If this is a database file (not a journal, master-journal or temp |
| 2994 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2995 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 2996 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2997 | || offset>=PENDING_BYTE+512 |
| 2998 | || offset+amt<=PENDING_BYTE |
| 2999 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3000 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3001 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3002 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3003 | if( got==amt ){ |
| 3004 | return SQLITE_OK; |
| 3005 | }else if( got<0 ){ |
| 3006 | /* lastErrno set by seekAndRead */ |
| 3007 | return SQLITE_IOERR_READ; |
| 3008 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3009 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3010 | /* Unread parts of the buffer must be zero-filled */ |
| 3011 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 3012 | return SQLITE_IOERR_SHORT_READ; |
| 3013 | } |
| 3014 | } |
| 3015 | |
| 3016 | /* |
| 3017 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 3018 | ** Return the number of bytes actually read. Update the offset. |
| 3019 | ** |
| 3020 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 3021 | ** is set before returning. |
| 3022 | */ |
| 3023 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
| 3024 | int got; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3025 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3026 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3027 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3028 | TIMER_START; |
| 3029 | #if defined(USE_PREAD) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 3030 | do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3031 | #elif defined(USE_PREAD64) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 3032 | do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3033 | #else |
drh | bd1e50c | 2011-08-19 14:54:12 +0000 | [diff] [blame] | 3034 | do{ |
| 3035 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 3036 | SimulateIOError( newOffset-- ); |
| 3037 | if( newOffset!=offset ){ |
| 3038 | if( newOffset == -1 ){ |
| 3039 | ((unixFile*)id)->lastErrno = errno; |
| 3040 | }else{ |
| 3041 | ((unixFile*)id)->lastErrno = 0; |
| 3042 | } |
| 3043 | return -1; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3044 | } |
drh | bd1e50c | 2011-08-19 14:54:12 +0000 | [diff] [blame] | 3045 | got = osWrite(id->h, pBuf, cnt); |
| 3046 | }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3047 | #endif |
| 3048 | TIMER_END; |
| 3049 | if( got<0 ){ |
| 3050 | ((unixFile*)id)->lastErrno = errno; |
| 3051 | } |
| 3052 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3053 | OSTRACE(("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3054 | return got; |
| 3055 | } |
| 3056 | |
| 3057 | |
| 3058 | /* |
| 3059 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 3060 | ** or some other error code on failure. |
| 3061 | */ |
| 3062 | static int unixWrite( |
| 3063 | sqlite3_file *id, |
| 3064 | const void *pBuf, |
| 3065 | int amt, |
| 3066 | sqlite3_int64 offset |
| 3067 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3068 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3069 | int wrote = 0; |
| 3070 | assert( id ); |
| 3071 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3072 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3073 | /* If this is a database file (not a journal, master-journal or temp |
| 3074 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3075 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3076 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3077 | || offset>=PENDING_BYTE+512 |
| 3078 | || offset+amt<=PENDING_BYTE |
| 3079 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3080 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3081 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3082 | #ifndef NDEBUG |
| 3083 | /* If we are doing a normal write to a database file (as opposed to |
| 3084 | ** doing a hot-journal rollback or a write to some file other than a |
| 3085 | ** normal database file) then record the fact that the database |
| 3086 | ** has changed. If the transaction counter is modified, record that |
| 3087 | ** fact too. |
| 3088 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3089 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3090 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 3091 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3092 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3093 | char oldCntr[4]; |
| 3094 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3095 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3096 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3097 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3098 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 3099 | } |
| 3100 | } |
| 3101 | } |
| 3102 | #endif |
| 3103 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3104 | while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3105 | amt -= wrote; |
| 3106 | offset += wrote; |
| 3107 | pBuf = &((char*)pBuf)[wrote]; |
| 3108 | } |
| 3109 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 3110 | SimulateDiskfullError(( wrote=0, amt=1 )); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3111 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3112 | if( amt>0 ){ |
drh | a21b83b | 2011-04-15 12:36:10 +0000 | [diff] [blame] | 3113 | if( wrote<0 && pFile->lastErrno!=ENOSPC ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3114 | /* lastErrno set by seekAndWrite */ |
| 3115 | return SQLITE_IOERR_WRITE; |
| 3116 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3117 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3118 | return SQLITE_FULL; |
| 3119 | } |
| 3120 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3121 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3122 | return SQLITE_OK; |
| 3123 | } |
| 3124 | |
| 3125 | #ifdef SQLITE_TEST |
| 3126 | /* |
| 3127 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3128 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3129 | */ |
| 3130 | int sqlite3_sync_count = 0; |
| 3131 | int sqlite3_fullsync_count = 0; |
| 3132 | #endif |
| 3133 | |
| 3134 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3135 | ** We do not trust systems to provide a working fdatasync(). Some do. |
drh | 20f8e13 | 2011-08-31 21:01:55 +0000 | [diff] [blame] | 3136 | ** Others do no. To be safe, we will stick with the (slightly slower) |
| 3137 | ** fsync(). If you know that your system does support fdatasync() correctly, |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3138 | ** then simply compile with -Dfdatasync=fdatasync |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3139 | */ |
drh | 20f8e13 | 2011-08-31 21:01:55 +0000 | [diff] [blame] | 3140 | #if !defined(fdatasync) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3141 | # define fdatasync fsync |
| 3142 | #endif |
| 3143 | |
| 3144 | /* |
| 3145 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 3146 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 3147 | ** only available on Mac OS X. But that could change. |
| 3148 | */ |
| 3149 | #ifdef F_FULLFSYNC |
| 3150 | # define HAVE_FULLFSYNC 1 |
| 3151 | #else |
| 3152 | # define HAVE_FULLFSYNC 0 |
| 3153 | #endif |
| 3154 | |
| 3155 | |
| 3156 | /* |
| 3157 | ** The fsync() system call does not work as advertised on many |
| 3158 | ** unix systems. The following procedure is an attempt to make |
| 3159 | ** it work better. |
| 3160 | ** |
| 3161 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 3162 | ** for testing when we want to run through the test suite quickly. |
| 3163 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 3164 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 3165 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3166 | ** |
| 3167 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 3168 | ** The idea behind dataOnly is that it should only write the file content |
| 3169 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 3170 | ** unchanged since the file size is part of the inode. However, |
| 3171 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 3172 | ** file size has changed. The only real difference between fdatasync() |
| 3173 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 3174 | ** inode if the mtime or owner or other inode attributes have changed. |
| 3175 | ** We only care about the file size, not the other file attributes, so |
| 3176 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 3177 | ** So, we always use fdatasync() if it is available, regardless of |
| 3178 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3179 | */ |
| 3180 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3181 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3182 | |
| 3183 | /* The following "ifdef/elif/else/" block has the same structure as |
| 3184 | ** the one below. It is replicated here solely to avoid cluttering |
| 3185 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 3186 | */ |
| 3187 | #ifdef SQLITE_NO_SYNC |
| 3188 | UNUSED_PARAMETER(fd); |
| 3189 | UNUSED_PARAMETER(fullSync); |
| 3190 | UNUSED_PARAMETER(dataOnly); |
| 3191 | #elif HAVE_FULLFSYNC |
| 3192 | UNUSED_PARAMETER(dataOnly); |
| 3193 | #else |
| 3194 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3195 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3196 | #endif |
| 3197 | |
| 3198 | /* Record the number of times that we do a normal fsync() and |
| 3199 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 3200 | ** gets called with the correct arguments. |
| 3201 | */ |
| 3202 | #ifdef SQLITE_TEST |
| 3203 | if( fullSync ) sqlite3_fullsync_count++; |
| 3204 | sqlite3_sync_count++; |
| 3205 | #endif |
| 3206 | |
| 3207 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 3208 | ** no-op |
| 3209 | */ |
| 3210 | #ifdef SQLITE_NO_SYNC |
| 3211 | rc = SQLITE_OK; |
| 3212 | #elif HAVE_FULLFSYNC |
| 3213 | if( fullSync ){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3214 | rc = osFcntl(fd, F_FULLFSYNC, 0); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3215 | }else{ |
| 3216 | rc = 1; |
| 3217 | } |
| 3218 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3219 | ** It shouldn't be possible for fullfsync to fail on the local |
| 3220 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 3221 | ** isn't supported for this file system. So, attempt an fsync |
| 3222 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 3223 | ** It'd be better to detect fullfsync support once and avoid |
| 3224 | ** the fcntl call every time sync is called. |
| 3225 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3226 | if( rc ) rc = fsync(fd); |
| 3227 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3228 | #elif defined(__APPLE__) |
| 3229 | /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 3230 | ** so currently we default to the macro that redefines fdatasync to fsync |
| 3231 | */ |
| 3232 | rc = fsync(fd); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3233 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3234 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 3235 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3236 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3237 | rc = fsync(fd); |
| 3238 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3239 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3240 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 3241 | |
| 3242 | if( OS_VXWORKS && rc!= -1 ){ |
| 3243 | rc = 0; |
| 3244 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3245 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3248 | /* |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3249 | ** Open a file descriptor to the directory containing file zFilename. |
| 3250 | ** If successful, *pFd is set to the opened file descriptor and |
| 3251 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 3252 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 3253 | ** value. |
| 3254 | ** |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3255 | ** The directory file descriptor is used for only one thing - to |
| 3256 | ** fsync() a directory to make sure file creation and deletion events |
| 3257 | ** are flushed to disk. Such fsyncs are not needed on newer |
| 3258 | ** journaling filesystems, but are required on older filesystems. |
| 3259 | ** |
| 3260 | ** This routine can be overridden using the xSetSysCall interface. |
| 3261 | ** The ability to override this routine was added in support of the |
| 3262 | ** chromium sandbox. Opening a directory is a security risk (we are |
| 3263 | ** told) so making it overrideable allows the chromium sandbox to |
| 3264 | ** replace this routine with a harmless no-op. To make this routine |
| 3265 | ** a no-op, replace it with a stub that returns SQLITE_OK but leaves |
| 3266 | ** *pFd set to a negative number. |
| 3267 | ** |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3268 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 3269 | ** the file descriptor *pFd using close(). |
| 3270 | */ |
| 3271 | static int openDirectory(const char *zFilename, int *pFd){ |
| 3272 | int ii; |
| 3273 | int fd = -1; |
| 3274 | char zDirname[MAX_PATHNAME+1]; |
| 3275 | |
| 3276 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
| 3277 | for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--); |
| 3278 | if( ii>0 ){ |
| 3279 | zDirname[ii] = '\0'; |
| 3280 | fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0); |
| 3281 | if( fd>=0 ){ |
| 3282 | #ifdef FD_CLOEXEC |
| 3283 | osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 3284 | #endif |
| 3285 | OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); |
| 3286 | } |
| 3287 | } |
| 3288 | *pFd = fd; |
| 3289 | return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname)); |
| 3290 | } |
| 3291 | |
| 3292 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3293 | ** Make sure all writes to a particular file are committed to disk. |
| 3294 | ** |
| 3295 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 3296 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 3297 | ** file data is synced. |
| 3298 | ** |
| 3299 | ** Under Unix, also make sure that the directory entry for the file |
| 3300 | ** has been created by fsync-ing the directory that contains the file. |
| 3301 | ** If we do not do this and we encounter a power failure, the directory |
| 3302 | ** entry for the journal might not exist after we reboot. The next |
| 3303 | ** SQLite to access the file will not know that the journal exists (because |
| 3304 | ** the directory entry for the journal was never created) and the transaction |
| 3305 | ** will not roll back - possibly leading to database corruption. |
| 3306 | */ |
| 3307 | static int unixSync(sqlite3_file *id, int flags){ |
| 3308 | int rc; |
| 3309 | unixFile *pFile = (unixFile*)id; |
| 3310 | |
| 3311 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 3312 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 3313 | |
| 3314 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 3315 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 3316 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 3317 | ); |
| 3318 | |
| 3319 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 3320 | ** line is to test that doing so does not cause any problems. |
| 3321 | */ |
| 3322 | SimulateDiskfullError( return SQLITE_FULL ); |
| 3323 | |
| 3324 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3325 | OSTRACE(("SYNC %-3d\n", pFile->h)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3326 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 3327 | SimulateIOError( rc=1 ); |
| 3328 | if( rc ){ |
| 3329 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3330 | return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3331 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3332 | |
| 3333 | /* Also fsync the directory containing the file if the DIRSYNC flag |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3334 | ** is set. This is a one-time occurrance. Many systems (examples: AIX) |
| 3335 | ** are unable to fsync a directory, so ignore errors on the fsync. |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3336 | */ |
| 3337 | if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){ |
| 3338 | int dirfd; |
| 3339 | OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3340 | HAVE_FULLFSYNC, isFullsync)); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3341 | rc = osOpenDirectory(pFile->zPath, &dirfd); |
| 3342 | if( rc==SQLITE_OK && dirfd>=0 ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3343 | full_fsync(dirfd, 0, 0); |
| 3344 | robust_close(pFile, dirfd, __LINE__); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 3345 | }else if( rc==SQLITE_CANTOPEN ){ |
| 3346 | rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3347 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3348 | pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3349 | } |
| 3350 | return rc; |
| 3351 | } |
| 3352 | |
| 3353 | /* |
| 3354 | ** Truncate an open file to a specified size |
| 3355 | */ |
| 3356 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3357 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3358 | int rc; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3359 | assert( pFile ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3360 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3361 | |
| 3362 | /* If the user has configured a chunk-size for this file, truncate the |
| 3363 | ** file so that it consists of an integer number of chunks (i.e. the |
| 3364 | ** actual file size after the operation may be larger than the requested |
| 3365 | ** size). |
| 3366 | */ |
| 3367 | if( pFile->szChunk ){ |
| 3368 | nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 3369 | } |
| 3370 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3371 | rc = robust_ftruncate(pFile->h, (off_t)nByte); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3372 | if( rc ){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3373 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3374 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3375 | }else{ |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3376 | #ifndef NDEBUG |
| 3377 | /* If we are doing a normal write to a database file (as opposed to |
| 3378 | ** doing a hot-journal rollback or a write to some file other than a |
| 3379 | ** normal database file) and we truncate the file to zero length, |
| 3380 | ** that effectively updates the change counter. This might happen |
| 3381 | ** when restoring a database using the backup API from a zero-length |
| 3382 | ** source. |
| 3383 | */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3384 | if( pFile->inNormalWrite && nByte==0 ){ |
| 3385 | pFile->transCntrChng = 1; |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3386 | } |
| 3387 | #endif |
| 3388 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3389 | return SQLITE_OK; |
| 3390 | } |
| 3391 | } |
| 3392 | |
| 3393 | /* |
| 3394 | ** Determine the current size of a file in bytes |
| 3395 | */ |
| 3396 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 3397 | int rc; |
| 3398 | struct stat buf; |
| 3399 | assert( id ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3400 | rc = osFstat(((unixFile*)id)->h, &buf); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3401 | SimulateIOError( rc=1 ); |
| 3402 | if( rc!=0 ){ |
| 3403 | ((unixFile*)id)->lastErrno = errno; |
| 3404 | return SQLITE_IOERR_FSTAT; |
| 3405 | } |
| 3406 | *pSize = buf.st_size; |
| 3407 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3408 | /* When opening a zero-size database, the findInodeInfo() procedure |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3409 | ** writes a single byte into that file in order to work around a bug |
| 3410 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 3411 | ** layers, we need to report this file size as zero even though it is |
| 3412 | ** really 1. Ticket #3260. |
| 3413 | */ |
| 3414 | if( *pSize==1 ) *pSize = 0; |
| 3415 | |
| 3416 | |
| 3417 | return SQLITE_OK; |
| 3418 | } |
| 3419 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3420 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3421 | /* |
| 3422 | ** Handler for proxy-locking file-control verbs. Defined below in the |
| 3423 | ** proxying locking division. |
| 3424 | */ |
| 3425 | static int proxyFileControl(sqlite3_file*,int,void*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3426 | #endif |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3427 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3428 | /* |
| 3429 | ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 3430 | ** file-control operation. Enlarge the database to nBytes in size |
| 3431 | ** (rounded up to the next chunk-size). If the database is already |
| 3432 | ** nBytes or larger, this routine is a no-op. |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3433 | */ |
| 3434 | static int fcntlSizeHint(unixFile *pFile, i64 nByte){ |
mistachkin | d589a54 | 2011-08-30 01:23:34 +0000 | [diff] [blame] | 3435 | if( pFile->szChunk>0 ){ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3436 | i64 nSize; /* Required file size */ |
| 3437 | struct stat buf; /* Used to hold return values of fstat() */ |
| 3438 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3439 | if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3440 | |
| 3441 | nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; |
| 3442 | if( nSize>(i64)buf.st_size ){ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3443 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3444 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3445 | /* The code below is handling the return value of osFallocate() |
| 3446 | ** correctly. posix_fallocate() is defined to "returns zero on success, |
| 3447 | ** or an error number on failure". See the manpage for details. */ |
| 3448 | int err; |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3449 | do{ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3450 | err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size); |
| 3451 | }while( err==EINTR ); |
| 3452 | if( err ) return SQLITE_IOERR_WRITE; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3453 | #else |
| 3454 | /* If the OS does not have posix_fallocate(), fake it. First use |
| 3455 | ** ftruncate() to set the file size, then write a single byte to |
| 3456 | ** the last byte in each block within the extended region. This |
| 3457 | ** is the same technique used by glibc to implement posix_fallocate() |
| 3458 | ** on systems that do not have a real fallocate() system call. |
| 3459 | */ |
| 3460 | int nBlk = buf.st_blksize; /* File-system block size */ |
| 3461 | i64 iWrite; /* Next offset to write to */ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3462 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3463 | if( robust_ftruncate(pFile->h, nSize) ){ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3464 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3465 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3466 | } |
| 3467 | iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1; |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3468 | while( iWrite<nSize ){ |
| 3469 | int nWrite = seekAndWrite(pFile, iWrite, "", 1); |
| 3470 | if( nWrite!=1 ) return SQLITE_IOERR_WRITE; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3471 | iWrite += nBlk; |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3472 | } |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3473 | #endif |
| 3474 | } |
| 3475 | } |
| 3476 | |
| 3477 | return SQLITE_OK; |
| 3478 | } |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3479 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3480 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3481 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3482 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3483 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3484 | unixFile *pFile = (unixFile*)id; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3485 | switch( op ){ |
| 3486 | case SQLITE_FCNTL_LOCKSTATE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3487 | *(int*)pArg = pFile->eFileLock; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3488 | return SQLITE_OK; |
| 3489 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3490 | case SQLITE_LAST_ERRNO: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3491 | *(int*)pArg = pFile->lastErrno; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3492 | return SQLITE_OK; |
| 3493 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3494 | case SQLITE_FCNTL_CHUNK_SIZE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3495 | pFile->szChunk = *(int *)pArg; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3496 | return SQLITE_OK; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3497 | } |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3498 | case SQLITE_FCNTL_SIZE_HINT: { |
dan | da04ea4 | 2011-08-23 05:10:39 +0000 | [diff] [blame] | 3499 | int rc; |
| 3500 | SimulateIOErrorBenign(1); |
| 3501 | rc = fcntlSizeHint(pFile, *(i64 *)pArg); |
| 3502 | SimulateIOErrorBenign(0); |
| 3503 | return rc; |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3504 | } |
| 3505 | case SQLITE_FCNTL_PERSIST_WAL: { |
| 3506 | int bPersist = *(int*)pArg; |
| 3507 | if( bPersist<0 ){ |
drh | 253cea5 | 2011-07-26 16:23:25 +0000 | [diff] [blame] | 3508 | *(int*)pArg = (pFile->ctrlFlags & UNIXFILE_PERSIST_WAL)!=0; |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3509 | }else if( bPersist==0 ){ |
| 3510 | pFile->ctrlFlags &= ~UNIXFILE_PERSIST_WAL; |
| 3511 | }else{ |
| 3512 | pFile->ctrlFlags |= UNIXFILE_PERSIST_WAL; |
| 3513 | } |
| 3514 | return SQLITE_OK; |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3515 | } |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3516 | #ifndef NDEBUG |
| 3517 | /* The pager calls this method to signal that it has done |
| 3518 | ** a rollback and that the database is therefore unchanged and |
| 3519 | ** it hence it is OK for the transaction change counter to be |
| 3520 | ** unchanged. |
| 3521 | */ |
| 3522 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 3523 | ((unixFile*)id)->dbUpdate = 0; |
| 3524 | return SQLITE_OK; |
| 3525 | } |
| 3526 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3527 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3528 | case SQLITE_SET_LOCKPROXYFILE: |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3529 | case SQLITE_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3530 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3531 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3532 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 3533 | case SQLITE_FCNTL_SYNC_OMITTED: { |
| 3534 | return SQLITE_OK; /* A no-op */ |
| 3535 | } |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3536 | } |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 3537 | return SQLITE_NOTFOUND; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | /* |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3541 | ** Return the sector size in bytes of the underlying block device for |
| 3542 | ** the specified file. This is almost always 512 bytes, but may be |
| 3543 | ** larger for some devices. |
| 3544 | ** |
| 3545 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 3546 | ** if two files are created in the same file-system directory (i.e. |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3547 | ** a database and its journal file) that the sector size will be the |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3548 | ** same for both. |
| 3549 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3550 | static int unixSectorSize(sqlite3_file *NotUsed){ |
| 3551 | UNUSED_PARAMETER(NotUsed); |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 3552 | return SQLITE_DEFAULT_SECTOR_SIZE; |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3555 | /* |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3556 | ** Return the device characteristics for the file. This is always 0 for unix. |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3557 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3558 | static int unixDeviceCharacteristics(sqlite3_file *NotUsed){ |
| 3559 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3560 | return 0; |
| 3561 | } |
| 3562 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3563 | #ifndef SQLITE_OMIT_WAL |
| 3564 | |
| 3565 | |
| 3566 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3567 | ** Object used to represent an shared memory buffer. |
| 3568 | ** |
| 3569 | ** When multiple threads all reference the same wal-index, each thread |
| 3570 | ** has its own unixShm object, but they all point to a single instance |
| 3571 | ** of this unixShmNode object. In other words, each wal-index is opened |
| 3572 | ** only once per process. |
| 3573 | ** |
| 3574 | ** Each unixShmNode object is connected to a single unixInodeInfo object. |
| 3575 | ** We could coalesce this object into unixInodeInfo, but that would mean |
| 3576 | ** every open file that does not use shared memory (in other words, most |
| 3577 | ** open files) would have to carry around this extra information. So |
| 3578 | ** the unixInodeInfo object contains a pointer to this unixShmNode object |
| 3579 | ** and the unixShmNode object is created only when needed. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3580 | ** |
| 3581 | ** unixMutexHeld() must be true when creating or destroying |
| 3582 | ** this object or while reading or writing the following fields: |
| 3583 | ** |
| 3584 | ** nRef |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3585 | ** |
| 3586 | ** The following fields are read-only after the object is created: |
| 3587 | ** |
| 3588 | ** fid |
| 3589 | ** zFilename |
| 3590 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3591 | ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3592 | ** unixMutexHeld() is true when reading or writing any other field |
| 3593 | ** in this structure. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3594 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3595 | struct unixShmNode { |
| 3596 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3597 | sqlite3_mutex *mutex; /* Mutex to access this object */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3598 | char *zFilename; /* Name of the mmapped file */ |
| 3599 | int h; /* Open file descriptor */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3600 | int szRegion; /* Size of shared-memory regions */ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 3601 | u16 nRegion; /* Size of array apRegion */ |
| 3602 | u8 isReadonly; /* True if read-only */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3603 | char **apRegion; /* Array of mapped shared-memory regions */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3604 | int nRef; /* Number of unixShm objects pointing to this */ |
| 3605 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3606 | #ifdef SQLITE_DEBUG |
| 3607 | u8 exclMask; /* Mask of exclusive locks held */ |
| 3608 | u8 sharedMask; /* Mask of shared locks held */ |
| 3609 | u8 nextShmId; /* Next available unixShm.id value */ |
| 3610 | #endif |
| 3611 | }; |
| 3612 | |
| 3613 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3614 | ** Structure used internally by this VFS to record the state of an |
| 3615 | ** open shared memory connection. |
| 3616 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3617 | ** The following fields are initialized when this object is created and |
| 3618 | ** are read-only thereafter: |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3619 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3620 | ** unixShm.pFile |
| 3621 | ** unixShm.id |
| 3622 | ** |
| 3623 | ** All other fields are read/write. The unixShm.pFile->mutex must be held |
| 3624 | ** while accessing any read/write fields. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3625 | */ |
| 3626 | struct unixShm { |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3627 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 3628 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3629 | u8 hasMutex; /* True if holding the unixShmNode mutex */ |
drh | fd53231 | 2011-08-31 18:35:34 +0000 | [diff] [blame] | 3630 | u8 id; /* Id of this connection within its unixShmNode */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3631 | u16 sharedMask; /* Mask of shared locks held */ |
| 3632 | u16 exclMask; /* Mask of exclusive locks held */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3633 | }; |
| 3634 | |
| 3635 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3636 | ** Constants used for locking |
| 3637 | */ |
drh | bd9676c | 2010-06-23 17:58:38 +0000 | [diff] [blame] | 3638 | #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
drh | 4222441 | 2010-05-31 14:28:25 +0000 | [diff] [blame] | 3639 | #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3640 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3641 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3642 | ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3643 | ** |
| 3644 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 3645 | ** otherwise. |
| 3646 | */ |
| 3647 | static int unixShmSystemLock( |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3648 | unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */ |
| 3649 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3650 | int ofst, /* First byte of the locking range */ |
| 3651 | int n /* Number of bytes to lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3652 | ){ |
| 3653 | struct flock f; /* The posix advisory locking structure */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3654 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3655 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3656 | /* Access to the unixShmNode object is serialized by the caller */ |
| 3657 | assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3658 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3659 | /* Shared locks never span more than one byte */ |
| 3660 | assert( n==1 || lockType!=F_RDLCK ); |
| 3661 | |
| 3662 | /* Locks are within range */ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3663 | assert( n>=1 && n<SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3664 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3665 | if( pShmNode->h>=0 ){ |
| 3666 | /* Initialize the locking parameters */ |
| 3667 | memset(&f, 0, sizeof(f)); |
| 3668 | f.l_type = lockType; |
| 3669 | f.l_whence = SEEK_SET; |
| 3670 | f.l_start = ofst; |
| 3671 | f.l_len = n; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3672 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3673 | rc = osFcntl(pShmNode->h, F_SETLK, &f); |
| 3674 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 3675 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3676 | |
| 3677 | /* Update the global lock state and do debug tracing */ |
| 3678 | #ifdef SQLITE_DEBUG |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3679 | { u16 mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3680 | OSTRACE(("SHM-LOCK ")); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3681 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3682 | if( rc==SQLITE_OK ){ |
| 3683 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3684 | OSTRACE(("unlock %d ok", ofst)); |
| 3685 | pShmNode->exclMask &= ~mask; |
| 3686 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3687 | }else if( lockType==F_RDLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3688 | OSTRACE(("read-lock %d ok", ofst)); |
| 3689 | pShmNode->exclMask &= ~mask; |
| 3690 | pShmNode->sharedMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3691 | }else{ |
| 3692 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3693 | OSTRACE(("write-lock %d ok", ofst)); |
| 3694 | pShmNode->exclMask |= mask; |
| 3695 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3696 | } |
| 3697 | }else{ |
| 3698 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3699 | OSTRACE(("unlock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3700 | }else if( lockType==F_RDLCK ){ |
| 3701 | OSTRACE(("read-lock failed")); |
| 3702 | }else{ |
| 3703 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3704 | OSTRACE(("write-lock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3705 | } |
| 3706 | } |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 3707 | OSTRACE((" - afterwards %03x,%03x\n", |
| 3708 | pShmNode->sharedMask, pShmNode->exclMask)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3709 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3710 | #endif |
| 3711 | |
| 3712 | return rc; |
| 3713 | } |
| 3714 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3715 | |
| 3716 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3717 | ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3718 | ** |
| 3719 | ** This is not a VFS shared-memory method; it is a utility function called |
| 3720 | ** by VFS shared-memory methods. |
| 3721 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3722 | static void unixShmPurge(unixFile *pFd){ |
| 3723 | unixShmNode *p = pFd->pInode->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3724 | assert( unixMutexHeld() ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3725 | if( p && p->nRef==0 ){ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3726 | int i; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3727 | assert( p->pInode==pFd->pInode ); |
drh | df3aa16 | 2011-06-24 11:29:51 +0000 | [diff] [blame] | 3728 | sqlite3_mutex_free(p->mutex); |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3729 | for(i=0; i<p->nRegion; i++){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3730 | if( p->h>=0 ){ |
| 3731 | munmap(p->apRegion[i], p->szRegion); |
| 3732 | }else{ |
| 3733 | sqlite3_free(p->apRegion[i]); |
| 3734 | } |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3735 | } |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3736 | sqlite3_free(p->apRegion); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 3737 | if( p->h>=0 ){ |
| 3738 | robust_close(pFd, p->h, __LINE__); |
| 3739 | p->h = -1; |
| 3740 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3741 | p->pInode->pShmNode = 0; |
| 3742 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3743 | } |
| 3744 | } |
| 3745 | |
| 3746 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3747 | ** Open a shared-memory area associated with open database file pDbFd. |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3748 | ** This particular implementation uses mmapped files. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3749 | ** |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3750 | ** The file used to implement shared-memory is in the same directory |
| 3751 | ** as the open database file and has the same name as the open database |
| 3752 | ** file with the "-shm" suffix added. For example, if the database file |
| 3753 | ** is "/home/user1/config.db" then the file that is created and mmapped |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3754 | ** for shared memory will be called "/home/user1/config.db-shm". |
| 3755 | ** |
| 3756 | ** Another approach to is to use files in /dev/shm or /dev/tmp or an |
| 3757 | ** some other tmpfs mount. But if a file in a different directory |
| 3758 | ** from the database file is used, then differing access permissions |
| 3759 | ** or a chroot() might cause two different processes on the same |
| 3760 | ** database to end up using different files for shared memory - |
| 3761 | ** meaning that their memory would not really be shared - resulting |
| 3762 | ** in database corruption. Nevertheless, this tmpfs file usage |
| 3763 | ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" |
| 3764 | ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time |
| 3765 | ** option results in an incompatible build of SQLite; builds of SQLite |
| 3766 | ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the |
| 3767 | ** same database file at the same time, database corruption will likely |
| 3768 | ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered |
| 3769 | ** "unsupported" and may go away in a future SQLite release. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3770 | ** |
| 3771 | ** When opening a new shared-memory file, if no other instances of that |
| 3772 | ** file are currently open, in this process or in other processes, then |
| 3773 | ** the file must be truncated to zero length or have its header cleared. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3774 | ** |
| 3775 | ** If the original database file (pDbFd) is using the "unix-excl" VFS |
| 3776 | ** that means that an exclusive lock is held on the database file and |
| 3777 | ** that no other processes are able to read or write the database. In |
| 3778 | ** that case, we do not really need shared memory. No shared memory |
| 3779 | ** file is created. The shared memory will be simulated with heap memory. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3780 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3781 | static int unixOpenSharedMemory(unixFile *pDbFd){ |
| 3782 | struct unixShm *p = 0; /* The connection to be opened */ |
| 3783 | struct unixShmNode *pShmNode; /* The underlying mmapped file */ |
| 3784 | int rc; /* Result code */ |
| 3785 | unixInodeInfo *pInode; /* The inode of fd */ |
| 3786 | char *zShmFilename; /* Name of the file used for SHM */ |
| 3787 | int nShmFilename; /* Size of the SHM filename in bytes */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3788 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3789 | /* Allocate space for the new unixShm object. */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3790 | p = sqlite3_malloc( sizeof(*p) ); |
| 3791 | if( p==0 ) return SQLITE_NOMEM; |
| 3792 | memset(p, 0, sizeof(*p)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3793 | assert( pDbFd->pShm==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3794 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3795 | /* Check to see if a unixShmNode object already exists. Reuse an existing |
| 3796 | ** one if present. Create a new one if necessary. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3797 | */ |
| 3798 | unixEnterMutex(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 3799 | pInode = pDbFd->pInode; |
| 3800 | pShmNode = pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3801 | if( pShmNode==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 3802 | struct stat sStat; /* fstat() info for database file */ |
| 3803 | |
| 3804 | /* Call fstat() to figure out the permissions on the database file. If |
| 3805 | ** a new *-shm file is created, an attempt will be made to create it |
| 3806 | ** with the same permissions. The actual permissions the file is created |
| 3807 | ** with are subject to the current umask setting. |
| 3808 | */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3809 | if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 3810 | rc = SQLITE_IOERR_FSTAT; |
| 3811 | goto shm_open_err; |
| 3812 | } |
| 3813 | |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3814 | #ifdef SQLITE_SHM_DIRECTORY |
| 3815 | nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 30; |
| 3816 | #else |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3817 | nShmFilename = 5 + (int)strlen(pDbFd->zPath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3818 | #endif |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3819 | pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3820 | if( pShmNode==0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3821 | rc = SQLITE_NOMEM; |
| 3822 | goto shm_open_err; |
| 3823 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3824 | memset(pShmNode, 0, sizeof(*pShmNode)); |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3825 | zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1]; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3826 | #ifdef SQLITE_SHM_DIRECTORY |
| 3827 | sqlite3_snprintf(nShmFilename, zShmFilename, |
| 3828 | SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", |
| 3829 | (u32)sStat.st_ino, (u32)sStat.st_dev); |
| 3830 | #else |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3831 | sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath); |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 3832 | sqlite3FileSuffix3(pDbFd->zPath, zShmFilename); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3833 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3834 | pShmNode->h = -1; |
| 3835 | pDbFd->pInode->pShmNode = pShmNode; |
| 3836 | pShmNode->pInode = pDbFd->pInode; |
| 3837 | pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 3838 | if( pShmNode->mutex==0 ){ |
| 3839 | rc = SQLITE_NOMEM; |
| 3840 | goto shm_open_err; |
| 3841 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3842 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3843 | if( pInode->bProcessLock==0 ){ |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 3844 | const char *zRO; |
| 3845 | int openFlags = O_RDWR | O_CREAT; |
| 3846 | zRO = sqlite3_uri_parameter(pDbFd->zPath, "readonly_shm"); |
| 3847 | if( zRO && sqlite3GetBoolean(zRO) ){ |
| 3848 | openFlags = O_RDONLY; |
| 3849 | pShmNode->isReadonly = 1; |
| 3850 | } |
| 3851 | pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777)); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3852 | if( pShmNode->h<0 ){ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 3853 | if( pShmNode->h<0 ){ |
| 3854 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename); |
| 3855 | goto shm_open_err; |
| 3856 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3857 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3858 | |
| 3859 | /* Check to see if another process is holding the dead-man switch. |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 3860 | ** If not, truncate the file to zero length. |
| 3861 | */ |
| 3862 | rc = SQLITE_OK; |
| 3863 | if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ |
| 3864 | if( robust_ftruncate(pShmNode->h, 0) ){ |
| 3865 | rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3866 | } |
| 3867 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 3868 | if( rc==SQLITE_OK ){ |
| 3869 | rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1); |
| 3870 | } |
| 3871 | if( rc ) goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3872 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3873 | } |
| 3874 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3875 | /* Make the new connection a child of the unixShmNode */ |
| 3876 | p->pShmNode = pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3877 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3878 | p->id = pShmNode->nextShmId++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3879 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3880 | pShmNode->nRef++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3881 | pDbFd->pShm = p; |
| 3882 | unixLeaveMutex(); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 3883 | |
| 3884 | /* The reference count on pShmNode has already been incremented under |
| 3885 | ** the cover of the unixEnterMutex() mutex and the pointer from the |
| 3886 | ** new (struct unixShm) object to the pShmNode has been set. All that is |
| 3887 | ** left to do is to link the new object into the linked list starting |
| 3888 | ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex |
| 3889 | ** mutex. |
| 3890 | */ |
| 3891 | sqlite3_mutex_enter(pShmNode->mutex); |
| 3892 | p->pNext = pShmNode->pFirst; |
| 3893 | pShmNode->pFirst = p; |
| 3894 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3895 | return SQLITE_OK; |
| 3896 | |
| 3897 | /* Jump here on any error */ |
| 3898 | shm_open_err: |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3899 | unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3900 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3901 | unixLeaveMutex(); |
| 3902 | return rc; |
| 3903 | } |
| 3904 | |
| 3905 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3906 | ** This function is called to obtain a pointer to region iRegion of the |
| 3907 | ** shared-memory associated with the database file fd. Shared-memory regions |
| 3908 | ** are numbered starting from zero. Each shared-memory region is szRegion |
| 3909 | ** bytes in size. |
| 3910 | ** |
| 3911 | ** If an error occurs, an error code is returned and *pp is set to NULL. |
| 3912 | ** |
| 3913 | ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory |
| 3914 | ** region has not been allocated (by any client, including one running in a |
| 3915 | ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 3916 | ** bExtend is non-zero and the requested shared-memory region has not yet |
| 3917 | ** been allocated, it is allocated by this function. |
| 3918 | ** |
| 3919 | ** If the shared-memory region has already been allocated or is allocated by |
| 3920 | ** this call as described above, then it is mapped into this processes |
| 3921 | ** address space (if it is not already), *pp is set to point to the mapped |
| 3922 | ** memory and SQLITE_OK returned. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3923 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3924 | static int unixShmMap( |
| 3925 | sqlite3_file *fd, /* Handle open on database file */ |
| 3926 | int iRegion, /* Region to retrieve */ |
| 3927 | int szRegion, /* Size of regions */ |
| 3928 | int bExtend, /* True to extend file if necessary */ |
| 3929 | void volatile **pp /* OUT: Mapped memory */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3930 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3931 | unixFile *pDbFd = (unixFile*)fd; |
| 3932 | unixShm *p; |
| 3933 | unixShmNode *pShmNode; |
| 3934 | int rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3935 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3936 | /* If the shared-memory file has not yet been opened, open it now. */ |
| 3937 | if( pDbFd->pShm==0 ){ |
| 3938 | rc = unixOpenSharedMemory(pDbFd); |
| 3939 | if( rc!=SQLITE_OK ) return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3940 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3941 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3942 | p = pDbFd->pShm; |
| 3943 | pShmNode = p->pShmNode; |
| 3944 | sqlite3_mutex_enter(pShmNode->mutex); |
| 3945 | assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3946 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 3947 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 3948 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3949 | |
| 3950 | if( pShmNode->nRegion<=iRegion ){ |
| 3951 | char **apNew; /* New apRegion[] array */ |
| 3952 | int nByte = (iRegion+1)*szRegion; /* Minimum required file size */ |
| 3953 | struct stat sStat; /* Used by fstat() */ |
| 3954 | |
| 3955 | pShmNode->szRegion = szRegion; |
| 3956 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3957 | if( pShmNode->h>=0 ){ |
| 3958 | /* The requested region is not mapped into this processes address space. |
| 3959 | ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 3960 | ** large enough to contain the requested region). |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3961 | */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3962 | if( osFstat(pShmNode->h, &sStat) ){ |
| 3963 | rc = SQLITE_IOERR_SHMSIZE; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3964 | goto shmpage_out; |
| 3965 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3966 | |
| 3967 | if( sStat.st_size<nByte ){ |
| 3968 | /* The requested memory region does not exist. If bExtend is set to |
| 3969 | ** false, exit early. *pp will be set to NULL and SQLITE_OK returned. |
| 3970 | ** |
| 3971 | ** Alternatively, if bExtend is true, use ftruncate() to allocate |
| 3972 | ** the requested memory region. |
| 3973 | */ |
| 3974 | if( !bExtend ) goto shmpage_out; |
| 3975 | if( robust_ftruncate(pShmNode->h, nByte) ){ |
| 3976 | rc = unixLogError(SQLITE_IOERR_SHMSIZE, "ftruncate", |
| 3977 | pShmNode->zFilename); |
| 3978 | goto shmpage_out; |
| 3979 | } |
| 3980 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3981 | } |
| 3982 | |
| 3983 | /* Map the requested memory region into this processes address space. */ |
| 3984 | apNew = (char **)sqlite3_realloc( |
| 3985 | pShmNode->apRegion, (iRegion+1)*sizeof(char *) |
| 3986 | ); |
| 3987 | if( !apNew ){ |
| 3988 | rc = SQLITE_IOERR_NOMEM; |
| 3989 | goto shmpage_out; |
| 3990 | } |
| 3991 | pShmNode->apRegion = apNew; |
| 3992 | while(pShmNode->nRegion<=iRegion){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3993 | void *pMem; |
| 3994 | if( pShmNode->h>=0 ){ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 3995 | pMem = mmap(0, szRegion, |
| 3996 | pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3997 | MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion |
| 3998 | ); |
| 3999 | if( pMem==MAP_FAILED ){ |
drh | 50990db | 2011-04-13 20:26:13 +0000 | [diff] [blame] | 4000 | rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4001 | goto shmpage_out; |
| 4002 | } |
| 4003 | }else{ |
| 4004 | pMem = sqlite3_malloc(szRegion); |
| 4005 | if( pMem==0 ){ |
| 4006 | rc = SQLITE_NOMEM; |
| 4007 | goto shmpage_out; |
| 4008 | } |
| 4009 | memset(pMem, 0, szRegion); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4010 | } |
| 4011 | pShmNode->apRegion[pShmNode->nRegion] = pMem; |
| 4012 | pShmNode->nRegion++; |
| 4013 | } |
| 4014 | } |
| 4015 | |
| 4016 | shmpage_out: |
| 4017 | if( pShmNode->nRegion>iRegion ){ |
| 4018 | *pp = pShmNode->apRegion[iRegion]; |
| 4019 | }else{ |
| 4020 | *pp = 0; |
| 4021 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4022 | if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4023 | sqlite3_mutex_leave(pShmNode->mutex); |
| 4024 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4028 | ** Change the lock state for a shared-memory segment. |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4029 | ** |
| 4030 | ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little |
| 4031 | ** different here than in posix. In xShmLock(), one can go from unlocked |
| 4032 | ** to shared and back or from unlocked to exclusive and back. But one may |
| 4033 | ** not go from shared to exclusive or from exclusive to shared. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4034 | */ |
| 4035 | static int unixShmLock( |
| 4036 | sqlite3_file *fd, /* Database file holding the shared memory */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4037 | int ofst, /* First lock to acquire or release */ |
| 4038 | int n, /* Number of locks to acquire or release */ |
| 4039 | int flags /* What to do with the lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4040 | ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4041 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 4042 | unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 4043 | unixShm *pX; /* For looping over all siblings */ |
| 4044 | unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 4045 | int rc = SQLITE_OK; /* Result code */ |
| 4046 | u16 mask; /* Mask of locks to take or release */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4047 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4048 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4049 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4050 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4051 | assert( n>=1 ); |
| 4052 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 4053 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 4054 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 4055 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 4056 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4057 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4058 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4059 | |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4060 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4061 | assert( n>1 || mask==(1<<ofst) ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4062 | sqlite3_mutex_enter(pShmNode->mutex); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4063 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 4064 | u16 allMask = 0; /* Mask of locks held by siblings */ |
| 4065 | |
| 4066 | /* See if any siblings hold this same lock */ |
| 4067 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 4068 | if( pX==p ) continue; |
| 4069 | assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
| 4070 | allMask |= pX->sharedMask; |
| 4071 | } |
| 4072 | |
| 4073 | /* Unlock the system-level locks */ |
| 4074 | if( (mask & allMask)==0 ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4075 | rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4076 | }else{ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4077 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4078 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4079 | |
| 4080 | /* Undo the local locks */ |
| 4081 | if( rc==SQLITE_OK ){ |
| 4082 | p->exclMask &= ~mask; |
| 4083 | p->sharedMask &= ~mask; |
| 4084 | } |
| 4085 | }else if( flags & SQLITE_SHM_SHARED ){ |
| 4086 | u16 allShared = 0; /* Union of locks held by connections other than "p" */ |
| 4087 | |
| 4088 | /* Find out which shared locks are already held by sibling connections. |
| 4089 | ** If any sibling already holds an exclusive lock, go ahead and return |
| 4090 | ** SQLITE_BUSY. |
| 4091 | */ |
| 4092 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4093 | if( (pX->exclMask & mask)!=0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4094 | rc = SQLITE_BUSY; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4095 | break; |
| 4096 | } |
| 4097 | allShared |= pX->sharedMask; |
| 4098 | } |
| 4099 | |
| 4100 | /* Get shared locks at the system level, if necessary */ |
| 4101 | if( rc==SQLITE_OK ){ |
| 4102 | if( (allShared & mask)==0 ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4103 | rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4104 | }else{ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4105 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4106 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4107 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4108 | |
| 4109 | /* Get the local shared locks */ |
| 4110 | if( rc==SQLITE_OK ){ |
| 4111 | p->sharedMask |= mask; |
| 4112 | } |
| 4113 | }else{ |
| 4114 | /* Make sure no sibling connections hold locks that will block this |
| 4115 | ** lock. If any do, return SQLITE_BUSY right away. |
| 4116 | */ |
| 4117 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4118 | if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ |
| 4119 | rc = SQLITE_BUSY; |
| 4120 | break; |
| 4121 | } |
| 4122 | } |
| 4123 | |
| 4124 | /* Get the exclusive locks at the system level. Then if successful |
| 4125 | ** also mark the local connection as being locked. |
| 4126 | */ |
| 4127 | if( rc==SQLITE_OK ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4128 | rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4129 | if( rc==SQLITE_OK ){ |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4130 | assert( (p->sharedMask & mask)==0 ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4131 | p->exclMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4132 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4133 | } |
| 4134 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4135 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 4136 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
| 4137 | p->id, getpid(), p->sharedMask, p->exclMask)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4138 | return rc; |
| 4139 | } |
| 4140 | |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4141 | /* |
| 4142 | ** Implement a memory barrier or memory fence on shared memory. |
| 4143 | ** |
| 4144 | ** All loads and stores begun before the barrier must complete before |
| 4145 | ** any load or store begun after the barrier. |
| 4146 | */ |
| 4147 | static void unixShmBarrier( |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4148 | sqlite3_file *fd /* Database file holding the shared memory */ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4149 | ){ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 4150 | UNUSED_PARAMETER(fd); |
drh | b29ad85 | 2010-06-01 00:03:57 +0000 | [diff] [blame] | 4151 | unixEnterMutex(); |
| 4152 | unixLeaveMutex(); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4153 | } |
| 4154 | |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4155 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4156 | ** Close a connection to shared-memory. Delete the underlying |
| 4157 | ** storage if deleteFlag is true. |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 4158 | ** |
| 4159 | ** If there is no shared memory associated with the connection then this |
| 4160 | ** routine is a harmless no-op. |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4161 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4162 | static int unixShmUnmap( |
| 4163 | sqlite3_file *fd, /* The underlying database file */ |
| 4164 | int deleteFlag /* Delete shared-memory if true */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4165 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4166 | unixShm *p; /* The connection to be closed */ |
| 4167 | unixShmNode *pShmNode; /* The underlying shared-memory file */ |
| 4168 | unixShm **pp; /* For looping over sibling connections */ |
| 4169 | unixFile *pDbFd; /* The underlying database file */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4170 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4171 | pDbFd = (unixFile*)fd; |
| 4172 | p = pDbFd->pShm; |
| 4173 | if( p==0 ) return SQLITE_OK; |
| 4174 | pShmNode = p->pShmNode; |
| 4175 | |
| 4176 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4177 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4178 | |
| 4179 | /* Remove connection p from the set of connections associated |
| 4180 | ** with pShmNode */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4181 | sqlite3_mutex_enter(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4182 | for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 4183 | *pp = p->pNext; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4184 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4185 | /* Free the connection p */ |
| 4186 | sqlite3_free(p); |
| 4187 | pDbFd->pShm = 0; |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4188 | sqlite3_mutex_leave(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4189 | |
| 4190 | /* If pShmNode->nRef has reached 0, then close the underlying |
| 4191 | ** shared-memory file, too */ |
| 4192 | unixEnterMutex(); |
| 4193 | assert( pShmNode->nRef>0 ); |
| 4194 | pShmNode->nRef--; |
| 4195 | if( pShmNode->nRef==0 ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 4196 | if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4197 | unixShmPurge(pDbFd); |
| 4198 | } |
| 4199 | unixLeaveMutex(); |
| 4200 | |
| 4201 | return SQLITE_OK; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4202 | } |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4203 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4204 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4205 | #else |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 4206 | # define unixShmMap 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4207 | # define unixShmLock 0 |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4208 | # define unixShmBarrier 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4209 | # define unixShmUnmap 0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4210 | #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 4211 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4212 | /* |
| 4213 | ** Here ends the implementation of all sqlite3_file methods. |
| 4214 | ** |
| 4215 | ********************** End sqlite3_file Methods ******************************* |
| 4216 | ******************************************************************************/ |
| 4217 | |
| 4218 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4219 | ** This division contains definitions of sqlite3_io_methods objects that |
| 4220 | ** implement various file locking strategies. It also contains definitions |
| 4221 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 4222 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 4223 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 4224 | ** the correct finder-function for that VFS. |
| 4225 | ** |
| 4226 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 4227 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 4228 | ** looks at the filesystem type and tries to guess the best locking |
| 4229 | ** strategy from that. |
| 4230 | ** |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4231 | ** For finder-funtion F, two objects are created: |
| 4232 | ** |
| 4233 | ** (1) The real finder-function named "FImpt()". |
| 4234 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4235 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4236 | ** |
| 4237 | ** |
| 4238 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 4239 | ** objects. We have to do this instead of letting pAppData point |
| 4240 | ** directly at the finder-function since C90 rules prevent a void* |
| 4241 | ** from be cast into a function pointer. |
| 4242 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4243 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4244 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4245 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4246 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 4247 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 4248 | ** |
| 4249 | ** * An I/O method finder function called FINDER that returns a pointer |
| 4250 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4251 | */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4252 | #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4253 | static const sqlite3_io_methods METHOD = { \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4254 | VERSION, /* iVersion */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4255 | CLOSE, /* xClose */ \ |
| 4256 | unixRead, /* xRead */ \ |
| 4257 | unixWrite, /* xWrite */ \ |
| 4258 | unixTruncate, /* xTruncate */ \ |
| 4259 | unixSync, /* xSync */ \ |
| 4260 | unixFileSize, /* xFileSize */ \ |
| 4261 | LOCK, /* xLock */ \ |
| 4262 | UNLOCK, /* xUnlock */ \ |
| 4263 | CKLOCK, /* xCheckReservedLock */ \ |
| 4264 | unixFileControl, /* xFileControl */ \ |
| 4265 | unixSectorSize, /* xSectorSize */ \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4266 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 4267 | unixShmMap, /* xShmMap */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4268 | unixShmLock, /* xShmLock */ \ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4269 | unixShmBarrier, /* xShmBarrier */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4270 | unixShmUnmap /* xShmUnmap */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4271 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4272 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 4273 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4274 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4275 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4276 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4277 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4278 | |
| 4279 | /* |
| 4280 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 4281 | ** locking strategies. Functions that return pointers to these methods |
| 4282 | ** are also created. |
| 4283 | */ |
| 4284 | IOMETHODS( |
| 4285 | posixIoFinder, /* Finder function name */ |
| 4286 | posixIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4287 | 2, /* shared memory is enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4288 | unixClose, /* xClose method */ |
| 4289 | unixLock, /* xLock method */ |
| 4290 | unixUnlock, /* xUnlock method */ |
| 4291 | unixCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4292 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4293 | IOMETHODS( |
| 4294 | nolockIoFinder, /* Finder function name */ |
| 4295 | nolockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4296 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4297 | nolockClose, /* xClose method */ |
| 4298 | nolockLock, /* xLock method */ |
| 4299 | nolockUnlock, /* xUnlock method */ |
| 4300 | nolockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4301 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4302 | IOMETHODS( |
| 4303 | dotlockIoFinder, /* Finder function name */ |
| 4304 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4305 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4306 | dotlockClose, /* xClose method */ |
| 4307 | dotlockLock, /* xLock method */ |
| 4308 | dotlockUnlock, /* xUnlock method */ |
| 4309 | dotlockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4310 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4311 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4312 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4313 | IOMETHODS( |
| 4314 | flockIoFinder, /* Finder function name */ |
| 4315 | flockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4316 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4317 | flockClose, /* xClose method */ |
| 4318 | flockLock, /* xLock method */ |
| 4319 | flockUnlock, /* xUnlock method */ |
| 4320 | flockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4321 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4322 | #endif |
| 4323 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4324 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4325 | IOMETHODS( |
| 4326 | semIoFinder, /* Finder function name */ |
| 4327 | semIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4328 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4329 | semClose, /* xClose method */ |
| 4330 | semLock, /* xLock method */ |
| 4331 | semUnlock, /* xUnlock method */ |
| 4332 | semCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4333 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4334 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4335 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4336 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4337 | IOMETHODS( |
| 4338 | afpIoFinder, /* Finder function name */ |
| 4339 | afpIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4340 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4341 | afpClose, /* xClose method */ |
| 4342 | afpLock, /* xLock method */ |
| 4343 | afpUnlock, /* xUnlock method */ |
| 4344 | afpCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4345 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4346 | #endif |
| 4347 | |
| 4348 | /* |
| 4349 | ** The proxy locking method is a "super-method" in the sense that it |
| 4350 | ** opens secondary file descriptors for the conch and lock files and |
| 4351 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 4352 | ** secondary files. For this reason, the division that implements |
| 4353 | ** proxy locking is located much further down in the file. But we need |
| 4354 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 4355 | ** for proxy locking here. So we forward declare the I/O methods. |
| 4356 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4357 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4358 | static int proxyClose(sqlite3_file*); |
| 4359 | static int proxyLock(sqlite3_file*, int); |
| 4360 | static int proxyUnlock(sqlite3_file*, int); |
| 4361 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4362 | IOMETHODS( |
| 4363 | proxyIoFinder, /* Finder function name */ |
| 4364 | proxyIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4365 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4366 | proxyClose, /* xClose method */ |
| 4367 | proxyLock, /* xLock method */ |
| 4368 | proxyUnlock, /* xUnlock method */ |
| 4369 | proxyCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4370 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4371 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4372 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4373 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 4374 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 4375 | IOMETHODS( |
| 4376 | nfsIoFinder, /* Finder function name */ |
| 4377 | nfsIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4378 | 1, /* shared memory is disabled */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4379 | unixClose, /* xClose method */ |
| 4380 | unixLock, /* xLock method */ |
| 4381 | nfsUnlock, /* xUnlock method */ |
| 4382 | unixCheckReservedLock /* xCheckReservedLock method */ |
| 4383 | ) |
| 4384 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4385 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4386 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4387 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4388 | ** This "finder" function attempts to determine the best locking strategy |
| 4389 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4390 | ** object that implements that strategy. |
| 4391 | ** |
| 4392 | ** This is for MacOSX only. |
| 4393 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4394 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4395 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4396 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4397 | ){ |
| 4398 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4399 | const char *zFilesystem; /* Filesystem type name */ |
| 4400 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4401 | } aMap[] = { |
| 4402 | { "hfs", &posixIoMethods }, |
| 4403 | { "ufs", &posixIoMethods }, |
| 4404 | { "afpfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4405 | { "smbfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4406 | { "webdav", &nolockIoMethods }, |
| 4407 | { 0, 0 } |
| 4408 | }; |
| 4409 | int i; |
| 4410 | struct statfs fsInfo; |
| 4411 | struct flock lockInfo; |
| 4412 | |
| 4413 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4414 | /* If filePath==NULL that means we are dealing with a transient file |
| 4415 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4416 | return &nolockIoMethods; |
| 4417 | } |
| 4418 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 4419 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 4420 | return &nolockIoMethods; |
| 4421 | } |
| 4422 | for(i=0; aMap[i].zFilesystem; i++){ |
| 4423 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 4424 | return aMap[i].pMethods; |
| 4425 | } |
| 4426 | } |
| 4427 | } |
| 4428 | |
| 4429 | /* Default case. Handles, amongst others, "nfs". |
| 4430 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 4431 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4432 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4433 | lockInfo.l_len = 1; |
| 4434 | lockInfo.l_start = 0; |
| 4435 | lockInfo.l_whence = SEEK_SET; |
| 4436 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4437 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4438 | if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 4439 | return &nfsIoMethods; |
| 4440 | } else { |
| 4441 | return &posixIoMethods; |
| 4442 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4443 | }else{ |
| 4444 | return &dotlockIoMethods; |
| 4445 | } |
| 4446 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4447 | static const sqlite3_io_methods |
| 4448 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4449 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4450 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4451 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4452 | #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE |
| 4453 | /* |
| 4454 | ** This "finder" function attempts to determine the best locking strategy |
| 4455 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
| 4456 | ** object that implements that strategy. |
| 4457 | ** |
| 4458 | ** This is for VXWorks only. |
| 4459 | */ |
| 4460 | static const sqlite3_io_methods *autolockIoFinderImpl( |
| 4461 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4462 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4463 | ){ |
| 4464 | struct flock lockInfo; |
| 4465 | |
| 4466 | if( !filePath ){ |
| 4467 | /* If filePath==NULL that means we are dealing with a transient file |
| 4468 | ** that does not need to be locked. */ |
| 4469 | return &nolockIoMethods; |
| 4470 | } |
| 4471 | |
| 4472 | /* Test if fcntl() is supported and use POSIX style locks. |
| 4473 | ** Otherwise fall back to the named semaphore method. |
| 4474 | */ |
| 4475 | lockInfo.l_len = 1; |
| 4476 | lockInfo.l_start = 0; |
| 4477 | lockInfo.l_whence = SEEK_SET; |
| 4478 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4479 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4480 | return &posixIoMethods; |
| 4481 | }else{ |
| 4482 | return &semIoMethods; |
| 4483 | } |
| 4484 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4485 | static const sqlite3_io_methods |
| 4486 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4487 | |
| 4488 | #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */ |
| 4489 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4490 | /* |
| 4491 | ** An abstract type for a pointer to a IO method finder function: |
| 4492 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4493 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4494 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4495 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4496 | /**************************************************************************** |
| 4497 | **************************** sqlite3_vfs methods **************************** |
| 4498 | ** |
| 4499 | ** This division contains the implementation of methods on the |
| 4500 | ** sqlite3_vfs object. |
| 4501 | */ |
| 4502 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4503 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4504 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4505 | */ |
| 4506 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4507 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4508 | int h, /* Open file descriptor of file being opened */ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 4509 | int syncDir, /* True to sync directory on first sync */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4510 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4511 | const char *zFilename, /* Name of the file being opened */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4512 | int noLock, /* Omit locking if true */ |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 4513 | int isDelete, /* Delete on close if true */ |
| 4514 | int isReadOnly /* True if the file is opened read-only */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4515 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4516 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4517 | unixFile *pNew = (unixFile *)pId; |
| 4518 | int rc = SQLITE_OK; |
| 4519 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4520 | assert( pNew->pInode==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4521 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4522 | /* Parameter isDelete is only used on vxworks. Express this explicitly |
| 4523 | ** here to prevent compiler warnings about unused parameters. |
danielk1977 | a03396a | 2008-11-19 14:35:46 +0000 | [diff] [blame] | 4524 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4525 | UNUSED_PARAMETER(isDelete); |
danielk1977 | a03396a | 2008-11-19 14:35:46 +0000 | [diff] [blame] | 4526 | |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4527 | /* Usually the path zFilename should not be a relative pathname. The |
| 4528 | ** exception is when opening the proxy "conch" file in builds that |
| 4529 | ** include the special Apple locking styles. |
| 4530 | */ |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4531 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | f7f55ed | 2010-10-05 18:22:47 +0000 | [diff] [blame] | 4532 | assert( zFilename==0 || zFilename[0]=='/' |
| 4533 | || pVfs->pAppData==(void*)&autolockIoFinder ); |
| 4534 | #else |
| 4535 | assert( zFilename==0 || zFilename[0]=='/' ); |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4536 | #endif |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4537 | |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame^] | 4538 | /* No locking occurs in temporary files */ |
| 4539 | assert( zFilename!=0 || noLock ); |
| 4540 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4541 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4542 | pNew->h = h; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4543 | pNew->zPath = zFilename; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 4544 | if( memcmp(pVfs->zName,"unix-excl",10)==0 ){ |
| 4545 | pNew->ctrlFlags = UNIXFILE_EXCL; |
| 4546 | }else{ |
| 4547 | pNew->ctrlFlags = 0; |
| 4548 | } |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 4549 | if( isReadOnly ){ |
| 4550 | pNew->ctrlFlags |= UNIXFILE_RDONLY; |
| 4551 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 4552 | if( syncDir ){ |
| 4553 | pNew->ctrlFlags |= UNIXFILE_DIRSYNC; |
| 4554 | } |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 4555 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4556 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 4557 | pNew->pId = vxworksFindFileId(zFilename); |
| 4558 | if( pNew->pId==0 ){ |
| 4559 | noLock = 1; |
| 4560 | rc = SQLITE_NOMEM; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4561 | } |
| 4562 | #endif |
| 4563 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4564 | if( noLock ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4565 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4566 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4567 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4568 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4569 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 4570 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 4571 | ** zFilename remains valid until file is closed, to support */ |
| 4572 | pNew->lockingContext = (void*)zFilename; |
| 4573 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4574 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4575 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4576 | if( pLockingStyle == &posixIoMethods |
| 4577 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 4578 | || pLockingStyle == &nfsIoMethods |
| 4579 | #endif |
| 4580 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4581 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4582 | rc = findInodeInfo(pNew, &pNew->pInode); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4583 | if( rc!=SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4584 | /* If an error occured in findInodeInfo(), close the file descriptor |
| 4585 | ** immediately, before releasing the mutex. findInodeInfo() may fail |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4586 | ** in two scenarios: |
| 4587 | ** |
| 4588 | ** (a) A call to fstat() failed. |
| 4589 | ** (b) A malloc failed. |
| 4590 | ** |
| 4591 | ** Scenario (b) may only occur if the process is holding no other |
| 4592 | ** file descriptors open on the same file. If there were other file |
| 4593 | ** descriptors on this file, then no malloc would be required by |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4594 | ** findInodeInfo(). If this is the case, it is quite safe to close |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4595 | ** handle h - as it is guaranteed that no posix locks will be released |
| 4596 | ** by doing so. |
| 4597 | ** |
| 4598 | ** If scenario (a) caused the error then things are not so safe. The |
| 4599 | ** implicit assumption here is that if fstat() fails, things are in |
| 4600 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 4601 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4602 | robust_close(pNew, h, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4603 | h = -1; |
| 4604 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4605 | unixLeaveMutex(); |
| 4606 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4607 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4608 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 4609 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4610 | /* AFP locking uses the file path so it needs to be included in |
| 4611 | ** the afpLockingContext. |
| 4612 | */ |
| 4613 | afpLockingContext *pCtx; |
| 4614 | pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 4615 | if( pCtx==0 ){ |
| 4616 | rc = SQLITE_NOMEM; |
| 4617 | }else{ |
| 4618 | /* NB: zFilename exists and remains valid until the file is closed |
| 4619 | ** according to requirement F11141. So we do not need to make a |
| 4620 | ** copy of the filename. */ |
| 4621 | pCtx->dbPath = zFilename; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4622 | pCtx->reserved = 0; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4623 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4624 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4625 | rc = findInodeInfo(pNew, &pNew->pInode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4626 | if( rc!=SQLITE_OK ){ |
| 4627 | sqlite3_free(pNew->lockingContext); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4628 | robust_close(pNew, h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4629 | h = -1; |
| 4630 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4631 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4632 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4633 | } |
| 4634 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4635 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4636 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 4637 | /* Dotfile locking uses the file path so it needs to be included in |
| 4638 | ** the dotlockLockingContext |
| 4639 | */ |
| 4640 | char *zLockFile; |
| 4641 | int nFilename; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame^] | 4642 | assert( zFilename!=0 ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4643 | nFilename = (int)strlen(zFilename) + 6; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4644 | zLockFile = (char *)sqlite3_malloc(nFilename); |
| 4645 | if( zLockFile==0 ){ |
| 4646 | rc = SQLITE_NOMEM; |
| 4647 | }else{ |
| 4648 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4649 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4650 | pNew->lockingContext = zLockFile; |
| 4651 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4652 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4653 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4654 | else if( pLockingStyle == &semIoMethods ){ |
| 4655 | /* Named semaphore locking uses the file path so it needs to be |
| 4656 | ** included in the semLockingContext |
| 4657 | */ |
| 4658 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4659 | rc = findInodeInfo(pNew, &pNew->pInode); |
| 4660 | if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 4661 | char *zSemName = pNew->pInode->aSemName; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4662 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 4663 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4664 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 4665 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4666 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4667 | pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 4668 | if( pNew->pInode->pSem == SEM_FAILED ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4669 | rc = SQLITE_NOMEM; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4670 | pNew->pInode->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4671 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4672 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4673 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4674 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4675 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 4676 | |
| 4677 | pNew->lastErrno = 0; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4678 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4679 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4680 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
drh | 309e655 | 2010-02-05 18:00:26 +0000 | [diff] [blame] | 4681 | h = -1; |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 4682 | osUnlink(zFilename); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4683 | isDelete = 0; |
| 4684 | } |
| 4685 | pNew->isDelete = isDelete; |
| 4686 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4687 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4688 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4689 | }else{ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4690 | pNew->pMethod = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4691 | OpenCounter(+1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4692 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4693 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 4694 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 4695 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4696 | /* |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4697 | ** Return the name of a directory in which to put temporary files. |
| 4698 | ** If no suitable temporary file directory can be found, return NULL. |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4699 | */ |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4700 | static const char *unixTempFileDir(void){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4701 | static const char *azDirs[] = { |
| 4702 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4703 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4704 | "/var/tmp", |
| 4705 | "/usr/tmp", |
| 4706 | "/tmp", |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4707 | 0 /* List terminator */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4708 | }; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4709 | unsigned int i; |
| 4710 | struct stat buf; |
| 4711 | const char *zDir = 0; |
| 4712 | |
| 4713 | azDirs[0] = sqlite3_temp_directory; |
| 4714 | if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); |
drh | 19515c8 | 2010-06-19 23:53:11 +0000 | [diff] [blame] | 4715 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){ |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4716 | if( zDir==0 ) continue; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4717 | if( osStat(zDir, &buf) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4718 | if( !S_ISDIR(buf.st_mode) ) continue; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4719 | if( osAccess(zDir, 07) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4720 | break; |
| 4721 | } |
| 4722 | return zDir; |
| 4723 | } |
| 4724 | |
| 4725 | /* |
| 4726 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 4727 | ** by the calling process and must be big enough to hold at least |
| 4728 | ** pVfs->mxPathname bytes. |
| 4729 | */ |
| 4730 | static int unixGetTempname(int nBuf, char *zBuf){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4731 | static const unsigned char zChars[] = |
| 4732 | "abcdefghijklmnopqrstuvwxyz" |
| 4733 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 4734 | "0123456789"; |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 4735 | unsigned int i, j; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4736 | const char *zDir; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4737 | |
| 4738 | /* It's odd to simulate an io-error here, but really this is just |
| 4739 | ** using the io-error infrastructure to test that SQLite handles this |
| 4740 | ** function failing. |
| 4741 | */ |
| 4742 | SimulateIOError( return SQLITE_IOERR ); |
| 4743 | |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4744 | zDir = unixTempFileDir(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4745 | if( zDir==0 ) zDir = "."; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4746 | |
| 4747 | /* Check that the output buffer is large enough for the temporary file |
| 4748 | ** name. If it is not, return SQLITE_ERROR. |
| 4749 | */ |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 4750 | if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4751 | return SQLITE_ERROR; |
| 4752 | } |
| 4753 | |
| 4754 | do{ |
| 4755 | sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4756 | j = (int)strlen(zBuf); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4757 | sqlite3_randomness(15, &zBuf[j]); |
| 4758 | for(i=0; i<15; i++, j++){ |
| 4759 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 4760 | } |
| 4761 | zBuf[j] = 0; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4762 | }while( osAccess(zBuf,0)==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4763 | return SQLITE_OK; |
| 4764 | } |
| 4765 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4766 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 4767 | /* |
| 4768 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 4769 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 4770 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 4771 | */ |
| 4772 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 4773 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 4774 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4775 | /* |
| 4776 | ** Search for an unused file descriptor that was opened on the database |
| 4777 | ** file (not a journal or master-journal file) identified by pathname |
| 4778 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 4779 | ** argument to this function. |
| 4780 | ** |
| 4781 | ** Such a file descriptor may exist if a database connection was closed |
| 4782 | ** but the associated file descriptor could not be closed because some |
| 4783 | ** other file descriptor open on the same file is holding a file-lock. |
| 4784 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 4785 | ** describing "Posix Advisory Locking" at the start of this file for |
| 4786 | ** further details. Also, ticket #4018. |
| 4787 | ** |
| 4788 | ** If a suitable file descriptor is found, then it is returned. If no |
| 4789 | ** such file descriptor is located, -1 is returned. |
| 4790 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4791 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 4792 | UnixUnusedFd *pUnused = 0; |
| 4793 | |
| 4794 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 4795 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 4796 | ** but because no way to test it is currently available. It is better |
| 4797 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 4798 | ** feature. */ |
| 4799 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4800 | struct stat sStat; /* Results of stat() call */ |
| 4801 | |
| 4802 | /* A stat() call may fail for various reasons. If this happens, it is |
| 4803 | ** almost certain that an open() call on the same path will also fail. |
| 4804 | ** For this reason, if an error occurs in the stat() call here, it is |
| 4805 | ** ignored and -1 is returned. The caller will try to open a new file |
| 4806 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 4807 | ** |
| 4808 | ** Even if a subsequent open() call does succeed, the consequences of |
| 4809 | ** not searching for a resusable file descriptor are not dire. */ |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 4810 | if( 0==osStat(zPath, &sStat) ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4811 | unixInodeInfo *pInode; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4812 | |
| 4813 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4814 | pInode = inodeList; |
| 4815 | while( pInode && (pInode->fileId.dev!=sStat.st_dev |
| 4816 | || pInode->fileId.ino!=sStat.st_ino) ){ |
| 4817 | pInode = pInode->pNext; |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 4818 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4819 | if( pInode ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4820 | UnixUnusedFd **pp; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4821 | for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4822 | pUnused = *pp; |
| 4823 | if( pUnused ){ |
| 4824 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4825 | } |
| 4826 | } |
| 4827 | unixLeaveMutex(); |
| 4828 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4829 | #endif /* if !OS_VXWORKS */ |
| 4830 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4831 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4832 | |
| 4833 | /* |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4834 | ** This function is called by unixOpen() to determine the unix permissions |
drh | f65bc91 | 2010-07-14 20:51:34 +0000 | [diff] [blame] | 4835 | ** 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] | 4836 | ** and a value suitable for passing as the third argument to open(2) is |
| 4837 | ** written to *pMode. If an IO error occurs, an SQLite error code is |
| 4838 | ** returned and the value of *pMode is not modified. |
| 4839 | ** |
| 4840 | ** If the file being opened is a temporary file, it is always created with |
| 4841 | ** the octal permissions 0600 (read/writable by owner only). If the file |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4842 | ** is a database or master journal file, it is created with the permissions |
| 4843 | ** mask SQLITE_DEFAULT_FILE_PERMISSIONS. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4844 | ** |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4845 | ** Finally, if the file being opened is a WAL or regular journal file, then |
| 4846 | ** this function queries the file-system for the permissions on the |
| 4847 | ** corresponding database file and sets *pMode to this value. Whenever |
| 4848 | ** possible, WAL and journal files are created using the same permissions |
| 4849 | ** as the associated database file. |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 4850 | ** |
| 4851 | ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the |
| 4852 | ** original filename is unavailable. But 8_3_NAMES is only used for |
| 4853 | ** FAT filesystems and permissions do not matter there, so just use |
| 4854 | ** the default permissions. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4855 | */ |
| 4856 | static int findCreateFileMode( |
| 4857 | const char *zPath, /* Path of file (possibly) being created */ |
| 4858 | int flags, /* Flags passed as 4th argument to xOpen() */ |
| 4859 | mode_t *pMode /* OUT: Permissions to open file with */ |
| 4860 | ){ |
| 4861 | int rc = SQLITE_OK; /* Return Code */ |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 4862 | *pMode = SQLITE_DEFAULT_FILE_PERMISSIONS; |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4863 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4864 | char zDb[MAX_PATHNAME+1]; /* Database file path */ |
| 4865 | int nDb; /* Number of valid bytes in zDb */ |
| 4866 | struct stat sStat; /* Output of stat() on database file */ |
| 4867 | |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 4868 | /* zPath is a path to a WAL or journal file. The following block derives |
| 4869 | ** the path to the associated database file from zPath. This block handles |
| 4870 | ** the following naming conventions: |
| 4871 | ** |
| 4872 | ** "<path to db>-journal" |
| 4873 | ** "<path to db>-wal" |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 4874 | ** "<path to db>-journalNN" |
| 4875 | ** "<path to db>-walNN" |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 4876 | ** |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 4877 | ** where NN is a 4 digit decimal number. The NN naming schemes are |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 4878 | ** used by the test_multiplex.c module. |
| 4879 | */ |
| 4880 | nDb = sqlite3Strlen30(zPath) - 1; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 4881 | #ifdef SQLITE_ENABLE_8_3_NAMES |
| 4882 | while( nDb>0 && zPath[nDb]!='-' && zPath[nDb]!='/' ) nDb--; |
| 4883 | if( nDb==0 || zPath[nDb]=='/' ) return SQLITE_OK; |
| 4884 | #else |
| 4885 | while( zPath[nDb]!='-' ){ |
| 4886 | assert( nDb>0 ); |
| 4887 | assert( zPath[nDb]!='\n' ); |
| 4888 | nDb--; |
| 4889 | } |
| 4890 | #endif |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4891 | memcpy(zDb, zPath, nDb); |
| 4892 | zDb[nDb] = '\0'; |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 4893 | |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 4894 | if( 0==osStat(zDb, &sStat) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4895 | *pMode = sStat.st_mode & 0777; |
| 4896 | }else{ |
| 4897 | rc = SQLITE_IOERR_FSTAT; |
| 4898 | } |
| 4899 | }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 4900 | *pMode = 0600; |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4901 | } |
| 4902 | return rc; |
| 4903 | } |
| 4904 | |
| 4905 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4906 | ** Open the file zPath. |
| 4907 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4908 | ** Previously, the SQLite OS layer used three functions in place of this |
| 4909 | ** one: |
| 4910 | ** |
| 4911 | ** sqlite3OsOpenReadWrite(); |
| 4912 | ** sqlite3OsOpenReadOnly(); |
| 4913 | ** sqlite3OsOpenExclusive(); |
| 4914 | ** |
| 4915 | ** These calls correspond to the following combinations of flags: |
| 4916 | ** |
| 4917 | ** ReadWrite() -> (READWRITE | CREATE) |
| 4918 | ** ReadOnly() -> (READONLY) |
| 4919 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 4920 | ** |
| 4921 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 4922 | ** true, the file was configured to be automatically deleted when the |
| 4923 | ** file handle closed. To achieve the same effect using this new |
| 4924 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 4925 | ** OpenExclusive(). |
| 4926 | */ |
| 4927 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4928 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 4929 | const char *zPath, /* Pathname of file to be opened */ |
| 4930 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 4931 | int flags, /* Input flags to control the opening */ |
| 4932 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4933 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4934 | unixFile *p = (unixFile *)pFile; |
| 4935 | int fd = -1; /* File descriptor returned by open() */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4936 | int openFlags = 0; /* Flags to pass to open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4937 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4938 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4939 | int rc = SQLITE_OK; /* Function Return Code */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4940 | |
| 4941 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 4942 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 4943 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 4944 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 4945 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4946 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4947 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 4948 | #endif |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 4949 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 4950 | struct statfs fsInfo; |
| 4951 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4952 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4953 | /* If creating a master or main-file journal, this function will open |
| 4954 | ** a file-descriptor on the directory too. The first time unixSync() |
| 4955 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 4956 | */ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 4957 | int syncDir = (isCreate && ( |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4958 | eType==SQLITE_OPEN_MASTER_JOURNAL |
| 4959 | || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 4960 | || eType==SQLITE_OPEN_WAL |
| 4961 | )); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4962 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4963 | /* If argument zPath is a NULL pointer, this function is required to open |
| 4964 | ** a temporary file. Use this buffer to store the file name in. |
| 4965 | */ |
| 4966 | char zTmpname[MAX_PATHNAME+1]; |
| 4967 | const char *zName = zPath; |
| 4968 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4969 | /* Check the following statements are true: |
| 4970 | ** |
| 4971 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 4972 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 4973 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4974 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4975 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4976 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4977 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4978 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4979 | assert(isDelete==0 || isCreate); |
| 4980 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4981 | /* The main DB, main journal, WAL file and master journal are never |
| 4982 | ** automatically deleted. Nor are they ever temporary files. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4983 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 4984 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 4985 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4986 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4987 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4988 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 4989 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 4990 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 4991 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4992 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4993 | ); |
| 4994 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4995 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4996 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4997 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4998 | UnixUnusedFd *pUnused; |
| 4999 | pUnused = findReusableFd(zName, flags); |
| 5000 | if( pUnused ){ |
| 5001 | fd = pUnused->fd; |
| 5002 | }else{ |
dan | 6aa657f | 2009-08-24 18:57:58 +0000 | [diff] [blame] | 5003 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5004 | if( !pUnused ){ |
| 5005 | return SQLITE_NOMEM; |
| 5006 | } |
| 5007 | } |
| 5008 | p->pUnused = pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5009 | }else if( !zName ){ |
| 5010 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5011 | assert(isDelete && !syncDir); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5012 | rc = unixGetTempname(MAX_PATHNAME+1, zTmpname); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5013 | if( rc!=SQLITE_OK ){ |
| 5014 | return rc; |
| 5015 | } |
| 5016 | zName = zTmpname; |
| 5017 | } |
| 5018 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5019 | /* Determine the value of the flags parameter passed to POSIX function |
| 5020 | ** open(). These must be calculated even if open() is not called, as |
| 5021 | ** they may be stored as part of the file handle and used by the |
| 5022 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5023 | if( isReadonly ) openFlags |= O_RDONLY; |
| 5024 | if( isReadWrite ) openFlags |= O_RDWR; |
| 5025 | if( isCreate ) openFlags |= O_CREAT; |
| 5026 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 5027 | openFlags |= (O_LARGEFILE|O_BINARY); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5028 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5029 | if( fd<0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5030 | mode_t openMode; /* Permissions to create file with */ |
| 5031 | rc = findCreateFileMode(zName, flags, &openMode); |
| 5032 | if( rc!=SQLITE_OK ){ |
| 5033 | assert( !p->pUnused ); |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5034 | assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5035 | return rc; |
| 5036 | } |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5037 | fd = robust_open(zName, openFlags, openMode); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5038 | OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5039 | if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ |
| 5040 | /* Failed to open the file for read/write access. Try read-only. */ |
| 5041 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5042 | openFlags &= ~(O_RDWR|O_CREAT); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5043 | flags |= SQLITE_OPEN_READONLY; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5044 | openFlags |= O_RDONLY; |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 5045 | isReadonly = 1; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5046 | fd = robust_open(zName, openFlags, openMode); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5047 | } |
| 5048 | if( fd<0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5049 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5050 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5051 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5052 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5053 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5054 | if( pOutFlags ){ |
| 5055 | *pOutFlags = flags; |
| 5056 | } |
| 5057 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5058 | if( p->pUnused ){ |
| 5059 | p->pUnused->fd = fd; |
| 5060 | p->pUnused->flags = flags; |
| 5061 | } |
| 5062 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5063 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5064 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5065 | zPath = zName; |
| 5066 | #else |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5067 | osUnlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5068 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5069 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 5070 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5071 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5072 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 5073 | } |
| 5074 | #endif |
| 5075 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5076 | #ifdef FD_CLOEXEC |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5077 | osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5078 | #endif |
| 5079 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5080 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5081 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5082 | |
| 5083 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5084 | if( fstatfs(fd, &fsInfo) == -1 ){ |
| 5085 | ((unixFile*)pFile)->lastErrno = errno; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5086 | robust_close(p, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5087 | return SQLITE_IOERR_ACCESS; |
| 5088 | } |
| 5089 | if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 5090 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 5091 | } |
| 5092 | #endif |
| 5093 | |
| 5094 | #if SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5095 | #if SQLITE_PREFER_PROXY_LOCKING |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5096 | isAutoProxy = 1; |
| 5097 | #endif |
| 5098 | if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5099 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 5100 | int useProxy = 0; |
| 5101 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5102 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 5103 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5104 | if( envforce!=NULL ){ |
| 5105 | useProxy = atoi(envforce)>0; |
| 5106 | }else{ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5107 | if( statfs(zPath, &fsInfo) == -1 ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5108 | /* In theory, the close(fd) call is sub-optimal. If the file opened |
| 5109 | ** with fd is a database file, and there are other connections open |
| 5110 | ** on that file that are currently holding advisory locks on it, |
| 5111 | ** then the call to close() will cancel those locks. In practice, |
| 5112 | ** we're assuming that statfs() doesn't fail very often. At least |
| 5113 | ** not while other file descriptors opened by the same process on |
| 5114 | ** the same file are working. */ |
| 5115 | p->lastErrno = errno; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5116 | robust_close(p, fd, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5117 | rc = SQLITE_IOERR_ACCESS; |
| 5118 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5119 | } |
| 5120 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 5121 | } |
| 5122 | if( useProxy ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5123 | rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock, |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 5124 | isDelete, isReadonly); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5125 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5126 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5127 | if( rc!=SQLITE_OK ){ |
| 5128 | /* Use unixClose to clean up the resources added in fillInUnixFile |
| 5129 | ** and clear all the structure's references. Specifically, |
| 5130 | ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 5131 | */ |
| 5132 | unixClose(pFile); |
| 5133 | return rc; |
| 5134 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5135 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5136 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5137 | } |
| 5138 | } |
| 5139 | #endif |
| 5140 | |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5141 | rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock, |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 5142 | isDelete, isReadonly); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5143 | open_finished: |
| 5144 | if( rc!=SQLITE_OK ){ |
| 5145 | sqlite3_free(p->pUnused); |
| 5146 | } |
| 5147 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5148 | } |
| 5149 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5150 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5151 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5152 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 5153 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5154 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5155 | static int unixDelete( |
| 5156 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 5157 | const char *zPath, /* Name of file to be deleted */ |
| 5158 | int dirSync /* If true, fsync() directory after deleting file */ |
| 5159 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5160 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5161 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5162 | SimulateIOError(return SQLITE_IOERR_DELETE); |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5163 | if( osUnlink(zPath)==(-1) && errno!=ENOENT ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5164 | return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath); |
drh | 5d4feff | 2010-07-14 01:45:22 +0000 | [diff] [blame] | 5165 | } |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 5166 | #ifndef SQLITE_DISABLE_DIRSYNC |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5167 | if( dirSync ){ |
| 5168 | int fd; |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 5169 | rc = osOpenDirectory(zPath, &fd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5170 | if( rc==SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5171 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5172 | if( fsync(fd)==-1 ) |
| 5173 | #else |
| 5174 | if( fsync(fd) ) |
| 5175 | #endif |
| 5176 | { |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5177 | rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5178 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5179 | robust_close(0, fd, __LINE__); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 5180 | }else if( rc==SQLITE_CANTOPEN ){ |
| 5181 | rc = SQLITE_OK; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5182 | } |
| 5183 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 5184 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5185 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5186 | } |
| 5187 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5188 | /* |
| 5189 | ** Test the existance of or access permissions of file zPath. The |
| 5190 | ** test performed depends on the value of flags: |
| 5191 | ** |
| 5192 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 5193 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 5194 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 5195 | ** |
| 5196 | ** Otherwise return 0. |
| 5197 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5198 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5199 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 5200 | const char *zPath, /* Path of the file to examine */ |
| 5201 | int flags, /* What do we want to learn about the zPath file? */ |
| 5202 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5203 | ){ |
rse | 25c0d1a | 2007-09-20 08:38:14 +0000 | [diff] [blame] | 5204 | int amode = 0; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5205 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5206 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5207 | switch( flags ){ |
| 5208 | case SQLITE_ACCESS_EXISTS: |
| 5209 | amode = F_OK; |
| 5210 | break; |
| 5211 | case SQLITE_ACCESS_READWRITE: |
| 5212 | amode = W_OK|R_OK; |
| 5213 | break; |
drh | 50d3f90 | 2007-08-27 21:10:36 +0000 | [diff] [blame] | 5214 | case SQLITE_ACCESS_READ: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5215 | amode = R_OK; |
| 5216 | break; |
| 5217 | |
| 5218 | default: |
| 5219 | assert(!"Invalid flags argument"); |
| 5220 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5221 | *pResOut = (osAccess(zPath, amode)==0); |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 5222 | if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){ |
| 5223 | struct stat buf; |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 5224 | if( 0==osStat(zPath, &buf) && buf.st_size==0 ){ |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 5225 | *pResOut = 0; |
| 5226 | } |
| 5227 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5228 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5229 | } |
| 5230 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5231 | |
| 5232 | /* |
| 5233 | ** Turn a relative pathname into a full pathname. The relative path |
| 5234 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 5235 | ** zPath. |
| 5236 | ** |
| 5237 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 5238 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 5239 | ** this buffer before returning. |
| 5240 | */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 5241 | static int unixFullPathname( |
| 5242 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 5243 | const char *zPath, /* Possibly relative input path */ |
| 5244 | int nOut, /* Size of output buffer in bytes */ |
| 5245 | char *zOut /* Output buffer */ |
| 5246 | ){ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 5247 | |
| 5248 | /* It's odd to simulate an io-error here, but really this is just |
| 5249 | ** using the io-error infrastructure to test that SQLite handles this |
| 5250 | ** function failing. This function could fail if, for example, the |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5251 | ** current working directory has been unlinked. |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 5252 | */ |
| 5253 | SimulateIOError( return SQLITE_ERROR ); |
| 5254 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 5255 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 5256 | UNUSED_PARAMETER(pVfs); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5257 | |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 5258 | zOut[nOut-1] = '\0'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5259 | if( zPath[0]=='/' ){ |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 5260 | sqlite3_snprintf(nOut, zOut, "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5261 | }else{ |
| 5262 | int nCwd; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5263 | if( osGetcwd(zOut, nOut-1)==0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5264 | return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5265 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5266 | nCwd = (int)strlen(zOut); |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 5267 | sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5268 | } |
| 5269 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5270 | } |
| 5271 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 5272 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5273 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 5274 | /* |
| 5275 | ** Interfaces for opening a shared library, finding entry points |
| 5276 | ** within the shared library, and closing the shared library. |
| 5277 | */ |
| 5278 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5279 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 5280 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5281 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 5282 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 5283 | |
| 5284 | /* |
| 5285 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 5286 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 5287 | ** message is available, it is written to zBufOut. If no error message |
| 5288 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 5289 | ** error message. |
| 5290 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5291 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
dan | 3239053 | 2010-11-29 18:36:22 +0000 | [diff] [blame] | 5292 | const char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5293 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5294 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5295 | zErr = dlerror(); |
| 5296 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 5297 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5298 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5299 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5300 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5301 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 5302 | /* |
| 5303 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 5304 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 5305 | ** returns a void* which is really a pointer to a function. So how do we |
| 5306 | ** use dlsym() with -pedantic-errors? |
| 5307 | ** |
| 5308 | ** Variable x below is defined to be a pointer to a function taking |
| 5309 | ** parameters void* and const char* and returning a pointer to a function. |
| 5310 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 5311 | ** (That assignment requires a cast.) Then we call the function that |
| 5312 | ** x points to. |
| 5313 | ** |
| 5314 | ** This work-around is unlikely to work correctly on any system where |
| 5315 | ** you really cannot cast a function pointer into void*. But then, on the |
| 5316 | ** other hand, dlsym() will not work on such a system either, so we have |
| 5317 | ** not really lost anything. |
| 5318 | */ |
| 5319 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5320 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5321 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 5322 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5323 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5324 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 5325 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5326 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5327 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5328 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 5329 | #define unixDlOpen 0 |
| 5330 | #define unixDlError 0 |
| 5331 | #define unixDlSym 0 |
| 5332 | #define unixDlClose 0 |
| 5333 | #endif |
| 5334 | |
| 5335 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5336 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5337 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5338 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 5339 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 5340 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5341 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5342 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 5343 | ** errors. The reports issued by valgrind are incorrect - we would |
| 5344 | ** prefer that the randomness be increased by making use of the |
| 5345 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 5346 | ** some users. Rather than argue, it seems easier just to initialize |
| 5347 | ** the whole array and silence valgrind, even if that means less randomness |
| 5348 | ** in the random seed. |
| 5349 | ** |
| 5350 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 5351 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5352 | ** tests repeatable. |
| 5353 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5354 | memset(zBuf, 0, nBuf); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5355 | #if !defined(SQLITE_TEST) |
| 5356 | { |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5357 | int pid, fd; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5358 | fd = robust_open("/dev/urandom", O_RDONLY, 0); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5359 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 5360 | time_t t; |
| 5361 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5362 | memcpy(zBuf, &t, sizeof(t)); |
| 5363 | pid = getpid(); |
| 5364 | memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid)); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 5365 | assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf ); |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 5366 | nBuf = sizeof(t) + sizeof(pid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5367 | }else{ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 5368 | do{ nBuf = osRead(fd, zBuf, nBuf); }while( nBuf<0 && errno==EINTR ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5369 | robust_close(0, fd, __LINE__); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5370 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5371 | } |
| 5372 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 5373 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5374 | } |
| 5375 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5376 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5377 | /* |
| 5378 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5379 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 5380 | ** The return value is the number of microseconds of sleep actually |
| 5381 | ** requested from the underlying operating system, a number which |
| 5382 | ** might be greater than or equal to the argument, but not less |
| 5383 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5384 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5385 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5386 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5387 | struct timespec sp; |
| 5388 | |
| 5389 | sp.tv_sec = microseconds / 1000000; |
| 5390 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 5391 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 5392 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5393 | return microseconds; |
| 5394 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5395 | usleep(microseconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 5396 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5397 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5398 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5399 | int seconds = (microseconds+999999)/1000000; |
| 5400 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 5401 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 5402 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 5403 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 5404 | } |
| 5405 | |
| 5406 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5407 | ** The following variable, if set to a non-zero value, is interpreted as |
| 5408 | ** the number of seconds since 1970 and is used to set the result of |
| 5409 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5410 | */ |
| 5411 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5412 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5413 | #endif |
| 5414 | |
| 5415 | /* |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5416 | ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 5417 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 5418 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 5419 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 5420 | ** proleptic Gregorian calendar. |
| 5421 | ** |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5422 | ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
| 5423 | ** cannot be found. |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5424 | */ |
| 5425 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 5426 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5427 | int rc = SQLITE_OK; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5428 | #if defined(NO_GETTOD) |
| 5429 | time_t t; |
| 5430 | time(&t); |
dan | 15eac4e | 2010-11-22 17:26:07 +0000 | [diff] [blame] | 5431 | *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5432 | #elif OS_VXWORKS |
| 5433 | struct timespec sNow; |
| 5434 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 5435 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 5436 | #else |
| 5437 | struct timeval sNow; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5438 | if( gettimeofday(&sNow, 0)==0 ){ |
| 5439 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
| 5440 | }else{ |
| 5441 | rc = SQLITE_ERROR; |
| 5442 | } |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5443 | #endif |
| 5444 | |
| 5445 | #ifdef SQLITE_TEST |
| 5446 | if( sqlite3_current_time ){ |
| 5447 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 5448 | } |
| 5449 | #endif |
| 5450 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5451 | return rc; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5452 | } |
| 5453 | |
| 5454 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5455 | ** Find the current time (in Universal Coordinated Time). Write the |
| 5456 | ** current time and date as a Julian Day number into *prNow and |
| 5457 | ** return 0. Return 1 if the time and date cannot be found. |
| 5458 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5459 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | b87a666 | 2011-10-13 01:01:14 +0000 | [diff] [blame] | 5460 | sqlite3_int64 i = 0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5461 | int rc; |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 5462 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5463 | rc = unixCurrentTimeInt64(0, &i); |
drh | 0dcb0a7 | 2010-05-03 18:22:52 +0000 | [diff] [blame] | 5464 | *prNow = i/86400000.0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5465 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5466 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5467 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5468 | /* |
| 5469 | ** We added the xGetLastError() method with the intention of providing |
| 5470 | ** better low-level error messages when operating-system problems come up |
| 5471 | ** during SQLite operation. But so far, none of that has been implemented |
| 5472 | ** in the core. So this routine is never called. For now, it is merely |
| 5473 | ** a place-holder. |
| 5474 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5475 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 5476 | UNUSED_PARAMETER(NotUsed); |
| 5477 | UNUSED_PARAMETER(NotUsed2); |
| 5478 | UNUSED_PARAMETER(NotUsed3); |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 5479 | return 0; |
| 5480 | } |
| 5481 | |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5482 | |
| 5483 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5484 | ************************ End of sqlite3_vfs methods *************************** |
| 5485 | ******************************************************************************/ |
| 5486 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5487 | /****************************************************************************** |
| 5488 | ************************** Begin Proxy Locking ******************************** |
| 5489 | ** |
| 5490 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 5491 | ** other locking methods on secondary lock files. Proxy locking is a |
| 5492 | ** meta-layer over top of the primitive locking implemented above. For |
| 5493 | ** this reason, the division that implements of proxy locking is deferred |
| 5494 | ** until late in the file (here) after all of the other I/O methods have |
| 5495 | ** been defined - so that the primitive locking methods are available |
| 5496 | ** as services to help with the implementation of proxy locking. |
| 5497 | ** |
| 5498 | **** |
| 5499 | ** |
| 5500 | ** The default locking schemes in SQLite use byte-range locks on the |
| 5501 | ** database file to coordinate safe, concurrent access by multiple readers |
| 5502 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 5503 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 5504 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 5505 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 5506 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 5507 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 5508 | ** address in the shared range is taken for a SHARED lock, the entire |
| 5509 | ** shared range is taken for an EXCLUSIVE lock): |
| 5510 | ** |
| 5511 | ** PENDING_BYTE 0x40000000 |
| 5512 | ** RESERVED_BYTE 0x40000001 |
| 5513 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 5514 | ** |
| 5515 | ** This works well on the local file system, but shows a nearly 100x |
| 5516 | ** slowdown in read performance on AFP because the AFP client disables |
| 5517 | ** the read cache when byte-range locks are present. Enabling the read |
| 5518 | ** cache exposes a cache coherency problem that is present on all OS X |
| 5519 | ** supported network file systems. NFS and AFP both observe the |
| 5520 | ** close-to-open semantics for ensuring cache coherency |
| 5521 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 5522 | ** address the requirements for concurrent database access by multiple |
| 5523 | ** readers and writers |
| 5524 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 5525 | ** |
| 5526 | ** To address the performance and cache coherency issues, proxy file locking |
| 5527 | ** changes the way database access is controlled by limiting access to a |
| 5528 | ** single host at a time and moving file locks off of the database file |
| 5529 | ** and onto a proxy file on the local file system. |
| 5530 | ** |
| 5531 | ** |
| 5532 | ** Using proxy locks |
| 5533 | ** ----------------- |
| 5534 | ** |
| 5535 | ** C APIs |
| 5536 | ** |
| 5537 | ** sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE, |
| 5538 | ** <proxy_path> | ":auto:"); |
| 5539 | ** sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>); |
| 5540 | ** |
| 5541 | ** |
| 5542 | ** SQL pragmas |
| 5543 | ** |
| 5544 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 5545 | ** PRAGMA [database.]lock_proxy_file |
| 5546 | ** |
| 5547 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 5548 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 5549 | ** a proxy path based on the user's temp dir |
| 5550 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 5551 | ** actual proxy file name is generated from the name and path of the |
| 5552 | ** database file. For example: |
| 5553 | ** |
| 5554 | ** For database path "/Users/me/foo.db" |
| 5555 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 5556 | ** |
| 5557 | ** Once a lock proxy is configured for a database connection, it can not |
| 5558 | ** be removed, however it may be switched to a different proxy path via |
| 5559 | ** the above APIs (assuming the conch file is not being held by another |
| 5560 | ** connection or process). |
| 5561 | ** |
| 5562 | ** |
| 5563 | ** How proxy locking works |
| 5564 | ** ----------------------- |
| 5565 | ** |
| 5566 | ** Proxy file locking relies primarily on two new supporting files: |
| 5567 | ** |
| 5568 | ** * conch file to limit access to the database file to a single host |
| 5569 | ** at a time |
| 5570 | ** |
| 5571 | ** * proxy file to act as a proxy for the advisory locks normally |
| 5572 | ** taken on the database |
| 5573 | ** |
| 5574 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 5575 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 5576 | ** contents and comparing the host's unique host ID (see below) and lock |
| 5577 | ** proxy path against the values stored in the conch. The conch file is |
| 5578 | ** stored in the same directory as the database file and the file name |
| 5579 | ** is patterned after the database file name as ".<databasename>-conch". |
| 5580 | ** If the conch file does not exist, or it's contents do not match the |
| 5581 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 5582 | ** lock and the conch file contents is updated with the host ID and proxy |
| 5583 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 5584 | ** is held by another process (with a shared lock), the exclusive lock |
| 5585 | ** will fail and SQLITE_BUSY is returned. |
| 5586 | ** |
| 5587 | ** The proxy file - a single-byte file used for all advisory file locks |
| 5588 | ** normally taken on the database file. This allows for safe sharing |
| 5589 | ** of the database file for multiple readers and writers on the same |
| 5590 | ** host (the conch ensures that they all use the same local lock file). |
| 5591 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5592 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 5593 | ** only taken when the first request to lock database file is made. |
| 5594 | ** This matches the semantics of the traditional locking behavior, where |
| 5595 | ** opening a connection to a database file does not take a lock on it. |
| 5596 | ** The shared lock and an open file descriptor are maintained until |
| 5597 | ** the connection to the database is closed. |
| 5598 | ** |
| 5599 | ** The proxy file and the lock file are never deleted so they only need |
| 5600 | ** to be created the first time they are used. |
| 5601 | ** |
| 5602 | ** Configuration options |
| 5603 | ** --------------------- |
| 5604 | ** |
| 5605 | ** SQLITE_PREFER_PROXY_LOCKING |
| 5606 | ** |
| 5607 | ** Database files accessed on non-local file systems are |
| 5608 | ** automatically configured for proxy locking, lock files are |
| 5609 | ** named automatically using the same logic as |
| 5610 | ** PRAGMA lock_proxy_file=":auto:" |
| 5611 | ** |
| 5612 | ** SQLITE_PROXY_DEBUG |
| 5613 | ** |
| 5614 | ** Enables the logging of error messages during host id file |
| 5615 | ** retrieval and creation |
| 5616 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5617 | ** LOCKPROXYDIR |
| 5618 | ** |
| 5619 | ** Overrides the default directory used for lock proxy files that |
| 5620 | ** are named automatically via the ":auto:" setting |
| 5621 | ** |
| 5622 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 5623 | ** |
| 5624 | ** Permissions to use when creating a directory for storing the |
| 5625 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 5626 | ** |
| 5627 | ** |
| 5628 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 5629 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 5630 | ** force proxy locking to be used for every database file opened, and 0 |
| 5631 | ** will force automatic proxy locking to be disabled for all database |
| 5632 | ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or |
| 5633 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 5634 | */ |
| 5635 | |
| 5636 | /* |
| 5637 | ** Proxy locking is only available on MacOSX |
| 5638 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5639 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5640 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5641 | /* |
| 5642 | ** The proxyLockingContext has the path and file structures for the remote |
| 5643 | ** and local proxy files in it |
| 5644 | */ |
| 5645 | typedef struct proxyLockingContext proxyLockingContext; |
| 5646 | struct proxyLockingContext { |
| 5647 | unixFile *conchFile; /* Open conch file */ |
| 5648 | char *conchFilePath; /* Name of the conch file */ |
| 5649 | unixFile *lockProxy; /* Open proxy lock file */ |
| 5650 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 5651 | char *dbPath; /* Name of the open file */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5652 | int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5653 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 5654 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 5655 | }; |
| 5656 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5657 | /* |
| 5658 | ** The proxy lock file path for the database at dbPath is written into lPath, |
| 5659 | ** which must point to valid, writable memory large enough for a maxLen length |
| 5660 | ** file path. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5661 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5662 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 5663 | int len; |
| 5664 | int dbLen; |
| 5665 | int i; |
| 5666 | |
| 5667 | #ifdef LOCKPROXYDIR |
| 5668 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 5669 | #else |
| 5670 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 5671 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5672 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5673 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
| 5674 | lPath, errno, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5675 | return SQLITE_IOERR_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5676 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5677 | len = strlcat(lPath, "sqliteplocks", maxLen); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5678 | } |
| 5679 | # else |
| 5680 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 5681 | # endif |
| 5682 | #endif |
| 5683 | |
| 5684 | if( lPath[len-1]!='/' ){ |
| 5685 | len = strlcat(lPath, "/", maxLen); |
| 5686 | } |
| 5687 | |
| 5688 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5689 | dbLen = (int)strlen(dbPath); |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5690 | for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5691 | char c = dbPath[i]; |
| 5692 | lPath[i+len] = (c=='/')?'_':c; |
| 5693 | } |
| 5694 | lPath[i+len]='\0'; |
| 5695 | strlcat(lPath, ":auto:", maxLen); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5696 | OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5697 | return SQLITE_OK; |
| 5698 | } |
| 5699 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5700 | /* |
| 5701 | ** Creates the lock file and any missing directories in lockPath |
| 5702 | */ |
| 5703 | static int proxyCreateLockPath(const char *lockPath){ |
| 5704 | int i, len; |
| 5705 | char buf[MAXPATHLEN]; |
| 5706 | int start = 0; |
| 5707 | |
| 5708 | assert(lockPath!=NULL); |
| 5709 | /* try to create all the intermediate directories */ |
| 5710 | len = (int)strlen(lockPath); |
| 5711 | buf[0] = lockPath[0]; |
| 5712 | for( i=1; i<len; i++ ){ |
| 5713 | if( lockPath[i] == '/' && (i - start > 0) ){ |
| 5714 | /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 5715 | if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 5716 | || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 5717 | buf[i]='\0'; |
| 5718 | if( mkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
| 5719 | int err=errno; |
| 5720 | if( err!=EEXIST ) { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5721 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5722 | "'%s' proxy lock path=%s pid=%d\n", |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5723 | buf, strerror(err), lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5724 | return err; |
| 5725 | } |
| 5726 | } |
| 5727 | } |
| 5728 | start=i+1; |
| 5729 | } |
| 5730 | buf[i] = lockPath[i]; |
| 5731 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5732 | OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5733 | return 0; |
| 5734 | } |
| 5735 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5736 | /* |
| 5737 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 5738 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 5739 | ** |
| 5740 | ** The caller is responsible not only for closing the file descriptor |
| 5741 | ** but also for freeing the memory associated with the file descriptor. |
| 5742 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5743 | static int proxyCreateUnixFile( |
| 5744 | const char *path, /* path for the new unixFile */ |
| 5745 | unixFile **ppFile, /* unixFile created and returned by ref */ |
| 5746 | int islockfile /* if non zero missing dirs will be created */ |
| 5747 | ) { |
| 5748 | int fd = -1; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5749 | unixFile *pNew; |
| 5750 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5751 | int openFlags = O_RDWR | O_CREAT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5752 | sqlite3_vfs dummyVfs; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5753 | int terrno = 0; |
| 5754 | UnixUnusedFd *pUnused = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5755 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5756 | /* 1. first try to open/create the file |
| 5757 | ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 5758 | ** the parent directories and then try again. |
| 5759 | ** 3. if that fails, try to open the file read-only |
| 5760 | ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 5761 | */ |
| 5762 | pUnused = findReusableFd(path, openFlags); |
| 5763 | if( pUnused ){ |
| 5764 | fd = pUnused->fd; |
| 5765 | }else{ |
| 5766 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
| 5767 | if( !pUnused ){ |
| 5768 | return SQLITE_NOMEM; |
| 5769 | } |
| 5770 | } |
| 5771 | if( fd<0 ){ |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5772 | fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5773 | terrno = errno; |
| 5774 | if( fd<0 && errno==ENOENT && islockfile ){ |
| 5775 | if( proxyCreateLockPath(path) == SQLITE_OK ){ |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5776 | fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5777 | } |
| 5778 | } |
| 5779 | } |
| 5780 | if( fd<0 ){ |
| 5781 | openFlags = O_RDONLY; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5782 | fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5783 | terrno = errno; |
| 5784 | } |
| 5785 | if( fd<0 ){ |
| 5786 | if( islockfile ){ |
| 5787 | return SQLITE_BUSY; |
| 5788 | } |
| 5789 | switch (terrno) { |
| 5790 | case EACCES: |
| 5791 | return SQLITE_PERM; |
| 5792 | case EIO: |
| 5793 | return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 5794 | default: |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 5795 | return SQLITE_CANTOPEN_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5796 | } |
| 5797 | } |
| 5798 | |
| 5799 | pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew)); |
| 5800 | if( pNew==NULL ){ |
| 5801 | rc = SQLITE_NOMEM; |
| 5802 | goto end_create_proxy; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5803 | } |
| 5804 | memset(pNew, 0, sizeof(unixFile)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5805 | pNew->openFlags = openFlags; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 5806 | memset(&dummyVfs, 0, sizeof(dummyVfs)); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5807 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 5808 | dummyVfs.zName = "dummy"; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5809 | pUnused->fd = fd; |
| 5810 | pUnused->flags = openFlags; |
| 5811 | pNew->pUnused = pUnused; |
| 5812 | |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5813 | rc = fillInUnixFile(&dummyVfs, fd, 0, (sqlite3_file*)pNew, path, 0, 0, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5814 | if( rc==SQLITE_OK ){ |
| 5815 | *ppFile = pNew; |
| 5816 | return SQLITE_OK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5817 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5818 | end_create_proxy: |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5819 | robust_close(pNew, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5820 | sqlite3_free(pNew); |
| 5821 | sqlite3_free(pUnused); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5822 | return rc; |
| 5823 | } |
| 5824 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5825 | #ifdef SQLITE_TEST |
| 5826 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 5827 | int sqlite3_hostid_num = 0; |
| 5828 | #endif |
| 5829 | |
| 5830 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 5831 | |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5832 | /* Not always defined in the headers as it ought to be */ |
| 5833 | extern int gethostuuid(uuid_t id, const struct timespec *wait); |
| 5834 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5835 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 5836 | ** bytes of writable memory. |
| 5837 | */ |
| 5838 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5839 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 5840 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 5841 | #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\ |
| 5842 | && __MAC_OS_X_VERSION_MIN_REQUIRED<1050 |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 5843 | { |
| 5844 | static const struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
| 5845 | if( gethostuuid(pHostID, &timeout) ){ |
| 5846 | int err = errno; |
| 5847 | if( pError ){ |
| 5848 | *pError = err; |
| 5849 | } |
| 5850 | return SQLITE_IOERR; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5851 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5852 | } |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 5853 | #else |
| 5854 | UNUSED_PARAMETER(pError); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 5855 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5856 | #ifdef SQLITE_TEST |
| 5857 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 5858 | if( sqlite3_hostid_num != 0){ |
| 5859 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 5860 | } |
| 5861 | #endif |
| 5862 | |
| 5863 | return SQLITE_OK; |
| 5864 | } |
| 5865 | |
| 5866 | /* The conch file contains the header, host id and lock file path |
| 5867 | */ |
| 5868 | #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 5869 | #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 5870 | #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 5871 | #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 5872 | |
| 5873 | /* |
| 5874 | ** Takes an open conch file, copies the contents to a new path and then moves |
| 5875 | ** it back. The newly created file's file descriptor is assigned to the |
| 5876 | ** conch file structure and finally the original conch file descriptor is |
| 5877 | ** closed. Returns zero if successful. |
| 5878 | */ |
| 5879 | static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 5880 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5881 | unixFile *conchFile = pCtx->conchFile; |
| 5882 | char tPath[MAXPATHLEN]; |
| 5883 | char buf[PROXY_MAXCONCHLEN]; |
| 5884 | char *cPath = pCtx->conchFilePath; |
| 5885 | size_t readLen = 0; |
| 5886 | size_t pathLen = 0; |
| 5887 | char errmsg[64] = ""; |
| 5888 | int fd = -1; |
| 5889 | int rc = -1; |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5890 | UNUSED_PARAMETER(myHostID); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5891 | |
| 5892 | /* create a new path by replace the trailing '-conch' with '-break' */ |
| 5893 | pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 5894 | if( pathLen>MAXPATHLEN || pathLen<6 || |
| 5895 | (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5896 | sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5897 | goto end_breaklock; |
| 5898 | } |
| 5899 | /* read the conch content */ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 5900 | readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5901 | if( readLen<PROXY_PATHINDEX ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5902 | sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5903 | goto end_breaklock; |
| 5904 | } |
| 5905 | /* write it out to the temporary break file */ |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5906 | fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), |
| 5907 | SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5908 | if( fd<0 ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5909 | sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5910 | goto end_breaklock; |
| 5911 | } |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 5912 | if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5913 | sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5914 | goto end_breaklock; |
| 5915 | } |
| 5916 | if( rename(tPath, cPath) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5917 | sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5918 | goto end_breaklock; |
| 5919 | } |
| 5920 | rc = 0; |
| 5921 | fprintf(stderr, "broke stale lock on %s\n", cPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5922 | robust_close(pFile, conchFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5923 | conchFile->h = fd; |
| 5924 | conchFile->openFlags = O_RDWR | O_CREAT; |
| 5925 | |
| 5926 | end_breaklock: |
| 5927 | if( rc ){ |
| 5928 | if( fd>=0 ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5929 | osUnlink(tPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5930 | robust_close(pFile, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5931 | } |
| 5932 | fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 5933 | } |
| 5934 | return rc; |
| 5935 | } |
| 5936 | |
| 5937 | /* Take the requested lock on the conch file and break a stale lock if the |
| 5938 | ** host id matches. |
| 5939 | */ |
| 5940 | static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 5941 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5942 | unixFile *conchFile = pCtx->conchFile; |
| 5943 | int rc = SQLITE_OK; |
| 5944 | int nTries = 0; |
| 5945 | struct timespec conchModTime; |
| 5946 | |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 5947 | memset(&conchModTime, 0, sizeof(conchModTime)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5948 | do { |
| 5949 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 5950 | nTries ++; |
| 5951 | if( rc==SQLITE_BUSY ){ |
| 5952 | /* If the lock failed (busy): |
| 5953 | * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 5954 | * 2nd try: fail if the mod time changed or host id is different, wait |
| 5955 | * 10 sec and try again |
| 5956 | * 3rd try: break the lock unless the mod time has changed. |
| 5957 | */ |
| 5958 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5959 | if( osFstat(conchFile->h, &buf) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5960 | pFile->lastErrno = errno; |
| 5961 | return SQLITE_IOERR_LOCK; |
| 5962 | } |
| 5963 | |
| 5964 | if( nTries==1 ){ |
| 5965 | conchModTime = buf.st_mtimespec; |
| 5966 | usleep(500000); /* wait 0.5 sec and try the lock again*/ |
| 5967 | continue; |
| 5968 | } |
| 5969 | |
| 5970 | assert( nTries>1 ); |
| 5971 | if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 5972 | conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 5973 | return SQLITE_BUSY; |
| 5974 | } |
| 5975 | |
| 5976 | if( nTries==2 ){ |
| 5977 | char tBuf[PROXY_MAXCONCHLEN]; |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 5978 | int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5979 | if( len<0 ){ |
| 5980 | pFile->lastErrno = errno; |
| 5981 | return SQLITE_IOERR_LOCK; |
| 5982 | } |
| 5983 | if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 5984 | /* don't break the lock if the host id doesn't match */ |
| 5985 | if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 5986 | return SQLITE_BUSY; |
| 5987 | } |
| 5988 | }else{ |
| 5989 | /* don't break the lock on short read or a version mismatch */ |
| 5990 | return SQLITE_BUSY; |
| 5991 | } |
| 5992 | usleep(10000000); /* wait 10 sec and try the lock again */ |
| 5993 | continue; |
| 5994 | } |
| 5995 | |
| 5996 | assert( nTries==3 ); |
| 5997 | if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 5998 | rc = SQLITE_OK; |
| 5999 | if( lockType==EXCLUSIVE_LOCK ){ |
| 6000 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 6001 | } |
| 6002 | if( !rc ){ |
| 6003 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 6004 | } |
| 6005 | } |
| 6006 | } |
| 6007 | } while( rc==SQLITE_BUSY && nTries<3 ); |
| 6008 | |
| 6009 | return rc; |
| 6010 | } |
| 6011 | |
| 6012 | /* 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] | 6013 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 6014 | ** lockPath means that the lockPath in the conch file will be used if the |
| 6015 | ** host IDs match, or a new lock path will be generated automatically |
| 6016 | ** and written to the conch file. |
| 6017 | */ |
| 6018 | static int proxyTakeConch(unixFile *pFile){ |
| 6019 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6020 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6021 | if( pCtx->conchHeld!=0 ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6022 | return SQLITE_OK; |
| 6023 | }else{ |
| 6024 | unixFile *conchFile = pCtx->conchFile; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6025 | uuid_t myHostID; |
| 6026 | int pError = 0; |
| 6027 | char readBuf[PROXY_MAXCONCHLEN]; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6028 | char lockPath[MAXPATHLEN]; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6029 | char *tempLockPath = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6030 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6031 | int createConch = 0; |
| 6032 | int hostIdMatch = 0; |
| 6033 | int readLen = 0; |
| 6034 | int tryOldLockPath = 0; |
| 6035 | int forceNewLockPath = 0; |
| 6036 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6037 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
| 6038 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6039 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6040 | rc = proxyGetHostID(myHostID, &pError); |
| 6041 | if( (rc&0xff)==SQLITE_IOERR ){ |
| 6042 | pFile->lastErrno = pError; |
| 6043 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6044 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6045 | rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6046 | if( rc!=SQLITE_OK ){ |
| 6047 | goto end_takeconch; |
| 6048 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6049 | /* read the existing conch file */ |
| 6050 | readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 6051 | if( readLen<0 ){ |
| 6052 | /* I/O error: lastErrno set by seekAndRead */ |
| 6053 | pFile->lastErrno = conchFile->lastErrno; |
| 6054 | rc = SQLITE_IOERR_READ; |
| 6055 | goto end_takeconch; |
| 6056 | }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 6057 | readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 6058 | /* a short read or version format mismatch means we need to create a new |
| 6059 | ** conch file. |
| 6060 | */ |
| 6061 | createConch = 1; |
| 6062 | } |
| 6063 | /* if the host id matches and the lock path already exists in the conch |
| 6064 | ** we'll try to use the path there, if we can't open that path, we'll |
| 6065 | ** retry with a new auto-generated path |
| 6066 | */ |
| 6067 | do { /* in case we need to try again for an :auto: named lock file */ |
| 6068 | |
| 6069 | if( !createConch && !forceNewLockPath ){ |
| 6070 | hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 6071 | PROXY_HOSTIDLEN); |
| 6072 | /* if the conch has data compare the contents */ |
| 6073 | if( !pCtx->lockProxyPath ){ |
| 6074 | /* for auto-named local lock file, just check the host ID and we'll |
| 6075 | ** use the local lock file path that's already in there |
| 6076 | */ |
| 6077 | if( hostIdMatch ){ |
| 6078 | size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 6079 | |
| 6080 | if( pathLen>=MAXPATHLEN ){ |
| 6081 | pathLen=MAXPATHLEN-1; |
| 6082 | } |
| 6083 | memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 6084 | lockPath[pathLen] = 0; |
| 6085 | tempLockPath = lockPath; |
| 6086 | tryOldLockPath = 1; |
| 6087 | /* create a copy of the lock path if the conch is taken */ |
| 6088 | goto end_takeconch; |
| 6089 | } |
| 6090 | }else if( hostIdMatch |
| 6091 | && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 6092 | readLen-PROXY_PATHINDEX) |
| 6093 | ){ |
| 6094 | /* conch host and lock path match */ |
| 6095 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6096 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6097 | } |
| 6098 | |
| 6099 | /* if the conch isn't writable and doesn't match, we can't take it */ |
| 6100 | if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 6101 | rc = SQLITE_BUSY; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6102 | goto end_takeconch; |
| 6103 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6104 | |
| 6105 | /* 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] | 6106 | if( !pCtx->lockProxyPath ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6107 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 6108 | tempLockPath = lockPath; |
| 6109 | /* create a copy of the lock path _only_ if the conch is taken */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6110 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6111 | |
| 6112 | /* update conch with host and path (this will fail if other process |
| 6113 | ** has a shared lock already), if the host id matches, use the big |
| 6114 | ** stick. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6115 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6116 | futimes(conchFile->h, NULL); |
| 6117 | if( hostIdMatch && !createConch ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 6118 | if( conchFile->pInode && conchFile->pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6119 | /* We are trying for an exclusive lock but another thread in this |
| 6120 | ** same process is still holding a shared lock. */ |
| 6121 | rc = SQLITE_BUSY; |
| 6122 | } else { |
| 6123 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6124 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6125 | }else{ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6126 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6127 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6128 | if( rc==SQLITE_OK ){ |
| 6129 | char writeBuffer[PROXY_MAXCONCHLEN]; |
| 6130 | int writeSize = 0; |
| 6131 | |
| 6132 | writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 6133 | memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 6134 | if( pCtx->lockProxyPath!=NULL ){ |
| 6135 | strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN); |
| 6136 | }else{ |
| 6137 | strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 6138 | } |
| 6139 | writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6140 | robust_ftruncate(conchFile->h, writeSize); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6141 | rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
| 6142 | fsync(conchFile->h); |
| 6143 | /* If we created a new conch file (not just updated the contents of a |
| 6144 | ** valid conch file), try to match the permissions of the database |
| 6145 | */ |
| 6146 | if( rc==SQLITE_OK && createConch ){ |
| 6147 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6148 | int err = osFstat(pFile->h, &buf); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6149 | if( err==0 ){ |
| 6150 | mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 6151 | S_IROTH|S_IWOTH); |
| 6152 | /* try to match the database file R/W permissions, ignore failure */ |
| 6153 | #ifndef SQLITE_PROXY_DEBUG |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6154 | osFchmod(conchFile->h, cmode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6155 | #else |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6156 | do{ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6157 | rc = osFchmod(conchFile->h, cmode); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6158 | }while( rc==(-1) && errno==EINTR ); |
| 6159 | if( rc!=0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6160 | int code = errno; |
| 6161 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 6162 | cmode, code, strerror(code)); |
| 6163 | } else { |
| 6164 | fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 6165 | } |
| 6166 | }else{ |
| 6167 | int code = errno; |
| 6168 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 6169 | err, code, strerror(code)); |
| 6170 | #endif |
| 6171 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6172 | } |
| 6173 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6174 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 6175 | |
| 6176 | end_takeconch: |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6177 | OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6178 | if( rc==SQLITE_OK && pFile->openFlags ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6179 | int fd; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6180 | if( pFile->h>=0 ){ |
drh | e84009f | 2011-03-02 17:54:32 +0000 | [diff] [blame] | 6181 | robust_close(pFile, pFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6182 | } |
| 6183 | pFile->h = -1; |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6184 | fd = robust_open(pCtx->dbPath, pFile->openFlags, |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6185 | SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6186 | OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6187 | if( fd>=0 ){ |
| 6188 | pFile->h = fd; |
| 6189 | }else{ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6190 | rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6191 | during locking */ |
| 6192 | } |
| 6193 | } |
| 6194 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 6195 | char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 6196 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 6197 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 6198 | /* we couldn't create the proxy lock file with the old lock file path |
| 6199 | ** so try again via auto-naming |
| 6200 | */ |
| 6201 | forceNewLockPath = 1; |
| 6202 | tryOldLockPath = 0; |
dan | 2b0ef47 | 2010-02-16 12:18:47 +0000 | [diff] [blame] | 6203 | continue; /* go back to the do {} while start point, try again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6204 | } |
| 6205 | } |
| 6206 | if( rc==SQLITE_OK ){ |
| 6207 | /* Need to make a copy of path if we extracted the value |
| 6208 | ** from the conch file or the path was allocated on the stack |
| 6209 | */ |
| 6210 | if( tempLockPath ){ |
| 6211 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 6212 | if( !pCtx->lockProxyPath ){ |
| 6213 | rc = SQLITE_NOMEM; |
| 6214 | } |
| 6215 | } |
| 6216 | } |
| 6217 | if( rc==SQLITE_OK ){ |
| 6218 | pCtx->conchHeld = 1; |
| 6219 | |
| 6220 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 6221 | afpLockingContext *afpCtx; |
| 6222 | afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 6223 | afpCtx->dbPath = pCtx->lockProxyPath; |
| 6224 | } |
| 6225 | } else { |
| 6226 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 6227 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6228 | OSTRACE(("TAKECONCH %d %s\n", conchFile->h, |
| 6229 | rc==SQLITE_OK?"ok":"failed")); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6230 | return rc; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6231 | } while (1); /* in case we need to retry the :auto: lock file - |
| 6232 | ** we should never get here except via the 'continue' call. */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6233 | } |
| 6234 | } |
| 6235 | |
| 6236 | /* |
| 6237 | ** If pFile holds a lock on a conch file, then release that lock. |
| 6238 | */ |
| 6239 | static int proxyReleaseConch(unixFile *pFile){ |
drh | 1c5bb4d | 2010-05-10 17:29:28 +0000 | [diff] [blame] | 6240 | int rc = SQLITE_OK; /* Subroutine return code */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6241 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 6242 | unixFile *conchFile; /* Name of the conch file */ |
| 6243 | |
| 6244 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6245 | conchFile = pCtx->conchFile; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6246 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6247 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6248 | getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6249 | if( pCtx->conchHeld>0 ){ |
| 6250 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 6251 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6252 | pCtx->conchHeld = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6253 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| 6254 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6255 | return rc; |
| 6256 | } |
| 6257 | |
| 6258 | /* |
| 6259 | ** Given the name of a database file, compute the name of its conch file. |
| 6260 | ** Store the conch filename in memory obtained from sqlite3_malloc(). |
| 6261 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 6262 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 6263 | ** |
| 6264 | ** The caller is responsible for ensuring that the allocated memory |
| 6265 | ** space is eventually freed. |
| 6266 | ** |
| 6267 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 6268 | */ |
| 6269 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 6270 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6271 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6272 | char *conchPath; /* buffer in which to construct conch name */ |
| 6273 | |
| 6274 | /* Allocate space for the conch filename and initialize the name to |
| 6275 | ** the name of the original database file. */ |
| 6276 | *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8); |
| 6277 | if( conchPath==0 ){ |
| 6278 | return SQLITE_NOMEM; |
| 6279 | } |
| 6280 | memcpy(conchPath, dbPath, len+1); |
| 6281 | |
| 6282 | /* now insert a "." before the last / character */ |
| 6283 | for( i=(len-1); i>=0; i-- ){ |
| 6284 | if( conchPath[i]=='/' ){ |
| 6285 | i++; |
| 6286 | break; |
| 6287 | } |
| 6288 | } |
| 6289 | conchPath[i]='.'; |
| 6290 | while ( i<len ){ |
| 6291 | conchPath[i+1]=dbPath[i]; |
| 6292 | i++; |
| 6293 | } |
| 6294 | |
| 6295 | /* append the "-conch" suffix to the file */ |
| 6296 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6297 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6298 | |
| 6299 | return SQLITE_OK; |
| 6300 | } |
| 6301 | |
| 6302 | |
| 6303 | /* Takes a fully configured proxy locking-style unix file and switches |
| 6304 | ** the local lock file path |
| 6305 | */ |
| 6306 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 6307 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 6308 | char *oldPath = pCtx->lockProxyPath; |
| 6309 | int rc = SQLITE_OK; |
| 6310 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6311 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6312 | return SQLITE_BUSY; |
| 6313 | } |
| 6314 | |
| 6315 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 6316 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 6317 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 6318 | return SQLITE_OK; |
| 6319 | }else{ |
| 6320 | unixFile *lockProxy = pCtx->lockProxy; |
| 6321 | pCtx->lockProxy=NULL; |
| 6322 | pCtx->conchHeld = 0; |
| 6323 | if( lockProxy!=NULL ){ |
| 6324 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 6325 | if( rc ) return rc; |
| 6326 | sqlite3_free(lockProxy); |
| 6327 | } |
| 6328 | sqlite3_free(oldPath); |
| 6329 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 6330 | } |
| 6331 | |
| 6332 | return rc; |
| 6333 | } |
| 6334 | |
| 6335 | /* |
| 6336 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 6337 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 6338 | ** |
| 6339 | ** This routine find the filename associated with pFile and writes it |
| 6340 | ** int dbPath. |
| 6341 | */ |
| 6342 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6343 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6344 | if( pFile->pMethod == &afpIoMethods ){ |
| 6345 | /* afp style keeps a reference to the db path in the filePath field |
| 6346 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6347 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6348 | strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN); |
| 6349 | } else |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6350 | #endif |
| 6351 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 6352 | /* dot lock style uses the locking context to store the dot lock |
| 6353 | ** file path */ |
| 6354 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 6355 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 6356 | }else{ |
| 6357 | /* all other styles use the locking context to store the db file path */ |
| 6358 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6359 | strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6360 | } |
| 6361 | return SQLITE_OK; |
| 6362 | } |
| 6363 | |
| 6364 | /* |
| 6365 | ** Takes an already filled in unix file and alters it so all file locking |
| 6366 | ** will be performed on the local proxy lock file. The following fields |
| 6367 | ** are preserved in the locking context so that they can be restored and |
| 6368 | ** the unix structure properly cleaned up at close time: |
| 6369 | ** ->lockingContext |
| 6370 | ** ->pMethod |
| 6371 | */ |
| 6372 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 6373 | proxyLockingContext *pCtx; |
| 6374 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 6375 | char *lockPath=NULL; |
| 6376 | int rc = SQLITE_OK; |
| 6377 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6378 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6379 | return SQLITE_BUSY; |
| 6380 | } |
| 6381 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 6382 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 6383 | lockPath=NULL; |
| 6384 | }else{ |
| 6385 | lockPath=(char *)path; |
| 6386 | } |
| 6387 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6388 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
| 6389 | (lockPath ? lockPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6390 | |
| 6391 | pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 6392 | if( pCtx==0 ){ |
| 6393 | return SQLITE_NOMEM; |
| 6394 | } |
| 6395 | memset(pCtx, 0, sizeof(*pCtx)); |
| 6396 | |
| 6397 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 6398 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6399 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 6400 | if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 6401 | /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 6402 | ** (c) the file system is read-only, then enable no-locking access. |
| 6403 | ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 6404 | ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 6405 | */ |
| 6406 | struct statfs fsInfo; |
| 6407 | struct stat conchInfo; |
| 6408 | int goLockless = 0; |
| 6409 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6410 | if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6411 | int err = errno; |
| 6412 | if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 6413 | goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 6414 | } |
| 6415 | } |
| 6416 | if( goLockless ){ |
| 6417 | pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 6418 | rc = SQLITE_OK; |
| 6419 | } |
| 6420 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6421 | } |
| 6422 | if( rc==SQLITE_OK && lockPath ){ |
| 6423 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 6424 | } |
| 6425 | |
| 6426 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6427 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 6428 | if( pCtx->dbPath==NULL ){ |
| 6429 | rc = SQLITE_NOMEM; |
| 6430 | } |
| 6431 | } |
| 6432 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6433 | /* all memory is allocated, proxys are created and assigned, |
| 6434 | ** switch the locking context and pMethod then return. |
| 6435 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6436 | pCtx->oldLockingContext = pFile->lockingContext; |
| 6437 | pFile->lockingContext = pCtx; |
| 6438 | pCtx->pOldMethod = pFile->pMethod; |
| 6439 | pFile->pMethod = &proxyIoMethods; |
| 6440 | }else{ |
| 6441 | if( pCtx->conchFile ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6442 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6443 | sqlite3_free(pCtx->conchFile); |
| 6444 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 6445 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6446 | sqlite3_free(pCtx->conchFilePath); |
| 6447 | sqlite3_free(pCtx); |
| 6448 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6449 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 6450 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6451 | return rc; |
| 6452 | } |
| 6453 | |
| 6454 | |
| 6455 | /* |
| 6456 | ** This routine handles sqlite3_file_control() calls that are specific |
| 6457 | ** to proxy locking. |
| 6458 | */ |
| 6459 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 6460 | switch( op ){ |
| 6461 | case SQLITE_GET_LOCKPROXYFILE: { |
| 6462 | unixFile *pFile = (unixFile*)id; |
| 6463 | if( pFile->pMethod == &proxyIoMethods ){ |
| 6464 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 6465 | proxyTakeConch(pFile); |
| 6466 | if( pCtx->lockProxyPath ){ |
| 6467 | *(const char **)pArg = pCtx->lockProxyPath; |
| 6468 | }else{ |
| 6469 | *(const char **)pArg = ":auto: (not held)"; |
| 6470 | } |
| 6471 | } else { |
| 6472 | *(const char **)pArg = NULL; |
| 6473 | } |
| 6474 | return SQLITE_OK; |
| 6475 | } |
| 6476 | case SQLITE_SET_LOCKPROXYFILE: { |
| 6477 | unixFile *pFile = (unixFile*)id; |
| 6478 | int rc = SQLITE_OK; |
| 6479 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 6480 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 6481 | if( isProxyStyle ){ |
| 6482 | /* turn off proxy locking - not supported */ |
| 6483 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 6484 | }else{ |
| 6485 | /* turn off proxy locking - already off - NOOP */ |
| 6486 | rc = SQLITE_OK; |
| 6487 | } |
| 6488 | }else{ |
| 6489 | const char *proxyPath = (const char *)pArg; |
| 6490 | if( isProxyStyle ){ |
| 6491 | proxyLockingContext *pCtx = |
| 6492 | (proxyLockingContext*)pFile->lockingContext; |
| 6493 | if( !strcmp(pArg, ":auto:") |
| 6494 | || (pCtx->lockProxyPath && |
| 6495 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 6496 | ){ |
| 6497 | rc = SQLITE_OK; |
| 6498 | }else{ |
| 6499 | rc = switchLockProxyPath(pFile, proxyPath); |
| 6500 | } |
| 6501 | }else{ |
| 6502 | /* turn on proxy file locking */ |
| 6503 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 6504 | } |
| 6505 | } |
| 6506 | return rc; |
| 6507 | } |
| 6508 | default: { |
| 6509 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 6510 | } |
| 6511 | } |
| 6512 | /*NOTREACHED*/ |
| 6513 | return SQLITE_ERROR; |
| 6514 | } |
| 6515 | |
| 6516 | /* |
| 6517 | ** Within this division (the proxying locking implementation) the procedures |
| 6518 | ** above this point are all utilities. The lock-related methods of the |
| 6519 | ** proxy-locking sqlite3_io_method object follow. |
| 6520 | */ |
| 6521 | |
| 6522 | |
| 6523 | /* |
| 6524 | ** This routine checks if there is a RESERVED lock held on the specified |
| 6525 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 6526 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 6527 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 6528 | */ |
| 6529 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 6530 | unixFile *pFile = (unixFile*)id; |
| 6531 | int rc = proxyTakeConch(pFile); |
| 6532 | if( rc==SQLITE_OK ){ |
| 6533 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6534 | if( pCtx->conchHeld>0 ){ |
| 6535 | unixFile *proxy = pCtx->lockProxy; |
| 6536 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 6537 | }else{ /* conchHeld < 0 is lockless */ |
| 6538 | pResOut=0; |
| 6539 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6540 | } |
| 6541 | return rc; |
| 6542 | } |
| 6543 | |
| 6544 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6545 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6546 | ** of the following: |
| 6547 | ** |
| 6548 | ** (1) SHARED_LOCK |
| 6549 | ** (2) RESERVED_LOCK |
| 6550 | ** (3) PENDING_LOCK |
| 6551 | ** (4) EXCLUSIVE_LOCK |
| 6552 | ** |
| 6553 | ** Sometimes when requesting one lock state, additional lock states |
| 6554 | ** are inserted in between. The locking might fail on one of the later |
| 6555 | ** transitions leaving the lock state different from what it started but |
| 6556 | ** still short of its goal. The following chart shows the allowed |
| 6557 | ** transitions and the inserted intermediate states: |
| 6558 | ** |
| 6559 | ** UNLOCKED -> SHARED |
| 6560 | ** SHARED -> RESERVED |
| 6561 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 6562 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 6563 | ** PENDING -> EXCLUSIVE |
| 6564 | ** |
| 6565 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 6566 | ** routine to lower a locking level. |
| 6567 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6568 | static int proxyLock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6569 | unixFile *pFile = (unixFile*)id; |
| 6570 | int rc = proxyTakeConch(pFile); |
| 6571 | if( rc==SQLITE_OK ){ |
| 6572 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6573 | if( pCtx->conchHeld>0 ){ |
| 6574 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6575 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); |
| 6576 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6577 | }else{ |
| 6578 | /* conchHeld < 0 is lockless */ |
| 6579 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6580 | } |
| 6581 | return rc; |
| 6582 | } |
| 6583 | |
| 6584 | |
| 6585 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6586 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6587 | ** must be either NO_LOCK or SHARED_LOCK. |
| 6588 | ** |
| 6589 | ** If the locking level of the file descriptor is already at or below |
| 6590 | ** the requested locking level, this routine is a no-op. |
| 6591 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6592 | static int proxyUnlock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6593 | unixFile *pFile = (unixFile*)id; |
| 6594 | int rc = proxyTakeConch(pFile); |
| 6595 | if( rc==SQLITE_OK ){ |
| 6596 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6597 | if( pCtx->conchHeld>0 ){ |
| 6598 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6599 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); |
| 6600 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6601 | }else{ |
| 6602 | /* conchHeld < 0 is lockless */ |
| 6603 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6604 | } |
| 6605 | return rc; |
| 6606 | } |
| 6607 | |
| 6608 | /* |
| 6609 | ** Close a file that uses proxy locks. |
| 6610 | */ |
| 6611 | static int proxyClose(sqlite3_file *id) { |
| 6612 | if( id ){ |
| 6613 | unixFile *pFile = (unixFile*)id; |
| 6614 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6615 | unixFile *lockProxy = pCtx->lockProxy; |
| 6616 | unixFile *conchFile = pCtx->conchFile; |
| 6617 | int rc = SQLITE_OK; |
| 6618 | |
| 6619 | if( lockProxy ){ |
| 6620 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 6621 | if( rc ) return rc; |
| 6622 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 6623 | if( rc ) return rc; |
| 6624 | sqlite3_free(lockProxy); |
| 6625 | pCtx->lockProxy = 0; |
| 6626 | } |
| 6627 | if( conchFile ){ |
| 6628 | if( pCtx->conchHeld ){ |
| 6629 | rc = proxyReleaseConch(pFile); |
| 6630 | if( rc ) return rc; |
| 6631 | } |
| 6632 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 6633 | if( rc ) return rc; |
| 6634 | sqlite3_free(conchFile); |
| 6635 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 6636 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6637 | sqlite3_free(pCtx->conchFilePath); |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 6638 | sqlite3DbFree(0, pCtx->dbPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6639 | /* restore the original locking context and pMethod then close it */ |
| 6640 | pFile->lockingContext = pCtx->oldLockingContext; |
| 6641 | pFile->pMethod = pCtx->pOldMethod; |
| 6642 | sqlite3_free(pCtx); |
| 6643 | return pFile->pMethod->xClose(id); |
| 6644 | } |
| 6645 | return SQLITE_OK; |
| 6646 | } |
| 6647 | |
| 6648 | |
| 6649 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6650 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6651 | /* |
| 6652 | ** The proxy locking style is intended for use with AFP filesystems. |
| 6653 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 6654 | ** restricted to MacOSX. |
| 6655 | ** |
| 6656 | ** |
| 6657 | ******************* End of the proxy lock implementation ********************** |
| 6658 | ******************************************************************************/ |
| 6659 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6660 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6661 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6662 | ** |
| 6663 | ** This routine registers all VFS implementations for unix-like operating |
| 6664 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 6665 | ** should be the only routines in this file that are visible from other |
| 6666 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6667 | ** |
| 6668 | ** This routine is called once during SQLite initialization and by a |
| 6669 | ** single thread. The memory allocation and mutex subsystems have not |
| 6670 | ** necessarily been initialized when this routine is called, and so they |
| 6671 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6672 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6673 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6674 | /* |
| 6675 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6676 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 6677 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 6678 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 6679 | ** and so we have to go through the intermediate pointer to avoid problems |
| 6680 | ** when compiling with -pedantic-errors on GCC.) |
| 6681 | ** |
| 6682 | ** 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] | 6683 | ** finder-function. The finder-function returns a pointer to the |
| 6684 | ** sqlite_io_methods object that implements the desired locking |
| 6685 | ** behaviors. See the division above that contains the IOMETHODS |
| 6686 | ** macro for addition information on finder-functions. |
| 6687 | ** |
| 6688 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 6689 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 6690 | ** more than that; it looks at the filesystem type that hosts the |
| 6691 | ** database file and tries to choose an locking method appropriate for |
| 6692 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6693 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6694 | #define UNIXVFS(VFSNAME, FINDER) { \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6695 | 3, /* iVersion */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6696 | sizeof(unixFile), /* szOsFile */ \ |
| 6697 | MAX_PATHNAME, /* mxPathname */ \ |
| 6698 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6699 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6700 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6701 | unixOpen, /* xOpen */ \ |
| 6702 | unixDelete, /* xDelete */ \ |
| 6703 | unixAccess, /* xAccess */ \ |
| 6704 | unixFullPathname, /* xFullPathname */ \ |
| 6705 | unixDlOpen, /* xDlOpen */ \ |
| 6706 | unixDlError, /* xDlError */ \ |
| 6707 | unixDlSym, /* xDlSym */ \ |
| 6708 | unixDlClose, /* xDlClose */ \ |
| 6709 | unixRandomness, /* xRandomness */ \ |
| 6710 | unixSleep, /* xSleep */ \ |
| 6711 | unixCurrentTime, /* xCurrentTime */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6712 | unixGetLastError, /* xGetLastError */ \ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6713 | unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6714 | unixSetSystemCall, /* xSetSystemCall */ \ |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 6715 | unixGetSystemCall, /* xGetSystemCall */ \ |
| 6716 | unixNextSystemCall, /* xNextSystemCall */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6717 | } |
| 6718 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6719 | /* |
| 6720 | ** All default VFSes for unix are contained in the following array. |
| 6721 | ** |
| 6722 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 6723 | ** by the SQLite core when the VFS is registered. So the following |
| 6724 | ** array cannot be const. |
| 6725 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6726 | static sqlite3_vfs aVfs[] = { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6727 | #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__)) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6728 | UNIXVFS("unix", autolockIoFinder ), |
| 6729 | #else |
| 6730 | UNIXVFS("unix", posixIoFinder ), |
| 6731 | #endif |
| 6732 | UNIXVFS("unix-none", nolockIoFinder ), |
| 6733 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 6734 | UNIXVFS("unix-excl", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6735 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6736 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6737 | #endif |
| 6738 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6739 | UNIXVFS("unix-posix", posixIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6740 | #if !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6741 | UNIXVFS("unix-flock", flockIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6742 | #endif |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6743 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6744 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6745 | UNIXVFS("unix-afp", afpIoFinder ), |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6746 | UNIXVFS("unix-nfs", nfsIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6747 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6748 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6749 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6750 | unsigned int i; /* Loop counter */ |
| 6751 | |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 6752 | /* Double-check that the aSyscall[] array has been constructed |
| 6753 | ** correctly. See ticket [bb3a86e890c8e96ab] */ |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 6754 | assert( ArraySize(aSyscall)==18 ); |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 6755 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6756 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6757 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6758 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6759 | } |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6760 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6761 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6762 | |
| 6763 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6764 | ** Shutdown the operating system interface. |
| 6765 | ** |
| 6766 | ** Some operating systems might need to do some cleanup in this routine, |
| 6767 | ** to release dynamically allocated objects. But not on unix. |
| 6768 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6769 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6770 | int sqlite3_os_end(void){ |
| 6771 | return SQLITE_OK; |
| 6772 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 6773 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 6774 | #endif /* SQLITE_OS_UNIX */ |