drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2004 May 22 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 13 | ** This file contains the VFS implementation for unix-like operating systems |
| 14 | ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others. |
danielk1977 | 822a516 | 2008-05-16 04:51:54 +0000 | [diff] [blame] | 15 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 16 | ** There are actually several different VFS implementations in this file. |
| 17 | ** The differences are in the way that file locking is done. The default |
| 18 | ** implementation uses Posix Advisory Locks. Alternative implementations |
| 19 | ** use flock(), dot-files, various proprietary locking schemas, or simply |
| 20 | ** skip locking all together. |
| 21 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 22 | ** This source file is organized into divisions where the logic for various |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 23 | ** subfunctions is contained within the appropriate division. PLEASE |
| 24 | ** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed |
| 25 | ** in the correct division and should be clearly labeled. |
| 26 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 27 | ** The layout of divisions is as follows: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 28 | ** |
| 29 | ** * General-purpose declarations and utility functions. |
| 30 | ** * Unique file ID logic used by VxWorks. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 31 | ** * Various locking primitive implementations (all except proxy locking): |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 32 | ** + for Posix Advisory Locks |
| 33 | ** + for no-op locks |
| 34 | ** + for dot-file locks |
| 35 | ** + for flock() locking |
| 36 | ** + for named semaphore locks (VxWorks only) |
| 37 | ** + for AFP filesystem locks (MacOSX only) |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 38 | ** * sqlite3_file methods not associated with locking. |
| 39 | ** * Definitions of sqlite3_io_methods objects for all locking |
| 40 | ** methods plus "finder" functions for each locking method. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 41 | ** * sqlite3_vfs method implementations. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 42 | ** * Locking primitives for the proxy uber-locking-method. (MacOSX only) |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 43 | ** * Definitions of sqlite3_vfs objects for all locking methods |
| 44 | ** plus implementations of sqlite3_os_init() and sqlite3_os_end(). |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 45 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 46 | #include "sqliteInt.h" |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 47 | #if SQLITE_OS_UNIX /* This file is used on unix only */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 48 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 49 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 50 | ** There are various methods for file locking used for concurrency |
| 51 | ** control: |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 52 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 53 | ** 1. POSIX locking (the default), |
| 54 | ** 2. No locking, |
| 55 | ** 3. Dot-file locking, |
| 56 | ** 4. flock() locking, |
| 57 | ** 5. AFP locking (OSX only), |
| 58 | ** 6. Named POSIX semaphores (VXWorks only), |
| 59 | ** 7. proxy locking. (OSX only) |
| 60 | ** |
| 61 | ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE |
| 62 | ** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic |
| 63 | ** selection of the appropriate locking style based on the filesystem |
| 64 | ** where the database is located. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 65 | */ |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 66 | #if !defined(SQLITE_ENABLE_LOCKING_STYLE) |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 67 | # if defined(__APPLE__) |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 68 | # define SQLITE_ENABLE_LOCKING_STYLE 1 |
| 69 | # else |
| 70 | # define SQLITE_ENABLE_LOCKING_STYLE 0 |
| 71 | # endif |
| 72 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 73 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 74 | /* |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 75 | ** Define the OS_VXWORKS pre-processor macro to 1 if building on |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 76 | ** vxworks, or 0 otherwise. |
| 77 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 78 | #ifndef OS_VXWORKS |
| 79 | # if defined(__RTP__) || defined(_WRS_KERNEL) |
| 80 | # define OS_VXWORKS 1 |
| 81 | # else |
| 82 | # define OS_VXWORKS 0 |
| 83 | # endif |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 84 | #endif |
| 85 | |
| 86 | /* |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 87 | ** These #defines should enable >2GB file support on Posix if the |
| 88 | ** underlying operating system supports it. If the OS lacks |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 89 | ** large file support, these should be no-ops. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 90 | ** |
| 91 | ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch |
| 92 | ** on the compiler command line. This is necessary if you are compiling |
| 93 | ** on a recent machine (ex: RedHat 7.2) but you want your code to work |
| 94 | ** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2 |
| 95 | ** without this option, LFS is enable. But LFS does not exist in the kernel |
| 96 | ** in RedHat 6.0, so the code won't work. Hence, for maximum binary |
| 97 | ** portability you should omit LFS. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 98 | ** |
| 99 | ** The previous paragraph was written in 2005. (This paragraph is written |
| 100 | ** on 2008-11-28.) These days, all Linux kernels support large files, so |
| 101 | ** you should probably leave LFS enabled. But some embedded platforms might |
| 102 | ** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 103 | */ |
| 104 | #ifndef SQLITE_DISABLE_LFS |
| 105 | # define _LARGE_FILE 1 |
| 106 | # ifndef _FILE_OFFSET_BITS |
| 107 | # define _FILE_OFFSET_BITS 64 |
| 108 | # endif |
| 109 | # define _LARGEFILE_SOURCE 1 |
| 110 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 111 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 112 | /* |
| 113 | ** standard include files. |
| 114 | */ |
| 115 | #include <sys/types.h> |
| 116 | #include <sys/stat.h> |
| 117 | #include <fcntl.h> |
| 118 | #include <unistd.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 119 | #include <time.h> |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 120 | #include <sys/time.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 121 | #include <errno.h> |
drh | b469f46 | 2010-12-22 21:48:50 +0000 | [diff] [blame] | 122 | #ifndef SQLITE_OMIT_WAL |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 123 | #include <sys/mman.h> |
drh | b469f46 | 2010-12-22 21:48:50 +0000 | [diff] [blame] | 124 | #endif |
drh | 1da88f0 | 2011-12-17 16:09:16 +0000 | [diff] [blame] | 125 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 126 | |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 127 | #if SQLITE_ENABLE_LOCKING_STYLE |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 128 | # include <sys/ioctl.h> |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 129 | # if OS_VXWORKS |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 130 | # include <semaphore.h> |
| 131 | # include <limits.h> |
| 132 | # else |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 133 | # include <sys/file.h> |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 134 | # include <sys/param.h> |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 135 | # endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 136 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 137 | |
drh | f8b4d8c | 2010-03-05 13:53:22 +0000 | [diff] [blame] | 138 | #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS) |
drh | 84a2bf6 | 2010-03-05 13:41:06 +0000 | [diff] [blame] | 139 | # include <sys/mount.h> |
| 140 | #endif |
| 141 | |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 142 | #ifdef HAVE_UTIME |
| 143 | # include <utime.h> |
| 144 | #endif |
| 145 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 146 | /* |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 147 | ** Allowed values of unixFile.fsFlags |
| 148 | */ |
| 149 | #define SQLITE_FSFLAGS_IS_MSDOS 0x1 |
| 150 | |
| 151 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 152 | ** If we are to be thread-safe, include the pthreads header and define |
| 153 | ** the SQLITE_UNIX_THREADS macro. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 154 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 155 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 156 | # include <pthread.h> |
| 157 | # define SQLITE_UNIX_THREADS 1 |
| 158 | #endif |
| 159 | |
| 160 | /* |
| 161 | ** Default permissions when creating a new file |
| 162 | */ |
| 163 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 164 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 165 | #endif |
| 166 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 167 | /* |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 168 | ** Default permissions when creating auto proxy dir |
| 169 | */ |
| 170 | #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 171 | # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 |
| 172 | #endif |
| 173 | |
| 174 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 175 | ** Maximum supported path-length. |
| 176 | */ |
| 177 | #define MAX_PATHNAME 512 |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 178 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 179 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 180 | ** Only set the lastErrno if the error code is a real error and not |
| 181 | ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 182 | */ |
| 183 | #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |
| 184 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 185 | /* Forward references */ |
| 186 | typedef struct unixShm unixShm; /* Connection shared memory */ |
| 187 | typedef struct unixShmNode unixShmNode; /* Shared memory instance */ |
| 188 | typedef struct unixInodeInfo unixInodeInfo; /* An i-node */ |
| 189 | typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 190 | |
| 191 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 192 | ** Sometimes, after a file handle is closed by SQLite, the file descriptor |
| 193 | ** cannot be closed immediately. In these cases, instances of the following |
| 194 | ** structure are used to store the file descriptor while waiting for an |
| 195 | ** opportunity to either close or reuse it. |
| 196 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 197 | struct UnixUnusedFd { |
| 198 | int fd; /* File descriptor to close */ |
| 199 | int flags; /* Flags this file descriptor was opened with */ |
| 200 | UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ |
| 201 | }; |
| 202 | |
| 203 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 204 | ** The unixFile structure is subclass of sqlite3_file specific to the unix |
| 205 | ** VFS implementations. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 206 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 207 | typedef struct unixFile unixFile; |
| 208 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 209 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 210 | sqlite3_vfs *pVfs; /* The VFS that created this unixFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 211 | unixInodeInfo *pInode; /* Info about locks on this inode */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 212 | int h; /* The file descriptor */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 213 | unsigned char eFileLock; /* The type of lock held on this fd */ |
drh | 3ee3484 | 2012-02-11 21:21:17 +0000 | [diff] [blame] | 214 | unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 215 | int lastErrno; /* The unix errno from last I/O error */ |
| 216 | void *lockingContext; /* Locking style specific state */ |
| 217 | UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 218 | const char *zPath; /* Name of the file */ |
| 219 | unixShm *pShm; /* Shared memory segment information */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 220 | int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 221 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 222 | int openFlags; /* The flags specified at open() */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 223 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 224 | #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 225 | unsigned fsFlags; /* cached details from statfs() */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 226 | #endif |
| 227 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 228 | struct vxworksFileId *pId; /* Unique file ID */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 229 | #endif |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 230 | #ifndef NDEBUG |
| 231 | /* The next group of variables are used to track whether or not the |
| 232 | ** transaction counter in bytes 24-27 of database files are updated |
| 233 | ** whenever any part of the database changes. An assertion fault will |
| 234 | ** occur if a file is updated without also updating the transaction |
| 235 | ** counter. This test is made to avoid new problems similar to the |
| 236 | ** one described by ticket #3584. |
| 237 | */ |
| 238 | unsigned char transCntrChng; /* True if the transaction counter changed */ |
| 239 | unsigned char dbUpdate; /* True if any part of database file changed */ |
| 240 | unsigned char inNormalWrite; /* True if in a normal write operation */ |
| 241 | #endif |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 242 | #ifdef SQLITE_TEST |
| 243 | /* In test mode, increase the size of this structure a bit so that |
| 244 | ** it is larger than the struct CrashFile defined in test6.c. |
| 245 | */ |
| 246 | char aPadding[32]; |
| 247 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 248 | }; |
| 249 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 250 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 251 | ** Allowed values for the unixFile.ctrlFlags bitmask: |
| 252 | */ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 253 | #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ |
| 254 | #define UNIXFILE_RDONLY 0x02 /* Connection is read only */ |
| 255 | #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ |
dan | ee140c4 | 2011-08-25 13:46:32 +0000 | [diff] [blame] | 256 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 257 | # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ |
| 258 | #else |
| 259 | # define UNIXFILE_DIRSYNC 0x00 |
| 260 | #endif |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 261 | #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 262 | #define UNIXFILE_DELETE 0x20 /* Delete on close */ |
| 263 | #define UNIXFILE_URI 0x40 /* Filename might have query parameters */ |
| 264 | #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */ |
drh | 3ee3484 | 2012-02-11 21:21:17 +0000 | [diff] [blame] | 265 | #define UNIXFILE_CHOWN 0x100 /* File ownership was changed */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 266 | |
| 267 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 268 | ** Include code that is common to all os_*.c files |
| 269 | */ |
| 270 | #include "os_common.h" |
| 271 | |
| 272 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 273 | ** Define various macros that are missing from some systems. |
| 274 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 275 | #ifndef O_LARGEFILE |
| 276 | # define O_LARGEFILE 0 |
| 277 | #endif |
| 278 | #ifdef SQLITE_DISABLE_LFS |
| 279 | # undef O_LARGEFILE |
| 280 | # define O_LARGEFILE 0 |
| 281 | #endif |
| 282 | #ifndef O_NOFOLLOW |
| 283 | # define O_NOFOLLOW 0 |
| 284 | #endif |
| 285 | #ifndef O_BINARY |
| 286 | # define O_BINARY 0 |
| 287 | #endif |
| 288 | |
| 289 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 290 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 291 | ** testing and debugging only. |
| 292 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 293 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 294 | #define threadid pthread_self() |
| 295 | #else |
| 296 | #define threadid 0 |
| 297 | #endif |
| 298 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 299 | /* |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 300 | ** Different Unix systems declare open() in different ways. Same use |
| 301 | ** open(const char*,int,mode_t). Others use open(const char*,int,...). |
| 302 | ** The difference is important when using a pointer to the function. |
| 303 | ** |
| 304 | ** The safest way to deal with the problem is to always use this wrapper |
| 305 | ** which always has the same well-defined interface. |
| 306 | */ |
| 307 | static int posixOpen(const char *zFile, int flags, int mode){ |
| 308 | return open(zFile, flags, mode); |
| 309 | } |
| 310 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 311 | /* Forward reference */ |
| 312 | static int openDirectory(const char*, int*); |
| 313 | |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 314 | /* |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 315 | ** Many system calls are accessed through pointer-to-functions so that |
| 316 | ** they may be overridden at runtime to facilitate fault injection during |
| 317 | ** testing and sandboxing. The following array holds the names and pointers |
| 318 | ** to all overrideable system calls. |
| 319 | */ |
| 320 | static struct unix_syscall { |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 321 | const char *zName; /* Name of the sytem call */ |
| 322 | sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ |
| 323 | sqlite3_syscall_ptr pDefault; /* Default value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 324 | } aSyscall[] = { |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 325 | { "open", (sqlite3_syscall_ptr)posixOpen, 0 }, |
| 326 | #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent) |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 327 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 328 | { "close", (sqlite3_syscall_ptr)close, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 329 | #define osClose ((int(*)(int))aSyscall[1].pCurrent) |
| 330 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 331 | { "access", (sqlite3_syscall_ptr)access, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 332 | #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent) |
| 333 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 334 | { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 335 | #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent) |
| 336 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 337 | { "stat", (sqlite3_syscall_ptr)stat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 338 | #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) |
| 339 | |
| 340 | /* |
| 341 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 342 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 343 | ** that always succeeds. This means that locking does not occur under |
| 344 | ** DJGPP. But it is DOS - what did you expect? |
| 345 | */ |
| 346 | #ifdef __DJGPP__ |
| 347 | { "fstat", 0, 0 }, |
| 348 | #define osFstat(a,b,c) 0 |
| 349 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 350 | { "fstat", (sqlite3_syscall_ptr)fstat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 351 | #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent) |
| 352 | #endif |
| 353 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 354 | { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 355 | #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent) |
| 356 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 357 | { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 358 | #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 359 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 360 | { "read", (sqlite3_syscall_ptr)read, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 361 | #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) |
| 362 | |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 363 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 364 | { "pread", (sqlite3_syscall_ptr)pread, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 365 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 366 | { "pread", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 367 | #endif |
| 368 | #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) |
| 369 | |
| 370 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 371 | { "pread64", (sqlite3_syscall_ptr)pread64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 372 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 373 | { "pread64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 374 | #endif |
| 375 | #define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent) |
| 376 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 377 | { "write", (sqlite3_syscall_ptr)write, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 378 | #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) |
| 379 | |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 380 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 381 | { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 382 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 383 | { "pwrite", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 384 | #endif |
| 385 | #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 386 | aSyscall[12].pCurrent) |
| 387 | |
| 388 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 389 | { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 390 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 391 | { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 392 | #endif |
| 393 | #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 394 | aSyscall[13].pCurrent) |
| 395 | |
drh | a6c4749 | 2011-04-11 18:35:09 +0000 | [diff] [blame] | 396 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 397 | { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 398 | #else |
| 399 | { "fchmod", (sqlite3_syscall_ptr)0, 0 }, |
drh | a6c4749 | 2011-04-11 18:35:09 +0000 | [diff] [blame] | 400 | #endif |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 401 | #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 402 | |
| 403 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 404 | { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 405 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 406 | { "fallocate", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 407 | #endif |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 408 | #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 409 | |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 410 | { "unlink", (sqlite3_syscall_ptr)unlink, 0 }, |
| 411 | #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent) |
| 412 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 413 | { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 }, |
| 414 | #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent) |
| 415 | |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 416 | { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 }, |
| 417 | #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent) |
| 418 | |
| 419 | { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 }, |
| 420 | #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent) |
| 421 | |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 422 | }; /* End of the overrideable system calls */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 423 | |
| 424 | /* |
| 425 | ** This is the xSetSystemCall() method of sqlite3_vfs for all of the |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 426 | ** "unix" VFSes. Return SQLITE_OK opon successfully updating the |
| 427 | ** system call pointer, or SQLITE_NOTFOUND if there is no configurable |
| 428 | ** system call named zName. |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 429 | */ |
| 430 | static int unixSetSystemCall( |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 431 | sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ |
| 432 | const char *zName, /* Name of system call to override */ |
| 433 | sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 434 | ){ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 435 | unsigned int i; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 436 | int rc = SQLITE_NOTFOUND; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 437 | |
| 438 | UNUSED_PARAMETER(pNotUsed); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 439 | if( zName==0 ){ |
| 440 | /* If no zName is given, restore all system calls to their default |
| 441 | ** settings and return NULL |
| 442 | */ |
dan | 51438a7 | 2011-04-02 17:00:47 +0000 | [diff] [blame] | 443 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 444 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 445 | if( aSyscall[i].pDefault ){ |
| 446 | aSyscall[i].pCurrent = aSyscall[i].pDefault; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | }else{ |
| 450 | /* If zName is specified, operate on only the one system call |
| 451 | ** specified. |
| 452 | */ |
| 453 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 454 | if( strcmp(zName, aSyscall[i].zName)==0 ){ |
| 455 | if( aSyscall[i].pDefault==0 ){ |
| 456 | aSyscall[i].pDefault = aSyscall[i].pCurrent; |
| 457 | } |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 458 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 459 | if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault; |
| 460 | aSyscall[i].pCurrent = pNewFunc; |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | return rc; |
| 466 | } |
| 467 | |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 468 | /* |
| 469 | ** Return the value of a system call. Return NULL if zName is not a |
| 470 | ** recognized system call name. NULL is also returned if the system call |
| 471 | ** is currently undefined. |
| 472 | */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 473 | static sqlite3_syscall_ptr unixGetSystemCall( |
| 474 | sqlite3_vfs *pNotUsed, |
| 475 | const char *zName |
| 476 | ){ |
| 477 | unsigned int i; |
| 478 | |
| 479 | UNUSED_PARAMETER(pNotUsed); |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 480 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 481 | if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent; |
| 482 | } |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | ** Return the name of the first system call after zName. If zName==NULL |
| 488 | ** then return the name of the first system call. Return NULL if zName |
| 489 | ** is the last system call or if zName is not the name of a valid |
| 490 | ** system call. |
| 491 | */ |
| 492 | static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){ |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 493 | int i = -1; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 494 | |
| 495 | UNUSED_PARAMETER(p); |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 496 | if( zName ){ |
| 497 | for(i=0; i<ArraySize(aSyscall)-1; i++){ |
| 498 | if( strcmp(zName, aSyscall[i].zName)==0 ) break; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 499 | } |
| 500 | } |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 501 | for(i++; i<ArraySize(aSyscall); i++){ |
| 502 | if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 503 | } |
| 504 | return 0; |
| 505 | } |
| 506 | |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 507 | /* |
| 508 | ** Retry open() calls that fail due to EINTR |
| 509 | */ |
| 510 | static int robust_open(const char *z, int f, int m){ |
| 511 | int rc; |
| 512 | do{ rc = osOpen(z,f,m); }while( rc<0 && errno==EINTR ); |
| 513 | return rc; |
| 514 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 515 | |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 516 | /* |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 517 | ** Helper functions to obtain and relinquish the global mutex. The |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 518 | ** global mutex is used to protect the unixInodeInfo and |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 519 | ** vxworksFileId objects used by this file, all of which may be |
| 520 | ** shared by multiple threads. |
| 521 | ** |
| 522 | ** Function unixMutexHeld() is used to assert() that the global mutex |
| 523 | ** is held when required. This function is only used as part of assert() |
| 524 | ** statements. e.g. |
| 525 | ** |
| 526 | ** unixEnterMutex() |
| 527 | ** assert( unixMutexHeld() ); |
| 528 | ** unixEnterLeave() |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 529 | */ |
| 530 | static void unixEnterMutex(void){ |
| 531 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 532 | } |
| 533 | static void unixLeaveMutex(void){ |
| 534 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 535 | } |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 536 | #ifdef SQLITE_DEBUG |
| 537 | static int unixMutexHeld(void) { |
| 538 | return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 539 | } |
| 540 | #endif |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 541 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 542 | |
drh | 30ddce6 | 2011-10-15 00:16:30 +0000 | [diff] [blame] | 543 | #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 544 | /* |
| 545 | ** Helper function for printing out trace information from debugging |
| 546 | ** binaries. This returns the string represetation of the supplied |
| 547 | ** integer lock-type. |
| 548 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 549 | static const char *azFileLock(int eFileLock){ |
| 550 | switch( eFileLock ){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 551 | case NO_LOCK: return "NONE"; |
| 552 | case SHARED_LOCK: return "SHARED"; |
| 553 | case RESERVED_LOCK: return "RESERVED"; |
| 554 | case PENDING_LOCK: return "PENDING"; |
| 555 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 556 | } |
| 557 | return "ERROR"; |
| 558 | } |
| 559 | #endif |
| 560 | |
| 561 | #ifdef SQLITE_LOCK_TRACE |
| 562 | /* |
| 563 | ** Print out information about all locking operations. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 564 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 565 | ** This routine is used for troubleshooting locks on multithreaded |
| 566 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 567 | ** command-line option on the compiler. This code is normally |
| 568 | ** turned off. |
| 569 | */ |
| 570 | static int lockTrace(int fd, int op, struct flock *p){ |
| 571 | char *zOpName, *zType; |
| 572 | int s; |
| 573 | int savedErrno; |
| 574 | if( op==F_GETLK ){ |
| 575 | zOpName = "GETLK"; |
| 576 | }else if( op==F_SETLK ){ |
| 577 | zOpName = "SETLK"; |
| 578 | }else{ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 579 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 580 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 581 | return s; |
| 582 | } |
| 583 | if( p->l_type==F_RDLCK ){ |
| 584 | zType = "RDLCK"; |
| 585 | }else if( p->l_type==F_WRLCK ){ |
| 586 | zType = "WRLCK"; |
| 587 | }else if( p->l_type==F_UNLCK ){ |
| 588 | zType = "UNLCK"; |
| 589 | }else{ |
| 590 | assert( 0 ); |
| 591 | } |
| 592 | assert( p->l_whence==SEEK_SET ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 593 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 594 | savedErrno = errno; |
| 595 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 596 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 597 | (int)p->l_pid, s); |
| 598 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 599 | struct flock l2; |
| 600 | l2 = *p; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 601 | osFcntl(fd, F_GETLK, &l2); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 602 | if( l2.l_type==F_RDLCK ){ |
| 603 | zType = "RDLCK"; |
| 604 | }else if( l2.l_type==F_WRLCK ){ |
| 605 | zType = "WRLCK"; |
| 606 | }else if( l2.l_type==F_UNLCK ){ |
| 607 | zType = "UNLCK"; |
| 608 | }else{ |
| 609 | assert( 0 ); |
| 610 | } |
| 611 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 612 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 613 | } |
| 614 | errno = savedErrno; |
| 615 | return s; |
| 616 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 617 | #undef osFcntl |
| 618 | #define osFcntl lockTrace |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 619 | #endif /* SQLITE_LOCK_TRACE */ |
| 620 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 621 | /* |
| 622 | ** Retry ftruncate() calls that fail due to EINTR |
| 623 | */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 624 | static int robust_ftruncate(int h, sqlite3_int64 sz){ |
| 625 | int rc; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 626 | do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR ); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 627 | return rc; |
| 628 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 629 | |
| 630 | /* |
| 631 | ** This routine translates a standard POSIX errno code into something |
| 632 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 633 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 634 | ** and a variety of "please close the file descriptor NOW" errors into |
| 635 | ** SQLITE_IOERR |
| 636 | ** |
| 637 | ** Errors during initialization of locks, or file system support for locks, |
| 638 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 639 | */ |
| 640 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
| 641 | switch (posixError) { |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 642 | #if 0 |
| 643 | /* At one point this code was not commented out. In theory, this branch |
| 644 | ** should never be hit, as this function should only be called after |
| 645 | ** a locking-related function (i.e. fcntl()) has returned non-zero with |
| 646 | ** the value of errno as the first argument. Since a system call has failed, |
| 647 | ** errno should be non-zero. |
| 648 | ** |
| 649 | ** Despite this, if errno really is zero, we still don't want to return |
| 650 | ** SQLITE_OK. The system call failed, and *some* SQLite error should be |
| 651 | ** propagated back to the caller. Commenting this branch out means errno==0 |
| 652 | ** will be handled by the "default:" case below. |
| 653 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 654 | case 0: |
| 655 | return SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 656 | #endif |
| 657 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 658 | case EAGAIN: |
| 659 | case ETIMEDOUT: |
| 660 | case EBUSY: |
| 661 | case EINTR: |
| 662 | case ENOLCK: |
| 663 | /* random NFS retry error, unless during file system support |
| 664 | * introspection, in which it actually means what it says */ |
| 665 | return SQLITE_BUSY; |
| 666 | |
| 667 | case EACCES: |
| 668 | /* EACCES is like EAGAIN during locking operations, but not any other time*/ |
| 669 | if( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
| 670 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 671 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 672 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){ |
| 673 | return SQLITE_BUSY; |
| 674 | } |
| 675 | /* else fall through */ |
| 676 | case EPERM: |
| 677 | return SQLITE_PERM; |
| 678 | |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 679 | /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And |
| 680 | ** this module never makes such a call. And the code in SQLite itself |
| 681 | ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons |
| 682 | ** this case is also commented out. If the system does set errno to EDEADLK, |
| 683 | ** the default SQLITE_IOERR_XXX code will be returned. */ |
| 684 | #if 0 |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 685 | case EDEADLK: |
| 686 | return SQLITE_IOERR_BLOCKED; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 687 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 688 | |
| 689 | #if EOPNOTSUPP!=ENOTSUP |
| 690 | case EOPNOTSUPP: |
| 691 | /* something went terribly awry, unless during file system support |
| 692 | * introspection, in which it actually means what it says */ |
| 693 | #endif |
| 694 | #ifdef ENOTSUP |
| 695 | case ENOTSUP: |
| 696 | /* invalid fd, unless during file system support introspection, in which |
| 697 | * it actually means what it says */ |
| 698 | #endif |
| 699 | case EIO: |
| 700 | case EBADF: |
| 701 | case EINVAL: |
| 702 | case ENOTCONN: |
| 703 | case ENODEV: |
| 704 | case ENXIO: |
| 705 | case ENOENT: |
dan | 33067e7 | 2011-07-15 13:43:34 +0000 | [diff] [blame] | 706 | #ifdef ESTALE /* ESTALE is not defined on Interix systems */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 707 | case ESTALE: |
dan | 33067e7 | 2011-07-15 13:43:34 +0000 | [diff] [blame] | 708 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 709 | case ENOSYS: |
| 710 | /* these should force the client to close the file and reconnect */ |
| 711 | |
| 712 | default: |
| 713 | return sqliteIOErr; |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | |
| 718 | |
| 719 | /****************************************************************************** |
| 720 | ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 721 | ** |
| 722 | ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 723 | ** the device number and the inode number. But this does not work on VxWorks. |
| 724 | ** On VxWorks, a unique file id must be based on the canonical filename. |
| 725 | ** |
| 726 | ** A pointer to an instance of the following structure can be used as a |
| 727 | ** unique file ID in VxWorks. Each instance of this structure contains |
| 728 | ** a copy of the canonical filename. There is also a reference count. |
| 729 | ** The structure is reclaimed when the number of pointers to it drops to |
| 730 | ** zero. |
| 731 | ** |
| 732 | ** There are never very many files open at one time and lookups are not |
| 733 | ** a performance-critical path, so it is sufficient to put these |
| 734 | ** structures on a linked list. |
| 735 | */ |
| 736 | struct vxworksFileId { |
| 737 | struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 738 | int nRef; /* Number of references to this one */ |
| 739 | int nName; /* Length of the zCanonicalName[] string */ |
| 740 | char *zCanonicalName; /* Canonical filename */ |
| 741 | }; |
| 742 | |
| 743 | #if OS_VXWORKS |
| 744 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 745 | ** All unique filenames are held on a linked list headed by this |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 746 | ** variable: |
| 747 | */ |
| 748 | static struct vxworksFileId *vxworksFileList = 0; |
| 749 | |
| 750 | /* |
| 751 | ** Simplify a filename into its canonical form |
| 752 | ** by making the following changes: |
| 753 | ** |
| 754 | ** * removing any trailing and duplicate / |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 755 | ** * convert /./ into just / |
| 756 | ** * convert /A/../ where A is any simple name into just / |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 757 | ** |
| 758 | ** Changes are made in-place. Return the new name length. |
| 759 | ** |
| 760 | ** The original filename is in z[0..n-1]. Return the number of |
| 761 | ** characters in the simplified name. |
| 762 | */ |
| 763 | static int vxworksSimplifyName(char *z, int n){ |
| 764 | int i, j; |
| 765 | while( n>1 && z[n-1]=='/' ){ n--; } |
| 766 | for(i=j=0; i<n; i++){ |
| 767 | if( z[i]=='/' ){ |
| 768 | if( z[i+1]=='/' ) continue; |
| 769 | if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 770 | i += 1; |
| 771 | continue; |
| 772 | } |
| 773 | if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 774 | while( j>0 && z[j-1]!='/' ){ j--; } |
| 775 | if( j>0 ){ j--; } |
| 776 | i += 2; |
| 777 | continue; |
| 778 | } |
| 779 | } |
| 780 | z[j++] = z[i]; |
| 781 | } |
| 782 | z[j] = 0; |
| 783 | return j; |
| 784 | } |
| 785 | |
| 786 | /* |
| 787 | ** Find a unique file ID for the given absolute pathname. Return |
| 788 | ** a pointer to the vxworksFileId object. This pointer is the unique |
| 789 | ** file ID. |
| 790 | ** |
| 791 | ** The nRef field of the vxworksFileId object is incremented before |
| 792 | ** the object is returned. A new vxworksFileId object is created |
| 793 | ** and added to the global list if necessary. |
| 794 | ** |
| 795 | ** If a memory allocation error occurs, return NULL. |
| 796 | */ |
| 797 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 798 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 799 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 800 | int n; /* Length of zAbsoluteName string */ |
| 801 | |
| 802 | assert( zAbsoluteName[0]=='/' ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 803 | n = (int)strlen(zAbsoluteName); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 804 | pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) ); |
| 805 | if( pNew==0 ) return 0; |
| 806 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 807 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 808 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 809 | |
| 810 | /* Search for an existing entry that matching the canonical name. |
| 811 | ** If found, increment the reference count and return a pointer to |
| 812 | ** the existing file ID. |
| 813 | */ |
| 814 | unixEnterMutex(); |
| 815 | for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 816 | if( pCandidate->nName==n |
| 817 | && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 818 | ){ |
| 819 | sqlite3_free(pNew); |
| 820 | pCandidate->nRef++; |
| 821 | unixLeaveMutex(); |
| 822 | return pCandidate; |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | /* No match was found. We will make a new file ID */ |
| 827 | pNew->nRef = 1; |
| 828 | pNew->nName = n; |
| 829 | pNew->pNext = vxworksFileList; |
| 830 | vxworksFileList = pNew; |
| 831 | unixLeaveMutex(); |
| 832 | return pNew; |
| 833 | } |
| 834 | |
| 835 | /* |
| 836 | ** Decrement the reference count on a vxworksFileId object. Free |
| 837 | ** the object when the reference count reaches zero. |
| 838 | */ |
| 839 | static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 840 | unixEnterMutex(); |
| 841 | assert( pId->nRef>0 ); |
| 842 | pId->nRef--; |
| 843 | if( pId->nRef==0 ){ |
| 844 | struct vxworksFileId **pp; |
| 845 | for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 846 | assert( *pp==pId ); |
| 847 | *pp = pId->pNext; |
| 848 | sqlite3_free(pId); |
| 849 | } |
| 850 | unixLeaveMutex(); |
| 851 | } |
| 852 | #endif /* OS_VXWORKS */ |
| 853 | /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 854 | ******************************************************************************/ |
| 855 | |
| 856 | |
| 857 | /****************************************************************************** |
| 858 | *************************** Posix Advisory Locking **************************** |
| 859 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 860 | ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 861 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 862 | ** sets or clears a lock, that operation overrides any prior locks set |
| 863 | ** by the same process. It does not explicitly say so, but this implies |
| 864 | ** that it overrides locks set by the same process using a different |
| 865 | ** file descriptor. Consider this test case: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 866 | ** |
| 867 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 868 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 869 | ** |
| 870 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 871 | ** one is a hard or symbolic link to the other) then if you set |
| 872 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 873 | ** on fd2, it works. I would have expected the second lock to |
| 874 | ** fail since there was already a lock on the file due to fd1. |
| 875 | ** But not so. Since both locks came from the same process, the |
| 876 | ** second overrides the first, even though they were on different |
| 877 | ** file descriptors opened on different file names. |
| 878 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 879 | ** This means that we cannot use POSIX locks to synchronize file access |
| 880 | ** among competing threads of the same process. POSIX locks will work fine |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 881 | ** to synchronize access for threads in separate processes, but not |
| 882 | ** threads within the same process. |
| 883 | ** |
| 884 | ** To work around the problem, SQLite has to manage file locks internally |
| 885 | ** on its own. Whenever a new database is opened, we have to find the |
| 886 | ** specific inode of the database file (the inode is determined by the |
| 887 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 888 | ** and check for locks already existing on that inode. When locks are |
| 889 | ** created or removed, we have to look at our own internal record of the |
| 890 | ** locks to see if another thread has previously set a lock on that same |
| 891 | ** inode. |
| 892 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 893 | ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 894 | ** For VxWorks, we have to use the alternative unique ID system based on |
| 895 | ** canonical filename and implemented in the previous division.) |
| 896 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 897 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 898 | ** descriptor. It is now a structure that holds the integer file |
| 899 | ** descriptor and a pointer to a structure that describes the internal |
| 900 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 901 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 902 | ** point to the same locking structure. The locking structure keeps |
| 903 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 904 | ** field that tells us its internal lock status. cnt==0 means the |
| 905 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 906 | ** cnt>0 means there are cnt shared locks on the file. |
| 907 | ** |
| 908 | ** Any attempt to lock or unlock a file first checks the locking |
| 909 | ** structure. The fcntl() system call is only invoked to set a |
| 910 | ** POSIX lock if the internal lock structure transitions between |
| 911 | ** a locked and an unlocked state. |
| 912 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 913 | ** But wait: there are yet more problems with POSIX advisory locks. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 914 | ** |
| 915 | ** If you close a file descriptor that points to a file that has locks, |
| 916 | ** all locks on that file that are owned by the current process are |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 917 | ** released. To work around this problem, each unixInodeInfo object |
| 918 | ** maintains a count of the number of pending locks on tha inode. |
| 919 | ** When an attempt is made to close an unixFile, if there are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 920 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 921 | ** to close() the file descriptor is deferred until all of the locks clear. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 922 | ** The unixInodeInfo structure keeps a list of file descriptors that need to |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 923 | ** be closed and that list is walked (and cleared) when the last lock |
| 924 | ** clears. |
| 925 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 926 | ** Yet another problem: LinuxThreads do not play well with posix locks. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 927 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 928 | ** Many older versions of linux use the LinuxThreads library which is |
| 929 | ** not posix compliant. Under LinuxThreads, a lock created by thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 930 | ** A cannot be modified or overridden by a different thread B. |
| 931 | ** Only thread A can modify the lock. Locking behavior is correct |
| 932 | ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 933 | ** on linux - with NPTL a lock created by thread A can override locks |
| 934 | ** in thread B. But there is no way to know at compile-time which |
| 935 | ** threading library is being used. So there is no way to know at |
| 936 | ** compile-time whether or not thread A can override locks on thread B. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 937 | ** 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] | 938 | ** current process. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 939 | ** |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 940 | ** SQLite used to support LinuxThreads. But support for LinuxThreads |
| 941 | ** was dropped beginning with version 3.7.0. SQLite will still work with |
| 942 | ** LinuxThreads provided that (1) there is no more than one connection |
| 943 | ** per database file in the same process and (2) database connections |
| 944 | ** do not move across threads. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 945 | */ |
| 946 | |
| 947 | /* |
| 948 | ** An instance of the following structure serves as the key used |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 949 | ** to locate a particular unixInodeInfo object. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 950 | */ |
| 951 | struct unixFileId { |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 952 | dev_t dev; /* Device number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 953 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 954 | struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 955 | #else |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 956 | ino_t ino; /* Inode number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 957 | #endif |
| 958 | }; |
| 959 | |
| 960 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 961 | ** An instance of the following structure is allocated for each open |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 962 | ** inode. Or, on LinuxThreads, there is one of these structures for |
| 963 | ** each inode opened by each thread. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 964 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 965 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 966 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 967 | ** object keeps a count of the number of unixFile pointing to it. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 968 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 969 | struct unixInodeInfo { |
| 970 | struct unixFileId fileId; /* The lookup key */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 971 | int nShared; /* Number of SHARED locks held */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 972 | unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
| 973 | unsigned char bProcessLock; /* An exclusive process lock is held */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 974 | int nRef; /* Number of pointers to this structure */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 975 | unixShmNode *pShmNode; /* Shared memory associated with this inode */ |
| 976 | int nLock; /* Number of outstanding file locks */ |
| 977 | UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
| 978 | unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ |
| 979 | unixInodeInfo *pPrev; /* .... doubly linked */ |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 980 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 981 | unsigned long long sharedByte; /* for AFP simulated shared lock */ |
| 982 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 983 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 984 | sem_t *pSem; /* Named POSIX semaphore */ |
| 985 | char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 986 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 987 | }; |
| 988 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 989 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 990 | ** A lists of all unixInodeInfo objects. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 991 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 992 | static unixInodeInfo *inodeList = 0; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 993 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 994 | /* |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 995 | ** |
| 996 | ** This function - unixLogError_x(), is only ever called via the macro |
| 997 | ** unixLogError(). |
| 998 | ** |
| 999 | ** It is invoked after an error occurs in an OS function and errno has been |
| 1000 | ** set. It logs a message using sqlite3_log() containing the current value of |
| 1001 | ** errno and, if possible, the human-readable equivalent from strerror() or |
| 1002 | ** strerror_r(). |
| 1003 | ** |
| 1004 | ** The first argument passed to the macro should be the error code that |
| 1005 | ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). |
| 1006 | ** The two subsequent arguments should be the name of the OS function that |
| 1007 | ** failed (e.g. "unlink", "open") and the the associated file-system path, |
| 1008 | ** if any. |
| 1009 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1010 | #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__) |
| 1011 | static int unixLogErrorAtLine( |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1012 | int errcode, /* SQLite error code */ |
| 1013 | const char *zFunc, /* Name of OS function that failed */ |
| 1014 | const char *zPath, /* File path associated with error */ |
| 1015 | int iLine /* Source line number where error occurred */ |
| 1016 | ){ |
| 1017 | char *zErr; /* Message from strerror() or equivalent */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1018 | int iErrno = errno; /* Saved syscall error number */ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1019 | |
| 1020 | /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use |
| 1021 | ** the strerror() function to obtain the human-readable error message |
| 1022 | ** equivalent to errno. Otherwise, use strerror_r(). |
| 1023 | */ |
| 1024 | #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) |
| 1025 | char aErr[80]; |
| 1026 | memset(aErr, 0, sizeof(aErr)); |
| 1027 | zErr = aErr; |
| 1028 | |
| 1029 | /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined, |
| 1030 | ** assume that the system provides the the GNU version of strerror_r() that |
| 1031 | ** returns a pointer to a buffer containing the error message. That pointer |
| 1032 | ** may point to aErr[], or it may point to some static storage somewhere. |
| 1033 | ** Otherwise, assume that the system provides the POSIX version of |
| 1034 | ** strerror_r(), which always writes an error message into aErr[]. |
| 1035 | ** |
| 1036 | ** If the code incorrectly assumes that it is the POSIX version that is |
| 1037 | ** available, the error message will often be an empty string. Not a |
| 1038 | ** huge problem. Incorrectly concluding that the GNU version is available |
| 1039 | ** could lead to a segfault though. |
| 1040 | */ |
| 1041 | #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) |
| 1042 | zErr = |
| 1043 | # endif |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1044 | strerror_r(iErrno, aErr, sizeof(aErr)-1); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1045 | |
| 1046 | #elif SQLITE_THREADSAFE |
| 1047 | /* This is a threadsafe build, but strerror_r() is not available. */ |
| 1048 | zErr = ""; |
| 1049 | #else |
| 1050 | /* Non-threadsafe build, use strerror(). */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1051 | zErr = strerror(iErrno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1052 | #endif |
| 1053 | |
| 1054 | assert( errcode!=SQLITE_OK ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1055 | if( zPath==0 ) zPath = ""; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1056 | sqlite3_log(errcode, |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1057 | "os_unix.c:%d: (%d) %s(%s) - %s", |
| 1058 | iLine, iErrno, zFunc, zPath, zErr |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1059 | ); |
| 1060 | |
| 1061 | return errcode; |
| 1062 | } |
| 1063 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1064 | /* |
| 1065 | ** Close a file descriptor. |
| 1066 | ** |
| 1067 | ** We assume that close() almost always works, since it is only in a |
| 1068 | ** very sick application or on a very sick platform that it might fail. |
| 1069 | ** If it does fail, simply leak the file descriptor, but do log the |
| 1070 | ** error. |
| 1071 | ** |
| 1072 | ** Note that it is not safe to retry close() after EINTR since the |
| 1073 | ** file descriptor might have already been reused by another thread. |
| 1074 | ** So we don't even try to recover from an EINTR. Just log the error |
| 1075 | ** and move on. |
| 1076 | */ |
| 1077 | static void robust_close(unixFile *pFile, int h, int lineno){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1078 | if( osClose(h) ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1079 | unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", |
| 1080 | pFile ? pFile->zPath : 0, lineno); |
| 1081 | } |
| 1082 | } |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1083 | |
| 1084 | /* |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1085 | ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1086 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1087 | static void closePendingFds(unixFile *pFile){ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1088 | unixInodeInfo *pInode = pFile->pInode; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1089 | UnixUnusedFd *p; |
| 1090 | UnixUnusedFd *pNext; |
| 1091 | for(p=pInode->pUnused; p; p=pNext){ |
| 1092 | pNext = p->pNext; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1093 | robust_close(pFile, p->fd, __LINE__); |
| 1094 | sqlite3_free(p); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1095 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1096 | pInode->pUnused = 0; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1100 | ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1101 | ** |
| 1102 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1103 | ** when this function is called. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1104 | */ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1105 | static void releaseInodeInfo(unixFile *pFile){ |
| 1106 | unixInodeInfo *pInode = pFile->pInode; |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1107 | assert( unixMutexHeld() ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1108 | if( ALWAYS(pInode) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1109 | pInode->nRef--; |
| 1110 | if( pInode->nRef==0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1111 | assert( pInode->pShmNode==0 ); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1112 | closePendingFds(pFile); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1113 | if( pInode->pPrev ){ |
| 1114 | assert( pInode->pPrev->pNext==pInode ); |
| 1115 | pInode->pPrev->pNext = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1116 | }else{ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1117 | assert( inodeList==pInode ); |
| 1118 | inodeList = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1119 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1120 | if( pInode->pNext ){ |
| 1121 | assert( pInode->pNext->pPrev==pInode ); |
| 1122 | pInode->pNext->pPrev = pInode->pPrev; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1123 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1124 | sqlite3_free(pInode); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1125 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1130 | ** Given a file descriptor, locate the unixInodeInfo object that |
| 1131 | ** describes that file descriptor. Create a new one if necessary. The |
| 1132 | ** return value might be uninitialized if an error occurs. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1133 | ** |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1134 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1135 | ** when this function is called. |
| 1136 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1137 | ** Return an appropriate error code. |
| 1138 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1139 | static int findInodeInfo( |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1140 | unixFile *pFile, /* Unix file with file desc used in the key */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1141 | unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1142 | ){ |
| 1143 | int rc; /* System call return code */ |
| 1144 | int fd; /* The file descriptor for pFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1145 | struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ |
| 1146 | struct stat statbuf; /* Low-level file information */ |
| 1147 | unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1148 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1149 | assert( unixMutexHeld() ); |
| 1150 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1151 | /* Get low-level information about the file that we can used to |
| 1152 | ** create a unique name for the file. |
| 1153 | */ |
| 1154 | fd = pFile->h; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1155 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1156 | if( rc!=0 ){ |
| 1157 | pFile->lastErrno = errno; |
| 1158 | #ifdef EOVERFLOW |
| 1159 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 1160 | #endif |
| 1161 | return SQLITE_IOERR; |
| 1162 | } |
| 1163 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1164 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1165 | /* On OS X on an msdos filesystem, the inode number is reported |
| 1166 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 1167 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 1168 | ** we always increase the file size to 1 by writing a single byte |
| 1169 | ** prior to accessing the inode number. The one byte written is |
| 1170 | ** an ASCII 'S' character which also happens to be the first byte |
| 1171 | ** in the header of every SQLite database. In this way, if there |
| 1172 | ** is a race condition such that another thread has already populated |
| 1173 | ** the first page of the database, no damage is done. |
| 1174 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1175 | if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 1176 | do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR ); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1177 | if( rc!=1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1178 | pFile->lastErrno = errno; |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1179 | return SQLITE_IOERR; |
| 1180 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1181 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1182 | if( rc!=0 ){ |
| 1183 | pFile->lastErrno = errno; |
| 1184 | return SQLITE_IOERR; |
| 1185 | } |
| 1186 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1187 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1188 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1189 | memset(&fileId, 0, sizeof(fileId)); |
| 1190 | fileId.dev = statbuf.st_dev; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1191 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1192 | fileId.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1193 | #else |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1194 | fileId.ino = statbuf.st_ino; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1195 | #endif |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1196 | pInode = inodeList; |
| 1197 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 1198 | pInode = pInode->pNext; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1199 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1200 | if( pInode==0 ){ |
| 1201 | pInode = sqlite3_malloc( sizeof(*pInode) ); |
| 1202 | if( pInode==0 ){ |
| 1203 | return SQLITE_NOMEM; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1204 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1205 | memset(pInode, 0, sizeof(*pInode)); |
| 1206 | memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
| 1207 | pInode->nRef = 1; |
| 1208 | pInode->pNext = inodeList; |
| 1209 | pInode->pPrev = 0; |
| 1210 | if( inodeList ) inodeList->pPrev = pInode; |
| 1211 | inodeList = pInode; |
| 1212 | }else{ |
| 1213 | pInode->nRef++; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1214 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1215 | *ppInode = pInode; |
| 1216 | return SQLITE_OK; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1217 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1218 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1219 | |
| 1220 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1221 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1222 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1223 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1224 | ** 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] | 1225 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1226 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1227 | int rc = SQLITE_OK; |
| 1228 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1229 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1230 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1231 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1232 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1233 | assert( pFile ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1234 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1235 | |
| 1236 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1237 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1238 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1241 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1242 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1243 | #ifndef __DJGPP__ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1244 | if( !reserved && !pFile->pInode->bProcessLock ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1245 | struct flock lock; |
| 1246 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1247 | lock.l_start = RESERVED_BYTE; |
| 1248 | lock.l_len = 1; |
| 1249 | lock.l_type = F_WRLCK; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1250 | if( osFcntl(pFile->h, F_GETLK, &lock) ){ |
| 1251 | rc = SQLITE_IOERR_CHECKRESERVEDLOCK; |
| 1252 | pFile->lastErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1253 | } else if( lock.l_type!=F_UNLCK ){ |
| 1254 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1255 | } |
| 1256 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1257 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1258 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1259 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1260 | OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1261 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1262 | *pResOut = reserved; |
| 1263 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1267 | ** Attempt to set a system-lock on the file pFile. The lock is |
| 1268 | ** described by pLock. |
| 1269 | ** |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1270 | ** If the pFile was opened read/write from unix-excl, then the only lock |
| 1271 | ** ever obtained is an exclusive lock, and it is obtained exactly once |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1272 | ** the first time any lock is attempted. All subsequent system locking |
| 1273 | ** operations become no-ops. Locking operations still happen internally, |
| 1274 | ** in order to coordinate access between separate database connections |
| 1275 | ** within this process, but all of that is handled in memory and the |
| 1276 | ** operating system does not participate. |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1277 | ** |
| 1278 | ** This function is a pass-through to fcntl(F_SETLK) if pFile is using |
| 1279 | ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl" |
| 1280 | ** and is read-only. |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1281 | ** |
| 1282 | ** Zero is returned if the call completes successfully, or -1 if a call |
| 1283 | ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()). |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1284 | */ |
| 1285 | static int unixFileLock(unixFile *pFile, struct flock *pLock){ |
| 1286 | int rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1287 | unixInodeInfo *pInode = pFile->pInode; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1288 | assert( unixMutexHeld() ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1289 | assert( pInode!=0 ); |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1290 | if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock) |
| 1291 | && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0) |
| 1292 | ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1293 | if( pInode->bProcessLock==0 ){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1294 | struct flock lock; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1295 | assert( pInode->nLock==0 ); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1296 | lock.l_whence = SEEK_SET; |
| 1297 | lock.l_start = SHARED_FIRST; |
| 1298 | lock.l_len = SHARED_SIZE; |
| 1299 | lock.l_type = F_WRLCK; |
| 1300 | rc = osFcntl(pFile->h, F_SETLK, &lock); |
| 1301 | if( rc<0 ) return rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1302 | pInode->bProcessLock = 1; |
| 1303 | pInode->nLock++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1304 | }else{ |
| 1305 | rc = 0; |
| 1306 | } |
| 1307 | }else{ |
| 1308 | rc = osFcntl(pFile->h, F_SETLK, pLock); |
| 1309 | } |
| 1310 | return rc; |
| 1311 | } |
| 1312 | |
| 1313 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1314 | ** Lock the file with the lock specified by parameter eFileLock - one |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1315 | ** of the following: |
| 1316 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1317 | ** (1) SHARED_LOCK |
| 1318 | ** (2) RESERVED_LOCK |
| 1319 | ** (3) PENDING_LOCK |
| 1320 | ** (4) EXCLUSIVE_LOCK |
| 1321 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1322 | ** Sometimes when requesting one lock state, additional lock states |
| 1323 | ** are inserted in between. The locking might fail on one of the later |
| 1324 | ** transitions leaving the lock state different from what it started but |
| 1325 | ** still short of its goal. The following chart shows the allowed |
| 1326 | ** transitions and the inserted intermediate states: |
| 1327 | ** |
| 1328 | ** UNLOCKED -> SHARED |
| 1329 | ** SHARED -> RESERVED |
| 1330 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1331 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1332 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1333 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1334 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1335 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1336 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1337 | static int unixLock(sqlite3_file *id, int eFileLock){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1338 | /* The following describes the implementation of the various locks and |
| 1339 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1340 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1341 | ** confusion with SQLite lock names). The algorithms are complicated |
| 1342 | ** slightly in order to be compatible with windows systems simultaneously |
| 1343 | ** accessing the same database file, in case that is ever required. |
| 1344 | ** |
| 1345 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1346 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1347 | ** range', a range of 510 bytes at a well known offset. |
| 1348 | ** |
| 1349 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 1350 | ** byte'. If this is successful, a random byte from the 'shared byte |
| 1351 | ** range' is read-locked and the lock on the 'pending byte' released. |
| 1352 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1353 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1354 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1355 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1356 | ** |
| 1357 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1358 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1359 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1360 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1361 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1362 | ** This property is used by the algorithm for rolling back a journal file |
| 1363 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1364 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1365 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1366 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1367 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1368 | ** within this range, this ensures that no other locks are held on the |
| 1369 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1370 | ** |
| 1371 | ** The reason a single byte cannot be used instead of the 'shared byte |
| 1372 | ** range' is that some versions of windows do not support read-locks. By |
| 1373 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 1374 | ** even if the locking primitive used is always a write-lock. |
| 1375 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1376 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1377 | unixFile *pFile = (unixFile*)id; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 1378 | unixInodeInfo *pInode; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1379 | struct flock lock; |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 1380 | int tErrno = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1381 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1382 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1383 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 1384 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 1385 | azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid())); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1386 | |
| 1387 | /* 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] | 1388 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1389 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1390 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1391 | if( pFile->eFileLock>=eFileLock ){ |
| 1392 | OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
| 1393 | azFileLock(eFileLock))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1394 | return SQLITE_OK; |
| 1395 | } |
| 1396 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1397 | /* Make sure the locking sequence is correct. |
| 1398 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 1399 | ** (2) SQLite never explicitly requests a pendig lock. |
| 1400 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1401 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1402 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 1403 | assert( eFileLock!=PENDING_LOCK ); |
| 1404 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1405 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1406 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1407 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1408 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1409 | pInode = pFile->pInode; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1410 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1411 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1412 | ** handle that precludes the requested lock, return BUSY. |
| 1413 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1414 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 1415 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1416 | ){ |
| 1417 | rc = SQLITE_BUSY; |
| 1418 | goto end_lock; |
| 1419 | } |
| 1420 | |
| 1421 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1422 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1423 | ** return SQLITE_OK. |
| 1424 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1425 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1426 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1427 | assert( eFileLock==SHARED_LOCK ); |
| 1428 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1429 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1430 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1431 | pInode->nShared++; |
| 1432 | pInode->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1433 | goto end_lock; |
| 1434 | } |
| 1435 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1436 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1437 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1438 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1439 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1440 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1441 | lock.l_len = 1L; |
| 1442 | lock.l_whence = SEEK_SET; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1443 | if( eFileLock==SHARED_LOCK |
| 1444 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1445 | ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1446 | lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1447 | lock.l_start = PENDING_BYTE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1448 | if( unixFileLock(pFile, &lock) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1449 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1450 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1451 | if( rc!=SQLITE_BUSY ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1452 | pFile->lastErrno = tErrno; |
| 1453 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1454 | goto end_lock; |
| 1455 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | |
| 1459 | /* If control gets to this point, then actually go ahead and make |
| 1460 | ** operating system calls for the specified lock. |
| 1461 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1462 | if( eFileLock==SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1463 | assert( pInode->nShared==0 ); |
| 1464 | assert( pInode->eFileLock==0 ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1465 | assert( rc==SQLITE_OK ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1466 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1467 | /* Now get the read-lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1468 | lock.l_start = SHARED_FIRST; |
| 1469 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1470 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1471 | tErrno = errno; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1472 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1473 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1474 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1475 | /* Drop the temporary PENDING lock */ |
| 1476 | lock.l_start = PENDING_BYTE; |
| 1477 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1478 | lock.l_type = F_UNLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1479 | if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){ |
| 1480 | /* This could happen with a network mount */ |
| 1481 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1482 | rc = SQLITE_IOERR_UNLOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1483 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1484 | |
| 1485 | if( rc ){ |
| 1486 | if( rc!=SQLITE_BUSY ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1487 | pFile->lastErrno = tErrno; |
| 1488 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1489 | goto end_lock; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1490 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1491 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1492 | pInode->nLock++; |
| 1493 | pInode->nShared = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1494 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1495 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1496 | /* We are trying for an exclusive lock but another thread in this |
| 1497 | ** same process is still holding a shared lock. */ |
| 1498 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1499 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1500 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1501 | ** assumed that there is a SHARED or greater lock on the file |
| 1502 | ** already. |
| 1503 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1504 | assert( 0!=pFile->eFileLock ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1505 | lock.l_type = F_WRLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1506 | |
| 1507 | assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK ); |
| 1508 | if( eFileLock==RESERVED_LOCK ){ |
| 1509 | lock.l_start = RESERVED_BYTE; |
| 1510 | lock.l_len = 1L; |
| 1511 | }else{ |
| 1512 | lock.l_start = SHARED_FIRST; |
| 1513 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1514 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1515 | |
| 1516 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1517 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1518 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1519 | if( rc!=SQLITE_BUSY ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1520 | pFile->lastErrno = tErrno; |
| 1521 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1522 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1523 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1524 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1525 | |
| 1526 | #ifndef NDEBUG |
| 1527 | /* Set up the transaction-counter change checking flags when |
| 1528 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1529 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1530 | ** write operation (not a hot journal rollback). |
| 1531 | */ |
| 1532 | if( rc==SQLITE_OK |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1533 | && pFile->eFileLock<=SHARED_LOCK |
| 1534 | && eFileLock==RESERVED_LOCK |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1535 | ){ |
| 1536 | pFile->transCntrChng = 0; |
| 1537 | pFile->dbUpdate = 0; |
| 1538 | pFile->inNormalWrite = 1; |
| 1539 | } |
| 1540 | #endif |
| 1541 | |
| 1542 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1543 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1544 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1545 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1546 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 1547 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1548 | pInode->eFileLock = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1549 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1550 | |
| 1551 | end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1552 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1553 | OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), |
| 1554 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1555 | return rc; |
| 1556 | } |
| 1557 | |
| 1558 | /* |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1559 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1560 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1561 | */ |
| 1562 | static void setPendingFd(unixFile *pFile){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1563 | unixInodeInfo *pInode = pFile->pInode; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1564 | UnixUnusedFd *p = pFile->pUnused; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1565 | p->pNext = pInode->pUnused; |
| 1566 | pInode->pUnused = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1567 | pFile->h = -1; |
| 1568 | pFile->pUnused = 0; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1572 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1573 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1574 | ** |
| 1575 | ** If the locking level of the file descriptor is already at or below |
| 1576 | ** the requested locking level, this routine is a no-op. |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1577 | ** |
| 1578 | ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 1579 | ** the byte range is divided into 2 parts and the first part is unlocked then |
| 1580 | ** set to a read lock, then the other part is simply unlocked. This works |
| 1581 | ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 1582 | ** remove the write lock on a region when a read lock is set. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1583 | */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1584 | static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1585 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1586 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1587 | struct flock lock; |
| 1588 | int rc = SQLITE_OK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1589 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1590 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1591 | 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] | 1592 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1593 | getpid())); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1594 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1595 | assert( eFileLock<=SHARED_LOCK ); |
| 1596 | if( pFile->eFileLock<=eFileLock ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1597 | return SQLITE_OK; |
| 1598 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1599 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1600 | pInode = pFile->pInode; |
| 1601 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1602 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1603 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1604 | |
| 1605 | #ifndef NDEBUG |
| 1606 | /* When reducing a lock such that other processes can start |
| 1607 | ** reading the database file again, make sure that the |
| 1608 | ** transaction counter was updated if any part of the database |
| 1609 | ** file changed. If the transaction counter is not updated, |
| 1610 | ** other connections to the same file might not realize that |
| 1611 | ** the file has changed and hence might not know to flush their |
| 1612 | ** cache. The use of a stale cache can lead to database corruption. |
| 1613 | */ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1614 | pFile->inNormalWrite = 0; |
| 1615 | #endif |
| 1616 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1617 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 1618 | ** before establishing the readlock - to avoid a race condition we downgrade |
| 1619 | ** the lock in 2 blocks, so that part of the range will be covered by a |
| 1620 | ** write lock until the rest is covered by a read lock: |
| 1621 | ** 1: [WWWWW] |
| 1622 | ** 2: [....W] |
| 1623 | ** 3: [RRRRW] |
| 1624 | ** 4: [RRRR.] |
| 1625 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1626 | if( eFileLock==SHARED_LOCK ){ |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1627 | |
| 1628 | #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE |
drh | 87e79ae | 2011-03-08 13:06:41 +0000 | [diff] [blame] | 1629 | (void)handleNFSUnlock; |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1630 | assert( handleNFSUnlock==0 ); |
| 1631 | #endif |
| 1632 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1633 | if( handleNFSUnlock ){ |
drh | 026663d | 2011-04-01 13:29:29 +0000 | [diff] [blame] | 1634 | int tErrno; /* Error code from system call errors */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1635 | off_t divSize = SHARED_SIZE - 1; |
| 1636 | |
| 1637 | lock.l_type = F_UNLCK; |
| 1638 | lock.l_whence = SEEK_SET; |
| 1639 | lock.l_start = SHARED_FIRST; |
| 1640 | lock.l_len = divSize; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 1641 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1642 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1643 | rc = SQLITE_IOERR_UNLOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1644 | if( IS_LOCK_ERROR(rc) ){ |
| 1645 | pFile->lastErrno = tErrno; |
| 1646 | } |
| 1647 | goto end_unlock; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1648 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1649 | lock.l_type = F_RDLCK; |
| 1650 | lock.l_whence = SEEK_SET; |
| 1651 | lock.l_start = SHARED_FIRST; |
| 1652 | lock.l_len = 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; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1655 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1656 | if( IS_LOCK_ERROR(rc) ){ |
| 1657 | pFile->lastErrno = tErrno; |
| 1658 | } |
| 1659 | goto end_unlock; |
| 1660 | } |
| 1661 | lock.l_type = F_UNLCK; |
| 1662 | lock.l_whence = SEEK_SET; |
| 1663 | lock.l_start = SHARED_FIRST+divSize; |
| 1664 | lock.l_len = SHARED_SIZE-divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1665 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1666 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1667 | rc = SQLITE_IOERR_UNLOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1668 | if( IS_LOCK_ERROR(rc) ){ |
| 1669 | pFile->lastErrno = tErrno; |
| 1670 | } |
| 1671 | goto end_unlock; |
| 1672 | } |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1673 | }else |
| 1674 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 1675 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1676 | lock.l_type = F_RDLCK; |
| 1677 | lock.l_whence = SEEK_SET; |
| 1678 | lock.l_start = SHARED_FIRST; |
| 1679 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1680 | if( unixFileLock(pFile, &lock) ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1681 | /* In theory, the call to unixFileLock() cannot fail because another |
| 1682 | ** process is holding an incompatible lock. If it does, this |
| 1683 | ** indicates that the other process is not following the locking |
| 1684 | ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning |
| 1685 | ** SQLITE_BUSY would confuse the upper layer (in practice it causes |
| 1686 | ** an assert to fail). */ |
| 1687 | rc = SQLITE_IOERR_RDLOCK; |
| 1688 | pFile->lastErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1689 | goto end_unlock; |
| 1690 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1691 | } |
| 1692 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1693 | lock.l_type = F_UNLCK; |
| 1694 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1695 | lock.l_start = PENDING_BYTE; |
| 1696 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1697 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1698 | pInode->eFileLock = SHARED_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1699 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1700 | rc = SQLITE_IOERR_UNLOCK; |
| 1701 | pFile->lastErrno = errno; |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 1702 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1703 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1704 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1705 | if( eFileLock==NO_LOCK ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1706 | /* Decrement the shared lock counter. Release the lock using an |
| 1707 | ** OS call only when all threads in this same process have released |
| 1708 | ** the lock. |
| 1709 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1710 | pInode->nShared--; |
| 1711 | if( pInode->nShared==0 ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1712 | lock.l_type = F_UNLCK; |
| 1713 | lock.l_whence = SEEK_SET; |
| 1714 | lock.l_start = lock.l_len = 0L; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1715 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1716 | pInode->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1717 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1718 | rc = SQLITE_IOERR_UNLOCK; |
| 1719 | pFile->lastErrno = errno; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1720 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1721 | pFile->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1722 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1725 | /* Decrement the count of locks against this same file. When the |
| 1726 | ** count reaches zero, close any other file descriptors whose close |
| 1727 | ** was deferred because of outstanding locks. |
| 1728 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1729 | pInode->nLock--; |
| 1730 | assert( pInode->nLock>=0 ); |
| 1731 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1732 | closePendingFds(pFile); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1733 | } |
| 1734 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1735 | |
| 1736 | end_unlock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1737 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1738 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1739 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1743 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1744 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1745 | ** |
| 1746 | ** If the locking level of the file descriptor is already at or below |
| 1747 | ** the requested locking level, this routine is a no-op. |
| 1748 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1749 | static int unixUnlock(sqlite3_file *id, int eFileLock){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1750 | return posixUnlock(id, eFileLock, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1754 | ** This function performs the parts of the "close file" operation |
| 1755 | ** common to all locking schemes. It closes the directory and file |
| 1756 | ** handles, if they are valid, and sets all fields of the unixFile |
| 1757 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1758 | ** |
| 1759 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 1760 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 1761 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1762 | */ |
| 1763 | static int closeUnixFile(sqlite3_file *id){ |
| 1764 | unixFile *pFile = (unixFile*)id; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1765 | if( pFile->h>=0 ){ |
| 1766 | robust_close(pFile, pFile->h, __LINE__); |
| 1767 | pFile->h = -1; |
| 1768 | } |
| 1769 | #if OS_VXWORKS |
| 1770 | if( pFile->pId ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 1771 | if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 1772 | osUnlink(pFile->pId->zCanonicalName); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1773 | } |
| 1774 | vxworksReleaseFileId(pFile->pId); |
| 1775 | pFile->pId = 0; |
| 1776 | } |
| 1777 | #endif |
| 1778 | OSTRACE(("CLOSE %-3d\n", pFile->h)); |
| 1779 | OpenCounter(-1); |
| 1780 | sqlite3_free(pFile->pUnused); |
| 1781 | memset(pFile, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1782 | return SQLITE_OK; |
| 1783 | } |
| 1784 | |
| 1785 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1786 | ** Close a file. |
| 1787 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1788 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1789 | int rc = SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1790 | unixFile *pFile = (unixFile *)id; |
| 1791 | unixUnlock(id, NO_LOCK); |
| 1792 | unixEnterMutex(); |
| 1793 | |
| 1794 | /* unixFile.pInode is always valid here. Otherwise, a different close |
| 1795 | ** routine (e.g. nolockClose()) would be called instead. |
| 1796 | */ |
| 1797 | assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); |
| 1798 | if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){ |
| 1799 | /* If there are outstanding locks, do not actually close the file just |
| 1800 | ** yet because that would clear those locks. Instead, add the file |
| 1801 | ** descriptor to pInode->pUnused list. It will be automatically closed |
| 1802 | ** when the last lock is cleared. |
| 1803 | */ |
| 1804 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1805 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1806 | releaseInodeInfo(pFile); |
| 1807 | rc = closeUnixFile(id); |
| 1808 | unixLeaveMutex(); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1809 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1812 | /************** End of the posix advisory lock implementation ***************** |
| 1813 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1814 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1815 | /****************************************************************************** |
| 1816 | ****************************** No-op Locking ********************************** |
| 1817 | ** |
| 1818 | ** Of the various locking implementations available, this is by far the |
| 1819 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 1820 | ** file for reading or writing. |
| 1821 | ** |
| 1822 | ** This locking mode is appropriate for use on read-only databases |
| 1823 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 1824 | ** also be used if the application employs some external mechanism to |
| 1825 | ** prevent simultaneous access of the same database by two or more |
| 1826 | ** database connections. But there is a serious risk of database |
| 1827 | ** corruption if this locking mode is used in situations where multiple |
| 1828 | ** database connections are accessing the same database file at the same |
| 1829 | ** time and one or more of those connections are writing. |
| 1830 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1831 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1832 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 1833 | UNUSED_PARAMETER(NotUsed); |
| 1834 | *pResOut = 0; |
| 1835 | return SQLITE_OK; |
| 1836 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1837 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1838 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1839 | return SQLITE_OK; |
| 1840 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1841 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1842 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1843 | return SQLITE_OK; |
| 1844 | } |
| 1845 | |
| 1846 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1847 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1848 | */ |
| 1849 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1850 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | /******************* End of the no-op lock implementation ********************* |
| 1854 | ******************************************************************************/ |
| 1855 | |
| 1856 | /****************************************************************************** |
| 1857 | ************************* Begin dot-file Locking ****************************** |
| 1858 | ** |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1859 | ** The dotfile locking implementation uses the existance of separate lock |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 1860 | ** files (really a directory) to control access to the database. This works |
| 1861 | ** on just about every filesystem imaginable. But there are serious downsides: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1862 | ** |
| 1863 | ** (1) There is zero concurrency. A single reader blocks all other |
| 1864 | ** connections from reading or writing the database. |
| 1865 | ** |
| 1866 | ** (2) An application crash or power loss can leave stale lock files |
| 1867 | ** sitting around that need to be cleared manually. |
| 1868 | ** |
| 1869 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 1870 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1871 | ** |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 1872 | ** Dotfile locking works by creating a subdirectory in the same directory as |
| 1873 | ** the database and with the same name but with a ".lock" extension added. |
| 1874 | ** The existance of a lock directory implies an EXCLUSIVE lock. All other |
| 1875 | ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1876 | */ |
| 1877 | |
| 1878 | /* |
| 1879 | ** The file suffix added to the data base filename in order to create the |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 1880 | ** lock directory. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1881 | */ |
| 1882 | #define DOTLOCK_SUFFIX ".lock" |
| 1883 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1884 | /* |
| 1885 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1886 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1887 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1888 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1889 | ** |
| 1890 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 1891 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 1892 | ** is held on the file and false if the file is unlocked. |
| 1893 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1894 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 1895 | int rc = SQLITE_OK; |
| 1896 | int reserved = 0; |
| 1897 | unixFile *pFile = (unixFile*)id; |
| 1898 | |
| 1899 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1900 | |
| 1901 | assert( pFile ); |
| 1902 | |
| 1903 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1904 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1905 | /* Either this connection or some other connection in the same process |
| 1906 | ** holds a lock on the file. No need to check further. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1907 | reserved = 1; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1908 | }else{ |
| 1909 | /* The lock is held if and only if the lockfile exists */ |
| 1910 | const char *zLockFile = (const char*)pFile->lockingContext; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1911 | reserved = osAccess(zLockFile, 0)==0; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1912 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1913 | OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1914 | *pResOut = reserved; |
| 1915 | return rc; |
| 1916 | } |
| 1917 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1918 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1919 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1920 | ** of the following: |
| 1921 | ** |
| 1922 | ** (1) SHARED_LOCK |
| 1923 | ** (2) RESERVED_LOCK |
| 1924 | ** (3) PENDING_LOCK |
| 1925 | ** (4) EXCLUSIVE_LOCK |
| 1926 | ** |
| 1927 | ** Sometimes when requesting one lock state, additional lock states |
| 1928 | ** are inserted in between. The locking might fail on one of the later |
| 1929 | ** transitions leaving the lock state different from what it started but |
| 1930 | ** still short of its goal. The following chart shows the allowed |
| 1931 | ** transitions and the inserted intermediate states: |
| 1932 | ** |
| 1933 | ** UNLOCKED -> SHARED |
| 1934 | ** SHARED -> RESERVED |
| 1935 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1936 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1937 | ** PENDING -> EXCLUSIVE |
| 1938 | ** |
| 1939 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1940 | ** routine to lower a locking level. |
| 1941 | ** |
| 1942 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 1943 | ** But we track the other locking levels internally. |
| 1944 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1945 | static int dotlockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1946 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1947 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1948 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1949 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1950 | |
| 1951 | /* If we have any lock, then the lock file already exists. All we have |
| 1952 | ** to do is adjust our internal record of the lock level. |
| 1953 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1954 | if( pFile->eFileLock > NO_LOCK ){ |
| 1955 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1956 | /* Always update the timestamp on the old file */ |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 1957 | #ifdef HAVE_UTIME |
| 1958 | utime(zLockFile, NULL); |
| 1959 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1960 | utimes(zLockFile, NULL); |
| 1961 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1962 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | /* grab an exclusive lock */ |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 1966 | rc = osMkdir(zLockFile, 0777); |
| 1967 | if( rc<0 ){ |
| 1968 | /* failed to open/create the lock directory */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1969 | int tErrno = errno; |
| 1970 | if( EEXIST == tErrno ){ |
| 1971 | rc = SQLITE_BUSY; |
| 1972 | } else { |
| 1973 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1974 | if( IS_LOCK_ERROR(rc) ){ |
| 1975 | pFile->lastErrno = tErrno; |
| 1976 | } |
| 1977 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1978 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1979 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1980 | |
| 1981 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1982 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1983 | return rc; |
| 1984 | } |
| 1985 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1986 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1987 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1988 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1989 | ** |
| 1990 | ** If the locking level of the file descriptor is already at or below |
| 1991 | ** the requested locking level, this routine is a no-op. |
| 1992 | ** |
| 1993 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 1994 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1995 | static int dotlockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1996 | unixFile *pFile = (unixFile*)id; |
| 1997 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 1998 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1999 | |
| 2000 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2001 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
| 2002 | pFile->eFileLock, getpid())); |
| 2003 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2004 | |
| 2005 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2006 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2007 | return SQLITE_OK; |
| 2008 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2009 | |
| 2010 | /* To downgrade to shared, simply update our internal notion of the |
| 2011 | ** lock state. No need to mess with the file on disk. |
| 2012 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2013 | if( eFileLock==SHARED_LOCK ){ |
| 2014 | pFile->eFileLock = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2015 | return SQLITE_OK; |
| 2016 | } |
| 2017 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2018 | /* To fully unlock the database, delete the lock file */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2019 | assert( eFileLock==NO_LOCK ); |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2020 | rc = osRmdir(zLockFile); |
| 2021 | if( rc<0 && errno==ENOTDIR ) rc = osUnlink(zLockFile); |
| 2022 | if( rc<0 ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 2023 | int tErrno = errno; |
drh | 13e0ea9 | 2011-12-11 02:29:25 +0000 | [diff] [blame] | 2024 | rc = 0; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2025 | if( ENOENT != tErrno ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2026 | rc = SQLITE_IOERR_UNLOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2027 | } |
| 2028 | if( IS_LOCK_ERROR(rc) ){ |
| 2029 | pFile->lastErrno = tErrno; |
| 2030 | } |
| 2031 | return rc; |
| 2032 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2033 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2034 | return SQLITE_OK; |
| 2035 | } |
| 2036 | |
| 2037 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2038 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2039 | */ |
| 2040 | static int dotlockClose(sqlite3_file *id) { |
| 2041 | int rc; |
| 2042 | if( id ){ |
| 2043 | unixFile *pFile = (unixFile*)id; |
| 2044 | dotlockUnlock(id, NO_LOCK); |
| 2045 | sqlite3_free(pFile->lockingContext); |
| 2046 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2047 | rc = closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2048 | return rc; |
| 2049 | } |
| 2050 | /****************** End of the dot-file lock implementation ******************* |
| 2051 | ******************************************************************************/ |
| 2052 | |
| 2053 | /****************************************************************************** |
| 2054 | ************************** Begin flock Locking ******************************** |
| 2055 | ** |
| 2056 | ** Use the flock() system call to do file locking. |
| 2057 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2058 | ** flock() locking is like dot-file locking in that the various |
| 2059 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 2060 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 2061 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 2062 | ** still works when you do this, but concurrency is reduced since |
| 2063 | ** only a single process can be reading the database at a time. |
| 2064 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2065 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if |
| 2066 | ** compiling for VXWORKS. |
| 2067 | */ |
| 2068 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2069 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2070 | /* |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2071 | ** Retry flock() calls that fail with EINTR |
| 2072 | */ |
| 2073 | #ifdef EINTR |
| 2074 | static int robust_flock(int fd, int op){ |
| 2075 | int rc; |
| 2076 | do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR ); |
| 2077 | return rc; |
| 2078 | } |
| 2079 | #else |
drh | 5c81927 | 2011-02-23 14:00:12 +0000 | [diff] [blame] | 2080 | # define robust_flock(a,b) flock(a,b) |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2081 | #endif |
| 2082 | |
| 2083 | |
| 2084 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2085 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2086 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2087 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2088 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2089 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2090 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 2091 | int rc = SQLITE_OK; |
| 2092 | int reserved = 0; |
| 2093 | unixFile *pFile = (unixFile*)id; |
| 2094 | |
| 2095 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2096 | |
| 2097 | assert( pFile ); |
| 2098 | |
| 2099 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2100 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2101 | reserved = 1; |
| 2102 | } |
| 2103 | |
| 2104 | /* Otherwise see if some other process holds it. */ |
| 2105 | if( !reserved ){ |
| 2106 | /* attempt to get the lock */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2107 | int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2108 | if( !lrc ){ |
| 2109 | /* got the lock, unlock it */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2110 | lrc = robust_flock(pFile->h, LOCK_UN); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2111 | if ( lrc ) { |
| 2112 | int tErrno = errno; |
| 2113 | /* unlock failed with an error */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2114 | lrc = SQLITE_IOERR_UNLOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2115 | if( IS_LOCK_ERROR(lrc) ){ |
| 2116 | pFile->lastErrno = tErrno; |
| 2117 | rc = lrc; |
| 2118 | } |
| 2119 | } |
| 2120 | } else { |
| 2121 | int tErrno = errno; |
| 2122 | reserved = 1; |
| 2123 | /* someone else might have it reserved */ |
| 2124 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2125 | if( IS_LOCK_ERROR(lrc) ){ |
| 2126 | pFile->lastErrno = tErrno; |
| 2127 | rc = lrc; |
| 2128 | } |
| 2129 | } |
| 2130 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2131 | OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2132 | |
| 2133 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2134 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2135 | rc = SQLITE_OK; |
| 2136 | reserved=1; |
| 2137 | } |
| 2138 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2139 | *pResOut = reserved; |
| 2140 | return rc; |
| 2141 | } |
| 2142 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2143 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2144 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2145 | ** of the following: |
| 2146 | ** |
| 2147 | ** (1) SHARED_LOCK |
| 2148 | ** (2) RESERVED_LOCK |
| 2149 | ** (3) PENDING_LOCK |
| 2150 | ** (4) EXCLUSIVE_LOCK |
| 2151 | ** |
| 2152 | ** Sometimes when requesting one lock state, additional lock states |
| 2153 | ** are inserted in between. The locking might fail on one of the later |
| 2154 | ** transitions leaving the lock state different from what it started but |
| 2155 | ** still short of its goal. The following chart shows the allowed |
| 2156 | ** transitions and the inserted intermediate states: |
| 2157 | ** |
| 2158 | ** UNLOCKED -> SHARED |
| 2159 | ** SHARED -> RESERVED |
| 2160 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2161 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2162 | ** PENDING -> EXCLUSIVE |
| 2163 | ** |
| 2164 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 2165 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2166 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2167 | ** access the file. |
| 2168 | ** |
| 2169 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2170 | ** routine to lower a locking level. |
| 2171 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2172 | static int flockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2173 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2174 | unixFile *pFile = (unixFile*)id; |
| 2175 | |
| 2176 | assert( pFile ); |
| 2177 | |
| 2178 | /* if we already have a lock, it is exclusive. |
| 2179 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2180 | if (pFile->eFileLock > NO_LOCK) { |
| 2181 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2182 | return SQLITE_OK; |
| 2183 | } |
| 2184 | |
| 2185 | /* grab an exclusive lock */ |
| 2186 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2187 | if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2188 | int tErrno = errno; |
| 2189 | /* didn't get, must be busy */ |
| 2190 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2191 | if( IS_LOCK_ERROR(rc) ){ |
| 2192 | pFile->lastErrno = tErrno; |
| 2193 | } |
| 2194 | } else { |
| 2195 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2196 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2197 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2198 | OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), |
| 2199 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2200 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2201 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2202 | rc = SQLITE_BUSY; |
| 2203 | } |
| 2204 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2205 | return rc; |
| 2206 | } |
| 2207 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2208 | |
| 2209 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2210 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2211 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2212 | ** |
| 2213 | ** If the locking level of the file descriptor is already at or below |
| 2214 | ** the requested locking level, this routine is a no-op. |
| 2215 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2216 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2217 | unixFile *pFile = (unixFile*)id; |
| 2218 | |
| 2219 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2220 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
| 2221 | pFile->eFileLock, getpid())); |
| 2222 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2223 | |
| 2224 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2225 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2226 | return SQLITE_OK; |
| 2227 | } |
| 2228 | |
| 2229 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2230 | if (eFileLock==SHARED_LOCK) { |
| 2231 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2232 | return SQLITE_OK; |
| 2233 | } |
| 2234 | |
| 2235 | /* no, really, unlock. */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2236 | if( robust_flock(pFile->h, LOCK_UN) ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2237 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2238 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2239 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2240 | return SQLITE_IOERR_UNLOCK; |
| 2241 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2242 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2243 | return SQLITE_OK; |
| 2244 | } |
| 2245 | } |
| 2246 | |
| 2247 | /* |
| 2248 | ** Close a file. |
| 2249 | */ |
| 2250 | static int flockClose(sqlite3_file *id) { |
| 2251 | if( id ){ |
| 2252 | flockUnlock(id, NO_LOCK); |
| 2253 | } |
| 2254 | return closeUnixFile(id); |
| 2255 | } |
| 2256 | |
| 2257 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 2258 | |
| 2259 | /******************* End of the flock lock implementation ********************* |
| 2260 | ******************************************************************************/ |
| 2261 | |
| 2262 | /****************************************************************************** |
| 2263 | ************************ Begin Named Semaphore Locking ************************ |
| 2264 | ** |
| 2265 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2266 | ** |
| 2267 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 2268 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 2269 | ** the database file at a time. This reduces potential concurrency, but |
| 2270 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2271 | */ |
| 2272 | #if OS_VXWORKS |
| 2273 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2274 | /* |
| 2275 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2276 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2277 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2278 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2279 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2280 | static int semCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 2281 | int rc = SQLITE_OK; |
| 2282 | int reserved = 0; |
| 2283 | unixFile *pFile = (unixFile*)id; |
| 2284 | |
| 2285 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2286 | |
| 2287 | assert( pFile ); |
| 2288 | |
| 2289 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2290 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2291 | reserved = 1; |
| 2292 | } |
| 2293 | |
| 2294 | /* Otherwise see if some other process holds it. */ |
| 2295 | if( !reserved ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2296 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2297 | struct stat statBuf; |
| 2298 | |
| 2299 | if( sem_trywait(pSem)==-1 ){ |
| 2300 | int tErrno = errno; |
| 2301 | if( EAGAIN != tErrno ){ |
| 2302 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 2303 | pFile->lastErrno = tErrno; |
| 2304 | } else { |
| 2305 | /* someone else has the lock when we are in NO_LOCK */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2306 | reserved = (pFile->eFileLock < SHARED_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2307 | } |
| 2308 | }else{ |
| 2309 | /* we could have it if we want it */ |
| 2310 | sem_post(pSem); |
| 2311 | } |
| 2312 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2313 | OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2314 | |
| 2315 | *pResOut = reserved; |
| 2316 | return rc; |
| 2317 | } |
| 2318 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2319 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2320 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2321 | ** of the following: |
| 2322 | ** |
| 2323 | ** (1) SHARED_LOCK |
| 2324 | ** (2) RESERVED_LOCK |
| 2325 | ** (3) PENDING_LOCK |
| 2326 | ** (4) EXCLUSIVE_LOCK |
| 2327 | ** |
| 2328 | ** Sometimes when requesting one lock state, additional lock states |
| 2329 | ** are inserted in between. The locking might fail on one of the later |
| 2330 | ** transitions leaving the lock state different from what it started but |
| 2331 | ** still short of its goal. The following chart shows the allowed |
| 2332 | ** transitions and the inserted intermediate states: |
| 2333 | ** |
| 2334 | ** UNLOCKED -> SHARED |
| 2335 | ** SHARED -> RESERVED |
| 2336 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2337 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2338 | ** PENDING -> EXCLUSIVE |
| 2339 | ** |
| 2340 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 2341 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2342 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2343 | ** access the file. |
| 2344 | ** |
| 2345 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2346 | ** routine to lower a locking level. |
| 2347 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2348 | static int semLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2349 | unixFile *pFile = (unixFile*)id; |
| 2350 | int fd; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2351 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2352 | int rc = SQLITE_OK; |
| 2353 | |
| 2354 | /* if we already have a lock, it is exclusive. |
| 2355 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2356 | if (pFile->eFileLock > NO_LOCK) { |
| 2357 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2358 | rc = SQLITE_OK; |
| 2359 | goto sem_end_lock; |
| 2360 | } |
| 2361 | |
| 2362 | /* lock semaphore now but bail out when already locked. */ |
| 2363 | if( sem_trywait(pSem)==-1 ){ |
| 2364 | rc = SQLITE_BUSY; |
| 2365 | goto sem_end_lock; |
| 2366 | } |
| 2367 | |
| 2368 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2369 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2370 | |
| 2371 | sem_end_lock: |
| 2372 | return rc; |
| 2373 | } |
| 2374 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2375 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2376 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2377 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2378 | ** |
| 2379 | ** If the locking level of the file descriptor is already at or below |
| 2380 | ** the requested locking level, this routine is a no-op. |
| 2381 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2382 | static int semUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2383 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2384 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2385 | |
| 2386 | assert( pFile ); |
| 2387 | assert( pSem ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2388 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
| 2389 | pFile->eFileLock, getpid())); |
| 2390 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2391 | |
| 2392 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2393 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2394 | return SQLITE_OK; |
| 2395 | } |
| 2396 | |
| 2397 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2398 | if (eFileLock==SHARED_LOCK) { |
| 2399 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2400 | return SQLITE_OK; |
| 2401 | } |
| 2402 | |
| 2403 | /* no, really unlock. */ |
| 2404 | if ( sem_post(pSem)==-1 ) { |
| 2405 | int rc, tErrno = errno; |
| 2406 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2407 | if( IS_LOCK_ERROR(rc) ){ |
| 2408 | pFile->lastErrno = tErrno; |
| 2409 | } |
| 2410 | return rc; |
| 2411 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2412 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2413 | return SQLITE_OK; |
| 2414 | } |
| 2415 | |
| 2416 | /* |
| 2417 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2418 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2419 | static int semClose(sqlite3_file *id) { |
| 2420 | if( id ){ |
| 2421 | unixFile *pFile = (unixFile*)id; |
| 2422 | semUnlock(id, NO_LOCK); |
| 2423 | assert( pFile ); |
| 2424 | unixEnterMutex(); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2425 | releaseInodeInfo(pFile); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2426 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2427 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2428 | } |
| 2429 | return SQLITE_OK; |
| 2430 | } |
| 2431 | |
| 2432 | #endif /* OS_VXWORKS */ |
| 2433 | /* |
| 2434 | ** Named semaphore locking is only available on VxWorks. |
| 2435 | ** |
| 2436 | *************** End of the named semaphore lock implementation **************** |
| 2437 | ******************************************************************************/ |
| 2438 | |
| 2439 | |
| 2440 | /****************************************************************************** |
| 2441 | *************************** Begin AFP Locking ********************************* |
| 2442 | ** |
| 2443 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2444 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2445 | ** |
| 2446 | ** Third-party implementations of AFP are available. But this code here |
| 2447 | ** only works on OSX. |
| 2448 | */ |
| 2449 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2450 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2451 | /* |
| 2452 | ** The afpLockingContext structure contains all afp lock specific state |
| 2453 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2454 | typedef struct afpLockingContext afpLockingContext; |
| 2455 | struct afpLockingContext { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2456 | int reserved; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2457 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2458 | }; |
| 2459 | |
| 2460 | struct ByteRangeLockPB2 |
| 2461 | { |
| 2462 | unsigned long long offset; /* offset to first byte to lock */ |
| 2463 | unsigned long long length; /* nbr of bytes to lock */ |
| 2464 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2465 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2466 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2467 | int fd; /* file desc to assoc this lock with */ |
| 2468 | }; |
| 2469 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2470 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2471 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2472 | /* |
| 2473 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2474 | ** AFP filesystem. |
| 2475 | ** |
| 2476 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2477 | */ |
| 2478 | static int afpSetLock( |
| 2479 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2480 | unixFile *pFile, /* Open file descriptor on path */ |
| 2481 | unsigned long long offset, /* First byte to be locked */ |
| 2482 | unsigned long long length, /* Number of bytes to lock */ |
| 2483 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2484 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2485 | struct ByteRangeLockPB2 pb; |
| 2486 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2487 | |
| 2488 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2489 | pb.startEndFlag = 0; |
| 2490 | pb.offset = offset; |
| 2491 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2492 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2493 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2494 | OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2495 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2496 | offset, length)); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2497 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2498 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2499 | int rc; |
| 2500 | int tErrno = errno; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2501 | OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2502 | path, tErrno, strerror(tErrno))); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2503 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2504 | rc = SQLITE_BUSY; |
| 2505 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2506 | rc = sqliteErrorFromPosixError(tErrno, |
| 2507 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2508 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2509 | if( IS_LOCK_ERROR(rc) ){ |
| 2510 | pFile->lastErrno = tErrno; |
| 2511 | } |
| 2512 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2513 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2514 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2515 | } |
| 2516 | } |
| 2517 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2518 | /* |
| 2519 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2520 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2521 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2522 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2523 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2524 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2525 | int rc = SQLITE_OK; |
| 2526 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2527 | unixFile *pFile = (unixFile*)id; |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2528 | afpLockingContext *context; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2529 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2530 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2531 | |
| 2532 | assert( pFile ); |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2533 | context = (afpLockingContext *) pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2534 | if( context->reserved ){ |
| 2535 | *pResOut = 1; |
| 2536 | return SQLITE_OK; |
| 2537 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2538 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2539 | |
| 2540 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2541 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2542 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2543 | } |
| 2544 | |
| 2545 | /* Otherwise see if some other process holds it. |
| 2546 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2547 | if( !reserved ){ |
| 2548 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2549 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2550 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2551 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2552 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2553 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2554 | } else { |
| 2555 | /* if we failed to get the lock then someone else must have it */ |
| 2556 | reserved = 1; |
| 2557 | } |
| 2558 | if( IS_LOCK_ERROR(lrc) ){ |
| 2559 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2560 | } |
| 2561 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2562 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2563 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2564 | OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2565 | |
| 2566 | *pResOut = reserved; |
| 2567 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2570 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2571 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2572 | ** of the following: |
| 2573 | ** |
| 2574 | ** (1) SHARED_LOCK |
| 2575 | ** (2) RESERVED_LOCK |
| 2576 | ** (3) PENDING_LOCK |
| 2577 | ** (4) EXCLUSIVE_LOCK |
| 2578 | ** |
| 2579 | ** Sometimes when requesting one lock state, additional lock states |
| 2580 | ** are inserted in between. The locking might fail on one of the later |
| 2581 | ** transitions leaving the lock state different from what it started but |
| 2582 | ** still short of its goal. The following chart shows the allowed |
| 2583 | ** transitions and the inserted intermediate states: |
| 2584 | ** |
| 2585 | ** UNLOCKED -> SHARED |
| 2586 | ** SHARED -> RESERVED |
| 2587 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2588 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2589 | ** PENDING -> EXCLUSIVE |
| 2590 | ** |
| 2591 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2592 | ** routine to lower a locking level. |
| 2593 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2594 | static int afpLock(sqlite3_file *id, int eFileLock){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2595 | int rc = SQLITE_OK; |
| 2596 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2597 | unixInodeInfo *pInode = pFile->pInode; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2598 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2599 | |
| 2600 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2601 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 2602 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2603 | azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2604 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2605 | /* 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] | 2606 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2607 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2608 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2609 | if( pFile->eFileLock>=eFileLock ){ |
| 2610 | OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
| 2611 | azFileLock(eFileLock))); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2612 | return SQLITE_OK; |
| 2613 | } |
| 2614 | |
| 2615 | /* Make sure the locking sequence is correct |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2616 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 2617 | ** (2) SQLite never explicitly requests a pendig lock. |
| 2618 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2619 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2620 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 2621 | assert( eFileLock!=PENDING_LOCK ); |
| 2622 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2623 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2624 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2625 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2626 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2627 | pInode = pFile->pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2628 | |
| 2629 | /* If some thread using this PID has a lock via a different unixFile* |
| 2630 | ** handle that precludes the requested lock, return BUSY. |
| 2631 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2632 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 2633 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2634 | ){ |
| 2635 | rc = SQLITE_BUSY; |
| 2636 | goto afp_end_lock; |
| 2637 | } |
| 2638 | |
| 2639 | /* If a SHARED lock is requested, and some thread using this PID already |
| 2640 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 2641 | ** return SQLITE_OK. |
| 2642 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2643 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2644 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2645 | assert( eFileLock==SHARED_LOCK ); |
| 2646 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2647 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2648 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2649 | pInode->nShared++; |
| 2650 | pInode->nLock++; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2651 | goto afp_end_lock; |
| 2652 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2653 | |
| 2654 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2655 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2656 | ** be released. |
| 2657 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2658 | if( eFileLock==SHARED_LOCK |
| 2659 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2660 | ){ |
| 2661 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2662 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2663 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2664 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2665 | goto afp_end_lock; |
| 2666 | } |
| 2667 | } |
| 2668 | |
| 2669 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2670 | ** operating system calls for the specified lock. |
| 2671 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2672 | if( eFileLock==SHARED_LOCK ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2673 | int lrc1, lrc2, lrc1Errno = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2674 | long lk, mask; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2675 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2676 | assert( pInode->nShared==0 ); |
| 2677 | assert( pInode->eFileLock==0 ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2678 | |
| 2679 | mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2680 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2681 | /* note that the quality of the randomness doesn't matter that much */ |
| 2682 | lk = random(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2683 | pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2684 | lrc1 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2685 | SHARED_FIRST+pInode->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2686 | if( IS_LOCK_ERROR(lrc1) ){ |
| 2687 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2688 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2689 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2690 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2691 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2692 | if( IS_LOCK_ERROR(lrc1) ) { |
| 2693 | pFile->lastErrno = lrc1Errno; |
| 2694 | rc = lrc1; |
| 2695 | goto afp_end_lock; |
| 2696 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 2697 | rc = lrc2; |
| 2698 | goto afp_end_lock; |
| 2699 | } else if( lrc1 != SQLITE_OK ) { |
| 2700 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2701 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2702 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2703 | pInode->nLock++; |
| 2704 | pInode->nShared = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2705 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2706 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2707 | /* We are trying for an exclusive lock but another thread in this |
| 2708 | ** same process is still holding a shared lock. */ |
| 2709 | rc = SQLITE_BUSY; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2710 | }else{ |
| 2711 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 2712 | ** assumed that there is a SHARED or greater lock on the file |
| 2713 | ** already. |
| 2714 | */ |
| 2715 | int failed = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2716 | assert( 0!=pFile->eFileLock ); |
| 2717 | if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2718 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2719 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2720 | if( !failed ){ |
| 2721 | context->reserved = 1; |
| 2722 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2723 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2724 | if (!failed && eFileLock == EXCLUSIVE_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2725 | /* Acquire an EXCLUSIVE lock */ |
| 2726 | |
| 2727 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2728 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2729 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2730 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2731 | pInode->sharedByte, 1, 0)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2732 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2733 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2734 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2735 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2736 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2737 | SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2738 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 2739 | ** a critical I/O error |
| 2740 | */ |
| 2741 | rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : |
| 2742 | SQLITE_IOERR_LOCK; |
| 2743 | goto afp_end_lock; |
| 2744 | } |
| 2745 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2746 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2747 | } |
| 2748 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2749 | if( failed ){ |
| 2750 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2751 | } |
| 2752 | } |
| 2753 | |
| 2754 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2755 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2756 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2757 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 2758 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2759 | pInode->eFileLock = PENDING_LOCK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | afp_end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2763 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2764 | OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), |
| 2765 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2766 | return rc; |
| 2767 | } |
| 2768 | |
| 2769 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2770 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2771 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2772 | ** |
| 2773 | ** If the locking level of the file descriptor is already at or below |
| 2774 | ** the requested locking level, this routine is a no-op. |
| 2775 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2776 | static int afpUnlock(sqlite3_file *id, int eFileLock) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2777 | int rc = SQLITE_OK; |
| 2778 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2779 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2780 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 2781 | int skipShared = 0; |
| 2782 | #ifdef SQLITE_TEST |
| 2783 | int h = pFile->h; |
| 2784 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2785 | |
| 2786 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2787 | 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] | 2788 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2789 | getpid())); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2790 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2791 | assert( eFileLock<=SHARED_LOCK ); |
| 2792 | if( pFile->eFileLock<=eFileLock ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2793 | return SQLITE_OK; |
| 2794 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2795 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2796 | pInode = pFile->pInode; |
| 2797 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2798 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2799 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2800 | SimulateIOErrorBenign(1); |
| 2801 | SimulateIOError( h=(-1) ) |
| 2802 | SimulateIOErrorBenign(0); |
| 2803 | |
| 2804 | #ifndef NDEBUG |
| 2805 | /* When reducing a lock such that other processes can start |
| 2806 | ** reading the database file again, make sure that the |
| 2807 | ** transaction counter was updated if any part of the database |
| 2808 | ** file changed. If the transaction counter is not updated, |
| 2809 | ** other connections to the same file might not realize that |
| 2810 | ** the file has changed and hence might not know to flush their |
| 2811 | ** cache. The use of a stale cache can lead to database corruption. |
| 2812 | */ |
| 2813 | assert( pFile->inNormalWrite==0 |
| 2814 | || pFile->dbUpdate==0 |
| 2815 | || pFile->transCntrChng==1 ); |
| 2816 | pFile->inNormalWrite = 0; |
| 2817 | #endif |
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( pFile->eFileLock==EXCLUSIVE_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2820 | rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2821 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2822 | /* only re-establish the shared lock if necessary */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2823 | int sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2824 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 2825 | } else { |
| 2826 | skipShared = 1; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2827 | } |
| 2828 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2829 | if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2830 | rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2831 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2832 | if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2833 | rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 2834 | if( !rc ){ |
| 2835 | context->reserved = 0; |
| 2836 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2837 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2838 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ |
| 2839 | pInode->eFileLock = SHARED_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2840 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2841 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2842 | if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2843 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2844 | /* Decrement the shared lock counter. Release the lock using an |
| 2845 | ** OS call only when all threads in this same process have released |
| 2846 | ** the lock. |
| 2847 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2848 | unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 2849 | pInode->nShared--; |
| 2850 | if( pInode->nShared==0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2851 | SimulateIOErrorBenign(1); |
| 2852 | SimulateIOError( h=(-1) ) |
| 2853 | SimulateIOErrorBenign(0); |
| 2854 | if( !skipShared ){ |
| 2855 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 2856 | } |
| 2857 | if( !rc ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2858 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2859 | pFile->eFileLock = NO_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2860 | } |
| 2861 | } |
| 2862 | if( rc==SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2863 | pInode->nLock--; |
| 2864 | assert( pInode->nLock>=0 ); |
| 2865 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 2866 | closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2867 | } |
| 2868 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2869 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2870 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2871 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2872 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2873 | return rc; |
| 2874 | } |
| 2875 | |
| 2876 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2877 | ** Close a file & cleanup AFP specific locking context |
| 2878 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2879 | static int afpClose(sqlite3_file *id) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2880 | int rc = SQLITE_OK; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2881 | if( id ){ |
| 2882 | unixFile *pFile = (unixFile*)id; |
| 2883 | afpUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2884 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2885 | if( pFile->pInode && pFile->pInode->nLock ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2886 | /* If there are outstanding locks, do not actually close the file just |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2887 | ** yet because that would clear those locks. Instead, add the file |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2888 | ** descriptor to pInode->aPending. It will be automatically closed when |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2889 | ** the last lock is cleared. |
| 2890 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2891 | setPendingFd(pFile); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2892 | } |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2893 | releaseInodeInfo(pFile); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2894 | sqlite3_free(pFile->lockingContext); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2895 | rc = closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2896 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2897 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2898 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2901 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2902 | /* |
| 2903 | ** The code above is the AFP lock implementation. The code is specific |
| 2904 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2905 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 2906 | ** VFS is not available. |
| 2907 | ** |
| 2908 | ********************* End of the AFP lock implementation ********************** |
| 2909 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2910 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2911 | /****************************************************************************** |
| 2912 | *************************** Begin NFS Locking ********************************/ |
| 2913 | |
| 2914 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 2915 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2916 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2917 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2918 | ** |
| 2919 | ** If the locking level of the file descriptor is already at or below |
| 2920 | ** the requested locking level, this routine is a no-op. |
| 2921 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2922 | static int nfsUnlock(sqlite3_file *id, int eFileLock){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 2923 | return posixUnlock(id, eFileLock, 1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2924 | } |
| 2925 | |
| 2926 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 2927 | /* |
| 2928 | ** The code above is the NFS lock implementation. The code is specific |
| 2929 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2930 | ** is available. |
| 2931 | ** |
| 2932 | ********************* End of the NFS lock implementation ********************** |
| 2933 | ******************************************************************************/ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2934 | |
| 2935 | /****************************************************************************** |
| 2936 | **************** Non-locking sqlite3_file methods ***************************** |
| 2937 | ** |
| 2938 | ** The next division contains implementations for all methods of the |
| 2939 | ** sqlite3_file object other than the locking methods. The locking |
| 2940 | ** methods were defined in divisions above (one locking method per |
| 2941 | ** division). Those methods that are common to all locking modes |
| 2942 | ** are gather together into this division. |
| 2943 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2944 | |
| 2945 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2946 | ** Seek to the offset passed as the second argument, then read cnt |
| 2947 | ** bytes into pBuf. Return the number of bytes actually read. |
| 2948 | ** |
| 2949 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 2950 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 2951 | ** one system to another. Since SQLite does not define USE_PREAD |
| 2952 | ** any any form by default, we will not attempt to define _XOPEN_SOURCE. |
| 2953 | ** See tickets #2741 and #2681. |
| 2954 | ** |
| 2955 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 2956 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2957 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2958 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 2959 | int got; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 2960 | int prior = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2961 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2962 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2963 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2964 | TIMER_START; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 2965 | do{ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2966 | #if defined(USE_PREAD) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 2967 | got = osPread(id->h, pBuf, cnt, offset); |
| 2968 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2969 | #elif defined(USE_PREAD64) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 2970 | got = osPread64(id->h, pBuf, cnt, offset); |
| 2971 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2972 | #else |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 2973 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 2974 | SimulateIOError( newOffset-- ); |
| 2975 | if( newOffset!=offset ){ |
| 2976 | if( newOffset == -1 ){ |
| 2977 | ((unixFile*)id)->lastErrno = errno; |
| 2978 | }else{ |
| 2979 | ((unixFile*)id)->lastErrno = 0; |
| 2980 | } |
| 2981 | return -1; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2982 | } |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 2983 | got = osRead(id->h, pBuf, cnt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2984 | #endif |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 2985 | if( got==cnt ) break; |
| 2986 | if( got<0 ){ |
| 2987 | if( errno==EINTR ){ got = 1; continue; } |
| 2988 | prior = 0; |
| 2989 | ((unixFile*)id)->lastErrno = errno; |
| 2990 | break; |
| 2991 | }else if( got>0 ){ |
| 2992 | cnt -= got; |
| 2993 | offset += got; |
| 2994 | prior += got; |
| 2995 | pBuf = (void*)(got + (char*)pBuf); |
| 2996 | } |
| 2997 | }while( got>0 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2998 | TIMER_END; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 2999 | OSTRACE(("READ %-3d %5d %7lld %llu\n", |
| 3000 | id->h, got+prior, offset-prior, TIMER_ELAPSED)); |
| 3001 | return got+prior; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3005 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 3006 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 3007 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3008 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3009 | static int unixRead( |
| 3010 | sqlite3_file *id, |
| 3011 | void *pBuf, |
| 3012 | int amt, |
| 3013 | sqlite3_int64 offset |
| 3014 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3015 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3016 | int got; |
| 3017 | assert( id ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3018 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3019 | /* If this is a database file (not a journal, master-journal or temp |
| 3020 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3021 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3022 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3023 | || offset>=PENDING_BYTE+512 |
| 3024 | || offset+amt<=PENDING_BYTE |
| 3025 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3026 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3027 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3028 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3029 | if( got==amt ){ |
| 3030 | return SQLITE_OK; |
| 3031 | }else if( got<0 ){ |
| 3032 | /* lastErrno set by seekAndRead */ |
| 3033 | return SQLITE_IOERR_READ; |
| 3034 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3035 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3036 | /* Unread parts of the buffer must be zero-filled */ |
| 3037 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 3038 | return SQLITE_IOERR_SHORT_READ; |
| 3039 | } |
| 3040 | } |
| 3041 | |
| 3042 | /* |
| 3043 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 3044 | ** Return the number of bytes actually read. Update the offset. |
| 3045 | ** |
| 3046 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 3047 | ** is set before returning. |
| 3048 | */ |
| 3049 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
| 3050 | int got; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3051 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3052 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3053 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3054 | TIMER_START; |
| 3055 | #if defined(USE_PREAD) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 3056 | do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3057 | #elif defined(USE_PREAD64) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 3058 | do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3059 | #else |
drh | bd1e50c | 2011-08-19 14:54:12 +0000 | [diff] [blame] | 3060 | do{ |
| 3061 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 3062 | SimulateIOError( newOffset-- ); |
| 3063 | if( newOffset!=offset ){ |
| 3064 | if( newOffset == -1 ){ |
| 3065 | ((unixFile*)id)->lastErrno = errno; |
| 3066 | }else{ |
| 3067 | ((unixFile*)id)->lastErrno = 0; |
| 3068 | } |
| 3069 | return -1; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3070 | } |
drh | bd1e50c | 2011-08-19 14:54:12 +0000 | [diff] [blame] | 3071 | got = osWrite(id->h, pBuf, cnt); |
| 3072 | }while( got<0 && errno==EINTR ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3073 | #endif |
| 3074 | TIMER_END; |
| 3075 | if( got<0 ){ |
| 3076 | ((unixFile*)id)->lastErrno = errno; |
| 3077 | } |
| 3078 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3079 | OSTRACE(("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3080 | return got; |
| 3081 | } |
| 3082 | |
| 3083 | |
| 3084 | /* |
| 3085 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 3086 | ** or some other error code on failure. |
| 3087 | */ |
| 3088 | static int unixWrite( |
| 3089 | sqlite3_file *id, |
| 3090 | const void *pBuf, |
| 3091 | int amt, |
| 3092 | sqlite3_int64 offset |
| 3093 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3094 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3095 | int wrote = 0; |
| 3096 | assert( id ); |
| 3097 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3098 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3099 | /* If this is a database file (not a journal, master-journal or temp |
| 3100 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3101 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3102 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3103 | || offset>=PENDING_BYTE+512 |
| 3104 | || offset+amt<=PENDING_BYTE |
| 3105 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3106 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3107 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3108 | #ifndef NDEBUG |
| 3109 | /* If we are doing a normal write to a database file (as opposed to |
| 3110 | ** doing a hot-journal rollback or a write to some file other than a |
| 3111 | ** normal database file) then record the fact that the database |
| 3112 | ** has changed. If the transaction counter is modified, record that |
| 3113 | ** fact too. |
| 3114 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3115 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3116 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 3117 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3118 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3119 | char oldCntr[4]; |
| 3120 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3121 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3122 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3123 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3124 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 3125 | } |
| 3126 | } |
| 3127 | } |
| 3128 | #endif |
| 3129 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3130 | while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3131 | amt -= wrote; |
| 3132 | offset += wrote; |
| 3133 | pBuf = &((char*)pBuf)[wrote]; |
| 3134 | } |
| 3135 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 3136 | SimulateDiskfullError(( wrote=0, amt=1 )); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3137 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3138 | if( amt>0 ){ |
drh | a21b83b | 2011-04-15 12:36:10 +0000 | [diff] [blame] | 3139 | if( wrote<0 && pFile->lastErrno!=ENOSPC ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3140 | /* lastErrno set by seekAndWrite */ |
| 3141 | return SQLITE_IOERR_WRITE; |
| 3142 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3143 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3144 | return SQLITE_FULL; |
| 3145 | } |
| 3146 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3147 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3148 | return SQLITE_OK; |
| 3149 | } |
| 3150 | |
| 3151 | #ifdef SQLITE_TEST |
| 3152 | /* |
| 3153 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3154 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3155 | */ |
| 3156 | int sqlite3_sync_count = 0; |
| 3157 | int sqlite3_fullsync_count = 0; |
| 3158 | #endif |
| 3159 | |
| 3160 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3161 | ** We do not trust systems to provide a working fdatasync(). Some do. |
drh | 20f8e13 | 2011-08-31 21:01:55 +0000 | [diff] [blame] | 3162 | ** Others do no. To be safe, we will stick with the (slightly slower) |
| 3163 | ** fsync(). If you know that your system does support fdatasync() correctly, |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3164 | ** then simply compile with -Dfdatasync=fdatasync |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3165 | */ |
drh | 20f8e13 | 2011-08-31 21:01:55 +0000 | [diff] [blame] | 3166 | #if !defined(fdatasync) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3167 | # define fdatasync fsync |
| 3168 | #endif |
| 3169 | |
| 3170 | /* |
| 3171 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 3172 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 3173 | ** only available on Mac OS X. But that could change. |
| 3174 | */ |
| 3175 | #ifdef F_FULLFSYNC |
| 3176 | # define HAVE_FULLFSYNC 1 |
| 3177 | #else |
| 3178 | # define HAVE_FULLFSYNC 0 |
| 3179 | #endif |
| 3180 | |
| 3181 | |
| 3182 | /* |
| 3183 | ** The fsync() system call does not work as advertised on many |
| 3184 | ** unix systems. The following procedure is an attempt to make |
| 3185 | ** it work better. |
| 3186 | ** |
| 3187 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 3188 | ** for testing when we want to run through the test suite quickly. |
| 3189 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 3190 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 3191 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3192 | ** |
| 3193 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 3194 | ** The idea behind dataOnly is that it should only write the file content |
| 3195 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 3196 | ** unchanged since the file size is part of the inode. However, |
| 3197 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 3198 | ** file size has changed. The only real difference between fdatasync() |
| 3199 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 3200 | ** inode if the mtime or owner or other inode attributes have changed. |
| 3201 | ** We only care about the file size, not the other file attributes, so |
| 3202 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 3203 | ** So, we always use fdatasync() if it is available, regardless of |
| 3204 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3205 | */ |
| 3206 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3207 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3208 | |
| 3209 | /* The following "ifdef/elif/else/" block has the same structure as |
| 3210 | ** the one below. It is replicated here solely to avoid cluttering |
| 3211 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 3212 | */ |
| 3213 | #ifdef SQLITE_NO_SYNC |
| 3214 | UNUSED_PARAMETER(fd); |
| 3215 | UNUSED_PARAMETER(fullSync); |
| 3216 | UNUSED_PARAMETER(dataOnly); |
| 3217 | #elif HAVE_FULLFSYNC |
| 3218 | UNUSED_PARAMETER(dataOnly); |
| 3219 | #else |
| 3220 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3221 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3222 | #endif |
| 3223 | |
| 3224 | /* Record the number of times that we do a normal fsync() and |
| 3225 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 3226 | ** gets called with the correct arguments. |
| 3227 | */ |
| 3228 | #ifdef SQLITE_TEST |
| 3229 | if( fullSync ) sqlite3_fullsync_count++; |
| 3230 | sqlite3_sync_count++; |
| 3231 | #endif |
| 3232 | |
| 3233 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 3234 | ** no-op |
| 3235 | */ |
| 3236 | #ifdef SQLITE_NO_SYNC |
| 3237 | rc = SQLITE_OK; |
| 3238 | #elif HAVE_FULLFSYNC |
| 3239 | if( fullSync ){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3240 | rc = osFcntl(fd, F_FULLFSYNC, 0); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3241 | }else{ |
| 3242 | rc = 1; |
| 3243 | } |
| 3244 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3245 | ** It shouldn't be possible for fullfsync to fail on the local |
| 3246 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 3247 | ** isn't supported for this file system. So, attempt an fsync |
| 3248 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 3249 | ** It'd be better to detect fullfsync support once and avoid |
| 3250 | ** the fcntl call every time sync is called. |
| 3251 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3252 | if( rc ) rc = fsync(fd); |
| 3253 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3254 | #elif defined(__APPLE__) |
| 3255 | /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 3256 | ** so currently we default to the macro that redefines fdatasync to fsync |
| 3257 | */ |
| 3258 | rc = fsync(fd); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3259 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3260 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 3261 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3262 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3263 | rc = fsync(fd); |
| 3264 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3265 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3266 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 3267 | |
| 3268 | if( OS_VXWORKS && rc!= -1 ){ |
| 3269 | rc = 0; |
| 3270 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3271 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3272 | } |
| 3273 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3274 | /* |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3275 | ** Open a file descriptor to the directory containing file zFilename. |
| 3276 | ** If successful, *pFd is set to the opened file descriptor and |
| 3277 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 3278 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 3279 | ** value. |
| 3280 | ** |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3281 | ** The directory file descriptor is used for only one thing - to |
| 3282 | ** fsync() a directory to make sure file creation and deletion events |
| 3283 | ** are flushed to disk. Such fsyncs are not needed on newer |
| 3284 | ** journaling filesystems, but are required on older filesystems. |
| 3285 | ** |
| 3286 | ** This routine can be overridden using the xSetSysCall interface. |
| 3287 | ** The ability to override this routine was added in support of the |
| 3288 | ** chromium sandbox. Opening a directory is a security risk (we are |
| 3289 | ** told) so making it overrideable allows the chromium sandbox to |
| 3290 | ** replace this routine with a harmless no-op. To make this routine |
| 3291 | ** a no-op, replace it with a stub that returns SQLITE_OK but leaves |
| 3292 | ** *pFd set to a negative number. |
| 3293 | ** |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3294 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 3295 | ** the file descriptor *pFd using close(). |
| 3296 | */ |
| 3297 | static int openDirectory(const char *zFilename, int *pFd){ |
| 3298 | int ii; |
| 3299 | int fd = -1; |
| 3300 | char zDirname[MAX_PATHNAME+1]; |
| 3301 | |
| 3302 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
| 3303 | for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--); |
| 3304 | if( ii>0 ){ |
| 3305 | zDirname[ii] = '\0'; |
| 3306 | fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0); |
| 3307 | if( fd>=0 ){ |
| 3308 | #ifdef FD_CLOEXEC |
| 3309 | osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 3310 | #endif |
| 3311 | OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); |
| 3312 | } |
| 3313 | } |
| 3314 | *pFd = fd; |
| 3315 | return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname)); |
| 3316 | } |
| 3317 | |
| 3318 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3319 | ** Make sure all writes to a particular file are committed to disk. |
| 3320 | ** |
| 3321 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 3322 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 3323 | ** file data is synced. |
| 3324 | ** |
| 3325 | ** Under Unix, also make sure that the directory entry for the file |
| 3326 | ** has been created by fsync-ing the directory that contains the file. |
| 3327 | ** If we do not do this and we encounter a power failure, the directory |
| 3328 | ** entry for the journal might not exist after we reboot. The next |
| 3329 | ** SQLite to access the file will not know that the journal exists (because |
| 3330 | ** the directory entry for the journal was never created) and the transaction |
| 3331 | ** will not roll back - possibly leading to database corruption. |
| 3332 | */ |
| 3333 | static int unixSync(sqlite3_file *id, int flags){ |
| 3334 | int rc; |
| 3335 | unixFile *pFile = (unixFile*)id; |
| 3336 | |
| 3337 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 3338 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 3339 | |
| 3340 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 3341 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 3342 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 3343 | ); |
| 3344 | |
| 3345 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 3346 | ** line is to test that doing so does not cause any problems. |
| 3347 | */ |
| 3348 | SimulateDiskfullError( return SQLITE_FULL ); |
| 3349 | |
| 3350 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3351 | OSTRACE(("SYNC %-3d\n", pFile->h)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3352 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 3353 | SimulateIOError( rc=1 ); |
| 3354 | if( rc ){ |
| 3355 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3356 | return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3357 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3358 | |
| 3359 | /* Also fsync the directory containing the file if the DIRSYNC flag |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3360 | ** is set. This is a one-time occurrance. Many systems (examples: AIX) |
| 3361 | ** are unable to fsync a directory, so ignore errors on the fsync. |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3362 | */ |
| 3363 | if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){ |
| 3364 | int dirfd; |
| 3365 | OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3366 | HAVE_FULLFSYNC, isFullsync)); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3367 | rc = osOpenDirectory(pFile->zPath, &dirfd); |
| 3368 | if( rc==SQLITE_OK && dirfd>=0 ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3369 | full_fsync(dirfd, 0, 0); |
| 3370 | robust_close(pFile, dirfd, __LINE__); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 3371 | }else if( rc==SQLITE_CANTOPEN ){ |
| 3372 | rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3373 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3374 | pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3375 | } |
| 3376 | return rc; |
| 3377 | } |
| 3378 | |
| 3379 | /* |
| 3380 | ** Truncate an open file to a specified size |
| 3381 | */ |
| 3382 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3383 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3384 | int rc; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3385 | assert( pFile ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3386 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3387 | |
| 3388 | /* If the user has configured a chunk-size for this file, truncate the |
| 3389 | ** file so that it consists of an integer number of chunks (i.e. the |
| 3390 | ** actual file size after the operation may be larger than the requested |
| 3391 | ** size). |
| 3392 | */ |
| 3393 | if( pFile->szChunk ){ |
| 3394 | nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 3395 | } |
| 3396 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3397 | rc = robust_ftruncate(pFile->h, (off_t)nByte); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3398 | if( rc ){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3399 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3400 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3401 | }else{ |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3402 | #ifndef NDEBUG |
| 3403 | /* If we are doing a normal write to a database file (as opposed to |
| 3404 | ** doing a hot-journal rollback or a write to some file other than a |
| 3405 | ** normal database file) and we truncate the file to zero length, |
| 3406 | ** that effectively updates the change counter. This might happen |
| 3407 | ** when restoring a database using the backup API from a zero-length |
| 3408 | ** source. |
| 3409 | */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3410 | if( pFile->inNormalWrite && nByte==0 ){ |
| 3411 | pFile->transCntrChng = 1; |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3412 | } |
| 3413 | #endif |
| 3414 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3415 | return SQLITE_OK; |
| 3416 | } |
| 3417 | } |
| 3418 | |
| 3419 | /* |
| 3420 | ** Determine the current size of a file in bytes |
| 3421 | */ |
| 3422 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 3423 | int rc; |
| 3424 | struct stat buf; |
| 3425 | assert( id ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3426 | rc = osFstat(((unixFile*)id)->h, &buf); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3427 | SimulateIOError( rc=1 ); |
| 3428 | if( rc!=0 ){ |
| 3429 | ((unixFile*)id)->lastErrno = errno; |
| 3430 | return SQLITE_IOERR_FSTAT; |
| 3431 | } |
| 3432 | *pSize = buf.st_size; |
| 3433 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3434 | /* When opening a zero-size database, the findInodeInfo() procedure |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3435 | ** writes a single byte into that file in order to work around a bug |
| 3436 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 3437 | ** layers, we need to report this file size as zero even though it is |
| 3438 | ** really 1. Ticket #3260. |
| 3439 | */ |
| 3440 | if( *pSize==1 ) *pSize = 0; |
| 3441 | |
| 3442 | |
| 3443 | return SQLITE_OK; |
| 3444 | } |
| 3445 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3446 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3447 | /* |
| 3448 | ** Handler for proxy-locking file-control verbs. Defined below in the |
| 3449 | ** proxying locking division. |
| 3450 | */ |
| 3451 | static int proxyFileControl(sqlite3_file*,int,void*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3452 | #endif |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3453 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3454 | /* |
| 3455 | ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 3456 | ** file-control operation. Enlarge the database to nBytes in size |
| 3457 | ** (rounded up to the next chunk-size). If the database is already |
| 3458 | ** nBytes or larger, this routine is a no-op. |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3459 | */ |
| 3460 | static int fcntlSizeHint(unixFile *pFile, i64 nByte){ |
mistachkin | d589a54 | 2011-08-30 01:23:34 +0000 | [diff] [blame] | 3461 | if( pFile->szChunk>0 ){ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3462 | i64 nSize; /* Required file size */ |
| 3463 | struct stat buf; /* Used to hold return values of fstat() */ |
| 3464 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3465 | if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3466 | |
| 3467 | nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; |
| 3468 | if( nSize>(i64)buf.st_size ){ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3469 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3470 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3471 | /* The code below is handling the return value of osFallocate() |
| 3472 | ** correctly. posix_fallocate() is defined to "returns zero on success, |
| 3473 | ** or an error number on failure". See the manpage for details. */ |
| 3474 | int err; |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3475 | do{ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3476 | err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size); |
| 3477 | }while( err==EINTR ); |
| 3478 | if( err ) return SQLITE_IOERR_WRITE; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3479 | #else |
| 3480 | /* If the OS does not have posix_fallocate(), fake it. First use |
| 3481 | ** ftruncate() to set the file size, then write a single byte to |
| 3482 | ** the last byte in each block within the extended region. This |
| 3483 | ** is the same technique used by glibc to implement posix_fallocate() |
| 3484 | ** on systems that do not have a real fallocate() system call. |
| 3485 | */ |
| 3486 | int nBlk = buf.st_blksize; /* File-system block size */ |
| 3487 | i64 iWrite; /* Next offset to write to */ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3488 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3489 | if( robust_ftruncate(pFile->h, nSize) ){ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3490 | pFile->lastErrno = errno; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3491 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3492 | } |
| 3493 | iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1; |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3494 | while( iWrite<nSize ){ |
| 3495 | int nWrite = seekAndWrite(pFile, iWrite, "", 1); |
| 3496 | if( nWrite!=1 ) return SQLITE_IOERR_WRITE; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3497 | iWrite += nBlk; |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3498 | } |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3499 | #endif |
| 3500 | } |
| 3501 | } |
| 3502 | |
| 3503 | return SQLITE_OK; |
| 3504 | } |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3505 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3506 | /* |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3507 | ** If *pArg is inititially negative then this is a query. Set *pArg to |
| 3508 | ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set. |
| 3509 | ** |
| 3510 | ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags. |
| 3511 | */ |
| 3512 | static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){ |
| 3513 | if( *pArg<0 ){ |
| 3514 | *pArg = (pFile->ctrlFlags & mask)!=0; |
| 3515 | }else if( (*pArg)==0 ){ |
| 3516 | pFile->ctrlFlags &= ~mask; |
| 3517 | }else{ |
| 3518 | pFile->ctrlFlags |= mask; |
| 3519 | } |
| 3520 | } |
| 3521 | |
| 3522 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3523 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3524 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3525 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3526 | unixFile *pFile = (unixFile*)id; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3527 | switch( op ){ |
| 3528 | case SQLITE_FCNTL_LOCKSTATE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3529 | *(int*)pArg = pFile->eFileLock; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3530 | return SQLITE_OK; |
| 3531 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3532 | case SQLITE_LAST_ERRNO: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3533 | *(int*)pArg = pFile->lastErrno; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3534 | return SQLITE_OK; |
| 3535 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3536 | case SQLITE_FCNTL_CHUNK_SIZE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3537 | pFile->szChunk = *(int *)pArg; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3538 | return SQLITE_OK; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3539 | } |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3540 | case SQLITE_FCNTL_SIZE_HINT: { |
dan | da04ea4 | 2011-08-23 05:10:39 +0000 | [diff] [blame] | 3541 | int rc; |
| 3542 | SimulateIOErrorBenign(1); |
| 3543 | rc = fcntlSizeHint(pFile, *(i64 *)pArg); |
| 3544 | SimulateIOErrorBenign(0); |
| 3545 | return rc; |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3546 | } |
| 3547 | case SQLITE_FCNTL_PERSIST_WAL: { |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3548 | unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg); |
| 3549 | return SQLITE_OK; |
| 3550 | } |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3551 | case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { |
| 3552 | unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg); |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3553 | return SQLITE_OK; |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3554 | } |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 3555 | case SQLITE_FCNTL_VFSNAME: { |
| 3556 | *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName); |
| 3557 | return SQLITE_OK; |
| 3558 | } |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3559 | #ifndef NDEBUG |
| 3560 | /* The pager calls this method to signal that it has done |
| 3561 | ** a rollback and that the database is therefore unchanged and |
| 3562 | ** it hence it is OK for the transaction change counter to be |
| 3563 | ** unchanged. |
| 3564 | */ |
| 3565 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 3566 | ((unixFile*)id)->dbUpdate = 0; |
| 3567 | return SQLITE_OK; |
| 3568 | } |
| 3569 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3570 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3571 | case SQLITE_SET_LOCKPROXYFILE: |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3572 | case SQLITE_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3573 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3574 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3575 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3576 | } |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 3577 | return SQLITE_NOTFOUND; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 3578 | } |
| 3579 | |
| 3580 | /* |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3581 | ** Return the sector size in bytes of the underlying block device for |
| 3582 | ** the specified file. This is almost always 512 bytes, but may be |
| 3583 | ** larger for some devices. |
| 3584 | ** |
| 3585 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 3586 | ** if two files are created in the same file-system directory (i.e. |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3587 | ** a database and its journal file) that the sector size will be the |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3588 | ** same for both. |
| 3589 | */ |
drh | 1da88f0 | 2011-12-17 16:09:16 +0000 | [diff] [blame] | 3590 | static int unixSectorSize(sqlite3_file *pFile){ |
drh | 8942d41 | 2012-01-02 18:20:14 +0000 | [diff] [blame] | 3591 | (void)pFile; |
| 3592 | return SQLITE_DEFAULT_SECTOR_SIZE; |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3593 | } |
| 3594 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3595 | /* |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3596 | ** Return the device characteristics for the file. |
| 3597 | ** |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3598 | ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default. |
| 3599 | ** However, that choice is contraversial since technically the underlying |
| 3600 | ** file system does not always provide powersafe overwrites. (In other |
| 3601 | ** words, after a power-loss event, parts of the file that were never |
| 3602 | ** written might end up being altered.) However, non-PSOW behavior is very, |
| 3603 | ** very rare. And asserting PSOW makes a large reduction in the amount |
| 3604 | ** of required I/O for journaling, since a lot of padding is eliminated. |
| 3605 | ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control |
| 3606 | ** available to turn it off and URI query parameter available to turn it off. |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3607 | */ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3608 | static int unixDeviceCharacteristics(sqlite3_file *id){ |
| 3609 | unixFile *p = (unixFile*)id; |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3610 | if( p->ctrlFlags & UNIXFILE_PSOW ){ |
| 3611 | return SQLITE_IOCAP_POWERSAFE_OVERWRITE; |
| 3612 | }else{ |
| 3613 | return 0; |
| 3614 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3615 | } |
| 3616 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3617 | #ifndef SQLITE_OMIT_WAL |
| 3618 | |
| 3619 | |
| 3620 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3621 | ** Object used to represent an shared memory buffer. |
| 3622 | ** |
| 3623 | ** When multiple threads all reference the same wal-index, each thread |
| 3624 | ** has its own unixShm object, but they all point to a single instance |
| 3625 | ** of this unixShmNode object. In other words, each wal-index is opened |
| 3626 | ** only once per process. |
| 3627 | ** |
| 3628 | ** Each unixShmNode object is connected to a single unixInodeInfo object. |
| 3629 | ** We could coalesce this object into unixInodeInfo, but that would mean |
| 3630 | ** every open file that does not use shared memory (in other words, most |
| 3631 | ** open files) would have to carry around this extra information. So |
| 3632 | ** the unixInodeInfo object contains a pointer to this unixShmNode object |
| 3633 | ** and the unixShmNode object is created only when needed. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3634 | ** |
| 3635 | ** unixMutexHeld() must be true when creating or destroying |
| 3636 | ** this object or while reading or writing the following fields: |
| 3637 | ** |
| 3638 | ** nRef |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3639 | ** |
| 3640 | ** The following fields are read-only after the object is created: |
| 3641 | ** |
| 3642 | ** fid |
| 3643 | ** zFilename |
| 3644 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3645 | ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3646 | ** unixMutexHeld() is true when reading or writing any other field |
| 3647 | ** in this structure. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3648 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3649 | struct unixShmNode { |
| 3650 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3651 | sqlite3_mutex *mutex; /* Mutex to access this object */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3652 | char *zFilename; /* Name of the mmapped file */ |
| 3653 | int h; /* Open file descriptor */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3654 | int szRegion; /* Size of shared-memory regions */ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 3655 | u16 nRegion; /* Size of array apRegion */ |
| 3656 | u8 isReadonly; /* True if read-only */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3657 | char **apRegion; /* Array of mapped shared-memory regions */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3658 | int nRef; /* Number of unixShm objects pointing to this */ |
| 3659 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3660 | #ifdef SQLITE_DEBUG |
| 3661 | u8 exclMask; /* Mask of exclusive locks held */ |
| 3662 | u8 sharedMask; /* Mask of shared locks held */ |
| 3663 | u8 nextShmId; /* Next available unixShm.id value */ |
| 3664 | #endif |
| 3665 | }; |
| 3666 | |
| 3667 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3668 | ** Structure used internally by this VFS to record the state of an |
| 3669 | ** open shared memory connection. |
| 3670 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3671 | ** The following fields are initialized when this object is created and |
| 3672 | ** are read-only thereafter: |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3673 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3674 | ** unixShm.pFile |
| 3675 | ** unixShm.id |
| 3676 | ** |
| 3677 | ** All other fields are read/write. The unixShm.pFile->mutex must be held |
| 3678 | ** while accessing any read/write fields. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3679 | */ |
| 3680 | struct unixShm { |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3681 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 3682 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3683 | u8 hasMutex; /* True if holding the unixShmNode mutex */ |
drh | fd53231 | 2011-08-31 18:35:34 +0000 | [diff] [blame] | 3684 | u8 id; /* Id of this connection within its unixShmNode */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3685 | u16 sharedMask; /* Mask of shared locks held */ |
| 3686 | u16 exclMask; /* Mask of exclusive locks held */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3687 | }; |
| 3688 | |
| 3689 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3690 | ** Constants used for locking |
| 3691 | */ |
drh | bd9676c | 2010-06-23 17:58:38 +0000 | [diff] [blame] | 3692 | #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
drh | 4222441 | 2010-05-31 14:28:25 +0000 | [diff] [blame] | 3693 | #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3694 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3695 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3696 | ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3697 | ** |
| 3698 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 3699 | ** otherwise. |
| 3700 | */ |
| 3701 | static int unixShmSystemLock( |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3702 | unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */ |
| 3703 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3704 | int ofst, /* First byte of the locking range */ |
| 3705 | int n /* Number of bytes to lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3706 | ){ |
| 3707 | struct flock f; /* The posix advisory locking structure */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3708 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3709 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3710 | /* Access to the unixShmNode object is serialized by the caller */ |
| 3711 | assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3712 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3713 | /* Shared locks never span more than one byte */ |
| 3714 | assert( n==1 || lockType!=F_RDLCK ); |
| 3715 | |
| 3716 | /* Locks are within range */ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3717 | assert( n>=1 && n<SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3718 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3719 | if( pShmNode->h>=0 ){ |
| 3720 | /* Initialize the locking parameters */ |
| 3721 | memset(&f, 0, sizeof(f)); |
| 3722 | f.l_type = lockType; |
| 3723 | f.l_whence = SEEK_SET; |
| 3724 | f.l_start = ofst; |
| 3725 | f.l_len = n; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3726 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3727 | rc = osFcntl(pShmNode->h, F_SETLK, &f); |
| 3728 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 3729 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3730 | |
| 3731 | /* Update the global lock state and do debug tracing */ |
| 3732 | #ifdef SQLITE_DEBUG |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3733 | { u16 mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3734 | OSTRACE(("SHM-LOCK ")); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3735 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3736 | if( rc==SQLITE_OK ){ |
| 3737 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3738 | OSTRACE(("unlock %d ok", ofst)); |
| 3739 | pShmNode->exclMask &= ~mask; |
| 3740 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3741 | }else if( lockType==F_RDLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3742 | OSTRACE(("read-lock %d ok", ofst)); |
| 3743 | pShmNode->exclMask &= ~mask; |
| 3744 | pShmNode->sharedMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3745 | }else{ |
| 3746 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3747 | OSTRACE(("write-lock %d ok", ofst)); |
| 3748 | pShmNode->exclMask |= mask; |
| 3749 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3750 | } |
| 3751 | }else{ |
| 3752 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3753 | OSTRACE(("unlock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3754 | }else if( lockType==F_RDLCK ){ |
| 3755 | OSTRACE(("read-lock failed")); |
| 3756 | }else{ |
| 3757 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3758 | OSTRACE(("write-lock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3759 | } |
| 3760 | } |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 3761 | OSTRACE((" - afterwards %03x,%03x\n", |
| 3762 | pShmNode->sharedMask, pShmNode->exclMask)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3763 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3764 | #endif |
| 3765 | |
| 3766 | return rc; |
| 3767 | } |
| 3768 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3769 | |
| 3770 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3771 | ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3772 | ** |
| 3773 | ** This is not a VFS shared-memory method; it is a utility function called |
| 3774 | ** by VFS shared-memory methods. |
| 3775 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3776 | static void unixShmPurge(unixFile *pFd){ |
| 3777 | unixShmNode *p = pFd->pInode->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3778 | assert( unixMutexHeld() ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3779 | if( p && p->nRef==0 ){ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3780 | int i; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3781 | assert( p->pInode==pFd->pInode ); |
drh | df3aa16 | 2011-06-24 11:29:51 +0000 | [diff] [blame] | 3782 | sqlite3_mutex_free(p->mutex); |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3783 | for(i=0; i<p->nRegion; i++){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3784 | if( p->h>=0 ){ |
| 3785 | munmap(p->apRegion[i], p->szRegion); |
| 3786 | }else{ |
| 3787 | sqlite3_free(p->apRegion[i]); |
| 3788 | } |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3789 | } |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3790 | sqlite3_free(p->apRegion); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 3791 | if( p->h>=0 ){ |
| 3792 | robust_close(pFd, p->h, __LINE__); |
| 3793 | p->h = -1; |
| 3794 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3795 | p->pInode->pShmNode = 0; |
| 3796 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3797 | } |
| 3798 | } |
| 3799 | |
| 3800 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3801 | ** Open a shared-memory area associated with open database file pDbFd. |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3802 | ** This particular implementation uses mmapped files. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3803 | ** |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3804 | ** The file used to implement shared-memory is in the same directory |
| 3805 | ** as the open database file and has the same name as the open database |
| 3806 | ** file with the "-shm" suffix added. For example, if the database file |
| 3807 | ** is "/home/user1/config.db" then the file that is created and mmapped |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3808 | ** for shared memory will be called "/home/user1/config.db-shm". |
| 3809 | ** |
| 3810 | ** Another approach to is to use files in /dev/shm or /dev/tmp or an |
| 3811 | ** some other tmpfs mount. But if a file in a different directory |
| 3812 | ** from the database file is used, then differing access permissions |
| 3813 | ** or a chroot() might cause two different processes on the same |
| 3814 | ** database to end up using different files for shared memory - |
| 3815 | ** meaning that their memory would not really be shared - resulting |
| 3816 | ** in database corruption. Nevertheless, this tmpfs file usage |
| 3817 | ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" |
| 3818 | ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time |
| 3819 | ** option results in an incompatible build of SQLite; builds of SQLite |
| 3820 | ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the |
| 3821 | ** same database file at the same time, database corruption will likely |
| 3822 | ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered |
| 3823 | ** "unsupported" and may go away in a future SQLite release. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3824 | ** |
| 3825 | ** When opening a new shared-memory file, if no other instances of that |
| 3826 | ** file are currently open, in this process or in other processes, then |
| 3827 | ** the file must be truncated to zero length or have its header cleared. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3828 | ** |
| 3829 | ** If the original database file (pDbFd) is using the "unix-excl" VFS |
| 3830 | ** that means that an exclusive lock is held on the database file and |
| 3831 | ** that no other processes are able to read or write the database. In |
| 3832 | ** that case, we do not really need shared memory. No shared memory |
| 3833 | ** file is created. The shared memory will be simulated with heap memory. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3834 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3835 | static int unixOpenSharedMemory(unixFile *pDbFd){ |
| 3836 | struct unixShm *p = 0; /* The connection to be opened */ |
| 3837 | struct unixShmNode *pShmNode; /* The underlying mmapped file */ |
| 3838 | int rc; /* Result code */ |
| 3839 | unixInodeInfo *pInode; /* The inode of fd */ |
| 3840 | char *zShmFilename; /* Name of the file used for SHM */ |
| 3841 | int nShmFilename; /* Size of the SHM filename in bytes */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3842 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3843 | /* Allocate space for the new unixShm object. */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3844 | p = sqlite3_malloc( sizeof(*p) ); |
| 3845 | if( p==0 ) return SQLITE_NOMEM; |
| 3846 | memset(p, 0, sizeof(*p)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3847 | assert( pDbFd->pShm==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3848 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3849 | /* Check to see if a unixShmNode object already exists. Reuse an existing |
| 3850 | ** one if present. Create a new one if necessary. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3851 | */ |
| 3852 | unixEnterMutex(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 3853 | pInode = pDbFd->pInode; |
| 3854 | pShmNode = pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3855 | if( pShmNode==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 3856 | struct stat sStat; /* fstat() info for database file */ |
| 3857 | |
| 3858 | /* Call fstat() to figure out the permissions on the database file. If |
| 3859 | ** a new *-shm file is created, an attempt will be made to create it |
| 3860 | ** with the same permissions. The actual permissions the file is created |
| 3861 | ** with are subject to the current umask setting. |
| 3862 | */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3863 | if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 3864 | rc = SQLITE_IOERR_FSTAT; |
| 3865 | goto shm_open_err; |
| 3866 | } |
| 3867 | |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3868 | #ifdef SQLITE_SHM_DIRECTORY |
drh | 52bcde0 | 2012-01-03 14:50:45 +0000 | [diff] [blame] | 3869 | nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3870 | #else |
drh | 52bcde0 | 2012-01-03 14:50:45 +0000 | [diff] [blame] | 3871 | nShmFilename = 6 + (int)strlen(pDbFd->zPath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3872 | #endif |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3873 | pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3874 | if( pShmNode==0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3875 | rc = SQLITE_NOMEM; |
| 3876 | goto shm_open_err; |
| 3877 | } |
drh | 9cb5a0d | 2012-01-05 21:19:54 +0000 | [diff] [blame] | 3878 | memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename); |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3879 | zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1]; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3880 | #ifdef SQLITE_SHM_DIRECTORY |
| 3881 | sqlite3_snprintf(nShmFilename, zShmFilename, |
| 3882 | SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", |
| 3883 | (u32)sStat.st_ino, (u32)sStat.st_dev); |
| 3884 | #else |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3885 | sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath); |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 3886 | sqlite3FileSuffix3(pDbFd->zPath, zShmFilename); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3887 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3888 | pShmNode->h = -1; |
| 3889 | pDbFd->pInode->pShmNode = pShmNode; |
| 3890 | pShmNode->pInode = pDbFd->pInode; |
| 3891 | pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 3892 | if( pShmNode->mutex==0 ){ |
| 3893 | rc = SQLITE_NOMEM; |
| 3894 | goto shm_open_err; |
| 3895 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3896 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3897 | if( pInode->bProcessLock==0 ){ |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 3898 | int openFlags = O_RDWR | O_CREAT; |
drh | 9291372 | 2011-12-23 00:07:33 +0000 | [diff] [blame] | 3899 | if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 3900 | openFlags = O_RDONLY; |
| 3901 | pShmNode->isReadonly = 1; |
| 3902 | } |
| 3903 | pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777)); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3904 | if( pShmNode->h<0 ){ |
drh | c96d1e7 | 2012-02-11 18:51:34 +0000 | [diff] [blame] | 3905 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename); |
| 3906 | goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3907 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 3908 | |
| 3909 | /* If this process is running as root, make sure that the SHM file |
| 3910 | ** is owned by the same user that owns the original database. Otherwise, |
| 3911 | ** the original owner will not be able to connect. If this process is |
drh | 3ee3484 | 2012-02-11 21:21:17 +0000 | [diff] [blame] | 3912 | ** not root, the following fchown() will fail, but we don't care. The |
| 3913 | ** if(){..} and the UNIXFILE_CHOWN flag are purely to silence compiler |
| 3914 | ** warnings. |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 3915 | */ |
drh | 3ee3484 | 2012-02-11 21:21:17 +0000 | [diff] [blame] | 3916 | if( fchown(pShmNode->h, sStat.st_uid, sStat.st_gid)==0 ){ |
| 3917 | pDbFd->ctrlFlags |= UNIXFILE_CHOWN; |
| 3918 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3919 | |
| 3920 | /* Check to see if another process is holding the dead-man switch. |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 3921 | ** If not, truncate the file to zero length. |
| 3922 | */ |
| 3923 | rc = SQLITE_OK; |
| 3924 | if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ |
| 3925 | if( robust_ftruncate(pShmNode->h, 0) ){ |
| 3926 | rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 3927 | } |
| 3928 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 3929 | if( rc==SQLITE_OK ){ |
| 3930 | rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1); |
| 3931 | } |
| 3932 | if( rc ) goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3933 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3936 | /* Make the new connection a child of the unixShmNode */ |
| 3937 | p->pShmNode = pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3938 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3939 | p->id = pShmNode->nextShmId++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3940 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3941 | pShmNode->nRef++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3942 | pDbFd->pShm = p; |
| 3943 | unixLeaveMutex(); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 3944 | |
| 3945 | /* The reference count on pShmNode has already been incremented under |
| 3946 | ** the cover of the unixEnterMutex() mutex and the pointer from the |
| 3947 | ** new (struct unixShm) object to the pShmNode has been set. All that is |
| 3948 | ** left to do is to link the new object into the linked list starting |
| 3949 | ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex |
| 3950 | ** mutex. |
| 3951 | */ |
| 3952 | sqlite3_mutex_enter(pShmNode->mutex); |
| 3953 | p->pNext = pShmNode->pFirst; |
| 3954 | pShmNode->pFirst = p; |
| 3955 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3956 | return SQLITE_OK; |
| 3957 | |
| 3958 | /* Jump here on any error */ |
| 3959 | shm_open_err: |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3960 | unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3961 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3962 | unixLeaveMutex(); |
| 3963 | return rc; |
| 3964 | } |
| 3965 | |
| 3966 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3967 | ** This function is called to obtain a pointer to region iRegion of the |
| 3968 | ** shared-memory associated with the database file fd. Shared-memory regions |
| 3969 | ** are numbered starting from zero. Each shared-memory region is szRegion |
| 3970 | ** bytes in size. |
| 3971 | ** |
| 3972 | ** If an error occurs, an error code is returned and *pp is set to NULL. |
| 3973 | ** |
| 3974 | ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory |
| 3975 | ** region has not been allocated (by any client, including one running in a |
| 3976 | ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 3977 | ** bExtend is non-zero and the requested shared-memory region has not yet |
| 3978 | ** been allocated, it is allocated by this function. |
| 3979 | ** |
| 3980 | ** If the shared-memory region has already been allocated or is allocated by |
| 3981 | ** this call as described above, then it is mapped into this processes |
| 3982 | ** address space (if it is not already), *pp is set to point to the mapped |
| 3983 | ** memory and SQLITE_OK returned. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3984 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3985 | static int unixShmMap( |
| 3986 | sqlite3_file *fd, /* Handle open on database file */ |
| 3987 | int iRegion, /* Region to retrieve */ |
| 3988 | int szRegion, /* Size of regions */ |
| 3989 | int bExtend, /* True to extend file if necessary */ |
| 3990 | void volatile **pp /* OUT: Mapped memory */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3991 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3992 | unixFile *pDbFd = (unixFile*)fd; |
| 3993 | unixShm *p; |
| 3994 | unixShmNode *pShmNode; |
| 3995 | int rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3996 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3997 | /* If the shared-memory file has not yet been opened, open it now. */ |
| 3998 | if( pDbFd->pShm==0 ){ |
| 3999 | rc = unixOpenSharedMemory(pDbFd); |
| 4000 | if( rc!=SQLITE_OK ) return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4001 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4002 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4003 | p = pDbFd->pShm; |
| 4004 | pShmNode = p->pShmNode; |
| 4005 | sqlite3_mutex_enter(pShmNode->mutex); |
| 4006 | assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4007 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4008 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4009 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4010 | |
| 4011 | if( pShmNode->nRegion<=iRegion ){ |
| 4012 | char **apNew; /* New apRegion[] array */ |
| 4013 | int nByte = (iRegion+1)*szRegion; /* Minimum required file size */ |
| 4014 | struct stat sStat; /* Used by fstat() */ |
| 4015 | |
| 4016 | pShmNode->szRegion = szRegion; |
| 4017 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4018 | if( pShmNode->h>=0 ){ |
| 4019 | /* The requested region is not mapped into this processes address space. |
| 4020 | ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 4021 | ** large enough to contain the requested region). |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4022 | */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4023 | if( osFstat(pShmNode->h, &sStat) ){ |
| 4024 | rc = SQLITE_IOERR_SHMSIZE; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4025 | goto shmpage_out; |
| 4026 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4027 | |
| 4028 | if( sStat.st_size<nByte ){ |
| 4029 | /* The requested memory region does not exist. If bExtend is set to |
| 4030 | ** false, exit early. *pp will be set to NULL and SQLITE_OK returned. |
| 4031 | ** |
| 4032 | ** Alternatively, if bExtend is true, use ftruncate() to allocate |
| 4033 | ** the requested memory region. |
| 4034 | */ |
| 4035 | if( !bExtend ) goto shmpage_out; |
| 4036 | if( robust_ftruncate(pShmNode->h, nByte) ){ |
| 4037 | rc = unixLogError(SQLITE_IOERR_SHMSIZE, "ftruncate", |
| 4038 | pShmNode->zFilename); |
| 4039 | goto shmpage_out; |
| 4040 | } |
| 4041 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4042 | } |
| 4043 | |
| 4044 | /* Map the requested memory region into this processes address space. */ |
| 4045 | apNew = (char **)sqlite3_realloc( |
| 4046 | pShmNode->apRegion, (iRegion+1)*sizeof(char *) |
| 4047 | ); |
| 4048 | if( !apNew ){ |
| 4049 | rc = SQLITE_IOERR_NOMEM; |
| 4050 | goto shmpage_out; |
| 4051 | } |
| 4052 | pShmNode->apRegion = apNew; |
| 4053 | while(pShmNode->nRegion<=iRegion){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4054 | void *pMem; |
| 4055 | if( pShmNode->h>=0 ){ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4056 | pMem = mmap(0, szRegion, |
| 4057 | pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4058 | MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion |
| 4059 | ); |
| 4060 | if( pMem==MAP_FAILED ){ |
drh | 50990db | 2011-04-13 20:26:13 +0000 | [diff] [blame] | 4061 | rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4062 | goto shmpage_out; |
| 4063 | } |
| 4064 | }else{ |
| 4065 | pMem = sqlite3_malloc(szRegion); |
| 4066 | if( pMem==0 ){ |
| 4067 | rc = SQLITE_NOMEM; |
| 4068 | goto shmpage_out; |
| 4069 | } |
| 4070 | memset(pMem, 0, szRegion); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4071 | } |
| 4072 | pShmNode->apRegion[pShmNode->nRegion] = pMem; |
| 4073 | pShmNode->nRegion++; |
| 4074 | } |
| 4075 | } |
| 4076 | |
| 4077 | shmpage_out: |
| 4078 | if( pShmNode->nRegion>iRegion ){ |
| 4079 | *pp = pShmNode->apRegion[iRegion]; |
| 4080 | }else{ |
| 4081 | *pp = 0; |
| 4082 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4083 | if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4084 | sqlite3_mutex_leave(pShmNode->mutex); |
| 4085 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4086 | } |
| 4087 | |
| 4088 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4089 | ** Change the lock state for a shared-memory segment. |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4090 | ** |
| 4091 | ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little |
| 4092 | ** different here than in posix. In xShmLock(), one can go from unlocked |
| 4093 | ** to shared and back or from unlocked to exclusive and back. But one may |
| 4094 | ** not go from shared to exclusive or from exclusive to shared. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4095 | */ |
| 4096 | static int unixShmLock( |
| 4097 | sqlite3_file *fd, /* Database file holding the shared memory */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4098 | int ofst, /* First lock to acquire or release */ |
| 4099 | int n, /* Number of locks to acquire or release */ |
| 4100 | int flags /* What to do with the lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4101 | ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4102 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 4103 | unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 4104 | unixShm *pX; /* For looping over all siblings */ |
| 4105 | unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 4106 | int rc = SQLITE_OK; /* Result code */ |
| 4107 | u16 mask; /* Mask of locks to take or release */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4108 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4109 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4110 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4111 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4112 | assert( n>=1 ); |
| 4113 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 4114 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 4115 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 4116 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 4117 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4118 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4119 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4120 | |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4121 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4122 | assert( n>1 || mask==(1<<ofst) ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4123 | sqlite3_mutex_enter(pShmNode->mutex); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4124 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 4125 | u16 allMask = 0; /* Mask of locks held by siblings */ |
| 4126 | |
| 4127 | /* See if any siblings hold this same lock */ |
| 4128 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 4129 | if( pX==p ) continue; |
| 4130 | assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
| 4131 | allMask |= pX->sharedMask; |
| 4132 | } |
| 4133 | |
| 4134 | /* Unlock the system-level locks */ |
| 4135 | if( (mask & allMask)==0 ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4136 | rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4137 | }else{ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4138 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4139 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4140 | |
| 4141 | /* Undo the local locks */ |
| 4142 | if( rc==SQLITE_OK ){ |
| 4143 | p->exclMask &= ~mask; |
| 4144 | p->sharedMask &= ~mask; |
| 4145 | } |
| 4146 | }else if( flags & SQLITE_SHM_SHARED ){ |
| 4147 | u16 allShared = 0; /* Union of locks held by connections other than "p" */ |
| 4148 | |
| 4149 | /* Find out which shared locks are already held by sibling connections. |
| 4150 | ** If any sibling already holds an exclusive lock, go ahead and return |
| 4151 | ** SQLITE_BUSY. |
| 4152 | */ |
| 4153 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4154 | if( (pX->exclMask & mask)!=0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4155 | rc = SQLITE_BUSY; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4156 | break; |
| 4157 | } |
| 4158 | allShared |= pX->sharedMask; |
| 4159 | } |
| 4160 | |
| 4161 | /* Get shared locks at the system level, if necessary */ |
| 4162 | if( rc==SQLITE_OK ){ |
| 4163 | if( (allShared & mask)==0 ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4164 | rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4165 | }else{ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4166 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4167 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4168 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4169 | |
| 4170 | /* Get the local shared locks */ |
| 4171 | if( rc==SQLITE_OK ){ |
| 4172 | p->sharedMask |= mask; |
| 4173 | } |
| 4174 | }else{ |
| 4175 | /* Make sure no sibling connections hold locks that will block this |
| 4176 | ** lock. If any do, return SQLITE_BUSY right away. |
| 4177 | */ |
| 4178 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4179 | if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ |
| 4180 | rc = SQLITE_BUSY; |
| 4181 | break; |
| 4182 | } |
| 4183 | } |
| 4184 | |
| 4185 | /* Get the exclusive locks at the system level. Then if successful |
| 4186 | ** also mark the local connection as being locked. |
| 4187 | */ |
| 4188 | if( rc==SQLITE_OK ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4189 | rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4190 | if( rc==SQLITE_OK ){ |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4191 | assert( (p->sharedMask & mask)==0 ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4192 | p->exclMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4193 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4194 | } |
| 4195 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4196 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 4197 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
| 4198 | p->id, getpid(), p->sharedMask, p->exclMask)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4199 | return rc; |
| 4200 | } |
| 4201 | |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4202 | /* |
| 4203 | ** Implement a memory barrier or memory fence on shared memory. |
| 4204 | ** |
| 4205 | ** All loads and stores begun before the barrier must complete before |
| 4206 | ** any load or store begun after the barrier. |
| 4207 | */ |
| 4208 | static void unixShmBarrier( |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4209 | sqlite3_file *fd /* Database file holding the shared memory */ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4210 | ){ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 4211 | UNUSED_PARAMETER(fd); |
drh | b29ad85 | 2010-06-01 00:03:57 +0000 | [diff] [blame] | 4212 | unixEnterMutex(); |
| 4213 | unixLeaveMutex(); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4216 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4217 | ** Close a connection to shared-memory. Delete the underlying |
| 4218 | ** storage if deleteFlag is true. |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 4219 | ** |
| 4220 | ** If there is no shared memory associated with the connection then this |
| 4221 | ** routine is a harmless no-op. |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4222 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4223 | static int unixShmUnmap( |
| 4224 | sqlite3_file *fd, /* The underlying database file */ |
| 4225 | int deleteFlag /* Delete shared-memory if true */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4226 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4227 | unixShm *p; /* The connection to be closed */ |
| 4228 | unixShmNode *pShmNode; /* The underlying shared-memory file */ |
| 4229 | unixShm **pp; /* For looping over sibling connections */ |
| 4230 | unixFile *pDbFd; /* The underlying database file */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4231 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4232 | pDbFd = (unixFile*)fd; |
| 4233 | p = pDbFd->pShm; |
| 4234 | if( p==0 ) return SQLITE_OK; |
| 4235 | pShmNode = p->pShmNode; |
| 4236 | |
| 4237 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4238 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4239 | |
| 4240 | /* Remove connection p from the set of connections associated |
| 4241 | ** with pShmNode */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4242 | sqlite3_mutex_enter(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4243 | for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 4244 | *pp = p->pNext; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4245 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4246 | /* Free the connection p */ |
| 4247 | sqlite3_free(p); |
| 4248 | pDbFd->pShm = 0; |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4249 | sqlite3_mutex_leave(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4250 | |
| 4251 | /* If pShmNode->nRef has reached 0, then close the underlying |
| 4252 | ** shared-memory file, too */ |
| 4253 | unixEnterMutex(); |
| 4254 | assert( pShmNode->nRef>0 ); |
| 4255 | pShmNode->nRef--; |
| 4256 | if( pShmNode->nRef==0 ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 4257 | if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4258 | unixShmPurge(pDbFd); |
| 4259 | } |
| 4260 | unixLeaveMutex(); |
| 4261 | |
| 4262 | return SQLITE_OK; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4263 | } |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4264 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4265 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4266 | #else |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 4267 | # define unixShmMap 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4268 | # define unixShmLock 0 |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4269 | # define unixShmBarrier 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4270 | # define unixShmUnmap 0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4271 | #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 4272 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4273 | /* |
| 4274 | ** Here ends the implementation of all sqlite3_file methods. |
| 4275 | ** |
| 4276 | ********************** End sqlite3_file Methods ******************************* |
| 4277 | ******************************************************************************/ |
| 4278 | |
| 4279 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4280 | ** This division contains definitions of sqlite3_io_methods objects that |
| 4281 | ** implement various file locking strategies. It also contains definitions |
| 4282 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 4283 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 4284 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 4285 | ** the correct finder-function for that VFS. |
| 4286 | ** |
| 4287 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 4288 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 4289 | ** looks at the filesystem type and tries to guess the best locking |
| 4290 | ** strategy from that. |
| 4291 | ** |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4292 | ** For finder-funtion F, two objects are created: |
| 4293 | ** |
| 4294 | ** (1) The real finder-function named "FImpt()". |
| 4295 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4296 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4297 | ** |
| 4298 | ** |
| 4299 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 4300 | ** objects. We have to do this instead of letting pAppData point |
| 4301 | ** directly at the finder-function since C90 rules prevent a void* |
| 4302 | ** from be cast into a function pointer. |
| 4303 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4304 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4305 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4306 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4307 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 4308 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 4309 | ** |
| 4310 | ** * An I/O method finder function called FINDER that returns a pointer |
| 4311 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4312 | */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4313 | #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4314 | static const sqlite3_io_methods METHOD = { \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4315 | VERSION, /* iVersion */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4316 | CLOSE, /* xClose */ \ |
| 4317 | unixRead, /* xRead */ \ |
| 4318 | unixWrite, /* xWrite */ \ |
| 4319 | unixTruncate, /* xTruncate */ \ |
| 4320 | unixSync, /* xSync */ \ |
| 4321 | unixFileSize, /* xFileSize */ \ |
| 4322 | LOCK, /* xLock */ \ |
| 4323 | UNLOCK, /* xUnlock */ \ |
| 4324 | CKLOCK, /* xCheckReservedLock */ \ |
| 4325 | unixFileControl, /* xFileControl */ \ |
| 4326 | unixSectorSize, /* xSectorSize */ \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4327 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 4328 | unixShmMap, /* xShmMap */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4329 | unixShmLock, /* xShmLock */ \ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4330 | unixShmBarrier, /* xShmBarrier */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4331 | unixShmUnmap /* xShmUnmap */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4332 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4333 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 4334 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4335 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4336 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4337 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4338 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4339 | |
| 4340 | /* |
| 4341 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 4342 | ** locking strategies. Functions that return pointers to these methods |
| 4343 | ** are also created. |
| 4344 | */ |
| 4345 | IOMETHODS( |
| 4346 | posixIoFinder, /* Finder function name */ |
| 4347 | posixIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4348 | 2, /* shared memory is enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4349 | unixClose, /* xClose method */ |
| 4350 | unixLock, /* xLock method */ |
| 4351 | unixUnlock, /* xUnlock method */ |
| 4352 | unixCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4353 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4354 | IOMETHODS( |
| 4355 | nolockIoFinder, /* Finder function name */ |
| 4356 | nolockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4357 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4358 | nolockClose, /* xClose method */ |
| 4359 | nolockLock, /* xLock method */ |
| 4360 | nolockUnlock, /* xUnlock method */ |
| 4361 | nolockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4362 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4363 | IOMETHODS( |
| 4364 | dotlockIoFinder, /* Finder function name */ |
| 4365 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4366 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4367 | dotlockClose, /* xClose method */ |
| 4368 | dotlockLock, /* xLock method */ |
| 4369 | dotlockUnlock, /* xUnlock method */ |
| 4370 | dotlockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4371 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4372 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4373 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4374 | IOMETHODS( |
| 4375 | flockIoFinder, /* Finder function name */ |
| 4376 | flockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4377 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4378 | flockClose, /* xClose method */ |
| 4379 | flockLock, /* xLock method */ |
| 4380 | flockUnlock, /* xUnlock method */ |
| 4381 | flockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4382 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4383 | #endif |
| 4384 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4385 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4386 | IOMETHODS( |
| 4387 | semIoFinder, /* Finder function name */ |
| 4388 | semIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4389 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4390 | semClose, /* xClose method */ |
| 4391 | semLock, /* xLock method */ |
| 4392 | semUnlock, /* xUnlock method */ |
| 4393 | semCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4394 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4395 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4396 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4397 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4398 | IOMETHODS( |
| 4399 | afpIoFinder, /* Finder function name */ |
| 4400 | afpIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4401 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4402 | afpClose, /* xClose method */ |
| 4403 | afpLock, /* xLock method */ |
| 4404 | afpUnlock, /* xUnlock method */ |
| 4405 | afpCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4406 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4407 | #endif |
| 4408 | |
| 4409 | /* |
| 4410 | ** The proxy locking method is a "super-method" in the sense that it |
| 4411 | ** opens secondary file descriptors for the conch and lock files and |
| 4412 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 4413 | ** secondary files. For this reason, the division that implements |
| 4414 | ** proxy locking is located much further down in the file. But we need |
| 4415 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 4416 | ** for proxy locking here. So we forward declare the I/O methods. |
| 4417 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4418 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4419 | static int proxyClose(sqlite3_file*); |
| 4420 | static int proxyLock(sqlite3_file*, int); |
| 4421 | static int proxyUnlock(sqlite3_file*, int); |
| 4422 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4423 | IOMETHODS( |
| 4424 | proxyIoFinder, /* Finder function name */ |
| 4425 | proxyIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4426 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4427 | proxyClose, /* xClose method */ |
| 4428 | proxyLock, /* xLock method */ |
| 4429 | proxyUnlock, /* xUnlock method */ |
| 4430 | proxyCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4431 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4432 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4433 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4434 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 4435 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 4436 | IOMETHODS( |
| 4437 | nfsIoFinder, /* Finder function name */ |
| 4438 | nfsIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4439 | 1, /* shared memory is disabled */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4440 | unixClose, /* xClose method */ |
| 4441 | unixLock, /* xLock method */ |
| 4442 | nfsUnlock, /* xUnlock method */ |
| 4443 | unixCheckReservedLock /* xCheckReservedLock method */ |
| 4444 | ) |
| 4445 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4446 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4447 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4448 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4449 | ** This "finder" function attempts to determine the best locking strategy |
| 4450 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4451 | ** object that implements that strategy. |
| 4452 | ** |
| 4453 | ** This is for MacOSX only. |
| 4454 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4455 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4456 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4457 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4458 | ){ |
| 4459 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4460 | const char *zFilesystem; /* Filesystem type name */ |
| 4461 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4462 | } aMap[] = { |
| 4463 | { "hfs", &posixIoMethods }, |
| 4464 | { "ufs", &posixIoMethods }, |
| 4465 | { "afpfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4466 | { "smbfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4467 | { "webdav", &nolockIoMethods }, |
| 4468 | { 0, 0 } |
| 4469 | }; |
| 4470 | int i; |
| 4471 | struct statfs fsInfo; |
| 4472 | struct flock lockInfo; |
| 4473 | |
| 4474 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4475 | /* If filePath==NULL that means we are dealing with a transient file |
| 4476 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4477 | return &nolockIoMethods; |
| 4478 | } |
| 4479 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 4480 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 4481 | return &nolockIoMethods; |
| 4482 | } |
| 4483 | for(i=0; aMap[i].zFilesystem; i++){ |
| 4484 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 4485 | return aMap[i].pMethods; |
| 4486 | } |
| 4487 | } |
| 4488 | } |
| 4489 | |
| 4490 | /* Default case. Handles, amongst others, "nfs". |
| 4491 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 4492 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4493 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4494 | lockInfo.l_len = 1; |
| 4495 | lockInfo.l_start = 0; |
| 4496 | lockInfo.l_whence = SEEK_SET; |
| 4497 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4498 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4499 | if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 4500 | return &nfsIoMethods; |
| 4501 | } else { |
| 4502 | return &posixIoMethods; |
| 4503 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4504 | }else{ |
| 4505 | return &dotlockIoMethods; |
| 4506 | } |
| 4507 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4508 | static const sqlite3_io_methods |
| 4509 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4510 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4511 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4512 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4513 | #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE |
| 4514 | /* |
| 4515 | ** This "finder" function attempts to determine the best locking strategy |
| 4516 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
| 4517 | ** object that implements that strategy. |
| 4518 | ** |
| 4519 | ** This is for VXWorks only. |
| 4520 | */ |
| 4521 | static const sqlite3_io_methods *autolockIoFinderImpl( |
| 4522 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4523 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4524 | ){ |
| 4525 | struct flock lockInfo; |
| 4526 | |
| 4527 | if( !filePath ){ |
| 4528 | /* If filePath==NULL that means we are dealing with a transient file |
| 4529 | ** that does not need to be locked. */ |
| 4530 | return &nolockIoMethods; |
| 4531 | } |
| 4532 | |
| 4533 | /* Test if fcntl() is supported and use POSIX style locks. |
| 4534 | ** Otherwise fall back to the named semaphore method. |
| 4535 | */ |
| 4536 | lockInfo.l_len = 1; |
| 4537 | lockInfo.l_start = 0; |
| 4538 | lockInfo.l_whence = SEEK_SET; |
| 4539 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4540 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4541 | return &posixIoMethods; |
| 4542 | }else{ |
| 4543 | return &semIoMethods; |
| 4544 | } |
| 4545 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4546 | static const sqlite3_io_methods |
| 4547 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4548 | |
| 4549 | #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */ |
| 4550 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4551 | /* |
| 4552 | ** An abstract type for a pointer to a IO method finder function: |
| 4553 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4554 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4555 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4556 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4557 | /**************************************************************************** |
| 4558 | **************************** sqlite3_vfs methods **************************** |
| 4559 | ** |
| 4560 | ** This division contains the implementation of methods on the |
| 4561 | ** sqlite3_vfs object. |
| 4562 | */ |
| 4563 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4564 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4565 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4566 | */ |
| 4567 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4568 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4569 | int h, /* Open file descriptor of file being opened */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4570 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4571 | const char *zFilename, /* Name of the file being opened */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 4572 | int ctrlFlags /* Zero or more UNIXFILE_* values */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4573 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4574 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4575 | unixFile *pNew = (unixFile *)pId; |
| 4576 | int rc = SQLITE_OK; |
| 4577 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4578 | assert( pNew->pInode==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4579 | |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4580 | /* Usually the path zFilename should not be a relative pathname. The |
| 4581 | ** exception is when opening the proxy "conch" file in builds that |
| 4582 | ** include the special Apple locking styles. |
| 4583 | */ |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4584 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | f7f55ed | 2010-10-05 18:22:47 +0000 | [diff] [blame] | 4585 | assert( zFilename==0 || zFilename[0]=='/' |
| 4586 | || pVfs->pAppData==(void*)&autolockIoFinder ); |
| 4587 | #else |
| 4588 | assert( zFilename==0 || zFilename[0]=='/' ); |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4589 | #endif |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4590 | |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 4591 | /* No locking occurs in temporary files */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 4592 | assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 4593 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4594 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4595 | pNew->h = h; |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 4596 | pNew->pVfs = pVfs; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4597 | pNew->zPath = zFilename; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 4598 | pNew->ctrlFlags = (u8)ctrlFlags; |
| 4599 | if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0), |
| 4600 | "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 4601 | pNew->ctrlFlags |= UNIXFILE_PSOW; |
drh | bec7c97 | 2011-12-23 00:25:02 +0000 | [diff] [blame] | 4602 | } |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 4603 | if( memcmp(pVfs->zName,"unix-excl",10)==0 ){ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 4604 | pNew->ctrlFlags |= UNIXFILE_EXCL; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 4605 | } |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 4606 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4607 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 4608 | pNew->pId = vxworksFindFileId(zFilename); |
| 4609 | if( pNew->pId==0 ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 4610 | ctrlFlags |= UNIXFILE_NOLOCK; |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 4611 | rc = SQLITE_NOMEM; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4612 | } |
| 4613 | #endif |
| 4614 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 4615 | if( ctrlFlags & UNIXFILE_NOLOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4616 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4617 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4618 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4619 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4620 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 4621 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 4622 | ** zFilename remains valid until file is closed, to support */ |
| 4623 | pNew->lockingContext = (void*)zFilename; |
| 4624 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4625 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4626 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4627 | if( pLockingStyle == &posixIoMethods |
| 4628 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 4629 | || pLockingStyle == &nfsIoMethods |
| 4630 | #endif |
| 4631 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4632 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4633 | rc = findInodeInfo(pNew, &pNew->pInode); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4634 | if( rc!=SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4635 | /* If an error occured in findInodeInfo(), close the file descriptor |
| 4636 | ** immediately, before releasing the mutex. findInodeInfo() may fail |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4637 | ** in two scenarios: |
| 4638 | ** |
| 4639 | ** (a) A call to fstat() failed. |
| 4640 | ** (b) A malloc failed. |
| 4641 | ** |
| 4642 | ** Scenario (b) may only occur if the process is holding no other |
| 4643 | ** file descriptors open on the same file. If there were other file |
| 4644 | ** descriptors on this file, then no malloc would be required by |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4645 | ** findInodeInfo(). If this is the case, it is quite safe to close |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4646 | ** handle h - as it is guaranteed that no posix locks will be released |
| 4647 | ** by doing so. |
| 4648 | ** |
| 4649 | ** If scenario (a) caused the error then things are not so safe. The |
| 4650 | ** implicit assumption here is that if fstat() fails, things are in |
| 4651 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 4652 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4653 | robust_close(pNew, h, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4654 | h = -1; |
| 4655 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4656 | unixLeaveMutex(); |
| 4657 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4658 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4659 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 4660 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4661 | /* AFP locking uses the file path so it needs to be included in |
| 4662 | ** the afpLockingContext. |
| 4663 | */ |
| 4664 | afpLockingContext *pCtx; |
| 4665 | pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 4666 | if( pCtx==0 ){ |
| 4667 | rc = SQLITE_NOMEM; |
| 4668 | }else{ |
| 4669 | /* NB: zFilename exists and remains valid until the file is closed |
| 4670 | ** according to requirement F11141. So we do not need to make a |
| 4671 | ** copy of the filename. */ |
| 4672 | pCtx->dbPath = zFilename; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4673 | pCtx->reserved = 0; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4674 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4675 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4676 | rc = findInodeInfo(pNew, &pNew->pInode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4677 | if( rc!=SQLITE_OK ){ |
| 4678 | sqlite3_free(pNew->lockingContext); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4679 | robust_close(pNew, h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4680 | h = -1; |
| 4681 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4682 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4683 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4684 | } |
| 4685 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4686 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4687 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 4688 | /* Dotfile locking uses the file path so it needs to be included in |
| 4689 | ** the dotlockLockingContext |
| 4690 | */ |
| 4691 | char *zLockFile; |
| 4692 | int nFilename; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 4693 | assert( zFilename!=0 ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4694 | nFilename = (int)strlen(zFilename) + 6; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4695 | zLockFile = (char *)sqlite3_malloc(nFilename); |
| 4696 | if( zLockFile==0 ){ |
| 4697 | rc = SQLITE_NOMEM; |
| 4698 | }else{ |
| 4699 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4700 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4701 | pNew->lockingContext = zLockFile; |
| 4702 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4703 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4704 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4705 | else if( pLockingStyle == &semIoMethods ){ |
| 4706 | /* Named semaphore locking uses the file path so it needs to be |
| 4707 | ** included in the semLockingContext |
| 4708 | */ |
| 4709 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4710 | rc = findInodeInfo(pNew, &pNew->pInode); |
| 4711 | if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 4712 | char *zSemName = pNew->pInode->aSemName; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4713 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 4714 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4715 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 4716 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4717 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4718 | pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 4719 | if( pNew->pInode->pSem == SEM_FAILED ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4720 | rc = SQLITE_NOMEM; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4721 | pNew->pInode->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4722 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4723 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4724 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4725 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4726 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 4727 | |
| 4728 | pNew->lastErrno = 0; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4729 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4730 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4731 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
drh | 309e655 | 2010-02-05 18:00:26 +0000 | [diff] [blame] | 4732 | h = -1; |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 4733 | osUnlink(zFilename); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4734 | isDelete = 0; |
| 4735 | } |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 4736 | if( isDelete ) pNew->ctrlFlags |= UNIXFILE_DELETE; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4737 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4738 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4739 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4740 | }else{ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4741 | pNew->pMethod = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4742 | OpenCounter(+1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4743 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4744 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 4745 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 4746 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4747 | /* |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4748 | ** Return the name of a directory in which to put temporary files. |
| 4749 | ** If no suitable temporary file directory can be found, return NULL. |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4750 | */ |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4751 | static const char *unixTempFileDir(void){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4752 | static const char *azDirs[] = { |
| 4753 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4754 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4755 | "/var/tmp", |
| 4756 | "/usr/tmp", |
| 4757 | "/tmp", |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4758 | 0 /* List terminator */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4759 | }; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4760 | unsigned int i; |
| 4761 | struct stat buf; |
| 4762 | const char *zDir = 0; |
| 4763 | |
| 4764 | azDirs[0] = sqlite3_temp_directory; |
| 4765 | if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); |
drh | 19515c8 | 2010-06-19 23:53:11 +0000 | [diff] [blame] | 4766 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){ |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4767 | if( zDir==0 ) continue; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4768 | if( osStat(zDir, &buf) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4769 | if( !S_ISDIR(buf.st_mode) ) continue; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4770 | if( osAccess(zDir, 07) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4771 | break; |
| 4772 | } |
| 4773 | return zDir; |
| 4774 | } |
| 4775 | |
| 4776 | /* |
| 4777 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 4778 | ** by the calling process and must be big enough to hold at least |
| 4779 | ** pVfs->mxPathname bytes. |
| 4780 | */ |
| 4781 | static int unixGetTempname(int nBuf, char *zBuf){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4782 | static const unsigned char zChars[] = |
| 4783 | "abcdefghijklmnopqrstuvwxyz" |
| 4784 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 4785 | "0123456789"; |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 4786 | unsigned int i, j; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4787 | const char *zDir; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4788 | |
| 4789 | /* It's odd to simulate an io-error here, but really this is just |
| 4790 | ** using the io-error infrastructure to test that SQLite handles this |
| 4791 | ** function failing. |
| 4792 | */ |
| 4793 | SimulateIOError( return SQLITE_IOERR ); |
| 4794 | |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4795 | zDir = unixTempFileDir(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4796 | if( zDir==0 ) zDir = "."; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4797 | |
| 4798 | /* Check that the output buffer is large enough for the temporary file |
| 4799 | ** name. If it is not, return SQLITE_ERROR. |
| 4800 | */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 4801 | if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4802 | return SQLITE_ERROR; |
| 4803 | } |
| 4804 | |
| 4805 | do{ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 4806 | sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4807 | j = (int)strlen(zBuf); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4808 | sqlite3_randomness(15, &zBuf[j]); |
| 4809 | for(i=0; i<15; i++, j++){ |
| 4810 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 4811 | } |
| 4812 | zBuf[j] = 0; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 4813 | zBuf[j+1] = 0; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 4814 | }while( osAccess(zBuf,0)==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4815 | return SQLITE_OK; |
| 4816 | } |
| 4817 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4818 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 4819 | /* |
| 4820 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 4821 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 4822 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 4823 | */ |
| 4824 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 4825 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 4826 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4827 | /* |
| 4828 | ** Search for an unused file descriptor that was opened on the database |
| 4829 | ** file (not a journal or master-journal file) identified by pathname |
| 4830 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 4831 | ** argument to this function. |
| 4832 | ** |
| 4833 | ** Such a file descriptor may exist if a database connection was closed |
| 4834 | ** but the associated file descriptor could not be closed because some |
| 4835 | ** other file descriptor open on the same file is holding a file-lock. |
| 4836 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 4837 | ** describing "Posix Advisory Locking" at the start of this file for |
| 4838 | ** further details. Also, ticket #4018. |
| 4839 | ** |
| 4840 | ** If a suitable file descriptor is found, then it is returned. If no |
| 4841 | ** such file descriptor is located, -1 is returned. |
| 4842 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4843 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 4844 | UnixUnusedFd *pUnused = 0; |
| 4845 | |
| 4846 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 4847 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 4848 | ** but because no way to test it is currently available. It is better |
| 4849 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 4850 | ** feature. */ |
| 4851 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4852 | struct stat sStat; /* Results of stat() call */ |
| 4853 | |
| 4854 | /* A stat() call may fail for various reasons. If this happens, it is |
| 4855 | ** almost certain that an open() call on the same path will also fail. |
| 4856 | ** For this reason, if an error occurs in the stat() call here, it is |
| 4857 | ** ignored and -1 is returned. The caller will try to open a new file |
| 4858 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 4859 | ** |
| 4860 | ** Even if a subsequent open() call does succeed, the consequences of |
| 4861 | ** not searching for a resusable file descriptor are not dire. */ |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 4862 | if( 0==osStat(zPath, &sStat) ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4863 | unixInodeInfo *pInode; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4864 | |
| 4865 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4866 | pInode = inodeList; |
| 4867 | while( pInode && (pInode->fileId.dev!=sStat.st_dev |
| 4868 | || pInode->fileId.ino!=sStat.st_ino) ){ |
| 4869 | pInode = pInode->pNext; |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 4870 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4871 | if( pInode ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4872 | UnixUnusedFd **pp; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4873 | for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4874 | pUnused = *pp; |
| 4875 | if( pUnused ){ |
| 4876 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4877 | } |
| 4878 | } |
| 4879 | unixLeaveMutex(); |
| 4880 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4881 | #endif /* if !OS_VXWORKS */ |
| 4882 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4883 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4884 | |
| 4885 | /* |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4886 | ** This function is called by unixOpen() to determine the unix permissions |
drh | f65bc91 | 2010-07-14 20:51:34 +0000 | [diff] [blame] | 4887 | ** 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] | 4888 | ** and a value suitable for passing as the third argument to open(2) is |
| 4889 | ** written to *pMode. If an IO error occurs, an SQLite error code is |
| 4890 | ** returned and the value of *pMode is not modified. |
| 4891 | ** |
| 4892 | ** If the file being opened is a temporary file, it is always created with |
| 4893 | ** the octal permissions 0600 (read/writable by owner only). If the file |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4894 | ** is a database or master journal file, it is created with the permissions |
| 4895 | ** mask SQLITE_DEFAULT_FILE_PERMISSIONS. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4896 | ** |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4897 | ** Finally, if the file being opened is a WAL or regular journal file, then |
| 4898 | ** this function queries the file-system for the permissions on the |
| 4899 | ** corresponding database file and sets *pMode to this value. Whenever |
| 4900 | ** possible, WAL and journal files are created using the same permissions |
| 4901 | ** as the associated database file. |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 4902 | ** |
| 4903 | ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the |
| 4904 | ** original filename is unavailable. But 8_3_NAMES is only used for |
| 4905 | ** FAT filesystems and permissions do not matter there, so just use |
| 4906 | ** the default permissions. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4907 | */ |
| 4908 | static int findCreateFileMode( |
| 4909 | const char *zPath, /* Path of file (possibly) being created */ |
| 4910 | int flags, /* Flags passed as 4th argument to xOpen() */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4911 | mode_t *pMode, /* OUT: Permissions to open file with */ |
| 4912 | uid_t *pUid, /* OUT: uid to set on the file */ |
| 4913 | gid_t *pGid /* OUT: gid to set on the file */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4914 | ){ |
| 4915 | int rc = SQLITE_OK; /* Return Code */ |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 4916 | *pMode = SQLITE_DEFAULT_FILE_PERMISSIONS; |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4917 | *pUid = 0; |
| 4918 | *pGid = 0; |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4919 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4920 | char zDb[MAX_PATHNAME+1]; /* Database file path */ |
| 4921 | int nDb; /* Number of valid bytes in zDb */ |
| 4922 | struct stat sStat; /* Output of stat() on database file */ |
| 4923 | |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 4924 | /* zPath is a path to a WAL or journal file. The following block derives |
| 4925 | ** the path to the associated database file from zPath. This block handles |
| 4926 | ** the following naming conventions: |
| 4927 | ** |
| 4928 | ** "<path to db>-journal" |
| 4929 | ** "<path to db>-wal" |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 4930 | ** "<path to db>-journalNN" |
| 4931 | ** "<path to db>-walNN" |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 4932 | ** |
drh | d337c5b | 2011-10-20 18:23:35 +0000 | [diff] [blame] | 4933 | ** where NN is a decimal number. The NN naming schemes are |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 4934 | ** used by the test_multiplex.c module. |
| 4935 | */ |
| 4936 | nDb = sqlite3Strlen30(zPath) - 1; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 4937 | #ifdef SQLITE_ENABLE_8_3_NAMES |
dan | 28a67fd | 2011-12-12 19:48:43 +0000 | [diff] [blame] | 4938 | while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--; |
drh | d337c5b | 2011-10-20 18:23:35 +0000 | [diff] [blame] | 4939 | if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 4940 | #else |
| 4941 | while( zPath[nDb]!='-' ){ |
| 4942 | assert( nDb>0 ); |
| 4943 | assert( zPath[nDb]!='\n' ); |
| 4944 | nDb--; |
| 4945 | } |
| 4946 | #endif |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4947 | memcpy(zDb, zPath, nDb); |
| 4948 | zDb[nDb] = '\0'; |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 4949 | |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 4950 | if( 0==osStat(zDb, &sStat) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4951 | *pMode = sStat.st_mode & 0777; |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4952 | *pUid = sStat.st_uid; |
| 4953 | *pGid = sStat.st_gid; |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4954 | }else{ |
| 4955 | rc = SQLITE_IOERR_FSTAT; |
| 4956 | } |
| 4957 | }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 4958 | *pMode = 0600; |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4959 | } |
| 4960 | return rc; |
| 4961 | } |
| 4962 | |
| 4963 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4964 | ** Open the file zPath. |
| 4965 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4966 | ** Previously, the SQLite OS layer used three functions in place of this |
| 4967 | ** one: |
| 4968 | ** |
| 4969 | ** sqlite3OsOpenReadWrite(); |
| 4970 | ** sqlite3OsOpenReadOnly(); |
| 4971 | ** sqlite3OsOpenExclusive(); |
| 4972 | ** |
| 4973 | ** These calls correspond to the following combinations of flags: |
| 4974 | ** |
| 4975 | ** ReadWrite() -> (READWRITE | CREATE) |
| 4976 | ** ReadOnly() -> (READONLY) |
| 4977 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 4978 | ** |
| 4979 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 4980 | ** true, the file was configured to be automatically deleted when the |
| 4981 | ** file handle closed. To achieve the same effect using this new |
| 4982 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 4983 | ** OpenExclusive(). |
| 4984 | */ |
| 4985 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4986 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 4987 | const char *zPath, /* Pathname of file to be opened */ |
| 4988 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 4989 | int flags, /* Input flags to control the opening */ |
| 4990 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4991 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4992 | unixFile *p = (unixFile *)pFile; |
| 4993 | int fd = -1; /* File descriptor returned by open() */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4994 | int openFlags = 0; /* Flags to pass to open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4995 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4996 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4997 | int rc = SQLITE_OK; /* Function Return Code */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 4998 | int ctrlFlags = 0; /* UNIXFILE_* flags */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4999 | |
| 5000 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 5001 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 5002 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 5003 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 5004 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5005 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5006 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 5007 | #endif |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 5008 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 5009 | struct statfs fsInfo; |
| 5010 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5011 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5012 | /* If creating a master or main-file journal, this function will open |
| 5013 | ** a file-descriptor on the directory too. The first time unixSync() |
| 5014 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 5015 | */ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5016 | int syncDir = (isCreate && ( |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5017 | eType==SQLITE_OPEN_MASTER_JOURNAL |
| 5018 | || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 5019 | || eType==SQLITE_OPEN_WAL |
| 5020 | )); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5021 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5022 | /* If argument zPath is a NULL pointer, this function is required to open |
| 5023 | ** a temporary file. Use this buffer to store the file name in. |
| 5024 | */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5025 | char zTmpname[MAX_PATHNAME+2]; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5026 | const char *zName = zPath; |
| 5027 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5028 | /* Check the following statements are true: |
| 5029 | ** |
| 5030 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 5031 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 5032 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5033 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5034 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5035 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5036 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5037 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5038 | assert(isDelete==0 || isCreate); |
| 5039 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5040 | /* The main DB, main journal, WAL file and master journal are never |
| 5041 | ** automatically deleted. Nor are they ever temporary files. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5042 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 5043 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 5044 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5045 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5046 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5047 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 5048 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 5049 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 5050 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5051 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5052 | ); |
| 5053 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5054 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5055 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5056 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5057 | UnixUnusedFd *pUnused; |
| 5058 | pUnused = findReusableFd(zName, flags); |
| 5059 | if( pUnused ){ |
| 5060 | fd = pUnused->fd; |
| 5061 | }else{ |
dan | 6aa657f | 2009-08-24 18:57:58 +0000 | [diff] [blame] | 5062 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5063 | if( !pUnused ){ |
| 5064 | return SQLITE_NOMEM; |
| 5065 | } |
| 5066 | } |
| 5067 | p->pUnused = pUnused; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5068 | |
| 5069 | /* Database filenames are double-zero terminated if they are not |
| 5070 | ** URIs with parameters. Hence, they can always be passed into |
| 5071 | ** sqlite3_uri_parameter(). */ |
| 5072 | assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 ); |
| 5073 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5074 | }else if( !zName ){ |
| 5075 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5076 | assert(isDelete && !syncDir); |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5077 | rc = unixGetTempname(MAX_PATHNAME+2, zTmpname); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5078 | if( rc!=SQLITE_OK ){ |
| 5079 | return rc; |
| 5080 | } |
| 5081 | zName = zTmpname; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5082 | |
| 5083 | /* Generated temporary filenames are always double-zero terminated |
| 5084 | ** for use by sqlite3_uri_parameter(). */ |
| 5085 | assert( zName[strlen(zName)+1]==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5088 | /* Determine the value of the flags parameter passed to POSIX function |
| 5089 | ** open(). These must be calculated even if open() is not called, as |
| 5090 | ** they may be stored as part of the file handle and used by the |
| 5091 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5092 | if( isReadonly ) openFlags |= O_RDONLY; |
| 5093 | if( isReadWrite ) openFlags |= O_RDWR; |
| 5094 | if( isCreate ) openFlags |= O_CREAT; |
| 5095 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 5096 | openFlags |= (O_LARGEFILE|O_BINARY); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5097 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5098 | if( fd<0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5099 | mode_t openMode; /* Permissions to create file with */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5100 | uid_t uid; /* Userid for the file */ |
| 5101 | gid_t gid; /* Groupid for the file */ |
| 5102 | rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5103 | if( rc!=SQLITE_OK ){ |
| 5104 | assert( !p->pUnused ); |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5105 | assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5106 | return rc; |
| 5107 | } |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5108 | fd = robust_open(zName, openFlags, openMode); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5109 | OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5110 | if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ |
| 5111 | /* Failed to open the file for read/write access. Try read-only. */ |
| 5112 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5113 | openFlags &= ~(O_RDWR|O_CREAT); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5114 | flags |= SQLITE_OPEN_READONLY; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5115 | openFlags |= O_RDONLY; |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 5116 | isReadonly = 1; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5117 | fd = robust_open(zName, openFlags, openMode); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5118 | } |
| 5119 | if( fd<0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5120 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5121 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5122 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5123 | |
| 5124 | /* If this process is running as root and if creating a new rollback |
| 5125 | ** journal or WAL file, set the ownership of the journal or WAL to be |
| 5126 | ** the same as the original database. If we are not running as root, |
drh | 3ee3484 | 2012-02-11 21:21:17 +0000 | [diff] [blame] | 5127 | ** then the fchown() call will fail, but that's ok. The "if(){}" and |
| 5128 | ** the setting of the UNIXFILE_CHOWN flag are purely to silence compiler |
| 5129 | ** warnings from gcc. |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5130 | */ |
| 5131 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
drh | 3ee3484 | 2012-02-11 21:21:17 +0000 | [diff] [blame] | 5132 | if( fchown(fd, uid, gid)==0 ){ p->ctrlFlags |= UNIXFILE_CHOWN; } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5133 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5134 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5135 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5136 | if( pOutFlags ){ |
| 5137 | *pOutFlags = flags; |
| 5138 | } |
| 5139 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5140 | if( p->pUnused ){ |
| 5141 | p->pUnused->fd = fd; |
| 5142 | p->pUnused->flags = flags; |
| 5143 | } |
| 5144 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5145 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5146 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5147 | zPath = zName; |
| 5148 | #else |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5149 | osUnlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5150 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5151 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 5152 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5153 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5154 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 5155 | } |
| 5156 | #endif |
| 5157 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5158 | #ifdef FD_CLOEXEC |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5159 | osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5160 | #endif |
| 5161 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5162 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5163 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5164 | |
| 5165 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5166 | if( fstatfs(fd, &fsInfo) == -1 ){ |
| 5167 | ((unixFile*)pFile)->lastErrno = errno; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5168 | robust_close(p, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5169 | return SQLITE_IOERR_ACCESS; |
| 5170 | } |
| 5171 | if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 5172 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 5173 | } |
| 5174 | #endif |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5175 | |
| 5176 | /* Set up appropriate ctrlFlags */ |
| 5177 | if( isDelete ) ctrlFlags |= UNIXFILE_DELETE; |
| 5178 | if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY; |
| 5179 | if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK; |
| 5180 | if( syncDir ) ctrlFlags |= UNIXFILE_DIRSYNC; |
| 5181 | if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI; |
| 5182 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5183 | #if SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5184 | #if SQLITE_PREFER_PROXY_LOCKING |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5185 | isAutoProxy = 1; |
| 5186 | #endif |
| 5187 | if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5188 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 5189 | int useProxy = 0; |
| 5190 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5191 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 5192 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5193 | if( envforce!=NULL ){ |
| 5194 | useProxy = atoi(envforce)>0; |
| 5195 | }else{ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5196 | if( statfs(zPath, &fsInfo) == -1 ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5197 | /* In theory, the close(fd) call is sub-optimal. If the file opened |
| 5198 | ** with fd is a database file, and there are other connections open |
| 5199 | ** on that file that are currently holding advisory locks on it, |
| 5200 | ** then the call to close() will cancel those locks. In practice, |
| 5201 | ** we're assuming that statfs() doesn't fail very often. At least |
| 5202 | ** not while other file descriptors opened by the same process on |
| 5203 | ** the same file are working. */ |
| 5204 | p->lastErrno = errno; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5205 | robust_close(p, fd, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5206 | rc = SQLITE_IOERR_ACCESS; |
| 5207 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5208 | } |
| 5209 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 5210 | } |
| 5211 | if( useProxy ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5212 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5213 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5214 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5215 | if( rc!=SQLITE_OK ){ |
| 5216 | /* Use unixClose to clean up the resources added in fillInUnixFile |
| 5217 | ** and clear all the structure's references. Specifically, |
| 5218 | ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 5219 | */ |
| 5220 | unixClose(pFile); |
| 5221 | return rc; |
| 5222 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5223 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5224 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5225 | } |
| 5226 | } |
| 5227 | #endif |
| 5228 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5229 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
| 5230 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5231 | open_finished: |
| 5232 | if( rc!=SQLITE_OK ){ |
| 5233 | sqlite3_free(p->pUnused); |
| 5234 | } |
| 5235 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5236 | } |
| 5237 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5238 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5239 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5240 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 5241 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5242 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5243 | static int unixDelete( |
| 5244 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 5245 | const char *zPath, /* Name of file to be deleted */ |
| 5246 | int dirSync /* If true, fsync() directory after deleting file */ |
| 5247 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5248 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5249 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5250 | SimulateIOError(return SQLITE_IOERR_DELETE); |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5251 | if( osUnlink(zPath)==(-1) && errno!=ENOENT ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5252 | return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath); |
drh | 5d4feff | 2010-07-14 01:45:22 +0000 | [diff] [blame] | 5253 | } |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 5254 | #ifndef SQLITE_DISABLE_DIRSYNC |
drh | e349519 | 2012-01-05 16:07:30 +0000 | [diff] [blame] | 5255 | if( (dirSync & 1)!=0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5256 | int fd; |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 5257 | rc = osOpenDirectory(zPath, &fd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5258 | if( rc==SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5259 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5260 | if( fsync(fd)==-1 ) |
| 5261 | #else |
| 5262 | if( fsync(fd) ) |
| 5263 | #endif |
| 5264 | { |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5265 | rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5266 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5267 | robust_close(0, fd, __LINE__); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 5268 | }else if( rc==SQLITE_CANTOPEN ){ |
| 5269 | rc = SQLITE_OK; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5270 | } |
| 5271 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 5272 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5273 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5274 | } |
| 5275 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5276 | /* |
| 5277 | ** Test the existance of or access permissions of file zPath. The |
| 5278 | ** test performed depends on the value of flags: |
| 5279 | ** |
| 5280 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 5281 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 5282 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 5283 | ** |
| 5284 | ** Otherwise return 0. |
| 5285 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5286 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5287 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 5288 | const char *zPath, /* Path of the file to examine */ |
| 5289 | int flags, /* What do we want to learn about the zPath file? */ |
| 5290 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5291 | ){ |
rse | 25c0d1a | 2007-09-20 08:38:14 +0000 | [diff] [blame] | 5292 | int amode = 0; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5293 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5294 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5295 | switch( flags ){ |
| 5296 | case SQLITE_ACCESS_EXISTS: |
| 5297 | amode = F_OK; |
| 5298 | break; |
| 5299 | case SQLITE_ACCESS_READWRITE: |
| 5300 | amode = W_OK|R_OK; |
| 5301 | break; |
drh | 50d3f90 | 2007-08-27 21:10:36 +0000 | [diff] [blame] | 5302 | case SQLITE_ACCESS_READ: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5303 | amode = R_OK; |
| 5304 | break; |
| 5305 | |
| 5306 | default: |
| 5307 | assert(!"Invalid flags argument"); |
| 5308 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5309 | *pResOut = (osAccess(zPath, amode)==0); |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 5310 | if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){ |
| 5311 | struct stat buf; |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 5312 | if( 0==osStat(zPath, &buf) && buf.st_size==0 ){ |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 5313 | *pResOut = 0; |
| 5314 | } |
| 5315 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5316 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5317 | } |
| 5318 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5319 | |
| 5320 | /* |
| 5321 | ** Turn a relative pathname into a full pathname. The relative path |
| 5322 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 5323 | ** zPath. |
| 5324 | ** |
| 5325 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 5326 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 5327 | ** this buffer before returning. |
| 5328 | */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 5329 | static int unixFullPathname( |
| 5330 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 5331 | const char *zPath, /* Possibly relative input path */ |
| 5332 | int nOut, /* Size of output buffer in bytes */ |
| 5333 | char *zOut /* Output buffer */ |
| 5334 | ){ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 5335 | |
| 5336 | /* It's odd to simulate an io-error here, but really this is just |
| 5337 | ** using the io-error infrastructure to test that SQLite handles this |
| 5338 | ** function failing. This function could fail if, for example, the |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5339 | ** current working directory has been unlinked. |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 5340 | */ |
| 5341 | SimulateIOError( return SQLITE_ERROR ); |
| 5342 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 5343 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 5344 | UNUSED_PARAMETER(pVfs); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5345 | |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 5346 | zOut[nOut-1] = '\0'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5347 | if( zPath[0]=='/' ){ |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 5348 | sqlite3_snprintf(nOut, zOut, "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5349 | }else{ |
| 5350 | int nCwd; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5351 | if( osGetcwd(zOut, nOut-1)==0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5352 | return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5353 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5354 | nCwd = (int)strlen(zOut); |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 5355 | sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5356 | } |
| 5357 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5358 | } |
| 5359 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 5360 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5361 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 5362 | /* |
| 5363 | ** Interfaces for opening a shared library, finding entry points |
| 5364 | ** within the shared library, and closing the shared library. |
| 5365 | */ |
| 5366 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5367 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 5368 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5369 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 5370 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 5371 | |
| 5372 | /* |
| 5373 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 5374 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 5375 | ** message is available, it is written to zBufOut. If no error message |
| 5376 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 5377 | ** error message. |
| 5378 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5379 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
dan | 3239053 | 2010-11-29 18:36:22 +0000 | [diff] [blame] | 5380 | const char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5381 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5382 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5383 | zErr = dlerror(); |
| 5384 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 5385 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5386 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5387 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5388 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5389 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 5390 | /* |
| 5391 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 5392 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 5393 | ** returns a void* which is really a pointer to a function. So how do we |
| 5394 | ** use dlsym() with -pedantic-errors? |
| 5395 | ** |
| 5396 | ** Variable x below is defined to be a pointer to a function taking |
| 5397 | ** parameters void* and const char* and returning a pointer to a function. |
| 5398 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 5399 | ** (That assignment requires a cast.) Then we call the function that |
| 5400 | ** x points to. |
| 5401 | ** |
| 5402 | ** This work-around is unlikely to work correctly on any system where |
| 5403 | ** you really cannot cast a function pointer into void*. But then, on the |
| 5404 | ** other hand, dlsym() will not work on such a system either, so we have |
| 5405 | ** not really lost anything. |
| 5406 | */ |
| 5407 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5408 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5409 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 5410 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5411 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5412 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 5413 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5414 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5415 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5416 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 5417 | #define unixDlOpen 0 |
| 5418 | #define unixDlError 0 |
| 5419 | #define unixDlSym 0 |
| 5420 | #define unixDlClose 0 |
| 5421 | #endif |
| 5422 | |
| 5423 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5424 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5425 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5426 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 5427 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 5428 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5429 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5430 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 5431 | ** errors. The reports issued by valgrind are incorrect - we would |
| 5432 | ** prefer that the randomness be increased by making use of the |
| 5433 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 5434 | ** some users. Rather than argue, it seems easier just to initialize |
| 5435 | ** the whole array and silence valgrind, even if that means less randomness |
| 5436 | ** in the random seed. |
| 5437 | ** |
| 5438 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 5439 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5440 | ** tests repeatable. |
| 5441 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5442 | memset(zBuf, 0, nBuf); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5443 | #if !defined(SQLITE_TEST) |
| 5444 | { |
drh | c18b404 | 2012-02-10 03:10:27 +0000 | [diff] [blame] | 5445 | int pid, fd, got; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5446 | fd = robust_open("/dev/urandom", O_RDONLY, 0); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5447 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 5448 | time_t t; |
| 5449 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5450 | memcpy(zBuf, &t, sizeof(t)); |
| 5451 | pid = getpid(); |
| 5452 | memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid)); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 5453 | assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf ); |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 5454 | nBuf = sizeof(t) + sizeof(pid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5455 | }else{ |
drh | c18b404 | 2012-02-10 03:10:27 +0000 | [diff] [blame] | 5456 | do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5457 | robust_close(0, fd, __LINE__); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 5458 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5459 | } |
| 5460 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 5461 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5462 | } |
| 5463 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5464 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5465 | /* |
| 5466 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5467 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 5468 | ** The return value is the number of microseconds of sleep actually |
| 5469 | ** requested from the underlying operating system, a number which |
| 5470 | ** might be greater than or equal to the argument, but not less |
| 5471 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5472 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5473 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5474 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5475 | struct timespec sp; |
| 5476 | |
| 5477 | sp.tv_sec = microseconds / 1000000; |
| 5478 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 5479 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 5480 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5481 | return microseconds; |
| 5482 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5483 | usleep(microseconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 5484 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5485 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5486 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5487 | int seconds = (microseconds+999999)/1000000; |
| 5488 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 5489 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 5490 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 5491 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 5492 | } |
| 5493 | |
| 5494 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5495 | ** The following variable, if set to a non-zero value, is interpreted as |
| 5496 | ** the number of seconds since 1970 and is used to set the result of |
| 5497 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5498 | */ |
| 5499 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5500 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5501 | #endif |
| 5502 | |
| 5503 | /* |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5504 | ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 5505 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 5506 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 5507 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 5508 | ** proleptic Gregorian calendar. |
| 5509 | ** |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5510 | ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
| 5511 | ** cannot be found. |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5512 | */ |
| 5513 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 5514 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5515 | int rc = SQLITE_OK; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5516 | #if defined(NO_GETTOD) |
| 5517 | time_t t; |
| 5518 | time(&t); |
dan | 15eac4e | 2010-11-22 17:26:07 +0000 | [diff] [blame] | 5519 | *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5520 | #elif OS_VXWORKS |
| 5521 | struct timespec sNow; |
| 5522 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 5523 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 5524 | #else |
| 5525 | struct timeval sNow; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5526 | if( gettimeofday(&sNow, 0)==0 ){ |
| 5527 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
| 5528 | }else{ |
| 5529 | rc = SQLITE_ERROR; |
| 5530 | } |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5531 | #endif |
| 5532 | |
| 5533 | #ifdef SQLITE_TEST |
| 5534 | if( sqlite3_current_time ){ |
| 5535 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 5536 | } |
| 5537 | #endif |
| 5538 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5539 | return rc; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5540 | } |
| 5541 | |
| 5542 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5543 | ** Find the current time (in Universal Coordinated Time). Write the |
| 5544 | ** current time and date as a Julian Day number into *prNow and |
| 5545 | ** return 0. Return 1 if the time and date cannot be found. |
| 5546 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5547 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | b87a666 | 2011-10-13 01:01:14 +0000 | [diff] [blame] | 5548 | sqlite3_int64 i = 0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5549 | int rc; |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 5550 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5551 | rc = unixCurrentTimeInt64(0, &i); |
drh | 0dcb0a7 | 2010-05-03 18:22:52 +0000 | [diff] [blame] | 5552 | *prNow = i/86400000.0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 5553 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5554 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5555 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5556 | /* |
| 5557 | ** We added the xGetLastError() method with the intention of providing |
| 5558 | ** better low-level error messages when operating-system problems come up |
| 5559 | ** during SQLite operation. But so far, none of that has been implemented |
| 5560 | ** in the core. So this routine is never called. For now, it is merely |
| 5561 | ** a place-holder. |
| 5562 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5563 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 5564 | UNUSED_PARAMETER(NotUsed); |
| 5565 | UNUSED_PARAMETER(NotUsed2); |
| 5566 | UNUSED_PARAMETER(NotUsed3); |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 5567 | return 0; |
| 5568 | } |
| 5569 | |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5570 | |
| 5571 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5572 | ************************ End of sqlite3_vfs methods *************************** |
| 5573 | ******************************************************************************/ |
| 5574 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5575 | /****************************************************************************** |
| 5576 | ************************** Begin Proxy Locking ******************************** |
| 5577 | ** |
| 5578 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 5579 | ** other locking methods on secondary lock files. Proxy locking is a |
| 5580 | ** meta-layer over top of the primitive locking implemented above. For |
| 5581 | ** this reason, the division that implements of proxy locking is deferred |
| 5582 | ** until late in the file (here) after all of the other I/O methods have |
| 5583 | ** been defined - so that the primitive locking methods are available |
| 5584 | ** as services to help with the implementation of proxy locking. |
| 5585 | ** |
| 5586 | **** |
| 5587 | ** |
| 5588 | ** The default locking schemes in SQLite use byte-range locks on the |
| 5589 | ** database file to coordinate safe, concurrent access by multiple readers |
| 5590 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 5591 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 5592 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 5593 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 5594 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 5595 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 5596 | ** address in the shared range is taken for a SHARED lock, the entire |
| 5597 | ** shared range is taken for an EXCLUSIVE lock): |
| 5598 | ** |
| 5599 | ** PENDING_BYTE 0x40000000 |
| 5600 | ** RESERVED_BYTE 0x40000001 |
| 5601 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 5602 | ** |
| 5603 | ** This works well on the local file system, but shows a nearly 100x |
| 5604 | ** slowdown in read performance on AFP because the AFP client disables |
| 5605 | ** the read cache when byte-range locks are present. Enabling the read |
| 5606 | ** cache exposes a cache coherency problem that is present on all OS X |
| 5607 | ** supported network file systems. NFS and AFP both observe the |
| 5608 | ** close-to-open semantics for ensuring cache coherency |
| 5609 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 5610 | ** address the requirements for concurrent database access by multiple |
| 5611 | ** readers and writers |
| 5612 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 5613 | ** |
| 5614 | ** To address the performance and cache coherency issues, proxy file locking |
| 5615 | ** changes the way database access is controlled by limiting access to a |
| 5616 | ** single host at a time and moving file locks off of the database file |
| 5617 | ** and onto a proxy file on the local file system. |
| 5618 | ** |
| 5619 | ** |
| 5620 | ** Using proxy locks |
| 5621 | ** ----------------- |
| 5622 | ** |
| 5623 | ** C APIs |
| 5624 | ** |
| 5625 | ** sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE, |
| 5626 | ** <proxy_path> | ":auto:"); |
| 5627 | ** sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>); |
| 5628 | ** |
| 5629 | ** |
| 5630 | ** SQL pragmas |
| 5631 | ** |
| 5632 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 5633 | ** PRAGMA [database.]lock_proxy_file |
| 5634 | ** |
| 5635 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 5636 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 5637 | ** a proxy path based on the user's temp dir |
| 5638 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 5639 | ** actual proxy file name is generated from the name and path of the |
| 5640 | ** database file. For example: |
| 5641 | ** |
| 5642 | ** For database path "/Users/me/foo.db" |
| 5643 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 5644 | ** |
| 5645 | ** Once a lock proxy is configured for a database connection, it can not |
| 5646 | ** be removed, however it may be switched to a different proxy path via |
| 5647 | ** the above APIs (assuming the conch file is not being held by another |
| 5648 | ** connection or process). |
| 5649 | ** |
| 5650 | ** |
| 5651 | ** How proxy locking works |
| 5652 | ** ----------------------- |
| 5653 | ** |
| 5654 | ** Proxy file locking relies primarily on two new supporting files: |
| 5655 | ** |
| 5656 | ** * conch file to limit access to the database file to a single host |
| 5657 | ** at a time |
| 5658 | ** |
| 5659 | ** * proxy file to act as a proxy for the advisory locks normally |
| 5660 | ** taken on the database |
| 5661 | ** |
| 5662 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 5663 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 5664 | ** contents and comparing the host's unique host ID (see below) and lock |
| 5665 | ** proxy path against the values stored in the conch. The conch file is |
| 5666 | ** stored in the same directory as the database file and the file name |
| 5667 | ** is patterned after the database file name as ".<databasename>-conch". |
| 5668 | ** If the conch file does not exist, or it's contents do not match the |
| 5669 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 5670 | ** lock and the conch file contents is updated with the host ID and proxy |
| 5671 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 5672 | ** is held by another process (with a shared lock), the exclusive lock |
| 5673 | ** will fail and SQLITE_BUSY is returned. |
| 5674 | ** |
| 5675 | ** The proxy file - a single-byte file used for all advisory file locks |
| 5676 | ** normally taken on the database file. This allows for safe sharing |
| 5677 | ** of the database file for multiple readers and writers on the same |
| 5678 | ** host (the conch ensures that they all use the same local lock file). |
| 5679 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5680 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 5681 | ** only taken when the first request to lock database file is made. |
| 5682 | ** This matches the semantics of the traditional locking behavior, where |
| 5683 | ** opening a connection to a database file does not take a lock on it. |
| 5684 | ** The shared lock and an open file descriptor are maintained until |
| 5685 | ** the connection to the database is closed. |
| 5686 | ** |
| 5687 | ** The proxy file and the lock file are never deleted so they only need |
| 5688 | ** to be created the first time they are used. |
| 5689 | ** |
| 5690 | ** Configuration options |
| 5691 | ** --------------------- |
| 5692 | ** |
| 5693 | ** SQLITE_PREFER_PROXY_LOCKING |
| 5694 | ** |
| 5695 | ** Database files accessed on non-local file systems are |
| 5696 | ** automatically configured for proxy locking, lock files are |
| 5697 | ** named automatically using the same logic as |
| 5698 | ** PRAGMA lock_proxy_file=":auto:" |
| 5699 | ** |
| 5700 | ** SQLITE_PROXY_DEBUG |
| 5701 | ** |
| 5702 | ** Enables the logging of error messages during host id file |
| 5703 | ** retrieval and creation |
| 5704 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5705 | ** LOCKPROXYDIR |
| 5706 | ** |
| 5707 | ** Overrides the default directory used for lock proxy files that |
| 5708 | ** are named automatically via the ":auto:" setting |
| 5709 | ** |
| 5710 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 5711 | ** |
| 5712 | ** Permissions to use when creating a directory for storing the |
| 5713 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 5714 | ** |
| 5715 | ** |
| 5716 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 5717 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 5718 | ** force proxy locking to be used for every database file opened, and 0 |
| 5719 | ** will force automatic proxy locking to be disabled for all database |
| 5720 | ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or |
| 5721 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 5722 | */ |
| 5723 | |
| 5724 | /* |
| 5725 | ** Proxy locking is only available on MacOSX |
| 5726 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5727 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5728 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5729 | /* |
| 5730 | ** The proxyLockingContext has the path and file structures for the remote |
| 5731 | ** and local proxy files in it |
| 5732 | */ |
| 5733 | typedef struct proxyLockingContext proxyLockingContext; |
| 5734 | struct proxyLockingContext { |
| 5735 | unixFile *conchFile; /* Open conch file */ |
| 5736 | char *conchFilePath; /* Name of the conch file */ |
| 5737 | unixFile *lockProxy; /* Open proxy lock file */ |
| 5738 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 5739 | char *dbPath; /* Name of the open file */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5740 | int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5741 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 5742 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 5743 | }; |
| 5744 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5745 | /* |
| 5746 | ** The proxy lock file path for the database at dbPath is written into lPath, |
| 5747 | ** which must point to valid, writable memory large enough for a maxLen length |
| 5748 | ** file path. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5749 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5750 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 5751 | int len; |
| 5752 | int dbLen; |
| 5753 | int i; |
| 5754 | |
| 5755 | #ifdef LOCKPROXYDIR |
| 5756 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 5757 | #else |
| 5758 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 5759 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5760 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5761 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
| 5762 | lPath, errno, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5763 | return SQLITE_IOERR_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5764 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5765 | len = strlcat(lPath, "sqliteplocks", maxLen); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5766 | } |
| 5767 | # else |
| 5768 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 5769 | # endif |
| 5770 | #endif |
| 5771 | |
| 5772 | if( lPath[len-1]!='/' ){ |
| 5773 | len = strlcat(lPath, "/", maxLen); |
| 5774 | } |
| 5775 | |
| 5776 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5777 | dbLen = (int)strlen(dbPath); |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5778 | for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5779 | char c = dbPath[i]; |
| 5780 | lPath[i+len] = (c=='/')?'_':c; |
| 5781 | } |
| 5782 | lPath[i+len]='\0'; |
| 5783 | strlcat(lPath, ":auto:", maxLen); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5784 | OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5785 | return SQLITE_OK; |
| 5786 | } |
| 5787 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5788 | /* |
| 5789 | ** Creates the lock file and any missing directories in lockPath |
| 5790 | */ |
| 5791 | static int proxyCreateLockPath(const char *lockPath){ |
| 5792 | int i, len; |
| 5793 | char buf[MAXPATHLEN]; |
| 5794 | int start = 0; |
| 5795 | |
| 5796 | assert(lockPath!=NULL); |
| 5797 | /* try to create all the intermediate directories */ |
| 5798 | len = (int)strlen(lockPath); |
| 5799 | buf[0] = lockPath[0]; |
| 5800 | for( i=1; i<len; i++ ){ |
| 5801 | if( lockPath[i] == '/' && (i - start > 0) ){ |
| 5802 | /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 5803 | if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 5804 | || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 5805 | buf[i]='\0'; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 5806 | if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5807 | int err=errno; |
| 5808 | if( err!=EEXIST ) { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5809 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5810 | "'%s' proxy lock path=%s pid=%d\n", |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5811 | buf, strerror(err), lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5812 | return err; |
| 5813 | } |
| 5814 | } |
| 5815 | } |
| 5816 | start=i+1; |
| 5817 | } |
| 5818 | buf[i] = lockPath[i]; |
| 5819 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5820 | OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5821 | return 0; |
| 5822 | } |
| 5823 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5824 | /* |
| 5825 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 5826 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 5827 | ** |
| 5828 | ** The caller is responsible not only for closing the file descriptor |
| 5829 | ** but also for freeing the memory associated with the file descriptor. |
| 5830 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5831 | static int proxyCreateUnixFile( |
| 5832 | const char *path, /* path for the new unixFile */ |
| 5833 | unixFile **ppFile, /* unixFile created and returned by ref */ |
| 5834 | int islockfile /* if non zero missing dirs will be created */ |
| 5835 | ) { |
| 5836 | int fd = -1; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5837 | unixFile *pNew; |
| 5838 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5839 | int openFlags = O_RDWR | O_CREAT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5840 | sqlite3_vfs dummyVfs; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5841 | int terrno = 0; |
| 5842 | UnixUnusedFd *pUnused = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5843 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5844 | /* 1. first try to open/create the file |
| 5845 | ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 5846 | ** the parent directories and then try again. |
| 5847 | ** 3. if that fails, try to open the file read-only |
| 5848 | ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 5849 | */ |
| 5850 | pUnused = findReusableFd(path, openFlags); |
| 5851 | if( pUnused ){ |
| 5852 | fd = pUnused->fd; |
| 5853 | }else{ |
| 5854 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
| 5855 | if( !pUnused ){ |
| 5856 | return SQLITE_NOMEM; |
| 5857 | } |
| 5858 | } |
| 5859 | if( fd<0 ){ |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5860 | fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5861 | terrno = errno; |
| 5862 | if( fd<0 && errno==ENOENT && islockfile ){ |
| 5863 | if( proxyCreateLockPath(path) == SQLITE_OK ){ |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5864 | fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5865 | } |
| 5866 | } |
| 5867 | } |
| 5868 | if( fd<0 ){ |
| 5869 | openFlags = O_RDONLY; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5870 | fd = robust_open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5871 | terrno = errno; |
| 5872 | } |
| 5873 | if( fd<0 ){ |
| 5874 | if( islockfile ){ |
| 5875 | return SQLITE_BUSY; |
| 5876 | } |
| 5877 | switch (terrno) { |
| 5878 | case EACCES: |
| 5879 | return SQLITE_PERM; |
| 5880 | case EIO: |
| 5881 | return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 5882 | default: |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 5883 | return SQLITE_CANTOPEN_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5884 | } |
| 5885 | } |
| 5886 | |
| 5887 | pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew)); |
| 5888 | if( pNew==NULL ){ |
| 5889 | rc = SQLITE_NOMEM; |
| 5890 | goto end_create_proxy; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5891 | } |
| 5892 | memset(pNew, 0, sizeof(unixFile)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5893 | pNew->openFlags = openFlags; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 5894 | memset(&dummyVfs, 0, sizeof(dummyVfs)); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5895 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 5896 | dummyVfs.zName = "dummy"; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5897 | pUnused->fd = fd; |
| 5898 | pUnused->flags = openFlags; |
| 5899 | pNew->pUnused = pUnused; |
| 5900 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5901 | rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5902 | if( rc==SQLITE_OK ){ |
| 5903 | *ppFile = pNew; |
| 5904 | return SQLITE_OK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5905 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5906 | end_create_proxy: |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5907 | robust_close(pNew, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5908 | sqlite3_free(pNew); |
| 5909 | sqlite3_free(pUnused); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5910 | return rc; |
| 5911 | } |
| 5912 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5913 | #ifdef SQLITE_TEST |
| 5914 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 5915 | int sqlite3_hostid_num = 0; |
| 5916 | #endif |
| 5917 | |
| 5918 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 5919 | |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5920 | /* Not always defined in the headers as it ought to be */ |
| 5921 | extern int gethostuuid(uuid_t id, const struct timespec *wait); |
| 5922 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5923 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 5924 | ** bytes of writable memory. |
| 5925 | */ |
| 5926 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5927 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 5928 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 5929 | #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\ |
| 5930 | && __MAC_OS_X_VERSION_MIN_REQUIRED<1050 |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 5931 | { |
| 5932 | static const struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
| 5933 | if( gethostuuid(pHostID, &timeout) ){ |
| 5934 | int err = errno; |
| 5935 | if( pError ){ |
| 5936 | *pError = err; |
| 5937 | } |
| 5938 | return SQLITE_IOERR; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5939 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5940 | } |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 5941 | #else |
| 5942 | UNUSED_PARAMETER(pError); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 5943 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5944 | #ifdef SQLITE_TEST |
| 5945 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 5946 | if( sqlite3_hostid_num != 0){ |
| 5947 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 5948 | } |
| 5949 | #endif |
| 5950 | |
| 5951 | return SQLITE_OK; |
| 5952 | } |
| 5953 | |
| 5954 | /* The conch file contains the header, host id and lock file path |
| 5955 | */ |
| 5956 | #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 5957 | #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 5958 | #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 5959 | #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 5960 | |
| 5961 | /* |
| 5962 | ** Takes an open conch file, copies the contents to a new path and then moves |
| 5963 | ** it back. The newly created file's file descriptor is assigned to the |
| 5964 | ** conch file structure and finally the original conch file descriptor is |
| 5965 | ** closed. Returns zero if successful. |
| 5966 | */ |
| 5967 | static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 5968 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5969 | unixFile *conchFile = pCtx->conchFile; |
| 5970 | char tPath[MAXPATHLEN]; |
| 5971 | char buf[PROXY_MAXCONCHLEN]; |
| 5972 | char *cPath = pCtx->conchFilePath; |
| 5973 | size_t readLen = 0; |
| 5974 | size_t pathLen = 0; |
| 5975 | char errmsg[64] = ""; |
| 5976 | int fd = -1; |
| 5977 | int rc = -1; |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5978 | UNUSED_PARAMETER(myHostID); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5979 | |
| 5980 | /* create a new path by replace the trailing '-conch' with '-break' */ |
| 5981 | pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 5982 | if( pathLen>MAXPATHLEN || pathLen<6 || |
| 5983 | (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5984 | sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5985 | goto end_breaklock; |
| 5986 | } |
| 5987 | /* read the conch content */ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 5988 | readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5989 | if( readLen<PROXY_PATHINDEX ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5990 | sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5991 | goto end_breaklock; |
| 5992 | } |
| 5993 | /* write it out to the temporary break file */ |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5994 | fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), |
| 5995 | SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5996 | if( fd<0 ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 5997 | sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5998 | goto end_breaklock; |
| 5999 | } |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6000 | if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6001 | sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6002 | goto end_breaklock; |
| 6003 | } |
| 6004 | if( rename(tPath, cPath) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6005 | sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6006 | goto end_breaklock; |
| 6007 | } |
| 6008 | rc = 0; |
| 6009 | fprintf(stderr, "broke stale lock on %s\n", cPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6010 | robust_close(pFile, conchFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6011 | conchFile->h = fd; |
| 6012 | conchFile->openFlags = O_RDWR | O_CREAT; |
| 6013 | |
| 6014 | end_breaklock: |
| 6015 | if( rc ){ |
| 6016 | if( fd>=0 ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 6017 | osUnlink(tPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6018 | robust_close(pFile, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6019 | } |
| 6020 | fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 6021 | } |
| 6022 | return rc; |
| 6023 | } |
| 6024 | |
| 6025 | /* Take the requested lock on the conch file and break a stale lock if the |
| 6026 | ** host id matches. |
| 6027 | */ |
| 6028 | static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 6029 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6030 | unixFile *conchFile = pCtx->conchFile; |
| 6031 | int rc = SQLITE_OK; |
| 6032 | int nTries = 0; |
| 6033 | struct timespec conchModTime; |
| 6034 | |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6035 | memset(&conchModTime, 0, sizeof(conchModTime)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6036 | do { |
| 6037 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 6038 | nTries ++; |
| 6039 | if( rc==SQLITE_BUSY ){ |
| 6040 | /* If the lock failed (busy): |
| 6041 | * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 6042 | * 2nd try: fail if the mod time changed or host id is different, wait |
| 6043 | * 10 sec and try again |
| 6044 | * 3rd try: break the lock unless the mod time has changed. |
| 6045 | */ |
| 6046 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6047 | if( osFstat(conchFile->h, &buf) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6048 | pFile->lastErrno = errno; |
| 6049 | return SQLITE_IOERR_LOCK; |
| 6050 | } |
| 6051 | |
| 6052 | if( nTries==1 ){ |
| 6053 | conchModTime = buf.st_mtimespec; |
| 6054 | usleep(500000); /* wait 0.5 sec and try the lock again*/ |
| 6055 | continue; |
| 6056 | } |
| 6057 | |
| 6058 | assert( nTries>1 ); |
| 6059 | if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 6060 | conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 6061 | return SQLITE_BUSY; |
| 6062 | } |
| 6063 | |
| 6064 | if( nTries==2 ){ |
| 6065 | char tBuf[PROXY_MAXCONCHLEN]; |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6066 | int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6067 | if( len<0 ){ |
| 6068 | pFile->lastErrno = errno; |
| 6069 | return SQLITE_IOERR_LOCK; |
| 6070 | } |
| 6071 | if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 6072 | /* don't break the lock if the host id doesn't match */ |
| 6073 | if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 6074 | return SQLITE_BUSY; |
| 6075 | } |
| 6076 | }else{ |
| 6077 | /* don't break the lock on short read or a version mismatch */ |
| 6078 | return SQLITE_BUSY; |
| 6079 | } |
| 6080 | usleep(10000000); /* wait 10 sec and try the lock again */ |
| 6081 | continue; |
| 6082 | } |
| 6083 | |
| 6084 | assert( nTries==3 ); |
| 6085 | if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 6086 | rc = SQLITE_OK; |
| 6087 | if( lockType==EXCLUSIVE_LOCK ){ |
| 6088 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 6089 | } |
| 6090 | if( !rc ){ |
| 6091 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 6092 | } |
| 6093 | } |
| 6094 | } |
| 6095 | } while( rc==SQLITE_BUSY && nTries<3 ); |
| 6096 | |
| 6097 | return rc; |
| 6098 | } |
| 6099 | |
| 6100 | /* 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] | 6101 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 6102 | ** lockPath means that the lockPath in the conch file will be used if the |
| 6103 | ** host IDs match, or a new lock path will be generated automatically |
| 6104 | ** and written to the conch file. |
| 6105 | */ |
| 6106 | static int proxyTakeConch(unixFile *pFile){ |
| 6107 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6108 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6109 | if( pCtx->conchHeld!=0 ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6110 | return SQLITE_OK; |
| 6111 | }else{ |
| 6112 | unixFile *conchFile = pCtx->conchFile; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6113 | uuid_t myHostID; |
| 6114 | int pError = 0; |
| 6115 | char readBuf[PROXY_MAXCONCHLEN]; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6116 | char lockPath[MAXPATHLEN]; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6117 | char *tempLockPath = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6118 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6119 | int createConch = 0; |
| 6120 | int hostIdMatch = 0; |
| 6121 | int readLen = 0; |
| 6122 | int tryOldLockPath = 0; |
| 6123 | int forceNewLockPath = 0; |
| 6124 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6125 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
| 6126 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6127 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6128 | rc = proxyGetHostID(myHostID, &pError); |
| 6129 | if( (rc&0xff)==SQLITE_IOERR ){ |
| 6130 | pFile->lastErrno = pError; |
| 6131 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6132 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6133 | rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6134 | if( rc!=SQLITE_OK ){ |
| 6135 | goto end_takeconch; |
| 6136 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6137 | /* read the existing conch file */ |
| 6138 | readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 6139 | if( readLen<0 ){ |
| 6140 | /* I/O error: lastErrno set by seekAndRead */ |
| 6141 | pFile->lastErrno = conchFile->lastErrno; |
| 6142 | rc = SQLITE_IOERR_READ; |
| 6143 | goto end_takeconch; |
| 6144 | }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 6145 | readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 6146 | /* a short read or version format mismatch means we need to create a new |
| 6147 | ** conch file. |
| 6148 | */ |
| 6149 | createConch = 1; |
| 6150 | } |
| 6151 | /* if the host id matches and the lock path already exists in the conch |
| 6152 | ** we'll try to use the path there, if we can't open that path, we'll |
| 6153 | ** retry with a new auto-generated path |
| 6154 | */ |
| 6155 | do { /* in case we need to try again for an :auto: named lock file */ |
| 6156 | |
| 6157 | if( !createConch && !forceNewLockPath ){ |
| 6158 | hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 6159 | PROXY_HOSTIDLEN); |
| 6160 | /* if the conch has data compare the contents */ |
| 6161 | if( !pCtx->lockProxyPath ){ |
| 6162 | /* for auto-named local lock file, just check the host ID and we'll |
| 6163 | ** use the local lock file path that's already in there |
| 6164 | */ |
| 6165 | if( hostIdMatch ){ |
| 6166 | size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 6167 | |
| 6168 | if( pathLen>=MAXPATHLEN ){ |
| 6169 | pathLen=MAXPATHLEN-1; |
| 6170 | } |
| 6171 | memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 6172 | lockPath[pathLen] = 0; |
| 6173 | tempLockPath = lockPath; |
| 6174 | tryOldLockPath = 1; |
| 6175 | /* create a copy of the lock path if the conch is taken */ |
| 6176 | goto end_takeconch; |
| 6177 | } |
| 6178 | }else if( hostIdMatch |
| 6179 | && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 6180 | readLen-PROXY_PATHINDEX) |
| 6181 | ){ |
| 6182 | /* conch host and lock path match */ |
| 6183 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6184 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6185 | } |
| 6186 | |
| 6187 | /* if the conch isn't writable and doesn't match, we can't take it */ |
| 6188 | if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 6189 | rc = SQLITE_BUSY; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6190 | goto end_takeconch; |
| 6191 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6192 | |
| 6193 | /* 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] | 6194 | if( !pCtx->lockProxyPath ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6195 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 6196 | tempLockPath = lockPath; |
| 6197 | /* create a copy of the lock path _only_ if the conch is taken */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6198 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6199 | |
| 6200 | /* update conch with host and path (this will fail if other process |
| 6201 | ** has a shared lock already), if the host id matches, use the big |
| 6202 | ** stick. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6203 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6204 | futimes(conchFile->h, NULL); |
| 6205 | if( hostIdMatch && !createConch ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 6206 | if( conchFile->pInode && conchFile->pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6207 | /* We are trying for an exclusive lock but another thread in this |
| 6208 | ** same process is still holding a shared lock. */ |
| 6209 | rc = SQLITE_BUSY; |
| 6210 | } else { |
| 6211 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6212 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6213 | }else{ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6214 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6215 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6216 | if( rc==SQLITE_OK ){ |
| 6217 | char writeBuffer[PROXY_MAXCONCHLEN]; |
| 6218 | int writeSize = 0; |
| 6219 | |
| 6220 | writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 6221 | memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 6222 | if( pCtx->lockProxyPath!=NULL ){ |
| 6223 | strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN); |
| 6224 | }else{ |
| 6225 | strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 6226 | } |
| 6227 | writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6228 | robust_ftruncate(conchFile->h, writeSize); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6229 | rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
| 6230 | fsync(conchFile->h); |
| 6231 | /* If we created a new conch file (not just updated the contents of a |
| 6232 | ** valid conch file), try to match the permissions of the database |
| 6233 | */ |
| 6234 | if( rc==SQLITE_OK && createConch ){ |
| 6235 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6236 | int err = osFstat(pFile->h, &buf); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6237 | if( err==0 ){ |
| 6238 | mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 6239 | S_IROTH|S_IWOTH); |
| 6240 | /* try to match the database file R/W permissions, ignore failure */ |
| 6241 | #ifndef SQLITE_PROXY_DEBUG |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6242 | osFchmod(conchFile->h, cmode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6243 | #else |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6244 | do{ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6245 | rc = osFchmod(conchFile->h, cmode); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6246 | }while( rc==(-1) && errno==EINTR ); |
| 6247 | if( rc!=0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6248 | int code = errno; |
| 6249 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 6250 | cmode, code, strerror(code)); |
| 6251 | } else { |
| 6252 | fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 6253 | } |
| 6254 | }else{ |
| 6255 | int code = errno; |
| 6256 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 6257 | err, code, strerror(code)); |
| 6258 | #endif |
| 6259 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6260 | } |
| 6261 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6262 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 6263 | |
| 6264 | end_takeconch: |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6265 | OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6266 | if( rc==SQLITE_OK && pFile->openFlags ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6267 | int fd; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6268 | if( pFile->h>=0 ){ |
drh | e84009f | 2011-03-02 17:54:32 +0000 | [diff] [blame] | 6269 | robust_close(pFile, pFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6270 | } |
| 6271 | pFile->h = -1; |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6272 | fd = robust_open(pCtx->dbPath, pFile->openFlags, |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6273 | SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6274 | OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6275 | if( fd>=0 ){ |
| 6276 | pFile->h = fd; |
| 6277 | }else{ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6278 | rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6279 | during locking */ |
| 6280 | } |
| 6281 | } |
| 6282 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 6283 | char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 6284 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 6285 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 6286 | /* we couldn't create the proxy lock file with the old lock file path |
| 6287 | ** so try again via auto-naming |
| 6288 | */ |
| 6289 | forceNewLockPath = 1; |
| 6290 | tryOldLockPath = 0; |
dan | 2b0ef47 | 2010-02-16 12:18:47 +0000 | [diff] [blame] | 6291 | continue; /* go back to the do {} while start point, try again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6292 | } |
| 6293 | } |
| 6294 | if( rc==SQLITE_OK ){ |
| 6295 | /* Need to make a copy of path if we extracted the value |
| 6296 | ** from the conch file or the path was allocated on the stack |
| 6297 | */ |
| 6298 | if( tempLockPath ){ |
| 6299 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 6300 | if( !pCtx->lockProxyPath ){ |
| 6301 | rc = SQLITE_NOMEM; |
| 6302 | } |
| 6303 | } |
| 6304 | } |
| 6305 | if( rc==SQLITE_OK ){ |
| 6306 | pCtx->conchHeld = 1; |
| 6307 | |
| 6308 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 6309 | afpLockingContext *afpCtx; |
| 6310 | afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 6311 | afpCtx->dbPath = pCtx->lockProxyPath; |
| 6312 | } |
| 6313 | } else { |
| 6314 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 6315 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6316 | OSTRACE(("TAKECONCH %d %s\n", conchFile->h, |
| 6317 | rc==SQLITE_OK?"ok":"failed")); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6318 | return rc; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6319 | } while (1); /* in case we need to retry the :auto: lock file - |
| 6320 | ** we should never get here except via the 'continue' call. */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6321 | } |
| 6322 | } |
| 6323 | |
| 6324 | /* |
| 6325 | ** If pFile holds a lock on a conch file, then release that lock. |
| 6326 | */ |
| 6327 | static int proxyReleaseConch(unixFile *pFile){ |
drh | 1c5bb4d | 2010-05-10 17:29:28 +0000 | [diff] [blame] | 6328 | int rc = SQLITE_OK; /* Subroutine return code */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6329 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 6330 | unixFile *conchFile; /* Name of the conch file */ |
| 6331 | |
| 6332 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6333 | conchFile = pCtx->conchFile; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6334 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6335 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6336 | getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6337 | if( pCtx->conchHeld>0 ){ |
| 6338 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 6339 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6340 | pCtx->conchHeld = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6341 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| 6342 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6343 | return rc; |
| 6344 | } |
| 6345 | |
| 6346 | /* |
| 6347 | ** Given the name of a database file, compute the name of its conch file. |
| 6348 | ** Store the conch filename in memory obtained from sqlite3_malloc(). |
| 6349 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 6350 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 6351 | ** |
| 6352 | ** The caller is responsible for ensuring that the allocated memory |
| 6353 | ** space is eventually freed. |
| 6354 | ** |
| 6355 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 6356 | */ |
| 6357 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 6358 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6359 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6360 | char *conchPath; /* buffer in which to construct conch name */ |
| 6361 | |
| 6362 | /* Allocate space for the conch filename and initialize the name to |
| 6363 | ** the name of the original database file. */ |
| 6364 | *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8); |
| 6365 | if( conchPath==0 ){ |
| 6366 | return SQLITE_NOMEM; |
| 6367 | } |
| 6368 | memcpy(conchPath, dbPath, len+1); |
| 6369 | |
| 6370 | /* now insert a "." before the last / character */ |
| 6371 | for( i=(len-1); i>=0; i-- ){ |
| 6372 | if( conchPath[i]=='/' ){ |
| 6373 | i++; |
| 6374 | break; |
| 6375 | } |
| 6376 | } |
| 6377 | conchPath[i]='.'; |
| 6378 | while ( i<len ){ |
| 6379 | conchPath[i+1]=dbPath[i]; |
| 6380 | i++; |
| 6381 | } |
| 6382 | |
| 6383 | /* append the "-conch" suffix to the file */ |
| 6384 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6385 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6386 | |
| 6387 | return SQLITE_OK; |
| 6388 | } |
| 6389 | |
| 6390 | |
| 6391 | /* Takes a fully configured proxy locking-style unix file and switches |
| 6392 | ** the local lock file path |
| 6393 | */ |
| 6394 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 6395 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 6396 | char *oldPath = pCtx->lockProxyPath; |
| 6397 | int rc = SQLITE_OK; |
| 6398 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6399 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6400 | return SQLITE_BUSY; |
| 6401 | } |
| 6402 | |
| 6403 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 6404 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 6405 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 6406 | return SQLITE_OK; |
| 6407 | }else{ |
| 6408 | unixFile *lockProxy = pCtx->lockProxy; |
| 6409 | pCtx->lockProxy=NULL; |
| 6410 | pCtx->conchHeld = 0; |
| 6411 | if( lockProxy!=NULL ){ |
| 6412 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 6413 | if( rc ) return rc; |
| 6414 | sqlite3_free(lockProxy); |
| 6415 | } |
| 6416 | sqlite3_free(oldPath); |
| 6417 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 6418 | } |
| 6419 | |
| 6420 | return rc; |
| 6421 | } |
| 6422 | |
| 6423 | /* |
| 6424 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 6425 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 6426 | ** |
| 6427 | ** This routine find the filename associated with pFile and writes it |
| 6428 | ** int dbPath. |
| 6429 | */ |
| 6430 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6431 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6432 | if( pFile->pMethod == &afpIoMethods ){ |
| 6433 | /* afp style keeps a reference to the db path in the filePath field |
| 6434 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6435 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6436 | strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN); |
| 6437 | } else |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6438 | #endif |
| 6439 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 6440 | /* dot lock style uses the locking context to store the dot lock |
| 6441 | ** file path */ |
| 6442 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 6443 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 6444 | }else{ |
| 6445 | /* all other styles use the locking context to store the db file path */ |
| 6446 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6447 | strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6448 | } |
| 6449 | return SQLITE_OK; |
| 6450 | } |
| 6451 | |
| 6452 | /* |
| 6453 | ** Takes an already filled in unix file and alters it so all file locking |
| 6454 | ** will be performed on the local proxy lock file. The following fields |
| 6455 | ** are preserved in the locking context so that they can be restored and |
| 6456 | ** the unix structure properly cleaned up at close time: |
| 6457 | ** ->lockingContext |
| 6458 | ** ->pMethod |
| 6459 | */ |
| 6460 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 6461 | proxyLockingContext *pCtx; |
| 6462 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 6463 | char *lockPath=NULL; |
| 6464 | int rc = SQLITE_OK; |
| 6465 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6466 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6467 | return SQLITE_BUSY; |
| 6468 | } |
| 6469 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 6470 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 6471 | lockPath=NULL; |
| 6472 | }else{ |
| 6473 | lockPath=(char *)path; |
| 6474 | } |
| 6475 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6476 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
| 6477 | (lockPath ? lockPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6478 | |
| 6479 | pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 6480 | if( pCtx==0 ){ |
| 6481 | return SQLITE_NOMEM; |
| 6482 | } |
| 6483 | memset(pCtx, 0, sizeof(*pCtx)); |
| 6484 | |
| 6485 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 6486 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6487 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 6488 | if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 6489 | /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 6490 | ** (c) the file system is read-only, then enable no-locking access. |
| 6491 | ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 6492 | ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 6493 | */ |
| 6494 | struct statfs fsInfo; |
| 6495 | struct stat conchInfo; |
| 6496 | int goLockless = 0; |
| 6497 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6498 | if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6499 | int err = errno; |
| 6500 | if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 6501 | goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 6502 | } |
| 6503 | } |
| 6504 | if( goLockless ){ |
| 6505 | pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 6506 | rc = SQLITE_OK; |
| 6507 | } |
| 6508 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6509 | } |
| 6510 | if( rc==SQLITE_OK && lockPath ){ |
| 6511 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 6512 | } |
| 6513 | |
| 6514 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6515 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 6516 | if( pCtx->dbPath==NULL ){ |
| 6517 | rc = SQLITE_NOMEM; |
| 6518 | } |
| 6519 | } |
| 6520 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6521 | /* all memory is allocated, proxys are created and assigned, |
| 6522 | ** switch the locking context and pMethod then return. |
| 6523 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6524 | pCtx->oldLockingContext = pFile->lockingContext; |
| 6525 | pFile->lockingContext = pCtx; |
| 6526 | pCtx->pOldMethod = pFile->pMethod; |
| 6527 | pFile->pMethod = &proxyIoMethods; |
| 6528 | }else{ |
| 6529 | if( pCtx->conchFile ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6530 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6531 | sqlite3_free(pCtx->conchFile); |
| 6532 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 6533 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6534 | sqlite3_free(pCtx->conchFilePath); |
| 6535 | sqlite3_free(pCtx); |
| 6536 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6537 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 6538 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6539 | return rc; |
| 6540 | } |
| 6541 | |
| 6542 | |
| 6543 | /* |
| 6544 | ** This routine handles sqlite3_file_control() calls that are specific |
| 6545 | ** to proxy locking. |
| 6546 | */ |
| 6547 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 6548 | switch( op ){ |
| 6549 | case SQLITE_GET_LOCKPROXYFILE: { |
| 6550 | unixFile *pFile = (unixFile*)id; |
| 6551 | if( pFile->pMethod == &proxyIoMethods ){ |
| 6552 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 6553 | proxyTakeConch(pFile); |
| 6554 | if( pCtx->lockProxyPath ){ |
| 6555 | *(const char **)pArg = pCtx->lockProxyPath; |
| 6556 | }else{ |
| 6557 | *(const char **)pArg = ":auto: (not held)"; |
| 6558 | } |
| 6559 | } else { |
| 6560 | *(const char **)pArg = NULL; |
| 6561 | } |
| 6562 | return SQLITE_OK; |
| 6563 | } |
| 6564 | case SQLITE_SET_LOCKPROXYFILE: { |
| 6565 | unixFile *pFile = (unixFile*)id; |
| 6566 | int rc = SQLITE_OK; |
| 6567 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 6568 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 6569 | if( isProxyStyle ){ |
| 6570 | /* turn off proxy locking - not supported */ |
| 6571 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 6572 | }else{ |
| 6573 | /* turn off proxy locking - already off - NOOP */ |
| 6574 | rc = SQLITE_OK; |
| 6575 | } |
| 6576 | }else{ |
| 6577 | const char *proxyPath = (const char *)pArg; |
| 6578 | if( isProxyStyle ){ |
| 6579 | proxyLockingContext *pCtx = |
| 6580 | (proxyLockingContext*)pFile->lockingContext; |
| 6581 | if( !strcmp(pArg, ":auto:") |
| 6582 | || (pCtx->lockProxyPath && |
| 6583 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 6584 | ){ |
| 6585 | rc = SQLITE_OK; |
| 6586 | }else{ |
| 6587 | rc = switchLockProxyPath(pFile, proxyPath); |
| 6588 | } |
| 6589 | }else{ |
| 6590 | /* turn on proxy file locking */ |
| 6591 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 6592 | } |
| 6593 | } |
| 6594 | return rc; |
| 6595 | } |
| 6596 | default: { |
| 6597 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 6598 | } |
| 6599 | } |
| 6600 | /*NOTREACHED*/ |
| 6601 | return SQLITE_ERROR; |
| 6602 | } |
| 6603 | |
| 6604 | /* |
| 6605 | ** Within this division (the proxying locking implementation) the procedures |
| 6606 | ** above this point are all utilities. The lock-related methods of the |
| 6607 | ** proxy-locking sqlite3_io_method object follow. |
| 6608 | */ |
| 6609 | |
| 6610 | |
| 6611 | /* |
| 6612 | ** This routine checks if there is a RESERVED lock held on the specified |
| 6613 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 6614 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 6615 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 6616 | */ |
| 6617 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 6618 | unixFile *pFile = (unixFile*)id; |
| 6619 | int rc = proxyTakeConch(pFile); |
| 6620 | if( rc==SQLITE_OK ){ |
| 6621 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6622 | if( pCtx->conchHeld>0 ){ |
| 6623 | unixFile *proxy = pCtx->lockProxy; |
| 6624 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 6625 | }else{ /* conchHeld < 0 is lockless */ |
| 6626 | pResOut=0; |
| 6627 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6628 | } |
| 6629 | return rc; |
| 6630 | } |
| 6631 | |
| 6632 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6633 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6634 | ** of the following: |
| 6635 | ** |
| 6636 | ** (1) SHARED_LOCK |
| 6637 | ** (2) RESERVED_LOCK |
| 6638 | ** (3) PENDING_LOCK |
| 6639 | ** (4) EXCLUSIVE_LOCK |
| 6640 | ** |
| 6641 | ** Sometimes when requesting one lock state, additional lock states |
| 6642 | ** are inserted in between. The locking might fail on one of the later |
| 6643 | ** transitions leaving the lock state different from what it started but |
| 6644 | ** still short of its goal. The following chart shows the allowed |
| 6645 | ** transitions and the inserted intermediate states: |
| 6646 | ** |
| 6647 | ** UNLOCKED -> SHARED |
| 6648 | ** SHARED -> RESERVED |
| 6649 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 6650 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 6651 | ** PENDING -> EXCLUSIVE |
| 6652 | ** |
| 6653 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 6654 | ** routine to lower a locking level. |
| 6655 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6656 | static int proxyLock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6657 | unixFile *pFile = (unixFile*)id; |
| 6658 | int rc = proxyTakeConch(pFile); |
| 6659 | if( rc==SQLITE_OK ){ |
| 6660 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6661 | if( pCtx->conchHeld>0 ){ |
| 6662 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6663 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); |
| 6664 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6665 | }else{ |
| 6666 | /* conchHeld < 0 is lockless */ |
| 6667 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6668 | } |
| 6669 | return rc; |
| 6670 | } |
| 6671 | |
| 6672 | |
| 6673 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6674 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6675 | ** must be either NO_LOCK or SHARED_LOCK. |
| 6676 | ** |
| 6677 | ** If the locking level of the file descriptor is already at or below |
| 6678 | ** the requested locking level, this routine is a no-op. |
| 6679 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6680 | static int proxyUnlock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6681 | unixFile *pFile = (unixFile*)id; |
| 6682 | int rc = proxyTakeConch(pFile); |
| 6683 | if( rc==SQLITE_OK ){ |
| 6684 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6685 | if( pCtx->conchHeld>0 ){ |
| 6686 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6687 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); |
| 6688 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6689 | }else{ |
| 6690 | /* conchHeld < 0 is lockless */ |
| 6691 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6692 | } |
| 6693 | return rc; |
| 6694 | } |
| 6695 | |
| 6696 | /* |
| 6697 | ** Close a file that uses proxy locks. |
| 6698 | */ |
| 6699 | static int proxyClose(sqlite3_file *id) { |
| 6700 | if( id ){ |
| 6701 | unixFile *pFile = (unixFile*)id; |
| 6702 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6703 | unixFile *lockProxy = pCtx->lockProxy; |
| 6704 | unixFile *conchFile = pCtx->conchFile; |
| 6705 | int rc = SQLITE_OK; |
| 6706 | |
| 6707 | if( lockProxy ){ |
| 6708 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 6709 | if( rc ) return rc; |
| 6710 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 6711 | if( rc ) return rc; |
| 6712 | sqlite3_free(lockProxy); |
| 6713 | pCtx->lockProxy = 0; |
| 6714 | } |
| 6715 | if( conchFile ){ |
| 6716 | if( pCtx->conchHeld ){ |
| 6717 | rc = proxyReleaseConch(pFile); |
| 6718 | if( rc ) return rc; |
| 6719 | } |
| 6720 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 6721 | if( rc ) return rc; |
| 6722 | sqlite3_free(conchFile); |
| 6723 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 6724 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6725 | sqlite3_free(pCtx->conchFilePath); |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 6726 | sqlite3DbFree(0, pCtx->dbPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6727 | /* restore the original locking context and pMethod then close it */ |
| 6728 | pFile->lockingContext = pCtx->oldLockingContext; |
| 6729 | pFile->pMethod = pCtx->pOldMethod; |
| 6730 | sqlite3_free(pCtx); |
| 6731 | return pFile->pMethod->xClose(id); |
| 6732 | } |
| 6733 | return SQLITE_OK; |
| 6734 | } |
| 6735 | |
| 6736 | |
| 6737 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6738 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6739 | /* |
| 6740 | ** The proxy locking style is intended for use with AFP filesystems. |
| 6741 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 6742 | ** restricted to MacOSX. |
| 6743 | ** |
| 6744 | ** |
| 6745 | ******************* End of the proxy lock implementation ********************** |
| 6746 | ******************************************************************************/ |
| 6747 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6748 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6749 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6750 | ** |
| 6751 | ** This routine registers all VFS implementations for unix-like operating |
| 6752 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 6753 | ** should be the only routines in this file that are visible from other |
| 6754 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6755 | ** |
| 6756 | ** This routine is called once during SQLite initialization and by a |
| 6757 | ** single thread. The memory allocation and mutex subsystems have not |
| 6758 | ** necessarily been initialized when this routine is called, and so they |
| 6759 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6760 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6761 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6762 | /* |
| 6763 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6764 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 6765 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 6766 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 6767 | ** and so we have to go through the intermediate pointer to avoid problems |
| 6768 | ** when compiling with -pedantic-errors on GCC.) |
| 6769 | ** |
| 6770 | ** 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] | 6771 | ** finder-function. The finder-function returns a pointer to the |
| 6772 | ** sqlite_io_methods object that implements the desired locking |
| 6773 | ** behaviors. See the division above that contains the IOMETHODS |
| 6774 | ** macro for addition information on finder-functions. |
| 6775 | ** |
| 6776 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 6777 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 6778 | ** more than that; it looks at the filesystem type that hosts the |
| 6779 | ** database file and tries to choose an locking method appropriate for |
| 6780 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6781 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6782 | #define UNIXVFS(VFSNAME, FINDER) { \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6783 | 3, /* iVersion */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6784 | sizeof(unixFile), /* szOsFile */ \ |
| 6785 | MAX_PATHNAME, /* mxPathname */ \ |
| 6786 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6787 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6788 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6789 | unixOpen, /* xOpen */ \ |
| 6790 | unixDelete, /* xDelete */ \ |
| 6791 | unixAccess, /* xAccess */ \ |
| 6792 | unixFullPathname, /* xFullPathname */ \ |
| 6793 | unixDlOpen, /* xDlOpen */ \ |
| 6794 | unixDlError, /* xDlError */ \ |
| 6795 | unixDlSym, /* xDlSym */ \ |
| 6796 | unixDlClose, /* xDlClose */ \ |
| 6797 | unixRandomness, /* xRandomness */ \ |
| 6798 | unixSleep, /* xSleep */ \ |
| 6799 | unixCurrentTime, /* xCurrentTime */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6800 | unixGetLastError, /* xGetLastError */ \ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6801 | unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6802 | unixSetSystemCall, /* xSetSystemCall */ \ |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 6803 | unixGetSystemCall, /* xGetSystemCall */ \ |
| 6804 | unixNextSystemCall, /* xNextSystemCall */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6805 | } |
| 6806 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6807 | /* |
| 6808 | ** All default VFSes for unix are contained in the following array. |
| 6809 | ** |
| 6810 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 6811 | ** by the SQLite core when the VFS is registered. So the following |
| 6812 | ** array cannot be const. |
| 6813 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6814 | static sqlite3_vfs aVfs[] = { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6815 | #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__)) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6816 | UNIXVFS("unix", autolockIoFinder ), |
| 6817 | #else |
| 6818 | UNIXVFS("unix", posixIoFinder ), |
| 6819 | #endif |
| 6820 | UNIXVFS("unix-none", nolockIoFinder ), |
| 6821 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 6822 | UNIXVFS("unix-excl", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6823 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6824 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6825 | #endif |
| 6826 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6827 | UNIXVFS("unix-posix", posixIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6828 | #if !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6829 | UNIXVFS("unix-flock", flockIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6830 | #endif |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6831 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6832 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6833 | UNIXVFS("unix-afp", afpIoFinder ), |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6834 | UNIXVFS("unix-nfs", nfsIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6835 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6836 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6837 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6838 | unsigned int i; /* Loop counter */ |
| 6839 | |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 6840 | /* Double-check that the aSyscall[] array has been constructed |
| 6841 | ** correctly. See ticket [bb3a86e890c8e96ab] */ |
drh | 8942d41 | 2012-01-02 18:20:14 +0000 | [diff] [blame] | 6842 | assert( ArraySize(aSyscall)==20 ); |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 6843 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6844 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6845 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6846 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6847 | } |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6848 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6849 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6850 | |
| 6851 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6852 | ** Shutdown the operating system interface. |
| 6853 | ** |
| 6854 | ** Some operating systems might need to do some cleanup in this routine, |
| 6855 | ** to release dynamically allocated objects. But not on unix. |
| 6856 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6857 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6858 | int sqlite3_os_end(void){ |
| 6859 | return SQLITE_OK; |
| 6860 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 6861 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 6862 | #endif /* SQLITE_OS_UNIX */ |