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