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> |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 122 | |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 123 | #if SQLITE_ENABLE_LOCKING_STYLE |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 124 | # include <sys/ioctl.h> |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 125 | # if OS_VXWORKS |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 126 | # include <semaphore.h> |
| 127 | # include <limits.h> |
| 128 | # else |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 129 | # include <sys/file.h> |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 130 | # include <sys/param.h> |
| 131 | # include <sys/mount.h> |
| 132 | # endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 133 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 134 | |
| 135 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 136 | ** If we are to be thread-safe, include the pthreads header and define |
| 137 | ** the SQLITE_UNIX_THREADS macro. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 138 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 139 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 140 | # include <pthread.h> |
| 141 | # define SQLITE_UNIX_THREADS 1 |
| 142 | #endif |
| 143 | |
| 144 | /* |
| 145 | ** Default permissions when creating a new file |
| 146 | */ |
| 147 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 148 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 149 | #endif |
| 150 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 151 | /* |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 152 | ** Default permissions when creating auto proxy dir |
| 153 | */ |
| 154 | #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 155 | # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 |
| 156 | #endif |
| 157 | |
| 158 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 159 | ** Maximum supported path-length. |
| 160 | */ |
| 161 | #define MAX_PATHNAME 512 |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 162 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 163 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 164 | ** Only set the lastErrno if the error code is a real error and not |
| 165 | ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 166 | */ |
| 167 | #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |
| 168 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 169 | |
| 170 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 171 | ** Sometimes, after a file handle is closed by SQLite, the file descriptor |
| 172 | ** cannot be closed immediately. In these cases, instances of the following |
| 173 | ** structure are used to store the file descriptor while waiting for an |
| 174 | ** opportunity to either close or reuse it. |
| 175 | */ |
| 176 | typedef struct UnixUnusedFd UnixUnusedFd; |
| 177 | struct UnixUnusedFd { |
| 178 | int fd; /* File descriptor to close */ |
| 179 | int flags; /* Flags this file descriptor was opened with */ |
| 180 | UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ |
| 181 | }; |
| 182 | |
| 183 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 184 | ** The unixFile structure is subclass of sqlite3_file specific to the unix |
| 185 | ** VFS implementations. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 186 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 187 | typedef struct unixFile unixFile; |
| 188 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 189 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 190 | struct unixOpenCnt *pOpen; /* Info about all open fd's on this inode */ |
| 191 | struct unixLockInfo *pLock; /* Info about locks on this inode */ |
| 192 | int h; /* The file descriptor */ |
| 193 | int dirfd; /* File descriptor for the directory */ |
| 194 | unsigned char locktype; /* The type of lock held on this fd */ |
| 195 | int lastErrno; /* The unix errno from the last I/O error */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 196 | void *lockingContext; /* Locking style specific state */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 197 | UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 198 | int fileFlags; /* Miscellanous flags */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 199 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 200 | int openFlags; /* The flags specified at open() */ |
| 201 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 202 | #if SQLITE_THREADSAFE && defined(__linux__) |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 203 | pthread_t tid; /* The thread that "owns" this unixFile */ |
| 204 | #endif |
| 205 | #if OS_VXWORKS |
| 206 | int isDelete; /* Delete on close if true */ |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 207 | struct vxworksFileId *pId; /* Unique file ID */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 208 | #endif |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 209 | #ifndef NDEBUG |
| 210 | /* The next group of variables are used to track whether or not the |
| 211 | ** transaction counter in bytes 24-27 of database files are updated |
| 212 | ** whenever any part of the database changes. An assertion fault will |
| 213 | ** occur if a file is updated without also updating the transaction |
| 214 | ** counter. This test is made to avoid new problems similar to the |
| 215 | ** one described by ticket #3584. |
| 216 | */ |
| 217 | unsigned char transCntrChng; /* True if the transaction counter changed */ |
| 218 | unsigned char dbUpdate; /* True if any part of database file changed */ |
| 219 | unsigned char inNormalWrite; /* True if in a normal write operation */ |
| 220 | #endif |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 221 | #ifdef SQLITE_TEST |
| 222 | /* In test mode, increase the size of this structure a bit so that |
| 223 | ** it is larger than the struct CrashFile defined in test6.c. |
| 224 | */ |
| 225 | char aPadding[32]; |
| 226 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 229 | /* |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 230 | ** The following macros define bits in unixFile.fileFlags |
| 231 | */ |
| 232 | #define SQLITE_WHOLE_FILE_LOCKING 0x0001 /* Use whole-file locking */ |
| 233 | |
| 234 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 235 | ** Include code that is common to all os_*.c files |
| 236 | */ |
| 237 | #include "os_common.h" |
| 238 | |
| 239 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 240 | ** Define various macros that are missing from some systems. |
| 241 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 242 | #ifndef O_LARGEFILE |
| 243 | # define O_LARGEFILE 0 |
| 244 | #endif |
| 245 | #ifdef SQLITE_DISABLE_LFS |
| 246 | # undef O_LARGEFILE |
| 247 | # define O_LARGEFILE 0 |
| 248 | #endif |
| 249 | #ifndef O_NOFOLLOW |
| 250 | # define O_NOFOLLOW 0 |
| 251 | #endif |
| 252 | #ifndef O_BINARY |
| 253 | # define O_BINARY 0 |
| 254 | #endif |
| 255 | |
| 256 | /* |
| 257 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 258 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 259 | ** that always succeeds. This means that locking does not occur under |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 260 | ** DJGPP. But it is DOS - what did you expect? |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 261 | */ |
| 262 | #ifdef __DJGPP__ |
| 263 | # define fcntl(A,B,C) 0 |
| 264 | #endif |
| 265 | |
| 266 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 267 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 268 | ** testing and debugging only. |
| 269 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 270 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 271 | #define threadid pthread_self() |
| 272 | #else |
| 273 | #define threadid 0 |
| 274 | #endif |
| 275 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 276 | |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 277 | /* |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 278 | ** Helper functions to obtain and relinquish the global mutex. The |
| 279 | ** global mutex is used to protect the unixOpenCnt, unixLockInfo and |
| 280 | ** vxworksFileId objects used by this file, all of which may be |
| 281 | ** shared by multiple threads. |
| 282 | ** |
| 283 | ** Function unixMutexHeld() is used to assert() that the global mutex |
| 284 | ** is held when required. This function is only used as part of assert() |
| 285 | ** statements. e.g. |
| 286 | ** |
| 287 | ** unixEnterMutex() |
| 288 | ** assert( unixMutexHeld() ); |
| 289 | ** unixEnterLeave() |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 290 | */ |
| 291 | static void unixEnterMutex(void){ |
| 292 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 293 | } |
| 294 | static void unixLeaveMutex(void){ |
| 295 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 296 | } |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 297 | #ifdef SQLITE_DEBUG |
| 298 | static int unixMutexHeld(void) { |
| 299 | return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 300 | } |
| 301 | #endif |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 302 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 303 | |
| 304 | #ifdef SQLITE_DEBUG |
| 305 | /* |
| 306 | ** Helper function for printing out trace information from debugging |
| 307 | ** binaries. This returns the string represetation of the supplied |
| 308 | ** integer lock-type. |
| 309 | */ |
| 310 | static const char *locktypeName(int locktype){ |
| 311 | switch( locktype ){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 312 | case NO_LOCK: return "NONE"; |
| 313 | case SHARED_LOCK: return "SHARED"; |
| 314 | case RESERVED_LOCK: return "RESERVED"; |
| 315 | case PENDING_LOCK: return "PENDING"; |
| 316 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 317 | } |
| 318 | return "ERROR"; |
| 319 | } |
| 320 | #endif |
| 321 | |
| 322 | #ifdef SQLITE_LOCK_TRACE |
| 323 | /* |
| 324 | ** Print out information about all locking operations. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 325 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 326 | ** This routine is used for troubleshooting locks on multithreaded |
| 327 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 328 | ** command-line option on the compiler. This code is normally |
| 329 | ** turned off. |
| 330 | */ |
| 331 | static int lockTrace(int fd, int op, struct flock *p){ |
| 332 | char *zOpName, *zType; |
| 333 | int s; |
| 334 | int savedErrno; |
| 335 | if( op==F_GETLK ){ |
| 336 | zOpName = "GETLK"; |
| 337 | }else if( op==F_SETLK ){ |
| 338 | zOpName = "SETLK"; |
| 339 | }else{ |
| 340 | s = fcntl(fd, op, p); |
| 341 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 342 | return s; |
| 343 | } |
| 344 | if( p->l_type==F_RDLCK ){ |
| 345 | zType = "RDLCK"; |
| 346 | }else if( p->l_type==F_WRLCK ){ |
| 347 | zType = "WRLCK"; |
| 348 | }else if( p->l_type==F_UNLCK ){ |
| 349 | zType = "UNLCK"; |
| 350 | }else{ |
| 351 | assert( 0 ); |
| 352 | } |
| 353 | assert( p->l_whence==SEEK_SET ); |
| 354 | s = fcntl(fd, op, p); |
| 355 | savedErrno = errno; |
| 356 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 357 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 358 | (int)p->l_pid, s); |
| 359 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 360 | struct flock l2; |
| 361 | l2 = *p; |
| 362 | fcntl(fd, F_GETLK, &l2); |
| 363 | if( l2.l_type==F_RDLCK ){ |
| 364 | zType = "RDLCK"; |
| 365 | }else if( l2.l_type==F_WRLCK ){ |
| 366 | zType = "WRLCK"; |
| 367 | }else if( l2.l_type==F_UNLCK ){ |
| 368 | zType = "UNLCK"; |
| 369 | }else{ |
| 370 | assert( 0 ); |
| 371 | } |
| 372 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 373 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 374 | } |
| 375 | errno = savedErrno; |
| 376 | return s; |
| 377 | } |
| 378 | #define fcntl lockTrace |
| 379 | #endif /* SQLITE_LOCK_TRACE */ |
| 380 | |
| 381 | |
| 382 | |
| 383 | /* |
| 384 | ** This routine translates a standard POSIX errno code into something |
| 385 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 386 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 387 | ** and a variety of "please close the file descriptor NOW" errors into |
| 388 | ** SQLITE_IOERR |
| 389 | ** |
| 390 | ** Errors during initialization of locks, or file system support for locks, |
| 391 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 392 | */ |
| 393 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
| 394 | switch (posixError) { |
| 395 | case 0: |
| 396 | return SQLITE_OK; |
| 397 | |
| 398 | case EAGAIN: |
| 399 | case ETIMEDOUT: |
| 400 | case EBUSY: |
| 401 | case EINTR: |
| 402 | case ENOLCK: |
| 403 | /* random NFS retry error, unless during file system support |
| 404 | * introspection, in which it actually means what it says */ |
| 405 | return SQLITE_BUSY; |
| 406 | |
| 407 | case EACCES: |
| 408 | /* EACCES is like EAGAIN during locking operations, but not any other time*/ |
| 409 | if( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
| 410 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 411 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 412 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){ |
| 413 | return SQLITE_BUSY; |
| 414 | } |
| 415 | /* else fall through */ |
| 416 | case EPERM: |
| 417 | return SQLITE_PERM; |
| 418 | |
| 419 | case EDEADLK: |
| 420 | return SQLITE_IOERR_BLOCKED; |
| 421 | |
| 422 | #if EOPNOTSUPP!=ENOTSUP |
| 423 | case EOPNOTSUPP: |
| 424 | /* something went terribly awry, unless during file system support |
| 425 | * introspection, in which it actually means what it says */ |
| 426 | #endif |
| 427 | #ifdef ENOTSUP |
| 428 | case ENOTSUP: |
| 429 | /* invalid fd, unless during file system support introspection, in which |
| 430 | * it actually means what it says */ |
| 431 | #endif |
| 432 | case EIO: |
| 433 | case EBADF: |
| 434 | case EINVAL: |
| 435 | case ENOTCONN: |
| 436 | case ENODEV: |
| 437 | case ENXIO: |
| 438 | case ENOENT: |
| 439 | case ESTALE: |
| 440 | case ENOSYS: |
| 441 | /* these should force the client to close the file and reconnect */ |
| 442 | |
| 443 | default: |
| 444 | return sqliteIOErr; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | |
| 449 | |
| 450 | /****************************************************************************** |
| 451 | ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 452 | ** |
| 453 | ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 454 | ** the device number and the inode number. But this does not work on VxWorks. |
| 455 | ** On VxWorks, a unique file id must be based on the canonical filename. |
| 456 | ** |
| 457 | ** A pointer to an instance of the following structure can be used as a |
| 458 | ** unique file ID in VxWorks. Each instance of this structure contains |
| 459 | ** a copy of the canonical filename. There is also a reference count. |
| 460 | ** The structure is reclaimed when the number of pointers to it drops to |
| 461 | ** zero. |
| 462 | ** |
| 463 | ** There are never very many files open at one time and lookups are not |
| 464 | ** a performance-critical path, so it is sufficient to put these |
| 465 | ** structures on a linked list. |
| 466 | */ |
| 467 | struct vxworksFileId { |
| 468 | struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 469 | int nRef; /* Number of references to this one */ |
| 470 | int nName; /* Length of the zCanonicalName[] string */ |
| 471 | char *zCanonicalName; /* Canonical filename */ |
| 472 | }; |
| 473 | |
| 474 | #if OS_VXWORKS |
| 475 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 476 | ** All unique filenames are held on a linked list headed by this |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 477 | ** variable: |
| 478 | */ |
| 479 | static struct vxworksFileId *vxworksFileList = 0; |
| 480 | |
| 481 | /* |
| 482 | ** Simplify a filename into its canonical form |
| 483 | ** by making the following changes: |
| 484 | ** |
| 485 | ** * removing any trailing and duplicate / |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 486 | ** * convert /./ into just / |
| 487 | ** * convert /A/../ where A is any simple name into just / |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 488 | ** |
| 489 | ** Changes are made in-place. Return the new name length. |
| 490 | ** |
| 491 | ** The original filename is in z[0..n-1]. Return the number of |
| 492 | ** characters in the simplified name. |
| 493 | */ |
| 494 | static int vxworksSimplifyName(char *z, int n){ |
| 495 | int i, j; |
| 496 | while( n>1 && z[n-1]=='/' ){ n--; } |
| 497 | for(i=j=0; i<n; i++){ |
| 498 | if( z[i]=='/' ){ |
| 499 | if( z[i+1]=='/' ) continue; |
| 500 | if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 501 | i += 1; |
| 502 | continue; |
| 503 | } |
| 504 | if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 505 | while( j>0 && z[j-1]!='/' ){ j--; } |
| 506 | if( j>0 ){ j--; } |
| 507 | i += 2; |
| 508 | continue; |
| 509 | } |
| 510 | } |
| 511 | z[j++] = z[i]; |
| 512 | } |
| 513 | z[j] = 0; |
| 514 | return j; |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | ** Find a unique file ID for the given absolute pathname. Return |
| 519 | ** a pointer to the vxworksFileId object. This pointer is the unique |
| 520 | ** file ID. |
| 521 | ** |
| 522 | ** The nRef field of the vxworksFileId object is incremented before |
| 523 | ** the object is returned. A new vxworksFileId object is created |
| 524 | ** and added to the global list if necessary. |
| 525 | ** |
| 526 | ** If a memory allocation error occurs, return NULL. |
| 527 | */ |
| 528 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 529 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 530 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 531 | int n; /* Length of zAbsoluteName string */ |
| 532 | |
| 533 | assert( zAbsoluteName[0]=='/' ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 534 | n = (int)strlen(zAbsoluteName); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 535 | pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) ); |
| 536 | if( pNew==0 ) return 0; |
| 537 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 538 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 539 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 540 | |
| 541 | /* Search for an existing entry that matching the canonical name. |
| 542 | ** If found, increment the reference count and return a pointer to |
| 543 | ** the existing file ID. |
| 544 | */ |
| 545 | unixEnterMutex(); |
| 546 | for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 547 | if( pCandidate->nName==n |
| 548 | && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 549 | ){ |
| 550 | sqlite3_free(pNew); |
| 551 | pCandidate->nRef++; |
| 552 | unixLeaveMutex(); |
| 553 | return pCandidate; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /* No match was found. We will make a new file ID */ |
| 558 | pNew->nRef = 1; |
| 559 | pNew->nName = n; |
| 560 | pNew->pNext = vxworksFileList; |
| 561 | vxworksFileList = pNew; |
| 562 | unixLeaveMutex(); |
| 563 | return pNew; |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | ** Decrement the reference count on a vxworksFileId object. Free |
| 568 | ** the object when the reference count reaches zero. |
| 569 | */ |
| 570 | static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 571 | unixEnterMutex(); |
| 572 | assert( pId->nRef>0 ); |
| 573 | pId->nRef--; |
| 574 | if( pId->nRef==0 ){ |
| 575 | struct vxworksFileId **pp; |
| 576 | for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 577 | assert( *pp==pId ); |
| 578 | *pp = pId->pNext; |
| 579 | sqlite3_free(pId); |
| 580 | } |
| 581 | unixLeaveMutex(); |
| 582 | } |
| 583 | #endif /* OS_VXWORKS */ |
| 584 | /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 585 | ******************************************************************************/ |
| 586 | |
| 587 | |
| 588 | /****************************************************************************** |
| 589 | *************************** Posix Advisory Locking **************************** |
| 590 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 591 | ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 592 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 593 | ** sets or clears a lock, that operation overrides any prior locks set |
| 594 | ** by the same process. It does not explicitly say so, but this implies |
| 595 | ** that it overrides locks set by the same process using a different |
| 596 | ** file descriptor. Consider this test case: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 597 | ** |
| 598 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 599 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 600 | ** |
| 601 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 602 | ** one is a hard or symbolic link to the other) then if you set |
| 603 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 604 | ** on fd2, it works. I would have expected the second lock to |
| 605 | ** fail since there was already a lock on the file due to fd1. |
| 606 | ** But not so. Since both locks came from the same process, the |
| 607 | ** second overrides the first, even though they were on different |
| 608 | ** file descriptors opened on different file names. |
| 609 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 610 | ** This means that we cannot use POSIX locks to synchronize file access |
| 611 | ** among competing threads of the same process. POSIX locks will work fine |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 612 | ** to synchronize access for threads in separate processes, but not |
| 613 | ** threads within the same process. |
| 614 | ** |
| 615 | ** To work around the problem, SQLite has to manage file locks internally |
| 616 | ** on its own. Whenever a new database is opened, we have to find the |
| 617 | ** specific inode of the database file (the inode is determined by the |
| 618 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 619 | ** and check for locks already existing on that inode. When locks are |
| 620 | ** created or removed, we have to look at our own internal record of the |
| 621 | ** locks to see if another thread has previously set a lock on that same |
| 622 | ** inode. |
| 623 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 624 | ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 625 | ** For VxWorks, we have to use the alternative unique ID system based on |
| 626 | ** canonical filename and implemented in the previous division.) |
| 627 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 628 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 629 | ** descriptor. It is now a structure that holds the integer file |
| 630 | ** descriptor and a pointer to a structure that describes the internal |
| 631 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 632 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 633 | ** point to the same locking structure. The locking structure keeps |
| 634 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 635 | ** field that tells us its internal lock status. cnt==0 means the |
| 636 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 637 | ** cnt>0 means there are cnt shared locks on the file. |
| 638 | ** |
| 639 | ** Any attempt to lock or unlock a file first checks the locking |
| 640 | ** structure. The fcntl() system call is only invoked to set a |
| 641 | ** POSIX lock if the internal lock structure transitions between |
| 642 | ** a locked and an unlocked state. |
| 643 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 644 | ** But wait: there are yet more problems with POSIX advisory locks. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 645 | ** |
| 646 | ** If you close a file descriptor that points to a file that has locks, |
| 647 | ** all locks on that file that are owned by the current process are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 648 | ** released. To work around this problem, each unixFile structure contains |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 649 | ** a pointer to an unixOpenCnt structure. There is one unixOpenCnt structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 650 | ** per open inode, which means that multiple unixFile can point to a single |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 651 | ** unixOpenCnt. When an attempt is made to close an unixFile, if there are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 652 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 653 | ** to close() the file descriptor is deferred until all of the locks clear. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 654 | ** The unixOpenCnt structure keeps a list of file descriptors that need to |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 655 | ** be closed and that list is walked (and cleared) when the last lock |
| 656 | ** clears. |
| 657 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 658 | ** Yet another problem: LinuxThreads do not play well with posix locks. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 659 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 660 | ** Many older versions of linux use the LinuxThreads library which is |
| 661 | ** not posix compliant. Under LinuxThreads, a lock created by thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 662 | ** A cannot be modified or overridden by a different thread B. |
| 663 | ** Only thread A can modify the lock. Locking behavior is correct |
| 664 | ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 665 | ** on linux - with NPTL a lock created by thread A can override locks |
| 666 | ** in thread B. But there is no way to know at compile-time which |
| 667 | ** threading library is being used. So there is no way to know at |
| 668 | ** compile-time whether or not thread A can override locks on thread B. |
| 669 | ** We have to do a run-time check to discover the behavior of the |
| 670 | ** current process. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 671 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 672 | ** On systems where thread A is unable to modify locks created by |
| 673 | ** thread B, we have to keep track of which thread created each |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 674 | ** lock. Hence there is an extra field in the key to the unixLockInfo |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 675 | ** structure to record this information. And on those systems it |
| 676 | ** is illegal to begin a transaction in one thread and finish it |
| 677 | ** in another. For this latter restriction, there is no work-around. |
| 678 | ** It is a limitation of LinuxThreads. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 679 | */ |
| 680 | |
| 681 | /* |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 682 | ** Set or check the unixFile.tid field. This field is set when an unixFile |
| 683 | ** is first opened. All subsequent uses of the unixFile verify that the |
| 684 | ** same thread is operating on the unixFile. Some operating systems do |
| 685 | ** not allow locks to be overridden by other threads and that restriction |
| 686 | ** means that sqlite3* database handles cannot be moved from one thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 687 | ** to another while locks are held. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 688 | ** |
| 689 | ** Version 3.3.1 (2006-01-15): unixFile can be moved from one thread to |
| 690 | ** another as long as we are running on a system that supports threads |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 691 | ** overriding each others locks (which is now the most common behavior) |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 692 | ** or if no locks are held. But the unixFile.pLock field needs to be |
| 693 | ** recomputed because its key includes the thread-id. See the |
| 694 | ** transferOwnership() function below for additional information |
| 695 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 696 | #if SQLITE_THREADSAFE && defined(__linux__) |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 697 | # define SET_THREADID(X) (X)->tid = pthread_self() |
| 698 | # define CHECK_THREADID(X) (threadsOverrideEachOthersLocks==0 && \ |
| 699 | !pthread_equal((X)->tid, pthread_self())) |
| 700 | #else |
| 701 | # define SET_THREADID(X) |
| 702 | # define CHECK_THREADID(X) 0 |
| 703 | #endif |
| 704 | |
| 705 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 706 | ** An instance of the following structure serves as the key used |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 707 | ** to locate a particular unixOpenCnt structure given its inode. This |
| 708 | ** is the same as the unixLockKey except that the thread ID is omitted. |
| 709 | */ |
| 710 | struct unixFileId { |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 711 | dev_t dev; /* Device number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 712 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 713 | struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 714 | #else |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 715 | ino_t ino; /* Inode number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 716 | #endif |
| 717 | }; |
| 718 | |
| 719 | /* |
| 720 | ** An instance of the following structure serves as the key used |
| 721 | ** to locate a particular unixLockInfo structure given its inode. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 722 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 723 | ** If threads cannot override each others locks (LinuxThreads), then we |
| 724 | ** set the unixLockKey.tid field to the thread ID. If threads can override |
| 725 | ** each others locks (Posix and NPTL) then tid is always set to zero. |
| 726 | ** tid is omitted if we compile without threading support or on an OS |
| 727 | ** other than linux. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 728 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 729 | struct unixLockKey { |
| 730 | struct unixFileId fid; /* Unique identifier for the file */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 731 | #if SQLITE_THREADSAFE && defined(__linux__) |
| 732 | pthread_t tid; /* Thread ID of lock owner. Zero if not using LinuxThreads */ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 733 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 734 | }; |
| 735 | |
| 736 | /* |
| 737 | ** An instance of the following structure is allocated for each open |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 738 | ** inode. Or, on LinuxThreads, there is one of these structures for |
| 739 | ** each inode opened by each thread. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 740 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 741 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 742 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 743 | ** object keeps a count of the number of unixFile pointing to it. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 744 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 745 | struct unixLockInfo { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 746 | struct unixLockKey lockKey; /* The lookup key */ |
| 747 | int cnt; /* Number of SHARED locks held */ |
| 748 | int locktype; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
| 749 | int nRef; /* Number of pointers to this structure */ |
| 750 | struct unixLockInfo *pNext; /* List of all unixLockInfo objects */ |
| 751 | struct unixLockInfo *pPrev; /* .... doubly linked */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 752 | }; |
| 753 | |
| 754 | /* |
| 755 | ** An instance of the following structure is allocated for each open |
| 756 | ** inode. This structure keeps track of the number of locks on that |
| 757 | ** inode. If a close is attempted against an inode that is holding |
| 758 | ** locks, the close is deferred until all locks clear by adding the |
| 759 | ** file descriptor to be closed to the pending list. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 760 | ** |
| 761 | ** TODO: Consider changing this so that there is only a single file |
| 762 | ** descriptor for each open file, even when it is opened multiple times. |
| 763 | ** The close() system call would only occur when the last database |
| 764 | ** using the file closes. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 765 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 766 | struct unixOpenCnt { |
| 767 | struct unixFileId fileId; /* The lookup key */ |
| 768 | int nRef; /* Number of pointers to this structure */ |
| 769 | int nLock; /* Number of outstanding locks */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 770 | UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 771 | #if OS_VXWORKS |
| 772 | sem_t *pSem; /* Named POSIX semaphore */ |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 773 | char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 774 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 775 | struct unixOpenCnt *pNext, *pPrev; /* List of all unixOpenCnt objects */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 776 | }; |
| 777 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 778 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 779 | ** Lists of all unixLockInfo and unixOpenCnt objects. These used to be hash |
| 780 | ** tables. But the number of objects is rarely more than a dozen and |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 781 | ** never exceeds a few thousand. And lookup is not on a critical |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 782 | ** path so a simple linked list will suffice. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 783 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 784 | static struct unixLockInfo *lockList = 0; |
| 785 | static struct unixOpenCnt *openList = 0; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 786 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 787 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 788 | ** This variable remembers whether or not threads can override each others |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 789 | ** locks. |
| 790 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 791 | ** 0: No. Threads cannot override each others locks. (LinuxThreads) |
| 792 | ** 1: Yes. Threads can override each others locks. (Posix & NLPT) |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 793 | ** -1: We don't know yet. |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 794 | ** |
drh | 5062d3a | 2006-01-31 23:03:35 +0000 | [diff] [blame] | 795 | ** On some systems, we know at compile-time if threads can override each |
| 796 | ** others locks. On those systems, the SQLITE_THREAD_OVERRIDE_LOCK macro |
| 797 | ** will be set appropriately. On other systems, we have to check at |
| 798 | ** runtime. On these latter systems, SQLTIE_THREAD_OVERRIDE_LOCK is |
| 799 | ** undefined. |
| 800 | ** |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 801 | ** This variable normally has file scope only. But during testing, we make |
| 802 | ** it a global so that the test code can change its value in order to verify |
| 803 | ** that the right stuff happens in either case. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 804 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 805 | #if SQLITE_THREADSAFE && defined(__linux__) |
| 806 | # ifndef SQLITE_THREAD_OVERRIDE_LOCK |
| 807 | # define SQLITE_THREAD_OVERRIDE_LOCK -1 |
| 808 | # endif |
| 809 | # ifdef SQLITE_TEST |
drh | 5062d3a | 2006-01-31 23:03:35 +0000 | [diff] [blame] | 810 | int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 811 | # else |
drh | 5062d3a | 2006-01-31 23:03:35 +0000 | [diff] [blame] | 812 | static int threadsOverrideEachOthersLocks = SQLITE_THREAD_OVERRIDE_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 813 | # endif |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 814 | #endif |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 815 | |
| 816 | /* |
| 817 | ** This structure holds information passed into individual test |
| 818 | ** threads by the testThreadLockingBehavior() routine. |
| 819 | */ |
| 820 | struct threadTestData { |
| 821 | int fd; /* File to be locked */ |
| 822 | struct flock lock; /* The locking operation */ |
| 823 | int result; /* Result of the locking operation */ |
| 824 | }; |
| 825 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 826 | #if SQLITE_THREADSAFE && defined(__linux__) |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 827 | /* |
danielk1977 | 41a6a61 | 2008-11-11 18:34:35 +0000 | [diff] [blame] | 828 | ** This function is used as the main routine for a thread launched by |
| 829 | ** testThreadLockingBehavior(). It tests whether the shared-lock obtained |
| 830 | ** by the main thread in testThreadLockingBehavior() conflicts with a |
| 831 | ** hypothetical write-lock obtained by this thread on the same file. |
| 832 | ** |
| 833 | ** The write-lock is not actually acquired, as this is not possible if |
| 834 | ** the file is open in read-only mode (see ticket #3472). |
| 835 | */ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 836 | static void *threadLockingTest(void *pArg){ |
| 837 | struct threadTestData *pData = (struct threadTestData*)pArg; |
danielk1977 | 41a6a61 | 2008-11-11 18:34:35 +0000 | [diff] [blame] | 838 | pData->result = fcntl(pData->fd, F_GETLK, &pData->lock); |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 839 | return pArg; |
| 840 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 841 | #endif /* SQLITE_THREADSAFE && defined(__linux__) */ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 842 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 843 | |
| 844 | #if SQLITE_THREADSAFE && defined(__linux__) |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 845 | /* |
| 846 | ** This procedure attempts to determine whether or not threads |
| 847 | ** can override each others locks then sets the |
| 848 | ** threadsOverrideEachOthersLocks variable appropriately. |
| 849 | */ |
danielk1977 | 4d5238f | 2006-01-27 06:32:00 +0000 | [diff] [blame] | 850 | static void testThreadLockingBehavior(int fd_orig){ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 851 | int fd; |
danielk1977 | 41a6a61 | 2008-11-11 18:34:35 +0000 | [diff] [blame] | 852 | int rc; |
| 853 | struct threadTestData d; |
| 854 | struct flock l; |
| 855 | pthread_t t; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 856 | |
| 857 | fd = dup(fd_orig); |
| 858 | if( fd<0 ) return; |
danielk1977 | 41a6a61 | 2008-11-11 18:34:35 +0000 | [diff] [blame] | 859 | memset(&l, 0, sizeof(l)); |
| 860 | l.l_type = F_RDLCK; |
| 861 | l.l_len = 1; |
| 862 | l.l_start = 0; |
| 863 | l.l_whence = SEEK_SET; |
| 864 | rc = fcntl(fd_orig, F_SETLK, &l); |
| 865 | if( rc!=0 ) return; |
| 866 | memset(&d, 0, sizeof(d)); |
| 867 | d.fd = fd; |
| 868 | d.lock = l; |
| 869 | d.lock.l_type = F_WRLCK; |
drh | 06150f9 | 2009-07-03 12:57:58 +0000 | [diff] [blame] | 870 | if( pthread_create(&t, 0, threadLockingTest, &d)==0 ){ |
| 871 | pthread_join(t, 0); |
| 872 | } |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 873 | close(fd); |
danielk1977 | 41a6a61 | 2008-11-11 18:34:35 +0000 | [diff] [blame] | 874 | if( d.result!=0 ) return; |
| 875 | threadsOverrideEachOthersLocks = (d.lock.l_type==F_UNLCK); |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 876 | } |
drh | 06150f9 | 2009-07-03 12:57:58 +0000 | [diff] [blame] | 877 | #endif /* SQLITE_THREADSAFE && defined(__linux__) */ |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 878 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 879 | /* |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 880 | ** Release a unixLockInfo structure previously allocated by findLockInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 881 | ** |
| 882 | ** The mutex entered using the unixEnterMutex() function must be held |
| 883 | ** when this function is called. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 884 | */ |
| 885 | static void releaseLockInfo(struct unixLockInfo *pLock){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 886 | assert( unixMutexHeld() ); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 887 | if( pLock ){ |
| 888 | pLock->nRef--; |
| 889 | if( pLock->nRef==0 ){ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 890 | if( pLock->pPrev ){ |
| 891 | assert( pLock->pPrev->pNext==pLock ); |
| 892 | pLock->pPrev->pNext = pLock->pNext; |
| 893 | }else{ |
| 894 | assert( lockList==pLock ); |
| 895 | lockList = pLock->pNext; |
| 896 | } |
| 897 | if( pLock->pNext ){ |
| 898 | assert( pLock->pNext->pPrev==pLock ); |
| 899 | pLock->pNext->pPrev = pLock->pPrev; |
| 900 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 901 | sqlite3_free(pLock); |
| 902 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | |
| 906 | /* |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 907 | ** Release a unixOpenCnt structure previously allocated by findLockInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 908 | ** |
| 909 | ** The mutex entered using the unixEnterMutex() function must be held |
| 910 | ** when this function is called. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 911 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 912 | static void releaseOpenCnt(struct unixOpenCnt *pOpen){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 913 | assert( unixMutexHeld() ); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 914 | if( pOpen ){ |
| 915 | pOpen->nRef--; |
| 916 | if( pOpen->nRef==0 ){ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 917 | if( pOpen->pPrev ){ |
| 918 | assert( pOpen->pPrev->pNext==pOpen ); |
| 919 | pOpen->pPrev->pNext = pOpen->pNext; |
| 920 | }else{ |
| 921 | assert( openList==pOpen ); |
| 922 | openList = pOpen->pNext; |
| 923 | } |
| 924 | if( pOpen->pNext ){ |
| 925 | assert( pOpen->pNext->pPrev==pOpen ); |
| 926 | pOpen->pNext->pPrev = pOpen->pPrev; |
| 927 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 928 | assert( !pOpen->pUnused ); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 929 | sqlite3_free(pOpen); |
| 930 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 934 | /* |
| 935 | ** Given a file descriptor, locate unixLockInfo and unixOpenCnt structures that |
| 936 | ** describes that file descriptor. Create new ones if necessary. The |
| 937 | ** return values might be uninitialized if an error occurs. |
| 938 | ** |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 939 | ** The mutex entered using the unixEnterMutex() function must be held |
| 940 | ** when this function is called. |
| 941 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 942 | ** Return an appropriate error code. |
| 943 | */ |
| 944 | static int findLockInfo( |
| 945 | unixFile *pFile, /* Unix file with file desc used in the key */ |
| 946 | struct unixLockInfo **ppLock, /* Return the unixLockInfo structure here */ |
| 947 | struct unixOpenCnt **ppOpen /* Return the unixOpenCnt structure here */ |
| 948 | ){ |
| 949 | int rc; /* System call return code */ |
| 950 | int fd; /* The file descriptor for pFile */ |
| 951 | struct unixLockKey lockKey; /* Lookup key for the unixLockInfo structure */ |
| 952 | struct unixFileId fileId; /* Lookup key for the unixOpenCnt struct */ |
| 953 | struct stat statbuf; /* Low-level file information */ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 954 | struct unixLockInfo *pLock = 0;/* Candidate unixLockInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 955 | struct unixOpenCnt *pOpen; /* Candidate unixOpenCnt object */ |
| 956 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 957 | assert( unixMutexHeld() ); |
| 958 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 959 | /* Get low-level information about the file that we can used to |
| 960 | ** create a unique name for the file. |
| 961 | */ |
| 962 | fd = pFile->h; |
| 963 | rc = fstat(fd, &statbuf); |
| 964 | if( rc!=0 ){ |
| 965 | pFile->lastErrno = errno; |
| 966 | #ifdef EOVERFLOW |
| 967 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 968 | #endif |
| 969 | return SQLITE_IOERR; |
| 970 | } |
| 971 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 972 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 973 | /* On OS X on an msdos filesystem, the inode number is reported |
| 974 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 975 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 976 | ** we always increase the file size to 1 by writing a single byte |
| 977 | ** prior to accessing the inode number. The one byte written is |
| 978 | ** an ASCII 'S' character which also happens to be the first byte |
| 979 | ** in the header of every SQLite database. In this way, if there |
| 980 | ** is a race condition such that another thread has already populated |
| 981 | ** the first page of the database, no damage is done. |
| 982 | */ |
| 983 | if( statbuf.st_size==0 ){ |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 984 | rc = write(fd, "S", 1); |
| 985 | if( rc!=1 ){ |
| 986 | return SQLITE_IOERR; |
| 987 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 988 | rc = fstat(fd, &statbuf); |
| 989 | if( rc!=0 ){ |
| 990 | pFile->lastErrno = errno; |
| 991 | return SQLITE_IOERR; |
| 992 | } |
| 993 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 994 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 995 | |
| 996 | memset(&lockKey, 0, sizeof(lockKey)); |
| 997 | lockKey.fid.dev = statbuf.st_dev; |
| 998 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 999 | lockKey.fid.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1000 | #else |
| 1001 | lockKey.fid.ino = statbuf.st_ino; |
| 1002 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1003 | #if SQLITE_THREADSAFE && defined(__linux__) |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1004 | if( threadsOverrideEachOthersLocks<0 ){ |
| 1005 | testThreadLockingBehavior(fd); |
| 1006 | } |
| 1007 | lockKey.tid = threadsOverrideEachOthersLocks ? 0 : pthread_self(); |
| 1008 | #endif |
| 1009 | fileId = lockKey.fid; |
| 1010 | if( ppLock!=0 ){ |
| 1011 | pLock = lockList; |
| 1012 | while( pLock && memcmp(&lockKey, &pLock->lockKey, sizeof(lockKey)) ){ |
| 1013 | pLock = pLock->pNext; |
| 1014 | } |
| 1015 | if( pLock==0 ){ |
| 1016 | pLock = sqlite3_malloc( sizeof(*pLock) ); |
| 1017 | if( pLock==0 ){ |
| 1018 | rc = SQLITE_NOMEM; |
| 1019 | goto exit_findlockinfo; |
| 1020 | } |
| 1021 | pLock->lockKey = lockKey; |
| 1022 | pLock->nRef = 1; |
| 1023 | pLock->cnt = 0; |
| 1024 | pLock->locktype = 0; |
| 1025 | pLock->pNext = lockList; |
| 1026 | pLock->pPrev = 0; |
| 1027 | if( lockList ) lockList->pPrev = pLock; |
| 1028 | lockList = pLock; |
| 1029 | }else{ |
| 1030 | pLock->nRef++; |
| 1031 | } |
| 1032 | *ppLock = pLock; |
| 1033 | } |
| 1034 | if( ppOpen!=0 ){ |
| 1035 | pOpen = openList; |
| 1036 | while( pOpen && memcmp(&fileId, &pOpen->fileId, sizeof(fileId)) ){ |
| 1037 | pOpen = pOpen->pNext; |
| 1038 | } |
| 1039 | if( pOpen==0 ){ |
| 1040 | pOpen = sqlite3_malloc( sizeof(*pOpen) ); |
| 1041 | if( pOpen==0 ){ |
| 1042 | releaseLockInfo(pLock); |
| 1043 | rc = SQLITE_NOMEM; |
| 1044 | goto exit_findlockinfo; |
| 1045 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1046 | memset(pOpen, 0, sizeof(*pOpen)); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1047 | pOpen->fileId = fileId; |
| 1048 | pOpen->nRef = 1; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1049 | pOpen->pNext = openList; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1050 | if( openList ) openList->pPrev = pOpen; |
| 1051 | openList = pOpen; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1052 | }else{ |
| 1053 | pOpen->nRef++; |
| 1054 | } |
| 1055 | *ppOpen = pOpen; |
| 1056 | } |
| 1057 | |
| 1058 | exit_findlockinfo: |
| 1059 | return rc; |
| 1060 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1061 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1062 | /* |
| 1063 | ** If we are currently in a different thread than the thread that the |
| 1064 | ** unixFile argument belongs to, then transfer ownership of the unixFile |
| 1065 | ** over to the current thread. |
| 1066 | ** |
| 1067 | ** A unixFile is only owned by a thread on systems that use LinuxThreads. |
| 1068 | ** |
| 1069 | ** Ownership transfer is only allowed if the unixFile is currently unlocked. |
| 1070 | ** If the unixFile is locked and an ownership is wrong, then return |
| 1071 | ** SQLITE_MISUSE. SQLITE_OK is returned if everything works. |
| 1072 | */ |
| 1073 | #if SQLITE_THREADSAFE && defined(__linux__) |
| 1074 | static int transferOwnership(unixFile *pFile){ |
| 1075 | int rc; |
| 1076 | pthread_t hSelf; |
| 1077 | if( threadsOverrideEachOthersLocks ){ |
| 1078 | /* Ownership transfers not needed on this system */ |
| 1079 | return SQLITE_OK; |
| 1080 | } |
| 1081 | hSelf = pthread_self(); |
| 1082 | if( pthread_equal(pFile->tid, hSelf) ){ |
| 1083 | /* We are still in the same thread */ |
| 1084 | OSTRACE1("No-transfer, same thread\n"); |
| 1085 | return SQLITE_OK; |
| 1086 | } |
| 1087 | if( pFile->locktype!=NO_LOCK ){ |
| 1088 | /* We cannot change ownership while we are holding a lock! */ |
| 1089 | return SQLITE_MISUSE; |
| 1090 | } |
| 1091 | OSTRACE4("Transfer ownership of %d from %d to %d\n", |
| 1092 | pFile->h, pFile->tid, hSelf); |
| 1093 | pFile->tid = hSelf; |
| 1094 | if (pFile->pLock != NULL) { |
| 1095 | releaseLockInfo(pFile->pLock); |
| 1096 | rc = findLockInfo(pFile, &pFile->pLock, 0); |
| 1097 | OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h, |
| 1098 | locktypeName(pFile->locktype), |
| 1099 | locktypeName(pFile->pLock->locktype), pFile->pLock->cnt); |
| 1100 | return rc; |
| 1101 | } else { |
| 1102 | return SQLITE_OK; |
| 1103 | } |
| 1104 | } |
| 1105 | #else /* if not SQLITE_THREADSAFE */ |
| 1106 | /* On single-threaded builds, ownership transfer is a no-op */ |
| 1107 | # define transferOwnership(X) SQLITE_OK |
| 1108 | #endif /* SQLITE_THREADSAFE */ |
| 1109 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1110 | |
| 1111 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1112 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1113 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1114 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1115 | ** 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] | 1116 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1117 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1118 | int rc = SQLITE_OK; |
| 1119 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1120 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1121 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1122 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1123 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1124 | assert( pFile ); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1125 | unixEnterMutex(); /* Because pFile->pLock is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1126 | |
| 1127 | /* Check if a thread in this process holds such a lock */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1128 | if( pFile->pLock->locktype>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1129 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1132 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1133 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1134 | #ifndef __DJGPP__ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1135 | if( !reserved ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1136 | struct flock lock; |
| 1137 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1138 | lock.l_start = RESERVED_BYTE; |
| 1139 | lock.l_len = 1; |
| 1140 | lock.l_type = F_WRLCK; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1141 | if (-1 == fcntl(pFile->h, F_GETLK, &lock)) { |
| 1142 | int tErrno = errno; |
| 1143 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 1144 | pFile->lastErrno = tErrno; |
| 1145 | } else if( lock.l_type!=F_UNLCK ){ |
| 1146 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1147 | } |
| 1148 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1149 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1150 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1151 | unixLeaveMutex(); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1152 | OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1153 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1154 | *pResOut = reserved; |
| 1155 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | /* |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1159 | ** Perform a file locking operation on a range of bytes in a file. |
| 1160 | ** The "op" parameter should be one of F_RDLCK, F_WRLCK, or F_UNLCK. |
| 1161 | ** Return 0 on success or -1 for failure. On failure, write the error |
| 1162 | ** code into *pErrcode. |
| 1163 | ** |
| 1164 | ** If the SQLITE_WHOLE_FILE_LOCKING bit is clear, then only lock |
| 1165 | ** the range of bytes on the locking page between SHARED_FIRST and |
| 1166 | ** SHARED_SIZE. If SQLITE_WHOLE_FILE_LOCKING is set, then lock all |
| 1167 | ** bytes from 0 up to but not including PENDING_BYTE, and all bytes |
| 1168 | ** that follow SHARED_FIRST. |
| 1169 | ** |
| 1170 | ** In other words, of SQLITE_WHOLE_FILE_LOCKING if false (the historical |
| 1171 | ** default case) then only lock a small range of bytes from SHARED_FIRST |
| 1172 | ** through SHARED_FIRST+SHARED_SIZE-1. But if SQLITE_WHOLE_FILE_LOCKING is |
| 1173 | ** true then lock every byte in the file except for PENDING_BYTE and |
| 1174 | ** RESERVED_BYTE. |
| 1175 | ** |
| 1176 | ** SQLITE_WHOLE_FILE_LOCKING=true overlaps SQLITE_WHOLE_FILE_LOCKING=false |
| 1177 | ** and so the locking schemes are compatible. One type of lock will |
| 1178 | ** effectively exclude the other type. The reason for using the |
| 1179 | ** SQLITE_WHOLE_FILE_LOCKING=true is that by indicating the full range |
| 1180 | ** of bytes to be read or written, we give hints to NFS to help it |
| 1181 | ** maintain cache coherency. On the other hand, whole file locking |
| 1182 | ** is slower, so we don't want to use it except for NFS. |
| 1183 | */ |
| 1184 | static int rangeLock(unixFile *pFile, int op, int *pErrcode){ |
| 1185 | struct flock lock; |
| 1186 | int rc; |
| 1187 | lock.l_type = op; |
| 1188 | lock.l_start = SHARED_FIRST; |
| 1189 | lock.l_whence = SEEK_SET; |
| 1190 | if( (pFile->fileFlags & SQLITE_WHOLE_FILE_LOCKING)==0 ){ |
| 1191 | lock.l_len = SHARED_SIZE; |
| 1192 | rc = fcntl(pFile->h, F_SETLK, &lock); |
| 1193 | *pErrcode = errno; |
| 1194 | }else{ |
| 1195 | lock.l_len = 0; |
| 1196 | rc = fcntl(pFile->h, F_SETLK, &lock); |
| 1197 | *pErrcode = errno; |
| 1198 | if( NEVER(op==F_UNLCK) || rc!=(-1) ){ |
| 1199 | lock.l_start = 0; |
| 1200 | lock.l_len = PENDING_BYTE; |
| 1201 | rc = fcntl(pFile->h, F_SETLK, &lock); |
| 1202 | if( ALWAYS(op!=F_UNLCK) && rc==(-1) ){ |
| 1203 | *pErrcode = errno; |
| 1204 | lock.l_type = F_UNLCK; |
| 1205 | lock.l_start = SHARED_FIRST; |
| 1206 | lock.l_len = 0; |
| 1207 | fcntl(pFile->h, F_SETLK, &lock); |
| 1208 | } |
| 1209 | } |
| 1210 | } |
| 1211 | return rc; |
| 1212 | } |
| 1213 | |
| 1214 | /* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1215 | ** Lock the file with the lock specified by parameter locktype - one |
| 1216 | ** of the following: |
| 1217 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1218 | ** (1) SHARED_LOCK |
| 1219 | ** (2) RESERVED_LOCK |
| 1220 | ** (3) PENDING_LOCK |
| 1221 | ** (4) EXCLUSIVE_LOCK |
| 1222 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1223 | ** Sometimes when requesting one lock state, additional lock states |
| 1224 | ** are inserted in between. The locking might fail on one of the later |
| 1225 | ** transitions leaving the lock state different from what it started but |
| 1226 | ** still short of its goal. The following chart shows the allowed |
| 1227 | ** transitions and the inserted intermediate states: |
| 1228 | ** |
| 1229 | ** UNLOCKED -> SHARED |
| 1230 | ** SHARED -> RESERVED |
| 1231 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1232 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1233 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1234 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1235 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1236 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1237 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1238 | static int unixLock(sqlite3_file *id, int locktype){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1239 | /* The following describes the implementation of the various locks and |
| 1240 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1241 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1242 | ** confusion with SQLite lock names). The algorithms are complicated |
| 1243 | ** slightly in order to be compatible with windows systems simultaneously |
| 1244 | ** accessing the same database file, in case that is ever required. |
| 1245 | ** |
| 1246 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1247 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1248 | ** range', a range of 510 bytes at a well known offset. |
| 1249 | ** |
| 1250 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 1251 | ** byte'. If this is successful, a random byte from the 'shared byte |
| 1252 | ** range' is read-locked and the lock on the 'pending byte' released. |
| 1253 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1254 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1255 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1256 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1257 | ** |
| 1258 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1259 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1260 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1261 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1262 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1263 | ** This property is used by the algorithm for rolling back a journal file |
| 1264 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1265 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1266 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1267 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1268 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1269 | ** within this range, this ensures that no other locks are held on the |
| 1270 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1271 | ** |
| 1272 | ** The reason a single byte cannot be used instead of the 'shared byte |
| 1273 | ** range' is that some versions of windows do not support read-locks. By |
| 1274 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 1275 | ** even if the locking primitive used is always a write-lock. |
| 1276 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1277 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1278 | unixFile *pFile = (unixFile*)id; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1279 | struct unixLockInfo *pLock = pFile->pLock; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1280 | struct flock lock; |
drh | 3f02218 | 2009-09-09 16:10:50 +0000 | [diff] [blame^] | 1281 | int s = 0; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1282 | int tErrno; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1283 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1284 | assert( pFile ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1285 | OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1286 | locktypeName(locktype), locktypeName(pFile->locktype), |
| 1287 | locktypeName(pLock->locktype), pLock->cnt , getpid()); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1288 | |
| 1289 | /* 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] | 1290 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1291 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1292 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1293 | if( pFile->locktype>=locktype ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1294 | OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1295 | locktypeName(locktype)); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1296 | return SQLITE_OK; |
| 1297 | } |
| 1298 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1299 | /* Make sure the locking sequence is correct. |
| 1300 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 1301 | ** (2) SQLite never explicitly requests a pendig lock. |
| 1302 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1303 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1304 | assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1305 | assert( locktype!=PENDING_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1306 | assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1307 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1308 | /* This mutex is needed because pFile->pLock is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1309 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1310 | unixEnterMutex(); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1311 | |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1312 | /* Make sure the current thread owns the pFile. |
| 1313 | */ |
| 1314 | rc = transferOwnership(pFile); |
| 1315 | if( rc!=SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1316 | unixLeaveMutex(); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1317 | return rc; |
| 1318 | } |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 1319 | pLock = pFile->pLock; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1320 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1321 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1322 | ** handle that precludes the requested lock, return BUSY. |
| 1323 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1324 | if( (pFile->locktype!=pLock->locktype && |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1325 | (pLock->locktype>=PENDING_LOCK || locktype>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1326 | ){ |
| 1327 | rc = SQLITE_BUSY; |
| 1328 | goto end_lock; |
| 1329 | } |
| 1330 | |
| 1331 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1332 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1333 | ** return SQLITE_OK. |
| 1334 | */ |
| 1335 | if( locktype==SHARED_LOCK && |
| 1336 | (pLock->locktype==SHARED_LOCK || pLock->locktype==RESERVED_LOCK) ){ |
| 1337 | assert( locktype==SHARED_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1338 | assert( pFile->locktype==0 ); |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1339 | assert( pLock->cnt>0 ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1340 | pFile->locktype = SHARED_LOCK; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1341 | pLock->cnt++; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1342 | pFile->pOpen->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1343 | goto end_lock; |
| 1344 | } |
| 1345 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1346 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1347 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1348 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1349 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1350 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1351 | lock.l_len = 1L; |
| 1352 | lock.l_whence = SEEK_SET; |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1353 | if( locktype==SHARED_LOCK |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1354 | || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1355 | ){ |
danielk1977 | 489468c | 2004-06-28 08:25:47 +0000 | [diff] [blame] | 1356 | lock.l_type = (locktype==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1357 | lock.l_start = PENDING_BYTE; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1358 | s = fcntl(pFile->h, F_SETLK, &lock); |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1359 | if( s==(-1) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1360 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1361 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1362 | if( IS_LOCK_ERROR(rc) ){ |
| 1363 | pFile->lastErrno = tErrno; |
| 1364 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1365 | goto end_lock; |
| 1366 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | |
| 1370 | /* If control gets to this point, then actually go ahead and make |
| 1371 | ** operating system calls for the specified lock. |
| 1372 | */ |
| 1373 | if( locktype==SHARED_LOCK ){ |
| 1374 | assert( pLock->cnt==0 ); |
| 1375 | assert( pLock->locktype==0 ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1376 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1377 | /* Now get the read-lock */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1378 | s = rangeLock(pFile, F_RDLCK, &tErrno); |
| 1379 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1380 | /* Drop the temporary PENDING lock */ |
| 1381 | lock.l_start = PENDING_BYTE; |
| 1382 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1383 | lock.l_type = F_UNLCK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1384 | if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1385 | if( s != -1 ){ |
| 1386 | /* This could happen with a network mount */ |
| 1387 | tErrno = errno; |
| 1388 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1389 | if( IS_LOCK_ERROR(rc) ){ |
| 1390 | pFile->lastErrno = tErrno; |
| 1391 | } |
| 1392 | goto end_lock; |
| 1393 | } |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1394 | } |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1395 | if( s==(-1) ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1396 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1397 | if( IS_LOCK_ERROR(rc) ){ |
| 1398 | pFile->lastErrno = tErrno; |
| 1399 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1400 | }else{ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1401 | pFile->locktype = SHARED_LOCK; |
| 1402 | pFile->pOpen->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1403 | pLock->cnt = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1404 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1405 | }else if( locktype==EXCLUSIVE_LOCK && pLock->cnt>1 ){ |
| 1406 | /* We are trying for an exclusive lock but another thread in this |
| 1407 | ** same process is still holding a shared lock. */ |
| 1408 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1409 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1410 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1411 | ** assumed that there is a SHARED or greater lock on the file |
| 1412 | ** already. |
| 1413 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1414 | assert( 0!=pFile->locktype ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1415 | lock.l_type = F_WRLCK; |
| 1416 | switch( locktype ){ |
| 1417 | case RESERVED_LOCK: |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1418 | lock.l_start = RESERVED_BYTE; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1419 | s = fcntl(pFile->h, F_SETLK, &lock); |
| 1420 | tErrno = errno; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1421 | break; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1422 | case EXCLUSIVE_LOCK: |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1423 | s = rangeLock(pFile, F_WRLCK, &tErrno); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1424 | break; |
| 1425 | default: |
| 1426 | assert(0); |
| 1427 | } |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1428 | if( s==(-1) ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1429 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1430 | if( IS_LOCK_ERROR(rc) ){ |
| 1431 | pFile->lastErrno = tErrno; |
| 1432 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1433 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1434 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1435 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1436 | |
| 1437 | #ifndef NDEBUG |
| 1438 | /* Set up the transaction-counter change checking flags when |
| 1439 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1440 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1441 | ** write operation (not a hot journal rollback). |
| 1442 | */ |
| 1443 | if( rc==SQLITE_OK |
| 1444 | && pFile->locktype<=SHARED_LOCK |
| 1445 | && locktype==RESERVED_LOCK |
| 1446 | ){ |
| 1447 | pFile->transCntrChng = 0; |
| 1448 | pFile->dbUpdate = 0; |
| 1449 | pFile->inNormalWrite = 1; |
| 1450 | } |
| 1451 | #endif |
| 1452 | |
| 1453 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1454 | if( rc==SQLITE_OK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1455 | pFile->locktype = locktype; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1456 | pLock->locktype = locktype; |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1457 | }else if( locktype==EXCLUSIVE_LOCK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1458 | pFile->locktype = PENDING_LOCK; |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1459 | pLock->locktype = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1460 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1461 | |
| 1462 | end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1463 | unixLeaveMutex(); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1464 | OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype), |
danielk1977 | 2b44485 | 2004-06-29 07:45:33 +0000 | [diff] [blame] | 1465 | rc==SQLITE_OK ? "ok" : "failed"); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1466 | return rc; |
| 1467 | } |
| 1468 | |
| 1469 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1470 | ** Close all file descriptors accumuated in the unixOpenCnt->pUnused list. |
| 1471 | ** If all such file descriptors are closed without error, the list is |
| 1472 | ** cleared and SQLITE_OK returned. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1473 | ** |
| 1474 | ** Otherwise, if an error occurs, then successfully closed file descriptor |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1475 | ** entries are removed from the list, and SQLITE_IOERR_CLOSE returned. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1476 | ** not deleted and SQLITE_IOERR_CLOSE returned. |
| 1477 | */ |
| 1478 | static int closePendingFds(unixFile *pFile){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1479 | int rc = SQLITE_OK; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1480 | struct unixOpenCnt *pOpen = pFile->pOpen; |
| 1481 | UnixUnusedFd *pError = 0; |
| 1482 | UnixUnusedFd *p; |
| 1483 | UnixUnusedFd *pNext; |
| 1484 | for(p=pOpen->pUnused; p; p=pNext){ |
| 1485 | pNext = p->pNext; |
| 1486 | if( close(p->fd) ){ |
| 1487 | pFile->lastErrno = errno; |
| 1488 | rc = SQLITE_IOERR_CLOSE; |
| 1489 | p->pNext = pError; |
| 1490 | pError = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1491 | }else{ |
| 1492 | sqlite3_free(p); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1493 | } |
| 1494 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1495 | pOpen->pUnused = pError; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1496 | return rc; |
| 1497 | } |
| 1498 | |
| 1499 | /* |
| 1500 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1501 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1502 | */ |
| 1503 | static void setPendingFd(unixFile *pFile){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1504 | struct unixOpenCnt *pOpen = pFile->pOpen; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1505 | UnixUnusedFd *p = pFile->pUnused; |
| 1506 | p->pNext = pOpen->pUnused; |
| 1507 | pOpen->pUnused = p; |
| 1508 | pFile->h = -1; |
| 1509 | pFile->pUnused = 0; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | /* |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1513 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1514 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1515 | ** |
| 1516 | ** If the locking level of the file descriptor is already at or below |
| 1517 | ** the requested locking level, this routine is a no-op. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1518 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1519 | static int unixUnlock(sqlite3_file *id, int locktype){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1520 | unixFile *pFile = (unixFile*)id; /* The open file */ |
| 1521 | struct unixLockInfo *pLock; /* Structure describing current lock state */ |
| 1522 | struct flock lock; /* Information passed into fcntl() */ |
| 1523 | int rc = SQLITE_OK; /* Return code from this interface */ |
| 1524 | int h; /* The underlying file descriptor */ |
| 1525 | int tErrno; /* Error code from system call errors */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1526 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1527 | assert( pFile ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1528 | OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype, |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1529 | pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid()); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1530 | |
| 1531 | assert( locktype<=SHARED_LOCK ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1532 | if( pFile->locktype<=locktype ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1533 | return SQLITE_OK; |
| 1534 | } |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 1535 | if( CHECK_THREADID(pFile) ){ |
| 1536 | return SQLITE_MISUSE; |
| 1537 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1538 | unixEnterMutex(); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1539 | h = pFile->h; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1540 | pLock = pFile->pLock; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1541 | assert( pLock->cnt!=0 ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1542 | if( pFile->locktype>SHARED_LOCK ){ |
| 1543 | assert( pLock->locktype==pFile->locktype ); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1544 | SimulateIOErrorBenign(1); |
| 1545 | SimulateIOError( h=(-1) ) |
| 1546 | SimulateIOErrorBenign(0); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1547 | |
| 1548 | #ifndef NDEBUG |
| 1549 | /* When reducing a lock such that other processes can start |
| 1550 | ** reading the database file again, make sure that the |
| 1551 | ** transaction counter was updated if any part of the database |
| 1552 | ** file changed. If the transaction counter is not updated, |
| 1553 | ** other connections to the same file might not realize that |
| 1554 | ** the file has changed and hence might not know to flush their |
| 1555 | ** cache. The use of a stale cache can lead to database corruption. |
| 1556 | */ |
| 1557 | assert( pFile->inNormalWrite==0 |
| 1558 | || pFile->dbUpdate==0 |
| 1559 | || pFile->transCntrChng==1 ); |
| 1560 | pFile->inNormalWrite = 0; |
| 1561 | #endif |
| 1562 | |
| 1563 | |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1564 | if( locktype==SHARED_LOCK ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1565 | if( rangeLock(pFile, F_RDLCK, &tErrno)==(-1) ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1566 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1567 | if( IS_LOCK_ERROR(rc) ){ |
| 1568 | pFile->lastErrno = tErrno; |
| 1569 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1570 | goto end_unlock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1571 | } |
| 1572 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1573 | lock.l_type = F_UNLCK; |
| 1574 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1575 | lock.l_start = PENDING_BYTE; |
| 1576 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1577 | if( fcntl(h, F_SETLK, &lock)!=(-1) ){ |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1578 | pLock->locktype = SHARED_LOCK; |
| 1579 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1580 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1581 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1582 | if( IS_LOCK_ERROR(rc) ){ |
| 1583 | pFile->lastErrno = tErrno; |
| 1584 | } |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 1585 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1586 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1587 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1588 | if( locktype==NO_LOCK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1589 | struct unixOpenCnt *pOpen; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1590 | |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1591 | /* Decrement the shared lock counter. Release the lock using an |
| 1592 | ** OS call only when all threads in this same process have released |
| 1593 | ** the lock. |
| 1594 | */ |
| 1595 | pLock->cnt--; |
| 1596 | if( pLock->cnt==0 ){ |
| 1597 | lock.l_type = F_UNLCK; |
| 1598 | lock.l_whence = SEEK_SET; |
| 1599 | lock.l_start = lock.l_len = 0L; |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1600 | SimulateIOErrorBenign(1); |
| 1601 | SimulateIOError( h=(-1) ) |
| 1602 | SimulateIOErrorBenign(0); |
| 1603 | if( fcntl(h, F_SETLK, &lock)!=(-1) ){ |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1604 | pLock->locktype = NO_LOCK; |
| 1605 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1606 | tErrno = errno; |
danielk1977 | 5ad6a88 | 2008-09-15 04:20:31 +0000 | [diff] [blame] | 1607 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1608 | if( IS_LOCK_ERROR(rc) ){ |
| 1609 | pFile->lastErrno = tErrno; |
| 1610 | } |
drh | f48f9ca | 2009-03-28 23:47:10 +0000 | [diff] [blame] | 1611 | pLock->locktype = NO_LOCK; |
| 1612 | pFile->locktype = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1613 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1616 | /* Decrement the count of locks against this same file. When the |
| 1617 | ** count reaches zero, close any other file descriptors whose close |
| 1618 | ** was deferred because of outstanding locks. |
| 1619 | */ |
danielk1977 | 64a54c5 | 2009-03-30 07:39:35 +0000 | [diff] [blame] | 1620 | pOpen = pFile->pOpen; |
| 1621 | pOpen->nLock--; |
| 1622 | assert( pOpen->nLock>=0 ); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1623 | if( pOpen->nLock==0 ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1624 | int rc2 = closePendingFds(pFile); |
| 1625 | if( rc==SQLITE_OK ){ |
| 1626 | rc = rc2; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1627 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1628 | } |
| 1629 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1630 | |
| 1631 | end_unlock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1632 | unixLeaveMutex(); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1633 | if( rc==SQLITE_OK ) pFile->locktype = locktype; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1634 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1638 | ** This function performs the parts of the "close file" operation |
| 1639 | ** common to all locking schemes. It closes the directory and file |
| 1640 | ** handles, if they are valid, and sets all fields of the unixFile |
| 1641 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1642 | ** |
| 1643 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 1644 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 1645 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1646 | */ |
| 1647 | static int closeUnixFile(sqlite3_file *id){ |
| 1648 | unixFile *pFile = (unixFile*)id; |
| 1649 | if( pFile ){ |
| 1650 | if( pFile->dirfd>=0 ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1651 | int err = close(pFile->dirfd); |
| 1652 | if( err ){ |
| 1653 | pFile->lastErrno = errno; |
| 1654 | return SQLITE_IOERR_DIR_CLOSE; |
| 1655 | }else{ |
| 1656 | pFile->dirfd=-1; |
| 1657 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1658 | } |
| 1659 | if( pFile->h>=0 ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1660 | int err = close(pFile->h); |
| 1661 | if( err ){ |
| 1662 | pFile->lastErrno = errno; |
| 1663 | return SQLITE_IOERR_CLOSE; |
| 1664 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1665 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1666 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1667 | if( pFile->pId ){ |
| 1668 | if( pFile->isDelete ){ |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1669 | unlink(pFile->pId->zCanonicalName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1670 | } |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1671 | vxworksReleaseFileId(pFile->pId); |
| 1672 | pFile->pId = 0; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1673 | } |
| 1674 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1675 | OSTRACE2("CLOSE %-3d\n", pFile->h); |
| 1676 | OpenCounter(-1); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1677 | sqlite3_free(pFile->pUnused); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1678 | memset(pFile, 0, sizeof(unixFile)); |
| 1679 | } |
| 1680 | return SQLITE_OK; |
| 1681 | } |
| 1682 | |
| 1683 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1684 | ** Close a file. |
| 1685 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1686 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1687 | int rc = SQLITE_OK; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1688 | if( id ){ |
| 1689 | unixFile *pFile = (unixFile *)id; |
| 1690 | unixUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1691 | unixEnterMutex(); |
danielk1977 | 6cb427f | 2008-06-30 10:16:04 +0000 | [diff] [blame] | 1692 | if( pFile->pOpen && pFile->pOpen->nLock ){ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1693 | /* If there are outstanding locks, do not actually close the file just |
| 1694 | ** yet because that would clear those locks. Instead, add the file |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1695 | ** descriptor to pOpen->pUnused list. It will be automatically closed |
| 1696 | ** when the last lock is cleared. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1697 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1698 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1699 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1700 | releaseLockInfo(pFile->pLock); |
| 1701 | releaseOpenCnt(pFile->pOpen); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1702 | rc = closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1703 | unixLeaveMutex(); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1704 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1705 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1708 | /************** End of the posix advisory lock implementation ***************** |
| 1709 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1710 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1711 | /****************************************************************************** |
| 1712 | ****************************** No-op Locking ********************************** |
| 1713 | ** |
| 1714 | ** Of the various locking implementations available, this is by far the |
| 1715 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 1716 | ** file for reading or writing. |
| 1717 | ** |
| 1718 | ** This locking mode is appropriate for use on read-only databases |
| 1719 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 1720 | ** also be used if the application employs some external mechanism to |
| 1721 | ** prevent simultaneous access of the same database by two or more |
| 1722 | ** database connections. But there is a serious risk of database |
| 1723 | ** corruption if this locking mode is used in situations where multiple |
| 1724 | ** database connections are accessing the same database file at the same |
| 1725 | ** time and one or more of those connections are writing. |
| 1726 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1727 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1728 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 1729 | UNUSED_PARAMETER(NotUsed); |
| 1730 | *pResOut = 0; |
| 1731 | return SQLITE_OK; |
| 1732 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1733 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1734 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1735 | return SQLITE_OK; |
| 1736 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1737 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1738 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1739 | return SQLITE_OK; |
| 1740 | } |
| 1741 | |
| 1742 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1743 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1744 | */ |
| 1745 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1746 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | /******************* End of the no-op lock implementation ********************* |
| 1750 | ******************************************************************************/ |
| 1751 | |
| 1752 | /****************************************************************************** |
| 1753 | ************************* Begin dot-file Locking ****************************** |
| 1754 | ** |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1755 | ** The dotfile locking implementation uses the existance of separate lock |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1756 | ** files in order to control access to the database. This works on just |
| 1757 | ** about every filesystem imaginable. But there are serious downsides: |
| 1758 | ** |
| 1759 | ** (1) There is zero concurrency. A single reader blocks all other |
| 1760 | ** connections from reading or writing the database. |
| 1761 | ** |
| 1762 | ** (2) An application crash or power loss can leave stale lock files |
| 1763 | ** sitting around that need to be cleared manually. |
| 1764 | ** |
| 1765 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 1766 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1767 | ** |
| 1768 | ** Dotfile locking works by creating a file in the same directory as the |
| 1769 | ** database and with the same name but with a ".lock" extension added. |
| 1770 | ** The existance of a lock file implies an EXCLUSIVE lock. All other lock |
| 1771 | ** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1772 | */ |
| 1773 | |
| 1774 | /* |
| 1775 | ** The file suffix added to the data base filename in order to create the |
| 1776 | ** lock file. |
| 1777 | */ |
| 1778 | #define DOTLOCK_SUFFIX ".lock" |
| 1779 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1780 | /* |
| 1781 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1782 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1783 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1784 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1785 | ** |
| 1786 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 1787 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 1788 | ** is held on the file and false if the file is unlocked. |
| 1789 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1790 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 1791 | int rc = SQLITE_OK; |
| 1792 | int reserved = 0; |
| 1793 | unixFile *pFile = (unixFile*)id; |
| 1794 | |
| 1795 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1796 | |
| 1797 | assert( pFile ); |
| 1798 | |
| 1799 | /* Check if a thread in this process holds such a lock */ |
| 1800 | if( pFile->locktype>SHARED_LOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1801 | /* Either this connection or some other connection in the same process |
| 1802 | ** holds a lock on the file. No need to check further. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1803 | reserved = 1; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1804 | }else{ |
| 1805 | /* The lock is held if and only if the lockfile exists */ |
| 1806 | const char *zLockFile = (const char*)pFile->lockingContext; |
| 1807 | reserved = access(zLockFile, 0)==0; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1808 | } |
| 1809 | OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1810 | *pResOut = reserved; |
| 1811 | return rc; |
| 1812 | } |
| 1813 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1814 | /* |
| 1815 | ** Lock the file with the lock specified by parameter locktype - one |
| 1816 | ** of the following: |
| 1817 | ** |
| 1818 | ** (1) SHARED_LOCK |
| 1819 | ** (2) RESERVED_LOCK |
| 1820 | ** (3) PENDING_LOCK |
| 1821 | ** (4) EXCLUSIVE_LOCK |
| 1822 | ** |
| 1823 | ** Sometimes when requesting one lock state, additional lock states |
| 1824 | ** are inserted in between. The locking might fail on one of the later |
| 1825 | ** transitions leaving the lock state different from what it started but |
| 1826 | ** still short of its goal. The following chart shows the allowed |
| 1827 | ** transitions and the inserted intermediate states: |
| 1828 | ** |
| 1829 | ** UNLOCKED -> SHARED |
| 1830 | ** SHARED -> RESERVED |
| 1831 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1832 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1833 | ** PENDING -> EXCLUSIVE |
| 1834 | ** |
| 1835 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1836 | ** routine to lower a locking level. |
| 1837 | ** |
| 1838 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 1839 | ** But we track the other locking levels internally. |
| 1840 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1841 | static int dotlockLock(sqlite3_file *id, int locktype) { |
| 1842 | unixFile *pFile = (unixFile*)id; |
| 1843 | int fd; |
| 1844 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1845 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1846 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1847 | |
| 1848 | /* If we have any lock, then the lock file already exists. All we have |
| 1849 | ** to do is adjust our internal record of the lock level. |
| 1850 | */ |
| 1851 | if( pFile->locktype > NO_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1852 | pFile->locktype = locktype; |
| 1853 | #if !OS_VXWORKS |
| 1854 | /* Always update the timestamp on the old file */ |
| 1855 | utimes(zLockFile, NULL); |
| 1856 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1857 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | /* grab an exclusive lock */ |
| 1861 | fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600); |
| 1862 | if( fd<0 ){ |
| 1863 | /* failed to open/create the file, someone else may have stolen the lock */ |
| 1864 | int tErrno = errno; |
| 1865 | if( EEXIST == tErrno ){ |
| 1866 | rc = SQLITE_BUSY; |
| 1867 | } else { |
| 1868 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1869 | if( IS_LOCK_ERROR(rc) ){ |
| 1870 | pFile->lastErrno = tErrno; |
| 1871 | } |
| 1872 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1873 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1874 | } |
| 1875 | if( close(fd) ){ |
| 1876 | pFile->lastErrno = errno; |
| 1877 | rc = SQLITE_IOERR_CLOSE; |
| 1878 | } |
| 1879 | |
| 1880 | /* got it, set the type and return ok */ |
| 1881 | pFile->locktype = locktype; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1882 | return rc; |
| 1883 | } |
| 1884 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1885 | /* |
| 1886 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
| 1887 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1888 | ** |
| 1889 | ** If the locking level of the file descriptor is already at or below |
| 1890 | ** the requested locking level, this routine is a no-op. |
| 1891 | ** |
| 1892 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 1893 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1894 | static int dotlockUnlock(sqlite3_file *id, int locktype) { |
| 1895 | unixFile *pFile = (unixFile*)id; |
| 1896 | char *zLockFile = (char *)pFile->lockingContext; |
| 1897 | |
| 1898 | assert( pFile ); |
| 1899 | OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype, |
| 1900 | pFile->locktype, getpid()); |
| 1901 | assert( locktype<=SHARED_LOCK ); |
| 1902 | |
| 1903 | /* no-op if possible */ |
| 1904 | if( pFile->locktype==locktype ){ |
| 1905 | return SQLITE_OK; |
| 1906 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1907 | |
| 1908 | /* To downgrade to shared, simply update our internal notion of the |
| 1909 | ** lock state. No need to mess with the file on disk. |
| 1910 | */ |
| 1911 | if( locktype==SHARED_LOCK ){ |
| 1912 | pFile->locktype = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1913 | return SQLITE_OK; |
| 1914 | } |
| 1915 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1916 | /* To fully unlock the database, delete the lock file */ |
| 1917 | assert( locktype==NO_LOCK ); |
| 1918 | if( unlink(zLockFile) ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 1919 | int rc = 0; |
| 1920 | int tErrno = errno; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1921 | if( ENOENT != tErrno ){ |
| 1922 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1923 | } |
| 1924 | if( IS_LOCK_ERROR(rc) ){ |
| 1925 | pFile->lastErrno = tErrno; |
| 1926 | } |
| 1927 | return rc; |
| 1928 | } |
| 1929 | pFile->locktype = NO_LOCK; |
| 1930 | return SQLITE_OK; |
| 1931 | } |
| 1932 | |
| 1933 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1934 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1935 | */ |
| 1936 | static int dotlockClose(sqlite3_file *id) { |
| 1937 | int rc; |
| 1938 | if( id ){ |
| 1939 | unixFile *pFile = (unixFile*)id; |
| 1940 | dotlockUnlock(id, NO_LOCK); |
| 1941 | sqlite3_free(pFile->lockingContext); |
| 1942 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1943 | rc = closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1944 | return rc; |
| 1945 | } |
| 1946 | /****************** End of the dot-file lock implementation ******************* |
| 1947 | ******************************************************************************/ |
| 1948 | |
| 1949 | /****************************************************************************** |
| 1950 | ************************** Begin flock Locking ******************************** |
| 1951 | ** |
| 1952 | ** Use the flock() system call to do file locking. |
| 1953 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1954 | ** flock() locking is like dot-file locking in that the various |
| 1955 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 1956 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 1957 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 1958 | ** still works when you do this, but concurrency is reduced since |
| 1959 | ** only a single process can be reading the database at a time. |
| 1960 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1961 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if |
| 1962 | ** compiling for VXWORKS. |
| 1963 | */ |
| 1964 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1965 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1966 | /* |
| 1967 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1968 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1969 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1970 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1971 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1972 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 1973 | int rc = SQLITE_OK; |
| 1974 | int reserved = 0; |
| 1975 | unixFile *pFile = (unixFile*)id; |
| 1976 | |
| 1977 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1978 | |
| 1979 | assert( pFile ); |
| 1980 | |
| 1981 | /* Check if a thread in this process holds such a lock */ |
| 1982 | if( pFile->locktype>SHARED_LOCK ){ |
| 1983 | reserved = 1; |
| 1984 | } |
| 1985 | |
| 1986 | /* Otherwise see if some other process holds it. */ |
| 1987 | if( !reserved ){ |
| 1988 | /* attempt to get the lock */ |
| 1989 | int lrc = flock(pFile->h, LOCK_EX | LOCK_NB); |
| 1990 | if( !lrc ){ |
| 1991 | /* got the lock, unlock it */ |
| 1992 | lrc = flock(pFile->h, LOCK_UN); |
| 1993 | if ( lrc ) { |
| 1994 | int tErrno = errno; |
| 1995 | /* unlock failed with an error */ |
| 1996 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1997 | if( IS_LOCK_ERROR(lrc) ){ |
| 1998 | pFile->lastErrno = tErrno; |
| 1999 | rc = lrc; |
| 2000 | } |
| 2001 | } |
| 2002 | } else { |
| 2003 | int tErrno = errno; |
| 2004 | reserved = 1; |
| 2005 | /* someone else might have it reserved */ |
| 2006 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2007 | if( IS_LOCK_ERROR(lrc) ){ |
| 2008 | pFile->lastErrno = tErrno; |
| 2009 | rc = lrc; |
| 2010 | } |
| 2011 | } |
| 2012 | } |
| 2013 | OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved); |
| 2014 | |
| 2015 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2016 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2017 | rc = SQLITE_OK; |
| 2018 | reserved=1; |
| 2019 | } |
| 2020 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2021 | *pResOut = reserved; |
| 2022 | return rc; |
| 2023 | } |
| 2024 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2025 | /* |
| 2026 | ** Lock the file with the lock specified by parameter locktype - one |
| 2027 | ** of the following: |
| 2028 | ** |
| 2029 | ** (1) SHARED_LOCK |
| 2030 | ** (2) RESERVED_LOCK |
| 2031 | ** (3) PENDING_LOCK |
| 2032 | ** (4) EXCLUSIVE_LOCK |
| 2033 | ** |
| 2034 | ** Sometimes when requesting one lock state, additional lock states |
| 2035 | ** are inserted in between. The locking might fail on one of the later |
| 2036 | ** transitions leaving the lock state different from what it started but |
| 2037 | ** still short of its goal. The following chart shows the allowed |
| 2038 | ** transitions and the inserted intermediate states: |
| 2039 | ** |
| 2040 | ** UNLOCKED -> SHARED |
| 2041 | ** SHARED -> RESERVED |
| 2042 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2043 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2044 | ** PENDING -> EXCLUSIVE |
| 2045 | ** |
| 2046 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 2047 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2048 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2049 | ** access the file. |
| 2050 | ** |
| 2051 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2052 | ** routine to lower a locking level. |
| 2053 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2054 | static int flockLock(sqlite3_file *id, int locktype) { |
| 2055 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2056 | unixFile *pFile = (unixFile*)id; |
| 2057 | |
| 2058 | assert( pFile ); |
| 2059 | |
| 2060 | /* if we already have a lock, it is exclusive. |
| 2061 | ** Just adjust level and punt on outta here. */ |
| 2062 | if (pFile->locktype > NO_LOCK) { |
| 2063 | pFile->locktype = locktype; |
| 2064 | return SQLITE_OK; |
| 2065 | } |
| 2066 | |
| 2067 | /* grab an exclusive lock */ |
| 2068 | |
| 2069 | if (flock(pFile->h, LOCK_EX | LOCK_NB)) { |
| 2070 | int tErrno = errno; |
| 2071 | /* didn't get, must be busy */ |
| 2072 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2073 | if( IS_LOCK_ERROR(rc) ){ |
| 2074 | pFile->lastErrno = tErrno; |
| 2075 | } |
| 2076 | } else { |
| 2077 | /* got it, set the type and return ok */ |
| 2078 | pFile->locktype = locktype; |
| 2079 | } |
| 2080 | OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype), |
| 2081 | rc==SQLITE_OK ? "ok" : "failed"); |
| 2082 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2083 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2084 | rc = SQLITE_BUSY; |
| 2085 | } |
| 2086 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2087 | return rc; |
| 2088 | } |
| 2089 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2090 | |
| 2091 | /* |
| 2092 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
| 2093 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2094 | ** |
| 2095 | ** If the locking level of the file descriptor is already at or below |
| 2096 | ** the requested locking level, this routine is a no-op. |
| 2097 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2098 | static int flockUnlock(sqlite3_file *id, int locktype) { |
| 2099 | unixFile *pFile = (unixFile*)id; |
| 2100 | |
| 2101 | assert( pFile ); |
| 2102 | OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype, |
| 2103 | pFile->locktype, getpid()); |
| 2104 | assert( locktype<=SHARED_LOCK ); |
| 2105 | |
| 2106 | /* no-op if possible */ |
| 2107 | if( pFile->locktype==locktype ){ |
| 2108 | return SQLITE_OK; |
| 2109 | } |
| 2110 | |
| 2111 | /* shared can just be set because we always have an exclusive */ |
| 2112 | if (locktype==SHARED_LOCK) { |
| 2113 | pFile->locktype = locktype; |
| 2114 | return SQLITE_OK; |
| 2115 | } |
| 2116 | |
| 2117 | /* no, really, unlock. */ |
| 2118 | int rc = flock(pFile->h, LOCK_UN); |
| 2119 | if (rc) { |
| 2120 | int r, tErrno = errno; |
| 2121 | r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2122 | if( IS_LOCK_ERROR(r) ){ |
| 2123 | pFile->lastErrno = tErrno; |
| 2124 | } |
| 2125 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2126 | if( (r & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2127 | r = SQLITE_BUSY; |
| 2128 | } |
| 2129 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2130 | |
| 2131 | return r; |
| 2132 | } else { |
| 2133 | pFile->locktype = NO_LOCK; |
| 2134 | return SQLITE_OK; |
| 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | /* |
| 2139 | ** Close a file. |
| 2140 | */ |
| 2141 | static int flockClose(sqlite3_file *id) { |
| 2142 | if( id ){ |
| 2143 | flockUnlock(id, NO_LOCK); |
| 2144 | } |
| 2145 | return closeUnixFile(id); |
| 2146 | } |
| 2147 | |
| 2148 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 2149 | |
| 2150 | /******************* End of the flock lock implementation ********************* |
| 2151 | ******************************************************************************/ |
| 2152 | |
| 2153 | /****************************************************************************** |
| 2154 | ************************ Begin Named Semaphore Locking ************************ |
| 2155 | ** |
| 2156 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2157 | ** |
| 2158 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 2159 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 2160 | ** the database file at a time. This reduces potential concurrency, but |
| 2161 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2162 | */ |
| 2163 | #if OS_VXWORKS |
| 2164 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2165 | /* |
| 2166 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2167 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2168 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2169 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2170 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2171 | static int semCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 2172 | int rc = SQLITE_OK; |
| 2173 | int reserved = 0; |
| 2174 | unixFile *pFile = (unixFile*)id; |
| 2175 | |
| 2176 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2177 | |
| 2178 | assert( pFile ); |
| 2179 | |
| 2180 | /* Check if a thread in this process holds such a lock */ |
| 2181 | if( pFile->locktype>SHARED_LOCK ){ |
| 2182 | reserved = 1; |
| 2183 | } |
| 2184 | |
| 2185 | /* Otherwise see if some other process holds it. */ |
| 2186 | if( !reserved ){ |
| 2187 | sem_t *pSem = pFile->pOpen->pSem; |
| 2188 | struct stat statBuf; |
| 2189 | |
| 2190 | if( sem_trywait(pSem)==-1 ){ |
| 2191 | int tErrno = errno; |
| 2192 | if( EAGAIN != tErrno ){ |
| 2193 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 2194 | pFile->lastErrno = tErrno; |
| 2195 | } else { |
| 2196 | /* someone else has the lock when we are in NO_LOCK */ |
| 2197 | reserved = (pFile->locktype < SHARED_LOCK); |
| 2198 | } |
| 2199 | }else{ |
| 2200 | /* we could have it if we want it */ |
| 2201 | sem_post(pSem); |
| 2202 | } |
| 2203 | } |
| 2204 | OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved); |
| 2205 | |
| 2206 | *pResOut = reserved; |
| 2207 | return rc; |
| 2208 | } |
| 2209 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2210 | /* |
| 2211 | ** Lock the file with the lock specified by parameter locktype - one |
| 2212 | ** of the following: |
| 2213 | ** |
| 2214 | ** (1) SHARED_LOCK |
| 2215 | ** (2) RESERVED_LOCK |
| 2216 | ** (3) PENDING_LOCK |
| 2217 | ** (4) EXCLUSIVE_LOCK |
| 2218 | ** |
| 2219 | ** Sometimes when requesting one lock state, additional lock states |
| 2220 | ** are inserted in between. The locking might fail on one of the later |
| 2221 | ** transitions leaving the lock state different from what it started but |
| 2222 | ** still short of its goal. The following chart shows the allowed |
| 2223 | ** transitions and the inserted intermediate states: |
| 2224 | ** |
| 2225 | ** UNLOCKED -> SHARED |
| 2226 | ** SHARED -> RESERVED |
| 2227 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2228 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2229 | ** PENDING -> EXCLUSIVE |
| 2230 | ** |
| 2231 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 2232 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2233 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2234 | ** access the file. |
| 2235 | ** |
| 2236 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2237 | ** routine to lower a locking level. |
| 2238 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2239 | static int semLock(sqlite3_file *id, int locktype) { |
| 2240 | unixFile *pFile = (unixFile*)id; |
| 2241 | int fd; |
| 2242 | sem_t *pSem = pFile->pOpen->pSem; |
| 2243 | int rc = SQLITE_OK; |
| 2244 | |
| 2245 | /* if we already have a lock, it is exclusive. |
| 2246 | ** Just adjust level and punt on outta here. */ |
| 2247 | if (pFile->locktype > NO_LOCK) { |
| 2248 | pFile->locktype = locktype; |
| 2249 | rc = SQLITE_OK; |
| 2250 | goto sem_end_lock; |
| 2251 | } |
| 2252 | |
| 2253 | /* lock semaphore now but bail out when already locked. */ |
| 2254 | if( sem_trywait(pSem)==-1 ){ |
| 2255 | rc = SQLITE_BUSY; |
| 2256 | goto sem_end_lock; |
| 2257 | } |
| 2258 | |
| 2259 | /* got it, set the type and return ok */ |
| 2260 | pFile->locktype = locktype; |
| 2261 | |
| 2262 | sem_end_lock: |
| 2263 | return rc; |
| 2264 | } |
| 2265 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2266 | /* |
| 2267 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
| 2268 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2269 | ** |
| 2270 | ** If the locking level of the file descriptor is already at or below |
| 2271 | ** the requested locking level, this routine is a no-op. |
| 2272 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2273 | static int semUnlock(sqlite3_file *id, int locktype) { |
| 2274 | unixFile *pFile = (unixFile*)id; |
| 2275 | sem_t *pSem = pFile->pOpen->pSem; |
| 2276 | |
| 2277 | assert( pFile ); |
| 2278 | assert( pSem ); |
| 2279 | OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype, |
| 2280 | pFile->locktype, getpid()); |
| 2281 | assert( locktype<=SHARED_LOCK ); |
| 2282 | |
| 2283 | /* no-op if possible */ |
| 2284 | if( pFile->locktype==locktype ){ |
| 2285 | return SQLITE_OK; |
| 2286 | } |
| 2287 | |
| 2288 | /* shared can just be set because we always have an exclusive */ |
| 2289 | if (locktype==SHARED_LOCK) { |
| 2290 | pFile->locktype = locktype; |
| 2291 | return SQLITE_OK; |
| 2292 | } |
| 2293 | |
| 2294 | /* no, really unlock. */ |
| 2295 | if ( sem_post(pSem)==-1 ) { |
| 2296 | int rc, tErrno = errno; |
| 2297 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2298 | if( IS_LOCK_ERROR(rc) ){ |
| 2299 | pFile->lastErrno = tErrno; |
| 2300 | } |
| 2301 | return rc; |
| 2302 | } |
| 2303 | pFile->locktype = NO_LOCK; |
| 2304 | return SQLITE_OK; |
| 2305 | } |
| 2306 | |
| 2307 | /* |
| 2308 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2309 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2310 | static int semClose(sqlite3_file *id) { |
| 2311 | if( id ){ |
| 2312 | unixFile *pFile = (unixFile*)id; |
| 2313 | semUnlock(id, NO_LOCK); |
| 2314 | assert( pFile ); |
| 2315 | unixEnterMutex(); |
| 2316 | releaseLockInfo(pFile->pLock); |
| 2317 | releaseOpenCnt(pFile->pOpen); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2318 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2319 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2320 | } |
| 2321 | return SQLITE_OK; |
| 2322 | } |
| 2323 | |
| 2324 | #endif /* OS_VXWORKS */ |
| 2325 | /* |
| 2326 | ** Named semaphore locking is only available on VxWorks. |
| 2327 | ** |
| 2328 | *************** End of the named semaphore lock implementation **************** |
| 2329 | ******************************************************************************/ |
| 2330 | |
| 2331 | |
| 2332 | /****************************************************************************** |
| 2333 | *************************** Begin AFP Locking ********************************* |
| 2334 | ** |
| 2335 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2336 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2337 | ** |
| 2338 | ** Third-party implementations of AFP are available. But this code here |
| 2339 | ** only works on OSX. |
| 2340 | */ |
| 2341 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2342 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2343 | /* |
| 2344 | ** The afpLockingContext structure contains all afp lock specific state |
| 2345 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2346 | typedef struct afpLockingContext afpLockingContext; |
| 2347 | struct afpLockingContext { |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2348 | unsigned long long sharedByte; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2349 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2350 | }; |
| 2351 | |
| 2352 | struct ByteRangeLockPB2 |
| 2353 | { |
| 2354 | unsigned long long offset; /* offset to first byte to lock */ |
| 2355 | unsigned long long length; /* nbr of bytes to lock */ |
| 2356 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2357 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2358 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2359 | int fd; /* file desc to assoc this lock with */ |
| 2360 | }; |
| 2361 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2362 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2363 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2364 | /* |
| 2365 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2366 | ** AFP filesystem. |
| 2367 | ** |
| 2368 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2369 | */ |
| 2370 | static int afpSetLock( |
| 2371 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2372 | unixFile *pFile, /* Open file descriptor on path */ |
| 2373 | unsigned long long offset, /* First byte to be locked */ |
| 2374 | unsigned long long length, /* Number of bytes to lock */ |
| 2375 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2376 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2377 | struct ByteRangeLockPB2 pb; |
| 2378 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2379 | |
| 2380 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2381 | pb.startEndFlag = 0; |
| 2382 | pb.offset = offset; |
| 2383 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2384 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2385 | |
| 2386 | OSTRACE6("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2387 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
| 2388 | offset, length); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2389 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2390 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2391 | int rc; |
| 2392 | int tErrno = errno; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2393 | OSTRACE4("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2394 | path, tErrno, strerror(tErrno)); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2395 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2396 | rc = SQLITE_BUSY; |
| 2397 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2398 | rc = sqliteErrorFromPosixError(tErrno, |
| 2399 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2400 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2401 | if( IS_LOCK_ERROR(rc) ){ |
| 2402 | pFile->lastErrno = tErrno; |
| 2403 | } |
| 2404 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2405 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2406 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2407 | } |
| 2408 | } |
| 2409 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2410 | /* |
| 2411 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2412 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2413 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2414 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2415 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2416 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2417 | int rc = SQLITE_OK; |
| 2418 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2419 | unixFile *pFile = (unixFile*)id; |
| 2420 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2421 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2422 | |
| 2423 | assert( pFile ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2424 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 2425 | |
| 2426 | /* Check if a thread in this process holds such a lock */ |
| 2427 | if( pFile->locktype>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2428 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | /* Otherwise see if some other process holds it. |
| 2432 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2433 | if( !reserved ){ |
| 2434 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2435 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2436 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2437 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2438 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2439 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2440 | } else { |
| 2441 | /* if we failed to get the lock then someone else must have it */ |
| 2442 | reserved = 1; |
| 2443 | } |
| 2444 | if( IS_LOCK_ERROR(lrc) ){ |
| 2445 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2446 | } |
| 2447 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2448 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2449 | OSTRACE4("TEST WR-LOCK %d %d %d\n", pFile->h, rc, reserved); |
| 2450 | |
| 2451 | *pResOut = reserved; |
| 2452 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2455 | /* |
| 2456 | ** Lock the file with the lock specified by parameter locktype - one |
| 2457 | ** of the following: |
| 2458 | ** |
| 2459 | ** (1) SHARED_LOCK |
| 2460 | ** (2) RESERVED_LOCK |
| 2461 | ** (3) PENDING_LOCK |
| 2462 | ** (4) EXCLUSIVE_LOCK |
| 2463 | ** |
| 2464 | ** Sometimes when requesting one lock state, additional lock states |
| 2465 | ** are inserted in between. The locking might fail on one of the later |
| 2466 | ** transitions leaving the lock state different from what it started but |
| 2467 | ** still short of its goal. The following chart shows the allowed |
| 2468 | ** transitions and the inserted intermediate states: |
| 2469 | ** |
| 2470 | ** UNLOCKED -> SHARED |
| 2471 | ** SHARED -> RESERVED |
| 2472 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2473 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2474 | ** PENDING -> EXCLUSIVE |
| 2475 | ** |
| 2476 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2477 | ** routine to lower a locking level. |
| 2478 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2479 | static int afpLock(sqlite3_file *id, int locktype){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2480 | int rc = SQLITE_OK; |
| 2481 | unixFile *pFile = (unixFile*)id; |
| 2482 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2483 | |
| 2484 | assert( pFile ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2485 | OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h, |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2486 | locktypeName(locktype), locktypeName(pFile->locktype), getpid()); |
| 2487 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2488 | /* 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] | 2489 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2490 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2491 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2492 | if( pFile->locktype>=locktype ){ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2493 | OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2494 | locktypeName(locktype)); |
| 2495 | return SQLITE_OK; |
| 2496 | } |
| 2497 | |
| 2498 | /* Make sure the locking sequence is correct |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2499 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2500 | assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |
| 2501 | assert( locktype!=PENDING_LOCK ); |
| 2502 | assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); |
| 2503 | |
| 2504 | /* This mutex is needed because pFile->pLock is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2505 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2506 | unixEnterMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2507 | |
| 2508 | /* Make sure the current thread owns the pFile. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2509 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2510 | rc = transferOwnership(pFile); |
| 2511 | if( rc!=SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2512 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2513 | return rc; |
| 2514 | } |
| 2515 | |
| 2516 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2517 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2518 | ** be released. |
| 2519 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2520 | if( locktype==SHARED_LOCK |
| 2521 | || (locktype==EXCLUSIVE_LOCK && pFile->locktype<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2522 | ){ |
| 2523 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2524 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2525 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2526 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2527 | goto afp_end_lock; |
| 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2532 | ** operating system calls for the specified lock. |
| 2533 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2534 | if( locktype==SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2535 | int lk, lrc1, lrc2, lrc1Errno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2536 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2537 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2538 | /* note that the quality of the randomness doesn't matter that much */ |
| 2539 | lk = random(); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2540 | context->sharedByte = (lk & 0x7fffffff)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2541 | lrc1 = afpSetLock(context->dbPath, pFile, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2542 | SHARED_FIRST+context->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2543 | if( IS_LOCK_ERROR(lrc1) ){ |
| 2544 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2545 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2546 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2547 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2548 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2549 | if( IS_LOCK_ERROR(lrc1) ) { |
| 2550 | pFile->lastErrno = lrc1Errno; |
| 2551 | rc = lrc1; |
| 2552 | goto afp_end_lock; |
| 2553 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 2554 | rc = lrc2; |
| 2555 | goto afp_end_lock; |
| 2556 | } else if( lrc1 != SQLITE_OK ) { |
| 2557 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2558 | } else { |
| 2559 | pFile->locktype = SHARED_LOCK; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2560 | pFile->pOpen->nLock++; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2561 | } |
| 2562 | }else{ |
| 2563 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 2564 | ** assumed that there is a SHARED or greater lock on the file |
| 2565 | ** already. |
| 2566 | */ |
| 2567 | int failed = 0; |
| 2568 | assert( 0!=pFile->locktype ); |
| 2569 | if (locktype >= RESERVED_LOCK && pFile->locktype < RESERVED_LOCK) { |
| 2570 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2571 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2572 | } |
| 2573 | if (!failed && locktype == EXCLUSIVE_LOCK) { |
| 2574 | /* Acquire an EXCLUSIVE lock */ |
| 2575 | |
| 2576 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2577 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2578 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2579 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2580 | context->sharedByte, 1, 0)) ){ |
| 2581 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2582 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2583 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2584 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2585 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2586 | SHARED_FIRST + context->sharedByte, 1, 1)) ){ |
| 2587 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 2588 | ** a critical I/O error |
| 2589 | */ |
| 2590 | rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : |
| 2591 | SQLITE_IOERR_LOCK; |
| 2592 | goto afp_end_lock; |
| 2593 | } |
| 2594 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2595 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2596 | } |
| 2597 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2598 | if( failed ){ |
| 2599 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2600 | } |
| 2601 | } |
| 2602 | |
| 2603 | if( rc==SQLITE_OK ){ |
| 2604 | pFile->locktype = locktype; |
| 2605 | }else if( locktype==EXCLUSIVE_LOCK ){ |
| 2606 | pFile->locktype = PENDING_LOCK; |
| 2607 | } |
| 2608 | |
| 2609 | afp_end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2610 | unixLeaveMutex(); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2611 | OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype), |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2612 | rc==SQLITE_OK ? "ok" : "failed"); |
| 2613 | return rc; |
| 2614 | } |
| 2615 | |
| 2616 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2617 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
| 2618 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2619 | ** |
| 2620 | ** If the locking level of the file descriptor is already at or below |
| 2621 | ** the requested locking level, this routine is a no-op. |
| 2622 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2623 | static int afpUnlock(sqlite3_file *id, int locktype) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2624 | int rc = SQLITE_OK; |
| 2625 | unixFile *pFile = (unixFile*)id; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2626 | afpLockingContext *pCtx = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2627 | |
| 2628 | assert( pFile ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2629 | OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2630 | pFile->locktype, getpid()); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2631 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2632 | assert( locktype<=SHARED_LOCK ); |
| 2633 | if( pFile->locktype<=locktype ){ |
| 2634 | return SQLITE_OK; |
| 2635 | } |
| 2636 | if( CHECK_THREADID(pFile) ){ |
| 2637 | return SQLITE_MISUSE; |
| 2638 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2639 | unixEnterMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2640 | if( pFile->locktype>SHARED_LOCK ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2641 | |
| 2642 | if( pFile->locktype==EXCLUSIVE_LOCK ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2643 | rc = afpSetLock(pCtx->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2644 | if( rc==SQLITE_OK && locktype==SHARED_LOCK ){ |
| 2645 | /* only re-establish the shared lock if necessary */ |
| 2646 | int sharedLockByte = SHARED_FIRST+pCtx->sharedByte; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2647 | rc = afpSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 1); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2648 | } |
| 2649 | } |
| 2650 | if( rc==SQLITE_OK && pFile->locktype>=PENDING_LOCK ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2651 | rc = afpSetLock(pCtx->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2652 | } |
| 2653 | if( rc==SQLITE_OK && pFile->locktype>=RESERVED_LOCK ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2654 | rc = afpSetLock(pCtx->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2655 | } |
| 2656 | }else if( locktype==NO_LOCK ){ |
| 2657 | /* clear the shared lock */ |
| 2658 | int sharedLockByte = SHARED_FIRST+pCtx->sharedByte; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2659 | rc = afpSetLock(pCtx->dbPath, pFile, sharedLockByte, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2660 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2661 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2662 | if( rc==SQLITE_OK ){ |
| 2663 | if( locktype==NO_LOCK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2664 | struct unixOpenCnt *pOpen = pFile->pOpen; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2665 | pOpen->nLock--; |
| 2666 | assert( pOpen->nLock>=0 ); |
dan | 6aa657f | 2009-08-24 18:57:58 +0000 | [diff] [blame] | 2667 | if( pOpen->nLock==0 ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2668 | rc = closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2669 | } |
| 2670 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2671 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2672 | unixLeaveMutex(); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2673 | if( rc==SQLITE_OK ){ |
| 2674 | pFile->locktype = locktype; |
| 2675 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2676 | return rc; |
| 2677 | } |
| 2678 | |
| 2679 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2680 | ** Close a file & cleanup AFP specific locking context |
| 2681 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2682 | static int afpClose(sqlite3_file *id) { |
| 2683 | if( id ){ |
| 2684 | unixFile *pFile = (unixFile*)id; |
| 2685 | afpUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2686 | unixEnterMutex(); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2687 | if( pFile->pOpen && pFile->pOpen->nLock ){ |
| 2688 | /* If there are outstanding locks, do not actually close the file just |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2689 | ** yet because that would clear those locks. Instead, add the file |
| 2690 | ** descriptor to pOpen->aPending. It will be automatically closed when |
| 2691 | ** the last lock is cleared. |
| 2692 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2693 | setPendingFd(pFile); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2694 | } |
| 2695 | releaseOpenCnt(pFile->pOpen); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2696 | sqlite3_free(pFile->lockingContext); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2697 | closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2698 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2699 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2700 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2703 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2704 | /* |
| 2705 | ** The code above is the AFP lock implementation. The code is specific |
| 2706 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2707 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 2708 | ** VFS is not available. |
| 2709 | ** |
| 2710 | ********************* End of the AFP lock implementation ********************** |
| 2711 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2712 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2713 | |
| 2714 | /****************************************************************************** |
| 2715 | **************** Non-locking sqlite3_file methods ***************************** |
| 2716 | ** |
| 2717 | ** The next division contains implementations for all methods of the |
| 2718 | ** sqlite3_file object other than the locking methods. The locking |
| 2719 | ** methods were defined in divisions above (one locking method per |
| 2720 | ** division). Those methods that are common to all locking modes |
| 2721 | ** are gather together into this division. |
| 2722 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2723 | |
| 2724 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2725 | ** Seek to the offset passed as the second argument, then read cnt |
| 2726 | ** bytes into pBuf. Return the number of bytes actually read. |
| 2727 | ** |
| 2728 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 2729 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 2730 | ** one system to another. Since SQLite does not define USE_PREAD |
| 2731 | ** any any form by default, we will not attempt to define _XOPEN_SOURCE. |
| 2732 | ** See tickets #2741 and #2681. |
| 2733 | ** |
| 2734 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 2735 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2736 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2737 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 2738 | int got; |
| 2739 | i64 newOffset; |
| 2740 | TIMER_START; |
| 2741 | #if defined(USE_PREAD) |
| 2742 | got = pread(id->h, pBuf, cnt, offset); |
| 2743 | SimulateIOError( got = -1 ); |
| 2744 | #elif defined(USE_PREAD64) |
| 2745 | got = pread64(id->h, pBuf, cnt, offset); |
| 2746 | SimulateIOError( got = -1 ); |
| 2747 | #else |
| 2748 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 2749 | SimulateIOError( newOffset-- ); |
| 2750 | if( newOffset!=offset ){ |
| 2751 | if( newOffset == -1 ){ |
| 2752 | ((unixFile*)id)->lastErrno = errno; |
| 2753 | }else{ |
| 2754 | ((unixFile*)id)->lastErrno = 0; |
| 2755 | } |
| 2756 | return -1; |
| 2757 | } |
| 2758 | got = read(id->h, pBuf, cnt); |
| 2759 | #endif |
| 2760 | TIMER_END; |
| 2761 | if( got<0 ){ |
| 2762 | ((unixFile*)id)->lastErrno = errno; |
| 2763 | } |
| 2764 | OSTRACE5("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED); |
| 2765 | return got; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2769 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 2770 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 2771 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2772 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2773 | static int unixRead( |
| 2774 | sqlite3_file *id, |
| 2775 | void *pBuf, |
| 2776 | int amt, |
| 2777 | sqlite3_int64 offset |
| 2778 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2779 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2780 | int got; |
| 2781 | assert( id ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2782 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2783 | /* If this is a database file (not a journal, master-journal or temp |
| 2784 | ** file), the bytes in the locking range should never be read or written. */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 2785 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2786 | || offset>=PENDING_BYTE+512 |
| 2787 | || offset+amt<=PENDING_BYTE |
| 2788 | ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2789 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2790 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2791 | if( got==amt ){ |
| 2792 | return SQLITE_OK; |
| 2793 | }else if( got<0 ){ |
| 2794 | /* lastErrno set by seekAndRead */ |
| 2795 | return SQLITE_IOERR_READ; |
| 2796 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2797 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2798 | /* Unread parts of the buffer must be zero-filled */ |
| 2799 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 2800 | return SQLITE_IOERR_SHORT_READ; |
| 2801 | } |
| 2802 | } |
| 2803 | |
| 2804 | /* |
| 2805 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 2806 | ** Return the number of bytes actually read. Update the offset. |
| 2807 | ** |
| 2808 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 2809 | ** is set before returning. |
| 2810 | */ |
| 2811 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
| 2812 | int got; |
| 2813 | i64 newOffset; |
| 2814 | TIMER_START; |
| 2815 | #if defined(USE_PREAD) |
| 2816 | got = pwrite(id->h, pBuf, cnt, offset); |
| 2817 | #elif defined(USE_PREAD64) |
| 2818 | got = pwrite64(id->h, pBuf, cnt, offset); |
| 2819 | #else |
| 2820 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 2821 | if( newOffset!=offset ){ |
| 2822 | if( newOffset == -1 ){ |
| 2823 | ((unixFile*)id)->lastErrno = errno; |
| 2824 | }else{ |
| 2825 | ((unixFile*)id)->lastErrno = 0; |
| 2826 | } |
| 2827 | return -1; |
| 2828 | } |
| 2829 | got = write(id->h, pBuf, cnt); |
| 2830 | #endif |
| 2831 | TIMER_END; |
| 2832 | if( got<0 ){ |
| 2833 | ((unixFile*)id)->lastErrno = errno; |
| 2834 | } |
| 2835 | |
| 2836 | OSTRACE5("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED); |
| 2837 | return got; |
| 2838 | } |
| 2839 | |
| 2840 | |
| 2841 | /* |
| 2842 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 2843 | ** or some other error code on failure. |
| 2844 | */ |
| 2845 | static int unixWrite( |
| 2846 | sqlite3_file *id, |
| 2847 | const void *pBuf, |
| 2848 | int amt, |
| 2849 | sqlite3_int64 offset |
| 2850 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2851 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2852 | int wrote = 0; |
| 2853 | assert( id ); |
| 2854 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2855 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2856 | /* If this is a database file (not a journal, master-journal or temp |
| 2857 | ** file), the bytes in the locking range should never be read or written. */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 2858 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2859 | || offset>=PENDING_BYTE+512 |
| 2860 | || offset+amt<=PENDING_BYTE |
| 2861 | ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2862 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2863 | #ifndef NDEBUG |
| 2864 | /* If we are doing a normal write to a database file (as opposed to |
| 2865 | ** doing a hot-journal rollback or a write to some file other than a |
| 2866 | ** normal database file) then record the fact that the database |
| 2867 | ** has changed. If the transaction counter is modified, record that |
| 2868 | ** fact too. |
| 2869 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2870 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2871 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 2872 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 2873 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2874 | char oldCntr[4]; |
| 2875 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 2876 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2877 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 2878 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2879 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 2880 | } |
| 2881 | } |
| 2882 | } |
| 2883 | #endif |
| 2884 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2885 | while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2886 | amt -= wrote; |
| 2887 | offset += wrote; |
| 2888 | pBuf = &((char*)pBuf)[wrote]; |
| 2889 | } |
| 2890 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 2891 | SimulateDiskfullError(( wrote=0, amt=1 )); |
| 2892 | if( amt>0 ){ |
| 2893 | if( wrote<0 ){ |
| 2894 | /* lastErrno set by seekAndWrite */ |
| 2895 | return SQLITE_IOERR_WRITE; |
| 2896 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2897 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2898 | return SQLITE_FULL; |
| 2899 | } |
| 2900 | } |
| 2901 | return SQLITE_OK; |
| 2902 | } |
| 2903 | |
| 2904 | #ifdef SQLITE_TEST |
| 2905 | /* |
| 2906 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2907 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2908 | */ |
| 2909 | int sqlite3_sync_count = 0; |
| 2910 | int sqlite3_fullsync_count = 0; |
| 2911 | #endif |
| 2912 | |
| 2913 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 2914 | ** We do not trust systems to provide a working fdatasync(). Some do. |
| 2915 | ** Others do no. To be safe, we will stick with the (slower) fsync(). |
| 2916 | ** If you know that your system does support fdatasync() correctly, |
| 2917 | ** then simply compile with -Dfdatasync=fdatasync |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2918 | */ |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 2919 | #if !defined(fdatasync) && !defined(__linux__) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2920 | # define fdatasync fsync |
| 2921 | #endif |
| 2922 | |
| 2923 | /* |
| 2924 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 2925 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 2926 | ** only available on Mac OS X. But that could change. |
| 2927 | */ |
| 2928 | #ifdef F_FULLFSYNC |
| 2929 | # define HAVE_FULLFSYNC 1 |
| 2930 | #else |
| 2931 | # define HAVE_FULLFSYNC 0 |
| 2932 | #endif |
| 2933 | |
| 2934 | |
| 2935 | /* |
| 2936 | ** The fsync() system call does not work as advertised on many |
| 2937 | ** unix systems. The following procedure is an attempt to make |
| 2938 | ** it work better. |
| 2939 | ** |
| 2940 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 2941 | ** for testing when we want to run through the test suite quickly. |
| 2942 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 2943 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 2944 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2945 | ** |
| 2946 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 2947 | ** The idea behind dataOnly is that it should only write the file content |
| 2948 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 2949 | ** unchanged since the file size is part of the inode. However, |
| 2950 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 2951 | ** file size has changed. The only real difference between fdatasync() |
| 2952 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 2953 | ** inode if the mtime or owner or other inode attributes have changed. |
| 2954 | ** We only care about the file size, not the other file attributes, so |
| 2955 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 2956 | ** So, we always use fdatasync() if it is available, regardless of |
| 2957 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2958 | */ |
| 2959 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 2960 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2961 | |
| 2962 | /* The following "ifdef/elif/else/" block has the same structure as |
| 2963 | ** the one below. It is replicated here solely to avoid cluttering |
| 2964 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 2965 | */ |
| 2966 | #ifdef SQLITE_NO_SYNC |
| 2967 | UNUSED_PARAMETER(fd); |
| 2968 | UNUSED_PARAMETER(fullSync); |
| 2969 | UNUSED_PARAMETER(dataOnly); |
| 2970 | #elif HAVE_FULLFSYNC |
| 2971 | UNUSED_PARAMETER(dataOnly); |
| 2972 | #else |
| 2973 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2974 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2975 | #endif |
| 2976 | |
| 2977 | /* Record the number of times that we do a normal fsync() and |
| 2978 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 2979 | ** gets called with the correct arguments. |
| 2980 | */ |
| 2981 | #ifdef SQLITE_TEST |
| 2982 | if( fullSync ) sqlite3_fullsync_count++; |
| 2983 | sqlite3_sync_count++; |
| 2984 | #endif |
| 2985 | |
| 2986 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 2987 | ** no-op |
| 2988 | */ |
| 2989 | #ifdef SQLITE_NO_SYNC |
| 2990 | rc = SQLITE_OK; |
| 2991 | #elif HAVE_FULLFSYNC |
| 2992 | if( fullSync ){ |
| 2993 | rc = fcntl(fd, F_FULLFSYNC, 0); |
| 2994 | }else{ |
| 2995 | rc = 1; |
| 2996 | } |
| 2997 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2998 | ** It shouldn't be possible for fullfsync to fail on the local |
| 2999 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 3000 | ** isn't supported for this file system. So, attempt an fsync |
| 3001 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 3002 | ** It'd be better to detect fullfsync support once and avoid |
| 3003 | ** the fcntl call every time sync is called. |
| 3004 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3005 | if( rc ) rc = fsync(fd); |
| 3006 | |
| 3007 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3008 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 3009 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3010 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3011 | rc = fsync(fd); |
| 3012 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3013 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3014 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 3015 | |
| 3016 | if( OS_VXWORKS && rc!= -1 ){ |
| 3017 | rc = 0; |
| 3018 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3019 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3022 | /* |
| 3023 | ** Make sure all writes to a particular file are committed to disk. |
| 3024 | ** |
| 3025 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 3026 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 3027 | ** file data is synced. |
| 3028 | ** |
| 3029 | ** Under Unix, also make sure that the directory entry for the file |
| 3030 | ** has been created by fsync-ing the directory that contains the file. |
| 3031 | ** If we do not do this and we encounter a power failure, the directory |
| 3032 | ** entry for the journal might not exist after we reboot. The next |
| 3033 | ** SQLite to access the file will not know that the journal exists (because |
| 3034 | ** the directory entry for the journal was never created) and the transaction |
| 3035 | ** will not roll back - possibly leading to database corruption. |
| 3036 | */ |
| 3037 | static int unixSync(sqlite3_file *id, int flags){ |
| 3038 | int rc; |
| 3039 | unixFile *pFile = (unixFile*)id; |
| 3040 | |
| 3041 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 3042 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 3043 | |
| 3044 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 3045 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 3046 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 3047 | ); |
| 3048 | |
| 3049 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 3050 | ** line is to test that doing so does not cause any problems. |
| 3051 | */ |
| 3052 | SimulateDiskfullError( return SQLITE_FULL ); |
| 3053 | |
| 3054 | assert( pFile ); |
| 3055 | OSTRACE2("SYNC %-3d\n", pFile->h); |
| 3056 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 3057 | SimulateIOError( rc=1 ); |
| 3058 | if( rc ){ |
| 3059 | pFile->lastErrno = errno; |
| 3060 | return SQLITE_IOERR_FSYNC; |
| 3061 | } |
| 3062 | if( pFile->dirfd>=0 ){ |
| 3063 | int err; |
| 3064 | OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd, |
| 3065 | HAVE_FULLFSYNC, isFullsync); |
| 3066 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 3067 | /* The directory sync is only attempted if full_fsync is |
| 3068 | ** turned off or unavailable. If a full_fsync occurred above, |
| 3069 | ** then the directory sync is superfluous. |
| 3070 | */ |
| 3071 | if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){ |
| 3072 | /* |
| 3073 | ** We have received multiple reports of fsync() returning |
| 3074 | ** errors when applied to directories on certain file systems. |
| 3075 | ** A failed directory sync is not a big deal. So it seems |
| 3076 | ** better to ignore the error. Ticket #1657 |
| 3077 | */ |
| 3078 | /* pFile->lastErrno = errno; */ |
| 3079 | /* return SQLITE_IOERR; */ |
| 3080 | } |
| 3081 | #endif |
| 3082 | err = close(pFile->dirfd); /* Only need to sync once, so close the */ |
| 3083 | if( err==0 ){ /* directory when we are done */ |
| 3084 | pFile->dirfd = -1; |
| 3085 | }else{ |
| 3086 | pFile->lastErrno = errno; |
| 3087 | rc = SQLITE_IOERR_DIR_CLOSE; |
| 3088 | } |
| 3089 | } |
| 3090 | return rc; |
| 3091 | } |
| 3092 | |
| 3093 | /* |
| 3094 | ** Truncate an open file to a specified size |
| 3095 | */ |
| 3096 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
| 3097 | int rc; |
| 3098 | assert( id ); |
| 3099 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
| 3100 | rc = ftruncate(((unixFile*)id)->h, (off_t)nByte); |
| 3101 | if( rc ){ |
| 3102 | ((unixFile*)id)->lastErrno = errno; |
| 3103 | return SQLITE_IOERR_TRUNCATE; |
| 3104 | }else{ |
| 3105 | return SQLITE_OK; |
| 3106 | } |
| 3107 | } |
| 3108 | |
| 3109 | /* |
| 3110 | ** Determine the current size of a file in bytes |
| 3111 | */ |
| 3112 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 3113 | int rc; |
| 3114 | struct stat buf; |
| 3115 | assert( id ); |
| 3116 | rc = fstat(((unixFile*)id)->h, &buf); |
| 3117 | SimulateIOError( rc=1 ); |
| 3118 | if( rc!=0 ){ |
| 3119 | ((unixFile*)id)->lastErrno = errno; |
| 3120 | return SQLITE_IOERR_FSTAT; |
| 3121 | } |
| 3122 | *pSize = buf.st_size; |
| 3123 | |
| 3124 | /* When opening a zero-size database, the findLockInfo() procedure |
| 3125 | ** writes a single byte into that file in order to work around a bug |
| 3126 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 3127 | ** layers, we need to report this file size as zero even though it is |
| 3128 | ** really 1. Ticket #3260. |
| 3129 | */ |
| 3130 | if( *pSize==1 ) *pSize = 0; |
| 3131 | |
| 3132 | |
| 3133 | return SQLITE_OK; |
| 3134 | } |
| 3135 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3136 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3137 | /* |
| 3138 | ** Handler for proxy-locking file-control verbs. Defined below in the |
| 3139 | ** proxying locking division. |
| 3140 | */ |
| 3141 | static int proxyFileControl(sqlite3_file*,int,void*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3142 | #endif |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3143 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3144 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3145 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3146 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3147 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3148 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3149 | switch( op ){ |
| 3150 | case SQLITE_FCNTL_LOCKSTATE: { |
| 3151 | *(int*)pArg = ((unixFile*)id)->locktype; |
| 3152 | return SQLITE_OK; |
| 3153 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3154 | case SQLITE_LAST_ERRNO: { |
| 3155 | *(int*)pArg = ((unixFile*)id)->lastErrno; |
| 3156 | return SQLITE_OK; |
| 3157 | } |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3158 | #ifndef NDEBUG |
| 3159 | /* The pager calls this method to signal that it has done |
| 3160 | ** a rollback and that the database is therefore unchanged and |
| 3161 | ** it hence it is OK for the transaction change counter to be |
| 3162 | ** unchanged. |
| 3163 | */ |
| 3164 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 3165 | ((unixFile*)id)->dbUpdate = 0; |
| 3166 | return SQLITE_OK; |
| 3167 | } |
| 3168 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3169 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3170 | case SQLITE_SET_LOCKPROXYFILE: |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3171 | case SQLITE_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3172 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3173 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3174 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3175 | } |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3176 | return SQLITE_ERROR; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | /* |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3180 | ** Return the sector size in bytes of the underlying block device for |
| 3181 | ** the specified file. This is almost always 512 bytes, but may be |
| 3182 | ** larger for some devices. |
| 3183 | ** |
| 3184 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 3185 | ** if two files are created in the same file-system directory (i.e. |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3186 | ** a database and its journal file) that the sector size will be the |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3187 | ** same for both. |
| 3188 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3189 | static int unixSectorSize(sqlite3_file *NotUsed){ |
| 3190 | UNUSED_PARAMETER(NotUsed); |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 3191 | return SQLITE_DEFAULT_SECTOR_SIZE; |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3192 | } |
| 3193 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3194 | /* |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3195 | ** Return the device characteristics for the file. This is always 0 for unix. |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3196 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3197 | static int unixDeviceCharacteristics(sqlite3_file *NotUsed){ |
| 3198 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3199 | return 0; |
| 3200 | } |
| 3201 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3202 | /* |
| 3203 | ** Here ends the implementation of all sqlite3_file methods. |
| 3204 | ** |
| 3205 | ********************** End sqlite3_file Methods ******************************* |
| 3206 | ******************************************************************************/ |
| 3207 | |
| 3208 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3209 | ** This division contains definitions of sqlite3_io_methods objects that |
| 3210 | ** implement various file locking strategies. It also contains definitions |
| 3211 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 3212 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 3213 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 3214 | ** the correct finder-function for that VFS. |
| 3215 | ** |
| 3216 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 3217 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 3218 | ** looks at the filesystem type and tries to guess the best locking |
| 3219 | ** strategy from that. |
| 3220 | ** |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3221 | ** For finder-funtion F, two objects are created: |
| 3222 | ** |
| 3223 | ** (1) The real finder-function named "FImpt()". |
| 3224 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3225 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3226 | ** |
| 3227 | ** |
| 3228 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 3229 | ** objects. We have to do this instead of letting pAppData point |
| 3230 | ** directly at the finder-function since C90 rules prevent a void* |
| 3231 | ** from be cast into a function pointer. |
| 3232 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3233 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3234 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3235 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3236 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 3237 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 3238 | ** |
| 3239 | ** * An I/O method finder function called FINDER that returns a pointer |
| 3240 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3241 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3242 | #define IOMETHODS(FINDER, METHOD, CLOSE, LOCK, UNLOCK, CKLOCK) \ |
| 3243 | static const sqlite3_io_methods METHOD = { \ |
| 3244 | 1, /* iVersion */ \ |
| 3245 | CLOSE, /* xClose */ \ |
| 3246 | unixRead, /* xRead */ \ |
| 3247 | unixWrite, /* xWrite */ \ |
| 3248 | unixTruncate, /* xTruncate */ \ |
| 3249 | unixSync, /* xSync */ \ |
| 3250 | unixFileSize, /* xFileSize */ \ |
| 3251 | LOCK, /* xLock */ \ |
| 3252 | UNLOCK, /* xUnlock */ \ |
| 3253 | CKLOCK, /* xCheckReservedLock */ \ |
| 3254 | unixFileControl, /* xFileControl */ \ |
| 3255 | unixSectorSize, /* xSectorSize */ \ |
| 3256 | unixDeviceCharacteristics /* xDeviceCapabilities */ \ |
| 3257 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3258 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 3259 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3260 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3261 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3262 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3263 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3264 | |
| 3265 | /* |
| 3266 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 3267 | ** locking strategies. Functions that return pointers to these methods |
| 3268 | ** are also created. |
| 3269 | */ |
| 3270 | IOMETHODS( |
| 3271 | posixIoFinder, /* Finder function name */ |
| 3272 | posixIoMethods, /* sqlite3_io_methods object name */ |
| 3273 | unixClose, /* xClose method */ |
| 3274 | unixLock, /* xLock method */ |
| 3275 | unixUnlock, /* xUnlock method */ |
| 3276 | unixCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3277 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3278 | IOMETHODS( |
| 3279 | nolockIoFinder, /* Finder function name */ |
| 3280 | nolockIoMethods, /* sqlite3_io_methods object name */ |
| 3281 | nolockClose, /* xClose method */ |
| 3282 | nolockLock, /* xLock method */ |
| 3283 | nolockUnlock, /* xUnlock method */ |
| 3284 | nolockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3285 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3286 | IOMETHODS( |
| 3287 | dotlockIoFinder, /* Finder function name */ |
| 3288 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
| 3289 | dotlockClose, /* xClose method */ |
| 3290 | dotlockLock, /* xLock method */ |
| 3291 | dotlockUnlock, /* xUnlock method */ |
| 3292 | dotlockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3293 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3294 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3295 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3296 | IOMETHODS( |
| 3297 | flockIoFinder, /* Finder function name */ |
| 3298 | flockIoMethods, /* sqlite3_io_methods object name */ |
| 3299 | flockClose, /* xClose method */ |
| 3300 | flockLock, /* xLock method */ |
| 3301 | flockUnlock, /* xUnlock method */ |
| 3302 | flockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3303 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3304 | #endif |
| 3305 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3306 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3307 | IOMETHODS( |
| 3308 | semIoFinder, /* Finder function name */ |
| 3309 | semIoMethods, /* sqlite3_io_methods object name */ |
| 3310 | semClose, /* xClose method */ |
| 3311 | semLock, /* xLock method */ |
| 3312 | semUnlock, /* xUnlock method */ |
| 3313 | semCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3314 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3315 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3316 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3317 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3318 | IOMETHODS( |
| 3319 | afpIoFinder, /* Finder function name */ |
| 3320 | afpIoMethods, /* sqlite3_io_methods object name */ |
| 3321 | afpClose, /* xClose method */ |
| 3322 | afpLock, /* xLock method */ |
| 3323 | afpUnlock, /* xUnlock method */ |
| 3324 | afpCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3325 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3326 | #endif |
| 3327 | |
| 3328 | /* |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3329 | ** The "Whole File Locking" finder returns the same set of methods as |
| 3330 | ** the posix locking finder. But it also sets the SQLITE_WHOLE_FILE_LOCKING |
| 3331 | ** flag to force the posix advisory locks to cover the whole file instead |
| 3332 | ** of just a small span of bytes near the 1GiB boundary. Whole File Locking |
| 3333 | ** is useful on NFS-mounted files since it helps NFS to maintain cache |
| 3334 | ** coherency. But it is a detriment to other filesystems since it runs |
| 3335 | ** slower. |
| 3336 | */ |
| 3337 | static const sqlite3_io_methods *posixWflIoFinderImpl(const char*z, unixFile*p){ |
| 3338 | UNUSED_PARAMETER(z); |
| 3339 | p->fileFlags = SQLITE_WHOLE_FILE_LOCKING; |
| 3340 | return &posixIoMethods; |
| 3341 | } |
| 3342 | static const sqlite3_io_methods |
| 3343 | *(*const posixWflIoFinder)(const char*,unixFile *p) = posixWflIoFinderImpl; |
| 3344 | |
| 3345 | /* |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3346 | ** The proxy locking method is a "super-method" in the sense that it |
| 3347 | ** opens secondary file descriptors for the conch and lock files and |
| 3348 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 3349 | ** secondary files. For this reason, the division that implements |
| 3350 | ** proxy locking is located much further down in the file. But we need |
| 3351 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 3352 | ** for proxy locking here. So we forward declare the I/O methods. |
| 3353 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3354 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3355 | static int proxyClose(sqlite3_file*); |
| 3356 | static int proxyLock(sqlite3_file*, int); |
| 3357 | static int proxyUnlock(sqlite3_file*, int); |
| 3358 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3359 | IOMETHODS( |
| 3360 | proxyIoFinder, /* Finder function name */ |
| 3361 | proxyIoMethods, /* sqlite3_io_methods object name */ |
| 3362 | proxyClose, /* xClose method */ |
| 3363 | proxyLock, /* xLock method */ |
| 3364 | proxyUnlock, /* xUnlock method */ |
| 3365 | proxyCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3366 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3367 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3368 | |
| 3369 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3370 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3371 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3372 | ** This "finder" function attempts to determine the best locking strategy |
| 3373 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3374 | ** object that implements that strategy. |
| 3375 | ** |
| 3376 | ** This is for MacOSX only. |
| 3377 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3378 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3379 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3380 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3381 | ){ |
| 3382 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3383 | const char *zFilesystem; /* Filesystem type name */ |
| 3384 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3385 | } aMap[] = { |
| 3386 | { "hfs", &posixIoMethods }, |
| 3387 | { "ufs", &posixIoMethods }, |
| 3388 | { "afpfs", &afpIoMethods }, |
| 3389 | #ifdef SQLITE_ENABLE_AFP_LOCKING_SMB |
| 3390 | { "smbfs", &afpIoMethods }, |
| 3391 | #else |
| 3392 | { "smbfs", &flockIoMethods }, |
| 3393 | #endif |
| 3394 | { "webdav", &nolockIoMethods }, |
| 3395 | { 0, 0 } |
| 3396 | }; |
| 3397 | int i; |
| 3398 | struct statfs fsInfo; |
| 3399 | struct flock lockInfo; |
| 3400 | |
| 3401 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3402 | /* If filePath==NULL that means we are dealing with a transient file |
| 3403 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3404 | return &nolockIoMethods; |
| 3405 | } |
| 3406 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 3407 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 3408 | return &nolockIoMethods; |
| 3409 | } |
| 3410 | for(i=0; aMap[i].zFilesystem; i++){ |
| 3411 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 3412 | return aMap[i].pMethods; |
| 3413 | } |
| 3414 | } |
| 3415 | } |
| 3416 | |
| 3417 | /* Default case. Handles, amongst others, "nfs". |
| 3418 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 3419 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3420 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3421 | lockInfo.l_len = 1; |
| 3422 | lockInfo.l_start = 0; |
| 3423 | lockInfo.l_whence = SEEK_SET; |
| 3424 | lockInfo.l_type = F_RDLCK; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3425 | if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
| 3426 | pNew->fileFlags = SQLITE_WHOLE_FILE_LOCKING; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3427 | return &posixIoMethods; |
| 3428 | }else{ |
| 3429 | return &dotlockIoMethods; |
| 3430 | } |
| 3431 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3432 | static const sqlite3_io_methods |
| 3433 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3434 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3435 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3436 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3437 | #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE |
| 3438 | /* |
| 3439 | ** This "finder" function attempts to determine the best locking strategy |
| 3440 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
| 3441 | ** object that implements that strategy. |
| 3442 | ** |
| 3443 | ** This is for VXWorks only. |
| 3444 | */ |
| 3445 | static const sqlite3_io_methods *autolockIoFinderImpl( |
| 3446 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3447 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3448 | ){ |
| 3449 | struct flock lockInfo; |
| 3450 | |
| 3451 | if( !filePath ){ |
| 3452 | /* If filePath==NULL that means we are dealing with a transient file |
| 3453 | ** that does not need to be locked. */ |
| 3454 | return &nolockIoMethods; |
| 3455 | } |
| 3456 | |
| 3457 | /* Test if fcntl() is supported and use POSIX style locks. |
| 3458 | ** Otherwise fall back to the named semaphore method. |
| 3459 | */ |
| 3460 | lockInfo.l_len = 1; |
| 3461 | lockInfo.l_start = 0; |
| 3462 | lockInfo.l_whence = SEEK_SET; |
| 3463 | lockInfo.l_type = F_RDLCK; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3464 | if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3465 | return &posixIoMethods; |
| 3466 | }else{ |
| 3467 | return &semIoMethods; |
| 3468 | } |
| 3469 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3470 | static const sqlite3_io_methods |
| 3471 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3472 | |
| 3473 | #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */ |
| 3474 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3475 | /* |
| 3476 | ** An abstract type for a pointer to a IO method finder function: |
| 3477 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3478 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3479 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3480 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3481 | /**************************************************************************** |
| 3482 | **************************** sqlite3_vfs methods **************************** |
| 3483 | ** |
| 3484 | ** This division contains the implementation of methods on the |
| 3485 | ** sqlite3_vfs object. |
| 3486 | */ |
| 3487 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3488 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3489 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3490 | */ |
| 3491 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3492 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3493 | int h, /* Open file descriptor of file being opened */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3494 | int dirfd, /* Directory file descriptor */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 3495 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 3496 | const char *zFilename, /* Name of the file being opened */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3497 | int noLock, /* Omit locking if true */ |
| 3498 | int isDelete /* Delete on close if true */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3499 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3500 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 3501 | unixFile *pNew = (unixFile *)pId; |
| 3502 | int rc = SQLITE_OK; |
| 3503 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3504 | assert( pNew->pLock==NULL ); |
| 3505 | assert( pNew->pOpen==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 3506 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3507 | /* Parameter isDelete is only used on vxworks. Express this explicitly |
| 3508 | ** here to prevent compiler warnings about unused parameters. |
danielk1977 | a03396a | 2008-11-19 14:35:46 +0000 | [diff] [blame] | 3509 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3510 | UNUSED_PARAMETER(isDelete); |
danielk1977 | a03396a | 2008-11-19 14:35:46 +0000 | [diff] [blame] | 3511 | |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 3512 | OSTRACE3("OPEN %-3d %s\n", h, zFilename); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3513 | pNew->h = h; |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 3514 | pNew->dirfd = dirfd; |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3515 | SET_THREADID(pNew); |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3516 | pNew->fileFlags = 0; |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3517 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3518 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 3519 | pNew->pId = vxworksFindFileId(zFilename); |
| 3520 | if( pNew->pId==0 ){ |
| 3521 | noLock = 1; |
| 3522 | rc = SQLITE_NOMEM; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3523 | } |
| 3524 | #endif |
| 3525 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 3526 | if( noLock ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3527 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 3528 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3529 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3530 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 3531 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 3532 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 3533 | ** zFilename remains valid until file is closed, to support */ |
| 3534 | pNew->lockingContext = (void*)zFilename; |
| 3535 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 3536 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3537 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3538 | if( pLockingStyle == &posixIoMethods ){ |
| 3539 | unixEnterMutex(); |
| 3540 | rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3541 | if( rc!=SQLITE_OK ){ |
| 3542 | /* If an error occured in findLockInfo(), close the file descriptor |
| 3543 | ** immediately, before releasing the mutex. findLockInfo() may fail |
| 3544 | ** in two scenarios: |
| 3545 | ** |
| 3546 | ** (a) A call to fstat() failed. |
| 3547 | ** (b) A malloc failed. |
| 3548 | ** |
| 3549 | ** Scenario (b) may only occur if the process is holding no other |
| 3550 | ** file descriptors open on the same file. If there were other file |
| 3551 | ** descriptors on this file, then no malloc would be required by |
| 3552 | ** findLockInfo(). If this is the case, it is quite safe to close |
| 3553 | ** handle h - as it is guaranteed that no posix locks will be released |
| 3554 | ** by doing so. |
| 3555 | ** |
| 3556 | ** If scenario (a) caused the error then things are not so safe. The |
| 3557 | ** implicit assumption here is that if fstat() fails, things are in |
| 3558 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 3559 | */ |
| 3560 | close(h); |
| 3561 | h = -1; |
| 3562 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3563 | unixLeaveMutex(); |
| 3564 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3565 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3566 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 3567 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3568 | /* AFP locking uses the file path so it needs to be included in |
| 3569 | ** the afpLockingContext. |
| 3570 | */ |
| 3571 | afpLockingContext *pCtx; |
| 3572 | pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 3573 | if( pCtx==0 ){ |
| 3574 | rc = SQLITE_NOMEM; |
| 3575 | }else{ |
| 3576 | /* NB: zFilename exists and remains valid until the file is closed |
| 3577 | ** according to requirement F11141. So we do not need to make a |
| 3578 | ** copy of the filename. */ |
| 3579 | pCtx->dbPath = zFilename; |
| 3580 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3581 | unixEnterMutex(); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3582 | rc = findLockInfo(pNew, NULL, &pNew->pOpen); |
| 3583 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3584 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3585 | } |
| 3586 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3587 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3588 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 3589 | /* Dotfile locking uses the file path so it needs to be included in |
| 3590 | ** the dotlockLockingContext |
| 3591 | */ |
| 3592 | char *zLockFile; |
| 3593 | int nFilename; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 3594 | nFilename = (int)strlen(zFilename) + 6; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3595 | zLockFile = (char *)sqlite3_malloc(nFilename); |
| 3596 | if( zLockFile==0 ){ |
| 3597 | rc = SQLITE_NOMEM; |
| 3598 | }else{ |
| 3599 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3600 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3601 | pNew->lockingContext = zLockFile; |
| 3602 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3603 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3604 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3605 | else if( pLockingStyle == &semIoMethods ){ |
| 3606 | /* Named semaphore locking uses the file path so it needs to be |
| 3607 | ** included in the semLockingContext |
| 3608 | */ |
| 3609 | unixEnterMutex(); |
| 3610 | rc = findLockInfo(pNew, &pNew->pLock, &pNew->pOpen); |
| 3611 | if( (rc==SQLITE_OK) && (pNew->pOpen->pSem==NULL) ){ |
| 3612 | char *zSemName = pNew->pOpen->aSemName; |
| 3613 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 3614 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3615 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 3616 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3617 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
| 3618 | pNew->pOpen->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 3619 | if( pNew->pOpen->pSem == SEM_FAILED ){ |
| 3620 | rc = SQLITE_NOMEM; |
| 3621 | pNew->pOpen->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3622 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3623 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3624 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3625 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3626 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3627 | |
| 3628 | pNew->lastErrno = 0; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3629 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3630 | if( rc!=SQLITE_OK ){ |
| 3631 | unlink(zFilename); |
| 3632 | isDelete = 0; |
| 3633 | } |
| 3634 | pNew->isDelete = isDelete; |
| 3635 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3636 | if( rc!=SQLITE_OK ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3637 | if( dirfd>=0 ) close(dirfd); /* silent leak if fail, already in error */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3638 | if( h>=0 ) close(h); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3639 | }else{ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3640 | pNew->pMethod = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3641 | OpenCounter(+1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3642 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3643 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 3644 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 3645 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3646 | /* |
| 3647 | ** Open a file descriptor to the directory containing file zFilename. |
| 3648 | ** If successful, *pFd is set to the opened file descriptor and |
| 3649 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 3650 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 3651 | ** value. |
| 3652 | ** |
| 3653 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 3654 | ** the file descriptor *pFd using close(). |
| 3655 | */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3656 | static int openDirectory(const char *zFilename, int *pFd){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3657 | int ii; |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 3658 | int fd = -1; |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 3659 | char zDirname[MAX_PATHNAME+1]; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3660 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 3661 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
drh | 617634e | 2009-01-08 14:36:20 +0000 | [diff] [blame] | 3662 | for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3663 | if( ii>0 ){ |
| 3664 | zDirname[ii] = '\0'; |
| 3665 | fd = open(zDirname, O_RDONLY|O_BINARY, 0); |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 3666 | if( fd>=0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3667 | #ifdef FD_CLOEXEC |
| 3668 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 3669 | #endif |
| 3670 | OSTRACE3("OPENDIR %-3d %s\n", fd, zDirname); |
| 3671 | } |
| 3672 | } |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3673 | *pFd = fd; |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 3674 | return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3675 | } |
| 3676 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3677 | /* |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3678 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 3679 | ** by the calling process and must be big enough to hold at least |
| 3680 | ** pVfs->mxPathname bytes. |
| 3681 | */ |
| 3682 | static int getTempname(int nBuf, char *zBuf){ |
| 3683 | static const char *azDirs[] = { |
| 3684 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3685 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3686 | "/var/tmp", |
| 3687 | "/usr/tmp", |
| 3688 | "/tmp", |
| 3689 | ".", |
| 3690 | }; |
| 3691 | static const unsigned char zChars[] = |
| 3692 | "abcdefghijklmnopqrstuvwxyz" |
| 3693 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 3694 | "0123456789"; |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 3695 | unsigned int i, j; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3696 | struct stat buf; |
| 3697 | const char *zDir = "."; |
| 3698 | |
| 3699 | /* It's odd to simulate an io-error here, but really this is just |
| 3700 | ** using the io-error infrastructure to test that SQLite handles this |
| 3701 | ** function failing. |
| 3702 | */ |
| 3703 | SimulateIOError( return SQLITE_IOERR ); |
| 3704 | |
| 3705 | azDirs[0] = sqlite3_temp_directory; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3706 | if (NULL == azDirs[1]) { |
| 3707 | azDirs[1] = getenv("TMPDIR"); |
| 3708 | } |
| 3709 | |
| 3710 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3711 | if( azDirs[i]==0 ) continue; |
| 3712 | if( stat(azDirs[i], &buf) ) continue; |
| 3713 | if( !S_ISDIR(buf.st_mode) ) continue; |
| 3714 | if( access(azDirs[i], 07) ) continue; |
| 3715 | zDir = azDirs[i]; |
| 3716 | break; |
| 3717 | } |
| 3718 | |
| 3719 | /* Check that the output buffer is large enough for the temporary file |
| 3720 | ** name. If it is not, return SQLITE_ERROR. |
| 3721 | */ |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 3722 | if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3723 | return SQLITE_ERROR; |
| 3724 | } |
| 3725 | |
| 3726 | do{ |
| 3727 | sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 3728 | j = (int)strlen(zBuf); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3729 | sqlite3_randomness(15, &zBuf[j]); |
| 3730 | for(i=0; i<15; i++, j++){ |
| 3731 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 3732 | } |
| 3733 | zBuf[j] = 0; |
| 3734 | }while( access(zBuf,0)==0 ); |
| 3735 | return SQLITE_OK; |
| 3736 | } |
| 3737 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3738 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 3739 | /* |
| 3740 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 3741 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 3742 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 3743 | */ |
| 3744 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3745 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 3746 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3747 | /* |
| 3748 | ** Search for an unused file descriptor that was opened on the database |
| 3749 | ** file (not a journal or master-journal file) identified by pathname |
| 3750 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 3751 | ** argument to this function. |
| 3752 | ** |
| 3753 | ** Such a file descriptor may exist if a database connection was closed |
| 3754 | ** but the associated file descriptor could not be closed because some |
| 3755 | ** other file descriptor open on the same file is holding a file-lock. |
| 3756 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 3757 | ** describing "Posix Advisory Locking" at the start of this file for |
| 3758 | ** further details. Also, ticket #4018. |
| 3759 | ** |
| 3760 | ** If a suitable file descriptor is found, then it is returned. If no |
| 3761 | ** such file descriptor is located, -1 is returned. |
| 3762 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3763 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 3764 | UnixUnusedFd *pUnused = 0; |
| 3765 | |
| 3766 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 3767 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 3768 | ** but because no way to test it is currently available. It is better |
| 3769 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 3770 | ** feature. */ |
| 3771 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3772 | struct stat sStat; /* Results of stat() call */ |
| 3773 | |
| 3774 | /* A stat() call may fail for various reasons. If this happens, it is |
| 3775 | ** almost certain that an open() call on the same path will also fail. |
| 3776 | ** For this reason, if an error occurs in the stat() call here, it is |
| 3777 | ** ignored and -1 is returned. The caller will try to open a new file |
| 3778 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 3779 | ** |
| 3780 | ** Even if a subsequent open() call does succeed, the consequences of |
| 3781 | ** not searching for a resusable file descriptor are not dire. */ |
| 3782 | if( 0==stat(zPath, &sStat) ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3783 | struct unixOpenCnt *pO; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3784 | struct unixFileId id; |
| 3785 | id.dev = sStat.st_dev; |
| 3786 | id.ino = sStat.st_ino; |
| 3787 | |
| 3788 | unixEnterMutex(); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3789 | for(pO=openList; pO && memcmp(&id, &pO->fileId, sizeof(id)); pO=pO->pNext); |
| 3790 | if( pO ){ |
| 3791 | UnixUnusedFd **pp; |
| 3792 | for(pp=&pO->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
| 3793 | pUnused = *pp; |
| 3794 | if( pUnused ){ |
| 3795 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3796 | } |
| 3797 | } |
| 3798 | unixLeaveMutex(); |
| 3799 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3800 | #endif /* if !OS_VXWORKS */ |
| 3801 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3802 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3803 | |
| 3804 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3805 | ** Open the file zPath. |
| 3806 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3807 | ** Previously, the SQLite OS layer used three functions in place of this |
| 3808 | ** one: |
| 3809 | ** |
| 3810 | ** sqlite3OsOpenReadWrite(); |
| 3811 | ** sqlite3OsOpenReadOnly(); |
| 3812 | ** sqlite3OsOpenExclusive(); |
| 3813 | ** |
| 3814 | ** These calls correspond to the following combinations of flags: |
| 3815 | ** |
| 3816 | ** ReadWrite() -> (READWRITE | CREATE) |
| 3817 | ** ReadOnly() -> (READONLY) |
| 3818 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 3819 | ** |
| 3820 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 3821 | ** true, the file was configured to be automatically deleted when the |
| 3822 | ** file handle closed. To achieve the same effect using this new |
| 3823 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 3824 | ** OpenExclusive(). |
| 3825 | */ |
| 3826 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3827 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 3828 | const char *zPath, /* Pathname of file to be opened */ |
| 3829 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 3830 | int flags, /* Input flags to control the opening */ |
| 3831 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3832 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3833 | unixFile *p = (unixFile *)pFile; |
| 3834 | int fd = -1; /* File descriptor returned by open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3835 | int dirfd = -1; /* Directory file descriptor */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3836 | int openFlags = 0; /* Flags to pass to open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3837 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 3838 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3839 | int rc = SQLITE_OK; /* Function Return Code */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3840 | |
| 3841 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 3842 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 3843 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 3844 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 3845 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
| 3846 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3847 | /* If creating a master or main-file journal, this function will open |
| 3848 | ** a file-descriptor on the directory too. The first time unixSync() |
| 3849 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 3850 | */ |
| 3851 | int isOpenDirectory = (isCreate && |
| 3852 | (eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL) |
| 3853 | ); |
| 3854 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3855 | /* If argument zPath is a NULL pointer, this function is required to open |
| 3856 | ** a temporary file. Use this buffer to store the file name in. |
| 3857 | */ |
| 3858 | char zTmpname[MAX_PATHNAME+1]; |
| 3859 | const char *zName = zPath; |
| 3860 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3861 | /* Check the following statements are true: |
| 3862 | ** |
| 3863 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 3864 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 3865 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 3866 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3867 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3868 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3869 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3870 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 3871 | assert(isDelete==0 || isCreate); |
| 3872 | |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 3873 | /* The main DB, main journal, and master journal are never automatically |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3874 | ** deleted. Nor are they ever temporary files. */ |
| 3875 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 3876 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 3877 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3878 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3879 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 3880 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 3881 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 3882 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 3883 | || eType==SQLITE_OPEN_TRANSIENT_DB |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3884 | ); |
| 3885 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3886 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3887 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3888 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3889 | UnixUnusedFd *pUnused; |
| 3890 | pUnused = findReusableFd(zName, flags); |
| 3891 | if( pUnused ){ |
| 3892 | fd = pUnused->fd; |
| 3893 | }else{ |
dan | 6aa657f | 2009-08-24 18:57:58 +0000 | [diff] [blame] | 3894 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3895 | if( !pUnused ){ |
| 3896 | return SQLITE_NOMEM; |
| 3897 | } |
| 3898 | } |
| 3899 | p->pUnused = pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3900 | }else if( !zName ){ |
| 3901 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3902 | assert(isDelete && !isOpenDirectory); |
| 3903 | rc = getTempname(MAX_PATHNAME+1, zTmpname); |
| 3904 | if( rc!=SQLITE_OK ){ |
| 3905 | return rc; |
| 3906 | } |
| 3907 | zName = zTmpname; |
| 3908 | } |
| 3909 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3910 | /* Determine the value of the flags parameter passed to POSIX function |
| 3911 | ** open(). These must be calculated even if open() is not called, as |
| 3912 | ** they may be stored as part of the file handle and used by the |
| 3913 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3914 | if( isReadonly ) openFlags |= O_RDONLY; |
| 3915 | if( isReadWrite ) openFlags |= O_RDWR; |
| 3916 | if( isCreate ) openFlags |= O_CREAT; |
| 3917 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 3918 | openFlags |= (O_LARGEFILE|O_BINARY); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3919 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3920 | if( fd<0 ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3921 | mode_t openMode = (isDelete?0600:SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 3922 | fd = open(zName, openFlags, openMode); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3923 | OSTRACE4("OPENX %-3d %s 0%o\n", fd, zName, openFlags); |
| 3924 | if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ |
| 3925 | /* Failed to open the file for read/write access. Try read-only. */ |
| 3926 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3927 | openFlags &= ~(O_RDWR|O_CREAT); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3928 | flags |= SQLITE_OPEN_READONLY; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3929 | openFlags |= O_RDONLY; |
| 3930 | fd = open(zName, openFlags, openMode); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3931 | } |
| 3932 | if( fd<0 ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3933 | rc = SQLITE_CANTOPEN; |
| 3934 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3935 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3936 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3937 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3938 | if( pOutFlags ){ |
| 3939 | *pOutFlags = flags; |
| 3940 | } |
| 3941 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3942 | if( p->pUnused ){ |
| 3943 | p->pUnused->fd = fd; |
| 3944 | p->pUnused->flags = flags; |
| 3945 | } |
| 3946 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3947 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3948 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3949 | zPath = zName; |
| 3950 | #else |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 3951 | unlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3952 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 3953 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 3954 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 3955 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3956 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3957 | } |
| 3958 | #endif |
| 3959 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3960 | if( isOpenDirectory ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3961 | rc = openDirectory(zPath, &dirfd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3962 | if( rc!=SQLITE_OK ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3963 | /* It is safe to close fd at this point, because it is guaranteed not |
| 3964 | ** to be open on a database file. If it were open on a database file, |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3965 | ** it would not be safe to close as this would release any locks held |
| 3966 | ** on the file by this process. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3967 | assert( eType!=SQLITE_OPEN_MAIN_DB ); |
| 3968 | close(fd); /* silently leak if fail, already in error */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3969 | goto open_finished; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 3970 | } |
| 3971 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3972 | |
| 3973 | #ifdef FD_CLOEXEC |
| 3974 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 3975 | #endif |
| 3976 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 3977 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3978 | |
| 3979 | #if SQLITE_PREFER_PROXY_LOCKING |
dan | 15edd58 | 2009-08-25 05:57:47 +0000 | [diff] [blame] | 3980 | if( zPath!=NULL && !noLock && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3981 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 3982 | int useProxy = 0; |
| 3983 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3984 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 3985 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3986 | if( envforce!=NULL ){ |
| 3987 | useProxy = atoi(envforce)>0; |
| 3988 | }else{ |
| 3989 | struct statfs fsInfo; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3990 | if( statfs(zPath, &fsInfo) == -1 ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3991 | /* In theory, the close(fd) call is sub-optimal. If the file opened |
| 3992 | ** with fd is a database file, and there are other connections open |
| 3993 | ** on that file that are currently holding advisory locks on it, |
| 3994 | ** then the call to close() will cancel those locks. In practice, |
| 3995 | ** we're assuming that statfs() doesn't fail very often. At least |
| 3996 | ** not while other file descriptors opened by the same process on |
| 3997 | ** the same file are working. */ |
| 3998 | p->lastErrno = errno; |
| 3999 | if( dirfd>=0 ){ |
| 4000 | close(dirfd); /* silently leak if fail, in error */ |
| 4001 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4002 | close(fd); /* silently leak if fail, in error */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4003 | rc = SQLITE_IOERR_ACCESS; |
| 4004 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4005 | } |
| 4006 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 4007 | } |
| 4008 | if( useProxy ){ |
| 4009 | rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete); |
| 4010 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4011 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4012 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4013 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4014 | } |
| 4015 | } |
| 4016 | #endif |
| 4017 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4018 | rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete); |
| 4019 | open_finished: |
| 4020 | if( rc!=SQLITE_OK ){ |
| 4021 | sqlite3_free(p->pUnused); |
| 4022 | } |
| 4023 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4024 | } |
| 4025 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4026 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4027 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4028 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 4029 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4030 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4031 | static int unixDelete( |
| 4032 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 4033 | const char *zPath, /* Name of file to be deleted */ |
| 4034 | int dirSync /* If true, fsync() directory after deleting file */ |
| 4035 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4036 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4037 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4038 | SimulateIOError(return SQLITE_IOERR_DELETE); |
| 4039 | unlink(zPath); |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 4040 | #ifndef SQLITE_DISABLE_DIRSYNC |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4041 | if( dirSync ){ |
| 4042 | int fd; |
| 4043 | rc = openDirectory(zPath, &fd); |
| 4044 | if( rc==SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4045 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4046 | if( fsync(fd)==-1 ) |
| 4047 | #else |
| 4048 | if( fsync(fd) ) |
| 4049 | #endif |
| 4050 | { |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4051 | rc = SQLITE_IOERR_DIR_FSYNC; |
| 4052 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4053 | if( close(fd)&&!rc ){ |
| 4054 | rc = SQLITE_IOERR_DIR_CLOSE; |
| 4055 | } |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4056 | } |
| 4057 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 4058 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4059 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4060 | } |
| 4061 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4062 | /* |
| 4063 | ** Test the existance of or access permissions of file zPath. The |
| 4064 | ** test performed depends on the value of flags: |
| 4065 | ** |
| 4066 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 4067 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 4068 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 4069 | ** |
| 4070 | ** Otherwise return 0. |
| 4071 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4072 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4073 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 4074 | const char *zPath, /* Path of the file to examine */ |
| 4075 | int flags, /* What do we want to learn about the zPath file? */ |
| 4076 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4077 | ){ |
rse | 25c0d1a | 2007-09-20 08:38:14 +0000 | [diff] [blame] | 4078 | int amode = 0; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4079 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4080 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4081 | switch( flags ){ |
| 4082 | case SQLITE_ACCESS_EXISTS: |
| 4083 | amode = F_OK; |
| 4084 | break; |
| 4085 | case SQLITE_ACCESS_READWRITE: |
| 4086 | amode = W_OK|R_OK; |
| 4087 | break; |
drh | 50d3f90 | 2007-08-27 21:10:36 +0000 | [diff] [blame] | 4088 | case SQLITE_ACCESS_READ: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4089 | amode = R_OK; |
| 4090 | break; |
| 4091 | |
| 4092 | default: |
| 4093 | assert(!"Invalid flags argument"); |
| 4094 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4095 | *pResOut = (access(zPath, amode)==0); |
| 4096 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4099 | |
| 4100 | /* |
| 4101 | ** Turn a relative pathname into a full pathname. The relative path |
| 4102 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 4103 | ** zPath. |
| 4104 | ** |
| 4105 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 4106 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 4107 | ** this buffer before returning. |
| 4108 | */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 4109 | static int unixFullPathname( |
| 4110 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 4111 | const char *zPath, /* Possibly relative input path */ |
| 4112 | int nOut, /* Size of output buffer in bytes */ |
| 4113 | char *zOut /* Output buffer */ |
| 4114 | ){ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 4115 | |
| 4116 | /* It's odd to simulate an io-error here, but really this is just |
| 4117 | ** using the io-error infrastructure to test that SQLite handles this |
| 4118 | ** function failing. This function could fail if, for example, the |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4119 | ** current working directory has been unlinked. |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 4120 | */ |
| 4121 | SimulateIOError( return SQLITE_ERROR ); |
| 4122 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4123 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 4124 | UNUSED_PARAMETER(pVfs); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4125 | |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4126 | zOut[nOut-1] = '\0'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4127 | if( zPath[0]=='/' ){ |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4128 | sqlite3_snprintf(nOut, zOut, "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4129 | }else{ |
| 4130 | int nCwd; |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4131 | if( getcwd(zOut, nOut-1)==0 ){ |
drh | 70c0145 | 2007-09-03 17:42:17 +0000 | [diff] [blame] | 4132 | return SQLITE_CANTOPEN; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4133 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4134 | nCwd = (int)strlen(zOut); |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4135 | sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4136 | } |
| 4137 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4138 | } |
| 4139 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 4140 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4141 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 4142 | /* |
| 4143 | ** Interfaces for opening a shared library, finding entry points |
| 4144 | ** within the shared library, and closing the shared library. |
| 4145 | */ |
| 4146 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4147 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 4148 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4149 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 4150 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 4151 | |
| 4152 | /* |
| 4153 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 4154 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 4155 | ** message is available, it is written to zBufOut. If no error message |
| 4156 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 4157 | ** error message. |
| 4158 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4159 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4160 | char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4161 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4162 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4163 | zErr = dlerror(); |
| 4164 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4165 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4166 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4167 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4168 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4169 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 4170 | /* |
| 4171 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 4172 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 4173 | ** returns a void* which is really a pointer to a function. So how do we |
| 4174 | ** use dlsym() with -pedantic-errors? |
| 4175 | ** |
| 4176 | ** Variable x below is defined to be a pointer to a function taking |
| 4177 | ** parameters void* and const char* and returning a pointer to a function. |
| 4178 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 4179 | ** (That assignment requires a cast.) Then we call the function that |
| 4180 | ** x points to. |
| 4181 | ** |
| 4182 | ** This work-around is unlikely to work correctly on any system where |
| 4183 | ** you really cannot cast a function pointer into void*. But then, on the |
| 4184 | ** other hand, dlsym() will not work on such a system either, so we have |
| 4185 | ** not really lost anything. |
| 4186 | */ |
| 4187 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4188 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4189 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 4190 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4191 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4192 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 4193 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4194 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4195 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4196 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 4197 | #define unixDlOpen 0 |
| 4198 | #define unixDlError 0 |
| 4199 | #define unixDlSym 0 |
| 4200 | #define unixDlClose 0 |
| 4201 | #endif |
| 4202 | |
| 4203 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4204 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4205 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4206 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 4207 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 4208 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4209 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4210 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 4211 | ** errors. The reports issued by valgrind are incorrect - we would |
| 4212 | ** prefer that the randomness be increased by making use of the |
| 4213 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 4214 | ** some users. Rather than argue, it seems easier just to initialize |
| 4215 | ** the whole array and silence valgrind, even if that means less randomness |
| 4216 | ** in the random seed. |
| 4217 | ** |
| 4218 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 4219 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4220 | ** tests repeatable. |
| 4221 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4222 | memset(zBuf, 0, nBuf); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4223 | #if !defined(SQLITE_TEST) |
| 4224 | { |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 4225 | int pid, fd; |
| 4226 | fd = open("/dev/urandom", O_RDONLY); |
| 4227 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 4228 | time_t t; |
| 4229 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4230 | memcpy(zBuf, &t, sizeof(t)); |
| 4231 | pid = getpid(); |
| 4232 | memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid)); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 4233 | assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf ); |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 4234 | nBuf = sizeof(t) + sizeof(pid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 4235 | }else{ |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 4236 | nBuf = read(fd, zBuf, nBuf); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 4237 | close(fd); |
| 4238 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4239 | } |
| 4240 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 4241 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4242 | } |
| 4243 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4244 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4245 | /* |
| 4246 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4247 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 4248 | ** The return value is the number of microseconds of sleep actually |
| 4249 | ** requested from the underlying operating system, a number which |
| 4250 | ** might be greater than or equal to the argument, but not less |
| 4251 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4252 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4253 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4254 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4255 | struct timespec sp; |
| 4256 | |
| 4257 | sp.tv_sec = microseconds / 1000000; |
| 4258 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 4259 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 4260 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4261 | return microseconds; |
| 4262 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4263 | usleep(microseconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 4264 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4265 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4266 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4267 | int seconds = (microseconds+999999)/1000000; |
| 4268 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 4269 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 4270 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 4271 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 4272 | } |
| 4273 | |
| 4274 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4275 | ** The following variable, if set to a non-zero value, is interpreted as |
| 4276 | ** the number of seconds since 1970 and is used to set the result of |
| 4277 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4278 | */ |
| 4279 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4280 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4281 | #endif |
| 4282 | |
| 4283 | /* |
| 4284 | ** Find the current time (in Universal Coordinated Time). Write the |
| 4285 | ** current time and date as a Julian Day number into *prNow and |
| 4286 | ** return 0. Return 1 if the time and date cannot be found. |
| 4287 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4288 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | 0b3bf92 | 2009-06-15 20:45:34 +0000 | [diff] [blame] | 4289 | #if defined(SQLITE_OMIT_FLOATING_POINT) |
| 4290 | time_t t; |
| 4291 | time(&t); |
| 4292 | *prNow = (((sqlite3_int64)t)/8640 + 24405875)/10; |
| 4293 | #elif defined(NO_GETTOD) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4294 | time_t t; |
| 4295 | time(&t); |
| 4296 | *prNow = t/86400.0 + 2440587.5; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4297 | #elif OS_VXWORKS |
| 4298 | struct timespec sNow; |
| 4299 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 4300 | *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_nsec/86400000000000.0; |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 4301 | #else |
| 4302 | struct timeval sNow; |
drh | bdcc276 | 2007-04-02 18:06:57 +0000 | [diff] [blame] | 4303 | gettimeofday(&sNow, 0); |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 4304 | *prNow = 2440587.5 + sNow.tv_sec/86400.0 + sNow.tv_usec/86400000000.0; |
| 4305 | #endif |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4306 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4307 | #ifdef SQLITE_TEST |
| 4308 | if( sqlite3_current_time ){ |
| 4309 | *prNow = sqlite3_current_time/86400.0 + 2440587.5; |
| 4310 | } |
| 4311 | #endif |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4312 | UNUSED_PARAMETER(NotUsed); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4313 | return 0; |
| 4314 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4315 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4316 | /* |
| 4317 | ** We added the xGetLastError() method with the intention of providing |
| 4318 | ** better low-level error messages when operating-system problems come up |
| 4319 | ** during SQLite operation. But so far, none of that has been implemented |
| 4320 | ** in the core. So this routine is never called. For now, it is merely |
| 4321 | ** a place-holder. |
| 4322 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4323 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 4324 | UNUSED_PARAMETER(NotUsed); |
| 4325 | UNUSED_PARAMETER(NotUsed2); |
| 4326 | UNUSED_PARAMETER(NotUsed3); |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 4327 | return 0; |
| 4328 | } |
| 4329 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4330 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4331 | ************************ End of sqlite3_vfs methods *************************** |
| 4332 | ******************************************************************************/ |
| 4333 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4334 | /****************************************************************************** |
| 4335 | ************************** Begin Proxy Locking ******************************** |
| 4336 | ** |
| 4337 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 4338 | ** other locking methods on secondary lock files. Proxy locking is a |
| 4339 | ** meta-layer over top of the primitive locking implemented above. For |
| 4340 | ** this reason, the division that implements of proxy locking is deferred |
| 4341 | ** until late in the file (here) after all of the other I/O methods have |
| 4342 | ** been defined - so that the primitive locking methods are available |
| 4343 | ** as services to help with the implementation of proxy locking. |
| 4344 | ** |
| 4345 | **** |
| 4346 | ** |
| 4347 | ** The default locking schemes in SQLite use byte-range locks on the |
| 4348 | ** database file to coordinate safe, concurrent access by multiple readers |
| 4349 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 4350 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 4351 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 4352 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 4353 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 4354 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 4355 | ** address in the shared range is taken for a SHARED lock, the entire |
| 4356 | ** shared range is taken for an EXCLUSIVE lock): |
| 4357 | ** |
| 4358 | ** PENDING_BYTE 0x40000000 |
| 4359 | ** RESERVED_BYTE 0x40000001 |
| 4360 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 4361 | ** |
| 4362 | ** This works well on the local file system, but shows a nearly 100x |
| 4363 | ** slowdown in read performance on AFP because the AFP client disables |
| 4364 | ** the read cache when byte-range locks are present. Enabling the read |
| 4365 | ** cache exposes a cache coherency problem that is present on all OS X |
| 4366 | ** supported network file systems. NFS and AFP both observe the |
| 4367 | ** close-to-open semantics for ensuring cache coherency |
| 4368 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 4369 | ** address the requirements for concurrent database access by multiple |
| 4370 | ** readers and writers |
| 4371 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 4372 | ** |
| 4373 | ** To address the performance and cache coherency issues, proxy file locking |
| 4374 | ** changes the way database access is controlled by limiting access to a |
| 4375 | ** single host at a time and moving file locks off of the database file |
| 4376 | ** and onto a proxy file on the local file system. |
| 4377 | ** |
| 4378 | ** |
| 4379 | ** Using proxy locks |
| 4380 | ** ----------------- |
| 4381 | ** |
| 4382 | ** C APIs |
| 4383 | ** |
| 4384 | ** sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE, |
| 4385 | ** <proxy_path> | ":auto:"); |
| 4386 | ** sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>); |
| 4387 | ** |
| 4388 | ** |
| 4389 | ** SQL pragmas |
| 4390 | ** |
| 4391 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 4392 | ** PRAGMA [database.]lock_proxy_file |
| 4393 | ** |
| 4394 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 4395 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 4396 | ** a proxy path based on the user's temp dir |
| 4397 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 4398 | ** actual proxy file name is generated from the name and path of the |
| 4399 | ** database file. For example: |
| 4400 | ** |
| 4401 | ** For database path "/Users/me/foo.db" |
| 4402 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 4403 | ** |
| 4404 | ** Once a lock proxy is configured for a database connection, it can not |
| 4405 | ** be removed, however it may be switched to a different proxy path via |
| 4406 | ** the above APIs (assuming the conch file is not being held by another |
| 4407 | ** connection or process). |
| 4408 | ** |
| 4409 | ** |
| 4410 | ** How proxy locking works |
| 4411 | ** ----------------------- |
| 4412 | ** |
| 4413 | ** Proxy file locking relies primarily on two new supporting files: |
| 4414 | ** |
| 4415 | ** * conch file to limit access to the database file to a single host |
| 4416 | ** at a time |
| 4417 | ** |
| 4418 | ** * proxy file to act as a proxy for the advisory locks normally |
| 4419 | ** taken on the database |
| 4420 | ** |
| 4421 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 4422 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 4423 | ** contents and comparing the host's unique host ID (see below) and lock |
| 4424 | ** proxy path against the values stored in the conch. The conch file is |
| 4425 | ** stored in the same directory as the database file and the file name |
| 4426 | ** is patterned after the database file name as ".<databasename>-conch". |
| 4427 | ** If the conch file does not exist, or it's contents do not match the |
| 4428 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 4429 | ** lock and the conch file contents is updated with the host ID and proxy |
| 4430 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 4431 | ** is held by another process (with a shared lock), the exclusive lock |
| 4432 | ** will fail and SQLITE_BUSY is returned. |
| 4433 | ** |
| 4434 | ** The proxy file - a single-byte file used for all advisory file locks |
| 4435 | ** normally taken on the database file. This allows for safe sharing |
| 4436 | ** of the database file for multiple readers and writers on the same |
| 4437 | ** host (the conch ensures that they all use the same local lock file). |
| 4438 | ** |
| 4439 | ** There is a third file - the host ID file - used as a persistent record |
| 4440 | ** of a unique identifier for the host, a 128-byte unique host id file |
| 4441 | ** in the path defined by the HOSTIDPATH macro (default value is |
| 4442 | ** /Library/Caches/.com.apple.sqliteConchHostId). |
| 4443 | ** |
| 4444 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 4445 | ** only taken when the first request to lock database file is made. |
| 4446 | ** This matches the semantics of the traditional locking behavior, where |
| 4447 | ** opening a connection to a database file does not take a lock on it. |
| 4448 | ** The shared lock and an open file descriptor are maintained until |
| 4449 | ** the connection to the database is closed. |
| 4450 | ** |
| 4451 | ** The proxy file and the lock file are never deleted so they only need |
| 4452 | ** to be created the first time they are used. |
| 4453 | ** |
| 4454 | ** Configuration options |
| 4455 | ** --------------------- |
| 4456 | ** |
| 4457 | ** SQLITE_PREFER_PROXY_LOCKING |
| 4458 | ** |
| 4459 | ** Database files accessed on non-local file systems are |
| 4460 | ** automatically configured for proxy locking, lock files are |
| 4461 | ** named automatically using the same logic as |
| 4462 | ** PRAGMA lock_proxy_file=":auto:" |
| 4463 | ** |
| 4464 | ** SQLITE_PROXY_DEBUG |
| 4465 | ** |
| 4466 | ** Enables the logging of error messages during host id file |
| 4467 | ** retrieval and creation |
| 4468 | ** |
| 4469 | ** HOSTIDPATH |
| 4470 | ** |
| 4471 | ** Overrides the default host ID file path location |
| 4472 | ** |
| 4473 | ** LOCKPROXYDIR |
| 4474 | ** |
| 4475 | ** Overrides the default directory used for lock proxy files that |
| 4476 | ** are named automatically via the ":auto:" setting |
| 4477 | ** |
| 4478 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 4479 | ** |
| 4480 | ** Permissions to use when creating a directory for storing the |
| 4481 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 4482 | ** |
| 4483 | ** |
| 4484 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 4485 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 4486 | ** force proxy locking to be used for every database file opened, and 0 |
| 4487 | ** will force automatic proxy locking to be disabled for all database |
| 4488 | ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or |
| 4489 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 4490 | */ |
| 4491 | |
| 4492 | /* |
| 4493 | ** Proxy locking is only available on MacOSX |
| 4494 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4495 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4496 | |
| 4497 | #ifdef SQLITE_TEST |
| 4498 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 4499 | int sqlite3_hostid_num = 0; |
| 4500 | #endif |
| 4501 | |
| 4502 | /* |
| 4503 | ** The proxyLockingContext has the path and file structures for the remote |
| 4504 | ** and local proxy files in it |
| 4505 | */ |
| 4506 | typedef struct proxyLockingContext proxyLockingContext; |
| 4507 | struct proxyLockingContext { |
| 4508 | unixFile *conchFile; /* Open conch file */ |
| 4509 | char *conchFilePath; /* Name of the conch file */ |
| 4510 | unixFile *lockProxy; /* Open proxy lock file */ |
| 4511 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 4512 | char *dbPath; /* Name of the open file */ |
| 4513 | int conchHeld; /* True if the conch is currently held */ |
| 4514 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 4515 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 4516 | }; |
| 4517 | |
| 4518 | /* HOSTIDLEN and CONCHLEN both include space for the string |
| 4519 | ** terminating nul |
| 4520 | */ |
| 4521 | #define HOSTIDLEN 128 |
| 4522 | #define CONCHLEN (MAXPATHLEN+HOSTIDLEN+1) |
| 4523 | #ifndef HOSTIDPATH |
| 4524 | # define HOSTIDPATH "/Library/Caches/.com.apple.sqliteConchHostId" |
| 4525 | #endif |
| 4526 | |
| 4527 | /* basically a copy of unixRandomness with different |
| 4528 | ** test behavior built in */ |
| 4529 | static int proxyGenerateHostID(char *pHostID){ |
| 4530 | int pid, fd, len; |
| 4531 | unsigned char *key = (unsigned char *)pHostID; |
| 4532 | |
| 4533 | memset(key, 0, HOSTIDLEN); |
| 4534 | len = 0; |
| 4535 | fd = open("/dev/urandom", O_RDONLY); |
| 4536 | if( fd>=0 ){ |
| 4537 | len = read(fd, key, HOSTIDLEN); |
| 4538 | close(fd); /* silently leak the fd if it fails */ |
| 4539 | } |
| 4540 | if( len < HOSTIDLEN ){ |
| 4541 | time_t t; |
| 4542 | time(&t); |
| 4543 | memcpy(key, &t, sizeof(t)); |
| 4544 | pid = getpid(); |
| 4545 | memcpy(&key[sizeof(t)], &pid, sizeof(pid)); |
| 4546 | } |
| 4547 | |
| 4548 | #ifdef MAKE_PRETTY_HOSTID |
| 4549 | { |
| 4550 | int i; |
| 4551 | /* filter the bytes into printable ascii characters and NUL terminate */ |
| 4552 | key[(HOSTIDLEN-1)] = 0x00; |
| 4553 | for( i=0; i<(HOSTIDLEN-1); i++ ){ |
| 4554 | unsigned char pa = key[i]&0x7F; |
| 4555 | if( pa<0x20 ){ |
| 4556 | key[i] = (key[i]&0x80 == 0x80) ? pa+0x40 : pa+0x20; |
| 4557 | }else if( pa==0x7F ){ |
| 4558 | key[i] = (key[i]&0x80 == 0x80) ? pa=0x20 : pa+0x7E; |
| 4559 | } |
| 4560 | } |
| 4561 | } |
| 4562 | #endif |
| 4563 | return SQLITE_OK; |
| 4564 | } |
| 4565 | |
| 4566 | /* writes the host id path to path, path should be an pre-allocated buffer |
| 4567 | ** with enough space for a path |
| 4568 | */ |
| 4569 | static void proxyGetHostIDPath(char *path, size_t len){ |
| 4570 | strlcpy(path, HOSTIDPATH, len); |
| 4571 | #ifdef SQLITE_TEST |
| 4572 | if( sqlite3_hostid_num>0 ){ |
| 4573 | char suffix[2] = "1"; |
| 4574 | suffix[0] = suffix[0] + sqlite3_hostid_num; |
| 4575 | strlcat(path, suffix, len); |
| 4576 | } |
| 4577 | #endif |
| 4578 | OSTRACE3("GETHOSTIDPATH %s pid=%d\n", path, getpid()); |
| 4579 | } |
| 4580 | |
| 4581 | /* get the host ID from a sqlite hostid file stored in the |
| 4582 | ** user-specific tmp directory, create the ID if it's not there already |
| 4583 | */ |
| 4584 | static int proxyGetHostID(char *pHostID, int *pError){ |
| 4585 | int fd; |
| 4586 | char path[MAXPATHLEN]; |
| 4587 | size_t len; |
| 4588 | int rc=SQLITE_OK; |
| 4589 | |
| 4590 | proxyGetHostIDPath(path, MAXPATHLEN); |
| 4591 | /* try to create the host ID file, if it already exists read the contents */ |
| 4592 | fd = open(path, O_CREAT|O_WRONLY|O_EXCL, 0644); |
| 4593 | if( fd<0 ){ |
| 4594 | int err=errno; |
| 4595 | |
| 4596 | if( err!=EEXIST ){ |
| 4597 | #ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */ |
| 4598 | fprintf(stderr, "sqlite error creating host ID file %s: %s\n", |
| 4599 | path, strerror(err)); |
| 4600 | #endif |
| 4601 | return SQLITE_PERM; |
| 4602 | } |
| 4603 | /* couldn't create the file, read it instead */ |
| 4604 | fd = open(path, O_RDONLY|O_EXCL); |
| 4605 | if( fd<0 ){ |
| 4606 | #ifdef SQLITE_PROXY_DEBUG /* set the sqlite error message instead */ |
| 4607 | int err = errno; |
| 4608 | fprintf(stderr, "sqlite error opening host ID file %s: %s\n", |
| 4609 | path, strerror(err)); |
| 4610 | #endif |
| 4611 | return SQLITE_PERM; |
| 4612 | } |
| 4613 | len = pread(fd, pHostID, HOSTIDLEN, 0); |
| 4614 | if( len<0 ){ |
| 4615 | *pError = errno; |
| 4616 | rc = SQLITE_IOERR_READ; |
| 4617 | }else if( len<HOSTIDLEN ){ |
| 4618 | *pError = 0; |
| 4619 | rc = SQLITE_IOERR_SHORT_READ; |
| 4620 | } |
| 4621 | close(fd); /* silently leak the fd if it fails */ |
| 4622 | OSTRACE3("GETHOSTID read %s pid=%d\n", pHostID, getpid()); |
| 4623 | return rc; |
| 4624 | }else{ |
| 4625 | /* we're creating the host ID file (use a random string of bytes) */ |
| 4626 | proxyGenerateHostID(pHostID); |
| 4627 | len = pwrite(fd, pHostID, HOSTIDLEN, 0); |
| 4628 | if( len<0 ){ |
| 4629 | *pError = errno; |
| 4630 | rc = SQLITE_IOERR_WRITE; |
| 4631 | }else if( len<HOSTIDLEN ){ |
| 4632 | *pError = 0; |
| 4633 | rc = SQLITE_IOERR_WRITE; |
| 4634 | } |
| 4635 | close(fd); /* silently leak the fd if it fails */ |
| 4636 | OSTRACE3("GETHOSTID wrote %s pid=%d\n", pHostID, getpid()); |
| 4637 | return rc; |
| 4638 | } |
| 4639 | } |
| 4640 | |
| 4641 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 4642 | int len; |
| 4643 | int dbLen; |
| 4644 | int i; |
| 4645 | |
| 4646 | #ifdef LOCKPROXYDIR |
| 4647 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 4648 | #else |
| 4649 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 4650 | { |
| 4651 | confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen); |
| 4652 | len = strlcat(lPath, "sqliteplocks", maxLen); |
| 4653 | if( mkdir(lPath, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
| 4654 | /* if mkdir fails, handle as lock file creation failure */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4655 | # ifdef SQLITE_DEBUG |
danielk1977 | 50c55a9 | 2009-05-08 11:34:37 +0000 | [diff] [blame] | 4656 | int err = errno; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4657 | if( err!=EEXIST ){ |
| 4658 | fprintf(stderr, "proxyGetLockPath: mkdir(%s,0%o) error %d %s\n", lPath, |
| 4659 | SQLITE_DEFAULT_PROXYDIR_PERMISSIONS, err, strerror(err)); |
| 4660 | } |
| 4661 | # endif |
| 4662 | }else{ |
| 4663 | OSTRACE3("GETLOCKPATH mkdir %s pid=%d\n", lPath, getpid()); |
| 4664 | } |
| 4665 | |
| 4666 | } |
| 4667 | # else |
| 4668 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 4669 | # endif |
| 4670 | #endif |
| 4671 | |
| 4672 | if( lPath[len-1]!='/' ){ |
| 4673 | len = strlcat(lPath, "/", maxLen); |
| 4674 | } |
| 4675 | |
| 4676 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4677 | dbLen = (int)strlen(dbPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4678 | for( i=0; i<dbLen && (i+len+7)<maxLen; i++){ |
| 4679 | char c = dbPath[i]; |
| 4680 | lPath[i+len] = (c=='/')?'_':c; |
| 4681 | } |
| 4682 | lPath[i+len]='\0'; |
| 4683 | strlcat(lPath, ":auto:", maxLen); |
| 4684 | return SQLITE_OK; |
| 4685 | } |
| 4686 | |
| 4687 | /* |
| 4688 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 4689 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 4690 | ** |
| 4691 | ** The caller is responsible not only for closing the file descriptor |
| 4692 | ** but also for freeing the memory associated with the file descriptor. |
| 4693 | */ |
| 4694 | static int proxyCreateUnixFile(const char *path, unixFile **ppFile) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4695 | unixFile *pNew; |
dan | 15edd58 | 2009-08-25 05:57:47 +0000 | [diff] [blame] | 4696 | int flags = SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4697 | int rc = SQLITE_OK; |
| 4698 | sqlite3_vfs dummyVfs; |
| 4699 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4700 | pNew = (unixFile *)sqlite3_malloc(sizeof(unixFile)); |
dan | 15edd58 | 2009-08-25 05:57:47 +0000 | [diff] [blame] | 4701 | if( !pNew ){ |
| 4702 | return SQLITE_NOMEM; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4703 | } |
| 4704 | memset(pNew, 0, sizeof(unixFile)); |
| 4705 | |
dan | 15edd58 | 2009-08-25 05:57:47 +0000 | [diff] [blame] | 4706 | /* Call unixOpen() to open the proxy file. The flags passed to unixOpen() |
| 4707 | ** suggest that the file being opened is a "main database". This is |
| 4708 | ** necessary as other file types do not necessarily support locking. It |
| 4709 | ** is better to use unixOpen() instead of opening the file directly with |
| 4710 | ** open(), as unixOpen() sets up the various mechanisms required to |
| 4711 | ** make sure a call to close() does not cause the system to discard |
| 4712 | ** POSIX locks prematurely. |
| 4713 | ** |
| 4714 | ** It is important that the xOpen member of the VFS object passed to |
| 4715 | ** unixOpen() is NULL. This tells unixOpen() may try to open a proxy-file |
| 4716 | ** for the proxy-file (creating a potential infinite loop). |
| 4717 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4718 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
dan | 15edd58 | 2009-08-25 05:57:47 +0000 | [diff] [blame] | 4719 | dummyVfs.xOpen = 0; |
| 4720 | rc = unixOpen(&dummyVfs, path, (sqlite3_file *)pNew, flags, &flags); |
| 4721 | if( rc==SQLITE_OK && (flags&SQLITE_OPEN_READONLY) ){ |
| 4722 | pNew->pMethod->xClose((sqlite3_file *)pNew); |
| 4723 | rc = SQLITE_CANTOPEN; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4724 | } |
dan | 15edd58 | 2009-08-25 05:57:47 +0000 | [diff] [blame] | 4725 | |
| 4726 | if( rc!=SQLITE_OK ){ |
| 4727 | sqlite3_free(pNew); |
| 4728 | pNew = 0; |
| 4729 | } |
| 4730 | |
| 4731 | *ppFile = pNew; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4732 | return rc; |
| 4733 | } |
| 4734 | |
| 4735 | /* takes the conch by taking a shared lock and read the contents conch, if |
| 4736 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 4737 | ** lockPath means that the lockPath in the conch file will be used if the |
| 4738 | ** host IDs match, or a new lock path will be generated automatically |
| 4739 | ** and written to the conch file. |
| 4740 | */ |
| 4741 | static int proxyTakeConch(unixFile *pFile){ |
| 4742 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 4743 | |
| 4744 | if( pCtx->conchHeld>0 ){ |
| 4745 | return SQLITE_OK; |
| 4746 | }else{ |
| 4747 | unixFile *conchFile = pCtx->conchFile; |
| 4748 | char testValue[CONCHLEN]; |
| 4749 | char conchValue[CONCHLEN]; |
| 4750 | char lockPath[MAXPATHLEN]; |
| 4751 | char *tLockPath = NULL; |
| 4752 | int rc = SQLITE_OK; |
| 4753 | int readRc = SQLITE_OK; |
| 4754 | int syncPerms = 0; |
| 4755 | |
| 4756 | OSTRACE4("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
| 4757 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid()); |
| 4758 | |
| 4759 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 4760 | if( rc==SQLITE_OK ){ |
| 4761 | int pError = 0; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4762 | memset(testValue, 0, CONCHLEN); /* conch is fixed size */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4763 | rc = proxyGetHostID(testValue, &pError); |
| 4764 | if( (rc&0xff)==SQLITE_IOERR ){ |
| 4765 | pFile->lastErrno = pError; |
| 4766 | } |
| 4767 | if( pCtx->lockProxyPath ){ |
| 4768 | strlcpy(&testValue[HOSTIDLEN], pCtx->lockProxyPath, MAXPATHLEN); |
| 4769 | } |
| 4770 | } |
| 4771 | if( rc!=SQLITE_OK ){ |
| 4772 | goto end_takeconch; |
| 4773 | } |
| 4774 | |
| 4775 | readRc = unixRead((sqlite3_file *)conchFile, conchValue, CONCHLEN, 0); |
| 4776 | if( readRc!=SQLITE_IOERR_SHORT_READ ){ |
| 4777 | if( readRc!=SQLITE_OK ){ |
| 4778 | if( (rc&0xff)==SQLITE_IOERR ){ |
| 4779 | pFile->lastErrno = conchFile->lastErrno; |
| 4780 | } |
| 4781 | rc = readRc; |
| 4782 | goto end_takeconch; |
| 4783 | } |
| 4784 | /* if the conch has data compare the contents */ |
| 4785 | if( !pCtx->lockProxyPath ){ |
| 4786 | /* for auto-named local lock file, just check the host ID and we'll |
| 4787 | ** use the local lock file path that's already in there */ |
| 4788 | if( !memcmp(testValue, conchValue, HOSTIDLEN) ){ |
| 4789 | tLockPath = (char *)&conchValue[HOSTIDLEN]; |
| 4790 | goto end_takeconch; |
| 4791 | } |
| 4792 | }else{ |
| 4793 | /* we've got the conch if conchValue matches our path and host ID */ |
| 4794 | if( !memcmp(testValue, conchValue, CONCHLEN) ){ |
| 4795 | goto end_takeconch; |
| 4796 | } |
| 4797 | } |
| 4798 | }else{ |
| 4799 | /* a short read means we're "creating" the conch (even though it could |
| 4800 | ** have been user-intervention), if we acquire the exclusive lock, |
| 4801 | ** we'll try to match the current on-disk permissions of the database |
| 4802 | */ |
| 4803 | syncPerms = 1; |
| 4804 | } |
| 4805 | |
| 4806 | /* either conch was emtpy or didn't match */ |
| 4807 | if( !pCtx->lockProxyPath ){ |
| 4808 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 4809 | tLockPath = lockPath; |
| 4810 | strlcpy(&testValue[HOSTIDLEN], lockPath, MAXPATHLEN); |
| 4811 | } |
| 4812 | |
| 4813 | /* update conch with host and path (this will fail if other process |
| 4814 | ** has a shared lock already) */ |
| 4815 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK); |
| 4816 | if( rc==SQLITE_OK ){ |
| 4817 | rc = unixWrite((sqlite3_file *)conchFile, testValue, CONCHLEN, 0); |
| 4818 | if( rc==SQLITE_OK && syncPerms ){ |
| 4819 | struct stat buf; |
| 4820 | int err = fstat(pFile->h, &buf); |
| 4821 | if( err==0 ){ |
| 4822 | /* try to match the database file permissions, ignore failure */ |
| 4823 | #ifndef SQLITE_PROXY_DEBUG |
| 4824 | fchmod(conchFile->h, buf.st_mode); |
| 4825 | #else |
| 4826 | if( fchmod(conchFile->h, buf.st_mode)!=0 ){ |
| 4827 | int code = errno; |
| 4828 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 4829 | buf.st_mode, code, strerror(code)); |
| 4830 | } else { |
| 4831 | fprintf(stderr, "fchmod %o SUCCEDED\n",buf.st_mode); |
| 4832 | } |
| 4833 | }else{ |
| 4834 | int code = errno; |
| 4835 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 4836 | err, code, strerror(code)); |
| 4837 | #endif |
| 4838 | } |
| 4839 | } |
| 4840 | } |
| 4841 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 4842 | |
| 4843 | end_takeconch: |
| 4844 | OSTRACE2("TRANSPROXY: CLOSE %d\n", pFile->h); |
| 4845 | if( rc==SQLITE_OK && pFile->openFlags ){ |
| 4846 | if( pFile->h>=0 ){ |
| 4847 | #ifdef STRICT_CLOSE_ERROR |
| 4848 | if( close(pFile->h) ){ |
| 4849 | pFile->lastErrno = errno; |
| 4850 | return SQLITE_IOERR_CLOSE; |
| 4851 | } |
| 4852 | #else |
| 4853 | close(pFile->h); /* silently leak fd if fail */ |
| 4854 | #endif |
| 4855 | } |
| 4856 | pFile->h = -1; |
| 4857 | int fd = open(pCtx->dbPath, pFile->openFlags, |
| 4858 | SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 4859 | OSTRACE2("TRANSPROXY: OPEN %d\n", fd); |
| 4860 | if( fd>=0 ){ |
| 4861 | pFile->h = fd; |
| 4862 | }else{ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4863 | rc=SQLITE_CANTOPEN; /* SQLITE_BUSY? proxyTakeConch called |
| 4864 | during locking */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4865 | } |
| 4866 | } |
| 4867 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 4868 | char *path = tLockPath ? tLockPath : pCtx->lockProxyPath; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4869 | /* ACS: Need to make a copy of path sometimes */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4870 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy); |
| 4871 | } |
| 4872 | if( rc==SQLITE_OK ){ |
| 4873 | pCtx->conchHeld = 1; |
| 4874 | |
| 4875 | if( tLockPath ){ |
| 4876 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tLockPath); |
| 4877 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 4878 | ((afpLockingContext *)pCtx->lockProxy->lockingContext)->dbPath = |
| 4879 | pCtx->lockProxyPath; |
| 4880 | } |
| 4881 | } |
| 4882 | } else { |
| 4883 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 4884 | } |
| 4885 | OSTRACE3("TAKECONCH %d %s\n", conchFile->h, rc==SQLITE_OK?"ok":"failed"); |
| 4886 | return rc; |
| 4887 | } |
| 4888 | } |
| 4889 | |
| 4890 | /* |
| 4891 | ** If pFile holds a lock on a conch file, then release that lock. |
| 4892 | */ |
| 4893 | static int proxyReleaseConch(unixFile *pFile){ |
| 4894 | int rc; /* Subroutine return code */ |
| 4895 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 4896 | unixFile *conchFile; /* Name of the conch file */ |
| 4897 | |
| 4898 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 4899 | conchFile = pCtx->conchFile; |
| 4900 | OSTRACE4("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
| 4901 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
| 4902 | getpid()); |
| 4903 | pCtx->conchHeld = 0; |
| 4904 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 4905 | OSTRACE3("RELEASECONCH %d %s\n", conchFile->h, |
| 4906 | (rc==SQLITE_OK ? "ok" : "failed")); |
| 4907 | return rc; |
| 4908 | } |
| 4909 | |
| 4910 | /* |
| 4911 | ** Given the name of a database file, compute the name of its conch file. |
| 4912 | ** Store the conch filename in memory obtained from sqlite3_malloc(). |
| 4913 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 4914 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 4915 | ** |
| 4916 | ** The caller is responsible for ensuring that the allocated memory |
| 4917 | ** space is eventually freed. |
| 4918 | ** |
| 4919 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 4920 | */ |
| 4921 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 4922 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4923 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4924 | char *conchPath; /* buffer in which to construct conch name */ |
| 4925 | |
| 4926 | /* Allocate space for the conch filename and initialize the name to |
| 4927 | ** the name of the original database file. */ |
| 4928 | *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8); |
| 4929 | if( conchPath==0 ){ |
| 4930 | return SQLITE_NOMEM; |
| 4931 | } |
| 4932 | memcpy(conchPath, dbPath, len+1); |
| 4933 | |
| 4934 | /* now insert a "." before the last / character */ |
| 4935 | for( i=(len-1); i>=0; i-- ){ |
| 4936 | if( conchPath[i]=='/' ){ |
| 4937 | i++; |
| 4938 | break; |
| 4939 | } |
| 4940 | } |
| 4941 | conchPath[i]='.'; |
| 4942 | while ( i<len ){ |
| 4943 | conchPath[i+1]=dbPath[i]; |
| 4944 | i++; |
| 4945 | } |
| 4946 | |
| 4947 | /* append the "-conch" suffix to the file */ |
| 4948 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4949 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4950 | |
| 4951 | return SQLITE_OK; |
| 4952 | } |
| 4953 | |
| 4954 | |
| 4955 | /* Takes a fully configured proxy locking-style unix file and switches |
| 4956 | ** the local lock file path |
| 4957 | */ |
| 4958 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 4959 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 4960 | char *oldPath = pCtx->lockProxyPath; |
| 4961 | int rc = SQLITE_OK; |
| 4962 | |
| 4963 | if( pFile->locktype!=NO_LOCK ){ |
| 4964 | return SQLITE_BUSY; |
| 4965 | } |
| 4966 | |
| 4967 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 4968 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 4969 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 4970 | return SQLITE_OK; |
| 4971 | }else{ |
| 4972 | unixFile *lockProxy = pCtx->lockProxy; |
| 4973 | pCtx->lockProxy=NULL; |
| 4974 | pCtx->conchHeld = 0; |
| 4975 | if( lockProxy!=NULL ){ |
| 4976 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 4977 | if( rc ) return rc; |
| 4978 | sqlite3_free(lockProxy); |
| 4979 | } |
| 4980 | sqlite3_free(oldPath); |
| 4981 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 4982 | } |
| 4983 | |
| 4984 | return rc; |
| 4985 | } |
| 4986 | |
| 4987 | /* |
| 4988 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 4989 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 4990 | ** |
| 4991 | ** This routine find the filename associated with pFile and writes it |
| 4992 | ** int dbPath. |
| 4993 | */ |
| 4994 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4995 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4996 | if( pFile->pMethod == &afpIoMethods ){ |
| 4997 | /* afp style keeps a reference to the db path in the filePath field |
| 4998 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4999 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5000 | strcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath); |
| 5001 | }else |
| 5002 | #endif |
| 5003 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 5004 | /* dot lock style uses the locking context to store the dot lock |
| 5005 | ** file path */ |
| 5006 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 5007 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 5008 | }else{ |
| 5009 | /* all other styles use the locking context to store the db file path */ |
| 5010 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
| 5011 | strcpy(dbPath, (char *)pFile->lockingContext); |
| 5012 | } |
| 5013 | return SQLITE_OK; |
| 5014 | } |
| 5015 | |
| 5016 | /* |
| 5017 | ** Takes an already filled in unix file and alters it so all file locking |
| 5018 | ** will be performed on the local proxy lock file. The following fields |
| 5019 | ** are preserved in the locking context so that they can be restored and |
| 5020 | ** the unix structure properly cleaned up at close time: |
| 5021 | ** ->lockingContext |
| 5022 | ** ->pMethod |
| 5023 | */ |
| 5024 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 5025 | proxyLockingContext *pCtx; |
| 5026 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 5027 | char *lockPath=NULL; |
| 5028 | int rc = SQLITE_OK; |
| 5029 | |
| 5030 | if( pFile->locktype!=NO_LOCK ){ |
| 5031 | return SQLITE_BUSY; |
| 5032 | } |
| 5033 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 5034 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 5035 | lockPath=NULL; |
| 5036 | }else{ |
| 5037 | lockPath=(char *)path; |
| 5038 | } |
| 5039 | |
| 5040 | OSTRACE4("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
| 5041 | (lockPath ? lockPath : ":auto:"), getpid()); |
| 5042 | |
| 5043 | pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 5044 | if( pCtx==0 ){ |
| 5045 | return SQLITE_NOMEM; |
| 5046 | } |
| 5047 | memset(pCtx, 0, sizeof(*pCtx)); |
| 5048 | |
| 5049 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 5050 | if( rc==SQLITE_OK ){ |
| 5051 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile); |
| 5052 | } |
| 5053 | if( rc==SQLITE_OK && lockPath ){ |
| 5054 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 5055 | } |
| 5056 | |
| 5057 | if( rc==SQLITE_OK ){ |
| 5058 | /* all memory is allocated, proxys are created and assigned, |
| 5059 | ** switch the locking context and pMethod then return. |
| 5060 | */ |
| 5061 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 5062 | pCtx->oldLockingContext = pFile->lockingContext; |
| 5063 | pFile->lockingContext = pCtx; |
| 5064 | pCtx->pOldMethod = pFile->pMethod; |
| 5065 | pFile->pMethod = &proxyIoMethods; |
| 5066 | }else{ |
| 5067 | if( pCtx->conchFile ){ |
| 5068 | rc = pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
| 5069 | if( rc ) return rc; |
| 5070 | sqlite3_free(pCtx->conchFile); |
| 5071 | } |
| 5072 | sqlite3_free(pCtx->conchFilePath); |
| 5073 | sqlite3_free(pCtx); |
| 5074 | } |
| 5075 | OSTRACE3("TRANSPROXY %d %s\n", pFile->h, |
| 5076 | (rc==SQLITE_OK ? "ok" : "failed")); |
| 5077 | return rc; |
| 5078 | } |
| 5079 | |
| 5080 | |
| 5081 | /* |
| 5082 | ** This routine handles sqlite3_file_control() calls that are specific |
| 5083 | ** to proxy locking. |
| 5084 | */ |
| 5085 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 5086 | switch( op ){ |
| 5087 | case SQLITE_GET_LOCKPROXYFILE: { |
| 5088 | unixFile *pFile = (unixFile*)id; |
| 5089 | if( pFile->pMethod == &proxyIoMethods ){ |
| 5090 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 5091 | proxyTakeConch(pFile); |
| 5092 | if( pCtx->lockProxyPath ){ |
| 5093 | *(const char **)pArg = pCtx->lockProxyPath; |
| 5094 | }else{ |
| 5095 | *(const char **)pArg = ":auto: (not held)"; |
| 5096 | } |
| 5097 | } else { |
| 5098 | *(const char **)pArg = NULL; |
| 5099 | } |
| 5100 | return SQLITE_OK; |
| 5101 | } |
| 5102 | case SQLITE_SET_LOCKPROXYFILE: { |
| 5103 | unixFile *pFile = (unixFile*)id; |
| 5104 | int rc = SQLITE_OK; |
| 5105 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 5106 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 5107 | if( isProxyStyle ){ |
| 5108 | /* turn off proxy locking - not supported */ |
| 5109 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 5110 | }else{ |
| 5111 | /* turn off proxy locking - already off - NOOP */ |
| 5112 | rc = SQLITE_OK; |
| 5113 | } |
| 5114 | }else{ |
| 5115 | const char *proxyPath = (const char *)pArg; |
| 5116 | if( isProxyStyle ){ |
| 5117 | proxyLockingContext *pCtx = |
| 5118 | (proxyLockingContext*)pFile->lockingContext; |
| 5119 | if( !strcmp(pArg, ":auto:") |
| 5120 | || (pCtx->lockProxyPath && |
| 5121 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 5122 | ){ |
| 5123 | rc = SQLITE_OK; |
| 5124 | }else{ |
| 5125 | rc = switchLockProxyPath(pFile, proxyPath); |
| 5126 | } |
| 5127 | }else{ |
| 5128 | /* turn on proxy file locking */ |
| 5129 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 5130 | } |
| 5131 | } |
| 5132 | return rc; |
| 5133 | } |
| 5134 | default: { |
| 5135 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 5136 | } |
| 5137 | } |
| 5138 | /*NOTREACHED*/ |
| 5139 | return SQLITE_ERROR; |
| 5140 | } |
| 5141 | |
| 5142 | /* |
| 5143 | ** Within this division (the proxying locking implementation) the procedures |
| 5144 | ** above this point are all utilities. The lock-related methods of the |
| 5145 | ** proxy-locking sqlite3_io_method object follow. |
| 5146 | */ |
| 5147 | |
| 5148 | |
| 5149 | /* |
| 5150 | ** This routine checks if there is a RESERVED lock held on the specified |
| 5151 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 5152 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 5153 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 5154 | */ |
| 5155 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 5156 | unixFile *pFile = (unixFile*)id; |
| 5157 | int rc = proxyTakeConch(pFile); |
| 5158 | if( rc==SQLITE_OK ){ |
| 5159 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5160 | unixFile *proxy = pCtx->lockProxy; |
| 5161 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 5162 | } |
| 5163 | return rc; |
| 5164 | } |
| 5165 | |
| 5166 | /* |
| 5167 | ** Lock the file with the lock specified by parameter locktype - one |
| 5168 | ** of the following: |
| 5169 | ** |
| 5170 | ** (1) SHARED_LOCK |
| 5171 | ** (2) RESERVED_LOCK |
| 5172 | ** (3) PENDING_LOCK |
| 5173 | ** (4) EXCLUSIVE_LOCK |
| 5174 | ** |
| 5175 | ** Sometimes when requesting one lock state, additional lock states |
| 5176 | ** are inserted in between. The locking might fail on one of the later |
| 5177 | ** transitions leaving the lock state different from what it started but |
| 5178 | ** still short of its goal. The following chart shows the allowed |
| 5179 | ** transitions and the inserted intermediate states: |
| 5180 | ** |
| 5181 | ** UNLOCKED -> SHARED |
| 5182 | ** SHARED -> RESERVED |
| 5183 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 5184 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 5185 | ** PENDING -> EXCLUSIVE |
| 5186 | ** |
| 5187 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 5188 | ** routine to lower a locking level. |
| 5189 | */ |
| 5190 | static int proxyLock(sqlite3_file *id, int locktype) { |
| 5191 | unixFile *pFile = (unixFile*)id; |
| 5192 | int rc = proxyTakeConch(pFile); |
| 5193 | if( rc==SQLITE_OK ){ |
| 5194 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5195 | unixFile *proxy = pCtx->lockProxy; |
| 5196 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, locktype); |
| 5197 | pFile->locktype = proxy->locktype; |
| 5198 | } |
| 5199 | return rc; |
| 5200 | } |
| 5201 | |
| 5202 | |
| 5203 | /* |
| 5204 | ** Lower the locking level on file descriptor pFile to locktype. locktype |
| 5205 | ** must be either NO_LOCK or SHARED_LOCK. |
| 5206 | ** |
| 5207 | ** If the locking level of the file descriptor is already at or below |
| 5208 | ** the requested locking level, this routine is a no-op. |
| 5209 | */ |
| 5210 | static int proxyUnlock(sqlite3_file *id, int locktype) { |
| 5211 | unixFile *pFile = (unixFile*)id; |
| 5212 | int rc = proxyTakeConch(pFile); |
| 5213 | if( rc==SQLITE_OK ){ |
| 5214 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5215 | unixFile *proxy = pCtx->lockProxy; |
| 5216 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, locktype); |
| 5217 | pFile->locktype = proxy->locktype; |
| 5218 | } |
| 5219 | return rc; |
| 5220 | } |
| 5221 | |
| 5222 | /* |
| 5223 | ** Close a file that uses proxy locks. |
| 5224 | */ |
| 5225 | static int proxyClose(sqlite3_file *id) { |
| 5226 | if( id ){ |
| 5227 | unixFile *pFile = (unixFile*)id; |
| 5228 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5229 | unixFile *lockProxy = pCtx->lockProxy; |
| 5230 | unixFile *conchFile = pCtx->conchFile; |
| 5231 | int rc = SQLITE_OK; |
| 5232 | |
| 5233 | if( lockProxy ){ |
| 5234 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 5235 | if( rc ) return rc; |
| 5236 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 5237 | if( rc ) return rc; |
| 5238 | sqlite3_free(lockProxy); |
| 5239 | pCtx->lockProxy = 0; |
| 5240 | } |
| 5241 | if( conchFile ){ |
| 5242 | if( pCtx->conchHeld ){ |
| 5243 | rc = proxyReleaseConch(pFile); |
| 5244 | if( rc ) return rc; |
| 5245 | } |
| 5246 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 5247 | if( rc ) return rc; |
| 5248 | sqlite3_free(conchFile); |
| 5249 | } |
| 5250 | sqlite3_free(pCtx->lockProxyPath); |
| 5251 | sqlite3_free(pCtx->conchFilePath); |
| 5252 | sqlite3_free(pCtx->dbPath); |
| 5253 | /* restore the original locking context and pMethod then close it */ |
| 5254 | pFile->lockingContext = pCtx->oldLockingContext; |
| 5255 | pFile->pMethod = pCtx->pOldMethod; |
| 5256 | sqlite3_free(pCtx); |
| 5257 | return pFile->pMethod->xClose(id); |
| 5258 | } |
| 5259 | return SQLITE_OK; |
| 5260 | } |
| 5261 | |
| 5262 | |
| 5263 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5264 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5265 | /* |
| 5266 | ** The proxy locking style is intended for use with AFP filesystems. |
| 5267 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 5268 | ** restricted to MacOSX. |
| 5269 | ** |
| 5270 | ** |
| 5271 | ******************* End of the proxy lock implementation ********************** |
| 5272 | ******************************************************************************/ |
| 5273 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5274 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5275 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5276 | ** |
| 5277 | ** This routine registers all VFS implementations for unix-like operating |
| 5278 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 5279 | ** should be the only routines in this file that are visible from other |
| 5280 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5281 | ** |
| 5282 | ** This routine is called once during SQLite initialization and by a |
| 5283 | ** single thread. The memory allocation and mutex subsystems have not |
| 5284 | ** necessarily been initialized when this routine is called, and so they |
| 5285 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 5286 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 5287 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5288 | /* |
| 5289 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5290 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 5291 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 5292 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 5293 | ** and so we have to go through the intermediate pointer to avoid problems |
| 5294 | ** when compiling with -pedantic-errors on GCC.) |
| 5295 | ** |
| 5296 | ** 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] | 5297 | ** finder-function. The finder-function returns a pointer to the |
| 5298 | ** sqlite_io_methods object that implements the desired locking |
| 5299 | ** behaviors. See the division above that contains the IOMETHODS |
| 5300 | ** macro for addition information on finder-functions. |
| 5301 | ** |
| 5302 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 5303 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 5304 | ** more than that; it looks at the filesystem type that hosts the |
| 5305 | ** database file and tries to choose an locking method appropriate for |
| 5306 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5307 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5308 | #define UNIXVFS(VFSNAME, FINDER) { \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5309 | 1, /* iVersion */ \ |
| 5310 | sizeof(unixFile), /* szOsFile */ \ |
| 5311 | MAX_PATHNAME, /* mxPathname */ \ |
| 5312 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5313 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5314 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5315 | unixOpen, /* xOpen */ \ |
| 5316 | unixDelete, /* xDelete */ \ |
| 5317 | unixAccess, /* xAccess */ \ |
| 5318 | unixFullPathname, /* xFullPathname */ \ |
| 5319 | unixDlOpen, /* xDlOpen */ \ |
| 5320 | unixDlError, /* xDlError */ \ |
| 5321 | unixDlSym, /* xDlSym */ \ |
| 5322 | unixDlClose, /* xDlClose */ \ |
| 5323 | unixRandomness, /* xRandomness */ \ |
| 5324 | unixSleep, /* xSleep */ \ |
| 5325 | unixCurrentTime, /* xCurrentTime */ \ |
| 5326 | unixGetLastError /* xGetLastError */ \ |
| 5327 | } |
| 5328 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5329 | /* |
| 5330 | ** All default VFSes for unix are contained in the following array. |
| 5331 | ** |
| 5332 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 5333 | ** by the SQLite core when the VFS is registered. So the following |
| 5334 | ** array cannot be const. |
| 5335 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5336 | static sqlite3_vfs aVfs[] = { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5337 | #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__)) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5338 | UNIXVFS("unix", autolockIoFinder ), |
| 5339 | #else |
| 5340 | UNIXVFS("unix", posixIoFinder ), |
| 5341 | #endif |
| 5342 | UNIXVFS("unix-none", nolockIoFinder ), |
| 5343 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5344 | UNIXVFS("unix-wfl", posixWflIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5345 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5346 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5347 | #endif |
| 5348 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5349 | UNIXVFS("unix-posix", posixIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5350 | #if !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5351 | UNIXVFS("unix-flock", flockIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5352 | #endif |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5353 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5354 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5355 | UNIXVFS("unix-afp", afpIoFinder ), |
| 5356 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5357 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 5358 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5359 | unsigned int i; /* Loop counter */ |
| 5360 | |
| 5361 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5362 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5363 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5364 | } |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 5365 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 5366 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5367 | |
| 5368 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5369 | ** Shutdown the operating system interface. |
| 5370 | ** |
| 5371 | ** Some operating systems might need to do some cleanup in this routine, |
| 5372 | ** to release dynamically allocated objects. But not on unix. |
| 5373 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5374 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 5375 | int sqlite3_os_end(void){ |
| 5376 | return SQLITE_OK; |
| 5377 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 5378 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 5379 | #endif /* SQLITE_OS_UNIX */ |