drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2004 May 22 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 13 | ** This file contains the VFS implementation for unix-like operating systems |
| 14 | ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others. |
danielk1977 | 822a516 | 2008-05-16 04:51:54 +0000 | [diff] [blame] | 15 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 16 | ** There are actually several different VFS implementations in this file. |
| 17 | ** The differences are in the way that file locking is done. The default |
| 18 | ** implementation uses Posix Advisory Locks. Alternative implementations |
| 19 | ** use flock(), dot-files, various proprietary locking schemas, or simply |
| 20 | ** skip locking all together. |
| 21 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 22 | ** This source file is organized into divisions where the logic for various |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 23 | ** subfunctions is contained within the appropriate division. PLEASE |
| 24 | ** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed |
| 25 | ** in the correct division and should be clearly labeled. |
| 26 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 27 | ** The layout of divisions is as follows: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 28 | ** |
| 29 | ** * General-purpose declarations and utility functions. |
| 30 | ** * Unique file ID logic used by VxWorks. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 31 | ** * Various locking primitive implementations (all except proxy locking): |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 32 | ** + for Posix Advisory Locks |
| 33 | ** + for no-op locks |
| 34 | ** + for dot-file locks |
| 35 | ** + for flock() locking |
| 36 | ** + for named semaphore locks (VxWorks only) |
| 37 | ** + for AFP filesystem locks (MacOSX only) |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 38 | ** * sqlite3_file methods not associated with locking. |
| 39 | ** * Definitions of sqlite3_io_methods objects for all locking |
| 40 | ** methods plus "finder" functions for each locking method. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 41 | ** * sqlite3_vfs method implementations. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 42 | ** * Locking primitives for the proxy uber-locking-method. (MacOSX only) |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 43 | ** * Definitions of sqlite3_vfs objects for all locking methods |
| 44 | ** plus implementations of sqlite3_os_init() and sqlite3_os_end(). |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 45 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 46 | #include "sqliteInt.h" |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 47 | #if SQLITE_OS_UNIX /* This file is used on unix only */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 48 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 49 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 50 | ** There are various methods for file locking used for concurrency |
| 51 | ** control: |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 52 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 53 | ** 1. POSIX locking (the default), |
| 54 | ** 2. No locking, |
| 55 | ** 3. Dot-file locking, |
| 56 | ** 4. flock() locking, |
| 57 | ** 5. AFP locking (OSX only), |
| 58 | ** 6. Named POSIX semaphores (VXWorks only), |
| 59 | ** 7. proxy locking. (OSX only) |
| 60 | ** |
| 61 | ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE |
| 62 | ** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic |
| 63 | ** selection of the appropriate locking style based on the filesystem |
| 64 | ** where the database is located. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 65 | */ |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 66 | #if !defined(SQLITE_ENABLE_LOCKING_STYLE) |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 67 | # if defined(__APPLE__) |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 68 | # define SQLITE_ENABLE_LOCKING_STYLE 1 |
| 69 | # else |
| 70 | # define SQLITE_ENABLE_LOCKING_STYLE 0 |
| 71 | # endif |
| 72 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 73 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 74 | /* |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 75 | ** standard include files. |
| 76 | */ |
| 77 | #include <sys/types.h> |
| 78 | #include <sys/stat.h> |
| 79 | #include <fcntl.h> |
| 80 | #include <unistd.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 81 | #include <time.h> |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 82 | #include <sys/time.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 83 | #include <errno.h> |
dan | 32c12fe | 2013-05-02 17:37:31 +0000 | [diff] [blame] | 84 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | 91be7dc | 2014-08-11 13:53:30 +0000 | [diff] [blame] | 85 | # include <sys/mman.h> |
drh | b469f46 | 2010-12-22 21:48:50 +0000 | [diff] [blame] | 86 | #endif |
drh | 1da88f0 | 2011-12-17 16:09:16 +0000 | [diff] [blame] | 87 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 88 | #if SQLITE_ENABLE_LOCKING_STYLE |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 89 | # include <sys/ioctl.h> |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 90 | # include <sys/file.h> |
| 91 | # include <sys/param.h> |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 92 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 93 | |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 94 | #if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \ |
| 95 | (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000)) |
| 96 | # if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \ |
| 97 | && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0)) |
| 98 | # define HAVE_GETHOSTUUID 1 |
| 99 | # else |
| 100 | # warning "gethostuuid() is disabled." |
| 101 | # endif |
| 102 | #endif |
| 103 | |
| 104 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 105 | #if OS_VXWORKS |
| 106 | # include <sys/ioctl.h> |
| 107 | # include <semaphore.h> |
| 108 | # include <limits.h> |
| 109 | #endif /* OS_VXWORKS */ |
| 110 | |
| 111 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 84a2bf6 | 2010-03-05 13:41:06 +0000 | [diff] [blame] | 112 | # include <sys/mount.h> |
| 113 | #endif |
| 114 | |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 115 | #ifdef HAVE_UTIME |
| 116 | # include <utime.h> |
| 117 | #endif |
| 118 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 119 | /* |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 120 | ** Allowed values of unixFile.fsFlags |
| 121 | */ |
| 122 | #define SQLITE_FSFLAGS_IS_MSDOS 0x1 |
| 123 | |
| 124 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 125 | ** If we are to be thread-safe, include the pthreads header and define |
| 126 | ** the SQLITE_UNIX_THREADS macro. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 127 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 128 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 129 | # include <pthread.h> |
| 130 | # define SQLITE_UNIX_THREADS 1 |
| 131 | #endif |
| 132 | |
| 133 | /* |
| 134 | ** Default permissions when creating a new file |
| 135 | */ |
| 136 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 137 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 138 | #endif |
| 139 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 140 | /* |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 141 | ** Default permissions when creating auto proxy dir |
| 142 | */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 143 | #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 144 | # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 |
| 145 | #endif |
| 146 | |
| 147 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 148 | ** Maximum supported path-length. |
| 149 | */ |
| 150 | #define MAX_PATHNAME 512 |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 151 | |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 152 | /* Always cast the getpid() return type for compatibility with |
| 153 | ** kernel modules in VxWorks. */ |
| 154 | #define osGetpid(X) (pid_t)getpid() |
| 155 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 156 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 157 | ** Only set the lastErrno if the error code is a real error and not |
| 158 | ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 159 | */ |
| 160 | #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |
| 161 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 162 | /* Forward references */ |
| 163 | typedef struct unixShm unixShm; /* Connection shared memory */ |
| 164 | typedef struct unixShmNode unixShmNode; /* Shared memory instance */ |
| 165 | typedef struct unixInodeInfo unixInodeInfo; /* An i-node */ |
| 166 | typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 167 | |
| 168 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 169 | ** Sometimes, after a file handle is closed by SQLite, the file descriptor |
| 170 | ** cannot be closed immediately. In these cases, instances of the following |
| 171 | ** structure are used to store the file descriptor while waiting for an |
| 172 | ** opportunity to either close or reuse it. |
| 173 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 174 | struct UnixUnusedFd { |
| 175 | int fd; /* File descriptor to close */ |
| 176 | int flags; /* Flags this file descriptor was opened with */ |
| 177 | UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ |
| 178 | }; |
| 179 | |
| 180 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 181 | ** The unixFile structure is subclass of sqlite3_file specific to the unix |
| 182 | ** VFS implementations. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 183 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 184 | typedef struct unixFile unixFile; |
| 185 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 186 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 187 | sqlite3_vfs *pVfs; /* The VFS that created this unixFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 188 | unixInodeInfo *pInode; /* Info about locks on this inode */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 189 | int h; /* The file descriptor */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 190 | unsigned char eFileLock; /* The type of lock held on this fd */ |
drh | 3ee3484 | 2012-02-11 21:21:17 +0000 | [diff] [blame] | 191 | unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 192 | int lastErrno; /* The unix errno from last I/O error */ |
| 193 | void *lockingContext; /* Locking style specific state */ |
| 194 | UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 195 | const char *zPath; /* Name of the file */ |
| 196 | unixShm *pShm; /* Shared memory segment information */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 197 | int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 198 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 199 | int nFetchOut; /* Number of outstanding xFetch refs */ |
| 200 | sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 201 | sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */ |
| 202 | sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 203 | void *pMapRegion; /* Memory mapped region */ |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 204 | #endif |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 205 | #ifdef __QNXNTO__ |
| 206 | int sectorSize; /* Device sector size */ |
| 207 | int deviceCharacteristics; /* Precomputed device characteristics */ |
| 208 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 209 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 210 | int openFlags; /* The flags specified at open() */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 211 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 212 | #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 213 | unsigned fsFlags; /* cached details from statfs() */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 214 | #endif |
| 215 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 216 | struct vxworksFileId *pId; /* Unique file ID */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 217 | #endif |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 218 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 219 | /* The next group of variables are used to track whether or not the |
| 220 | ** transaction counter in bytes 24-27 of database files are updated |
| 221 | ** whenever any part of the database changes. An assertion fault will |
| 222 | ** occur if a file is updated without also updating the transaction |
| 223 | ** counter. This test is made to avoid new problems similar to the |
| 224 | ** one described by ticket #3584. |
| 225 | */ |
| 226 | unsigned char transCntrChng; /* True if the transaction counter changed */ |
| 227 | unsigned char dbUpdate; /* True if any part of database file changed */ |
| 228 | unsigned char inNormalWrite; /* True if in a normal write operation */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 229 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 230 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 231 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 232 | #ifdef SQLITE_TEST |
| 233 | /* In test mode, increase the size of this structure a bit so that |
| 234 | ** it is larger than the struct CrashFile defined in test6.c. |
| 235 | */ |
| 236 | char aPadding[32]; |
| 237 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 240 | /* This variable holds the process id (pid) from when the xRandomness() |
| 241 | ** method was called. If xOpen() is called from a different process id, |
| 242 | ** indicating that a fork() has occurred, the PRNG will be reset. |
| 243 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 244 | static pid_t randomnessPid = 0; |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 245 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 246 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 247 | ** Allowed values for the unixFile.ctrlFlags bitmask: |
| 248 | */ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 249 | #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ |
| 250 | #define UNIXFILE_RDONLY 0x02 /* Connection is read only */ |
| 251 | #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ |
dan | ee140c4 | 2011-08-25 13:46:32 +0000 | [diff] [blame] | 252 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 253 | # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ |
| 254 | #else |
| 255 | # define UNIXFILE_DIRSYNC 0x00 |
| 256 | #endif |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 257 | #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 258 | #define UNIXFILE_DELETE 0x20 /* Delete on close */ |
| 259 | #define UNIXFILE_URI 0x40 /* Filename might have query parameters */ |
| 260 | #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 261 | |
| 262 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 263 | ** Include code that is common to all os_*.c files |
| 264 | */ |
| 265 | #include "os_common.h" |
| 266 | |
| 267 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 268 | ** Define various macros that are missing from some systems. |
| 269 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 270 | #ifndef O_LARGEFILE |
| 271 | # define O_LARGEFILE 0 |
| 272 | #endif |
| 273 | #ifdef SQLITE_DISABLE_LFS |
| 274 | # undef O_LARGEFILE |
| 275 | # define O_LARGEFILE 0 |
| 276 | #endif |
| 277 | #ifndef O_NOFOLLOW |
| 278 | # define O_NOFOLLOW 0 |
| 279 | #endif |
| 280 | #ifndef O_BINARY |
| 281 | # define O_BINARY 0 |
| 282 | #endif |
| 283 | |
| 284 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 285 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 286 | ** testing and debugging only. |
| 287 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 288 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 289 | #define threadid pthread_self() |
| 290 | #else |
| 291 | #define threadid 0 |
| 292 | #endif |
| 293 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 294 | /* |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 295 | ** HAVE_MREMAP defaults to true on Linux and false everywhere else. |
| 296 | */ |
| 297 | #if !defined(HAVE_MREMAP) |
| 298 | # if defined(__linux__) && defined(_GNU_SOURCE) |
| 299 | # define HAVE_MREMAP 1 |
| 300 | # else |
| 301 | # define HAVE_MREMAP 0 |
| 302 | # endif |
| 303 | #endif |
| 304 | |
| 305 | /* |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 306 | ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek() |
| 307 | ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined. |
| 308 | */ |
| 309 | #ifdef __ANDROID__ |
| 310 | # define lseek lseek64 |
| 311 | #endif |
| 312 | |
| 313 | /* |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 314 | ** Different Unix systems declare open() in different ways. Same use |
| 315 | ** open(const char*,int,mode_t). Others use open(const char*,int,...). |
| 316 | ** The difference is important when using a pointer to the function. |
| 317 | ** |
| 318 | ** The safest way to deal with the problem is to always use this wrapper |
| 319 | ** which always has the same well-defined interface. |
| 320 | */ |
| 321 | static int posixOpen(const char *zFile, int flags, int mode){ |
| 322 | return open(zFile, flags, mode); |
| 323 | } |
| 324 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 325 | /* Forward reference */ |
| 326 | static int openDirectory(const char*, int*); |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 327 | static int unixGetpagesize(void); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 328 | |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 329 | /* |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 330 | ** Many system calls are accessed through pointer-to-functions so that |
| 331 | ** they may be overridden at runtime to facilitate fault injection during |
| 332 | ** testing and sandboxing. The following array holds the names and pointers |
| 333 | ** to all overrideable system calls. |
| 334 | */ |
| 335 | static struct unix_syscall { |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 336 | const char *zName; /* Name of the system call */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 337 | sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ |
| 338 | sqlite3_syscall_ptr pDefault; /* Default value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 339 | } aSyscall[] = { |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 340 | { "open", (sqlite3_syscall_ptr)posixOpen, 0 }, |
| 341 | #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent) |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 342 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 343 | { "close", (sqlite3_syscall_ptr)close, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 344 | #define osClose ((int(*)(int))aSyscall[1].pCurrent) |
| 345 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 346 | { "access", (sqlite3_syscall_ptr)access, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 347 | #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent) |
| 348 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 349 | { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 350 | #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent) |
| 351 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 352 | { "stat", (sqlite3_syscall_ptr)stat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 353 | #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) |
| 354 | |
| 355 | /* |
| 356 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 357 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 358 | ** that always succeeds. This means that locking does not occur under |
| 359 | ** DJGPP. But it is DOS - what did you expect? |
| 360 | */ |
| 361 | #ifdef __DJGPP__ |
| 362 | { "fstat", 0, 0 }, |
| 363 | #define osFstat(a,b,c) 0 |
| 364 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 365 | { "fstat", (sqlite3_syscall_ptr)fstat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 366 | #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent) |
| 367 | #endif |
| 368 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 369 | { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 370 | #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent) |
| 371 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 372 | { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 373 | #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 374 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 375 | { "read", (sqlite3_syscall_ptr)read, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 376 | #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) |
| 377 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 378 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 379 | { "pread", (sqlite3_syscall_ptr)pread, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 380 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 381 | { "pread", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 382 | #endif |
| 383 | #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) |
| 384 | |
| 385 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 386 | { "pread64", (sqlite3_syscall_ptr)pread64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 387 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 388 | { "pread64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 389 | #endif |
| 390 | #define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent) |
| 391 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 392 | { "write", (sqlite3_syscall_ptr)write, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 393 | #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) |
| 394 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 395 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 396 | { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 397 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 398 | { "pwrite", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 399 | #endif |
| 400 | #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 401 | aSyscall[12].pCurrent) |
| 402 | |
| 403 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 404 | { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 405 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 406 | { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 407 | #endif |
| 408 | #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 409 | aSyscall[13].pCurrent) |
| 410 | |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 411 | { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 412 | #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 413 | |
| 414 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 415 | { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 416 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 417 | { "fallocate", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 418 | #endif |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 419 | #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 420 | |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 421 | { "unlink", (sqlite3_syscall_ptr)unlink, 0 }, |
| 422 | #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent) |
| 423 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 424 | { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 }, |
| 425 | #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent) |
| 426 | |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 427 | { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 }, |
| 428 | #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent) |
| 429 | |
| 430 | { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 }, |
| 431 | #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent) |
| 432 | |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 433 | { "fchown", (sqlite3_syscall_ptr)fchown, 0 }, |
dan | d3eaebd | 2012-02-13 08:50:23 +0000 | [diff] [blame] | 434 | #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent) |
drh | 23c4b97 | 2012-02-11 23:55:15 +0000 | [diff] [blame] | 435 | |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 436 | { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 }, |
| 437 | #define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent) |
| 438 | |
dan | 4dd5144 | 2013-08-26 14:30:25 +0000 | [diff] [blame] | 439 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
dan | 893c0ff | 2013-03-25 19:05:07 +0000 | [diff] [blame] | 440 | { "mmap", (sqlite3_syscall_ptr)mmap, 0 }, |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 441 | #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent) |
dan | 893c0ff | 2013-03-25 19:05:07 +0000 | [diff] [blame] | 442 | |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 443 | { "munmap", (sqlite3_syscall_ptr)munmap, 0 }, |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 444 | #define osMunmap ((void*(*)(void*,size_t))aSyscall[23].pCurrent) |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 445 | |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 446 | #if HAVE_MREMAP |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 447 | { "mremap", (sqlite3_syscall_ptr)mremap, 0 }, |
| 448 | #else |
| 449 | { "mremap", (sqlite3_syscall_ptr)0, 0 }, |
| 450 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 451 | #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent) |
| 452 | |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 453 | { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 }, |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 454 | #define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent) |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 455 | |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 456 | { "readlink", (sqlite3_syscall_ptr)readlink, 0 }, |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 457 | #define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent) |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 458 | |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 459 | #endif |
| 460 | |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 461 | }; /* End of the overrideable system calls */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 462 | |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 463 | |
| 464 | /* |
| 465 | ** On some systems, calls to fchown() will trigger a message in a security |
| 466 | ** log if they come from non-root processes. So avoid calling fchown() if |
| 467 | ** we are not running as root. |
| 468 | */ |
| 469 | static int robustFchown(int fd, uid_t uid, gid_t gid){ |
| 470 | #if OS_VXWORKS |
| 471 | return 0; |
| 472 | #else |
| 473 | return osGeteuid() ? 0 : osFchown(fd,uid,gid); |
| 474 | #endif |
| 475 | } |
| 476 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 477 | /* |
| 478 | ** This is the xSetSystemCall() method of sqlite3_vfs for all of the |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 479 | ** "unix" VFSes. Return SQLITE_OK opon successfully updating the |
| 480 | ** system call pointer, or SQLITE_NOTFOUND if there is no configurable |
| 481 | ** system call named zName. |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 482 | */ |
| 483 | static int unixSetSystemCall( |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 484 | sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ |
| 485 | const char *zName, /* Name of system call to override */ |
| 486 | sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 487 | ){ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 488 | unsigned int i; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 489 | int rc = SQLITE_NOTFOUND; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 490 | |
| 491 | UNUSED_PARAMETER(pNotUsed); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 492 | if( zName==0 ){ |
| 493 | /* If no zName is given, restore all system calls to their default |
| 494 | ** settings and return NULL |
| 495 | */ |
dan | 51438a7 | 2011-04-02 17:00:47 +0000 | [diff] [blame] | 496 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 497 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 498 | if( aSyscall[i].pDefault ){ |
| 499 | aSyscall[i].pCurrent = aSyscall[i].pDefault; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | }else{ |
| 503 | /* If zName is specified, operate on only the one system call |
| 504 | ** specified. |
| 505 | */ |
| 506 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 507 | if( strcmp(zName, aSyscall[i].zName)==0 ){ |
| 508 | if( aSyscall[i].pDefault==0 ){ |
| 509 | aSyscall[i].pDefault = aSyscall[i].pCurrent; |
| 510 | } |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 511 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 512 | if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault; |
| 513 | aSyscall[i].pCurrent = pNewFunc; |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | return rc; |
| 519 | } |
| 520 | |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 521 | /* |
| 522 | ** Return the value of a system call. Return NULL if zName is not a |
| 523 | ** recognized system call name. NULL is also returned if the system call |
| 524 | ** is currently undefined. |
| 525 | */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 526 | static sqlite3_syscall_ptr unixGetSystemCall( |
| 527 | sqlite3_vfs *pNotUsed, |
| 528 | const char *zName |
| 529 | ){ |
| 530 | unsigned int i; |
| 531 | |
| 532 | UNUSED_PARAMETER(pNotUsed); |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 533 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 534 | if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent; |
| 535 | } |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | ** Return the name of the first system call after zName. If zName==NULL |
| 541 | ** then return the name of the first system call. Return NULL if zName |
| 542 | ** is the last system call or if zName is not the name of a valid |
| 543 | ** system call. |
| 544 | */ |
| 545 | static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){ |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 546 | int i = -1; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 547 | |
| 548 | UNUSED_PARAMETER(p); |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 549 | if( zName ){ |
| 550 | for(i=0; i<ArraySize(aSyscall)-1; i++){ |
| 551 | if( strcmp(zName, aSyscall[i].zName)==0 ) break; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 554 | for(i++; i<ArraySize(aSyscall); i++){ |
| 555 | if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 556 | } |
| 557 | return 0; |
| 558 | } |
| 559 | |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 560 | /* |
drh | 77a3fdc | 2013-08-30 14:24:12 +0000 | [diff] [blame] | 561 | ** Do not accept any file descriptor less than this value, in order to avoid |
| 562 | ** opening database file using file descriptors that are commonly used for |
| 563 | ** standard input, output, and error. |
| 564 | */ |
| 565 | #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR |
| 566 | # define SQLITE_MINIMUM_FILE_DESCRIPTOR 3 |
| 567 | #endif |
| 568 | |
| 569 | /* |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 570 | ** Invoke open(). Do so multiple times, until it either succeeds or |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 571 | ** fails for some reason other than EINTR. |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 572 | ** |
| 573 | ** If the file creation mode "m" is 0 then set it to the default for |
| 574 | ** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally |
| 575 | ** 0644) as modified by the system umask. If m is not 0, then |
| 576 | ** make the file creation mode be exactly m ignoring the umask. |
| 577 | ** |
| 578 | ** The m parameter will be non-zero only when creating -wal, -journal, |
| 579 | ** and -shm files. We want those files to have *exactly* the same |
| 580 | ** permissions as their original database, unadulterated by the umask. |
| 581 | ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a |
| 582 | ** transaction crashes and leaves behind hot journals, then any |
| 583 | ** process that is able to write to the database will also be able to |
| 584 | ** recover the hot journals. |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 585 | */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 586 | static int robust_open(const char *z, int f, mode_t m){ |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 587 | int fd; |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 588 | mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS; |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 589 | while(1){ |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 590 | #if defined(O_CLOEXEC) |
| 591 | fd = osOpen(z,f|O_CLOEXEC,m2); |
| 592 | #else |
| 593 | fd = osOpen(z,f,m2); |
| 594 | #endif |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 595 | if( fd<0 ){ |
| 596 | if( errno==EINTR ) continue; |
| 597 | break; |
| 598 | } |
drh | 77a3fdc | 2013-08-30 14:24:12 +0000 | [diff] [blame] | 599 | if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break; |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 600 | osClose(fd); |
| 601 | sqlite3_log(SQLITE_WARNING, |
| 602 | "attempt to open \"%s\" as file descriptor %d", z, fd); |
| 603 | fd = -1; |
| 604 | if( osOpen("/dev/null", f, m)<0 ) break; |
| 605 | } |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 606 | if( fd>=0 ){ |
| 607 | if( m!=0 ){ |
| 608 | struct stat statbuf; |
dan | b83c21e | 2013-03-05 15:27:34 +0000 | [diff] [blame] | 609 | if( osFstat(fd, &statbuf)==0 |
| 610 | && statbuf.st_size==0 |
drh | cfc1769 | 2013-03-06 01:41:53 +0000 | [diff] [blame] | 611 | && (statbuf.st_mode&0777)!=m |
dan | b83c21e | 2013-03-05 15:27:34 +0000 | [diff] [blame] | 612 | ){ |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 613 | osFchmod(fd, m); |
| 614 | } |
| 615 | } |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 616 | #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0) |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 617 | osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 618 | #endif |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 619 | } |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 620 | return fd; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 621 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 622 | |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 623 | /* |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 624 | ** Helper functions to obtain and relinquish the global mutex. The |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 625 | ** global mutex is used to protect the unixInodeInfo and |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 626 | ** vxworksFileId objects used by this file, all of which may be |
| 627 | ** shared by multiple threads. |
| 628 | ** |
| 629 | ** Function unixMutexHeld() is used to assert() that the global mutex |
| 630 | ** is held when required. This function is only used as part of assert() |
| 631 | ** statements. e.g. |
| 632 | ** |
| 633 | ** unixEnterMutex() |
| 634 | ** assert( unixMutexHeld() ); |
| 635 | ** unixEnterLeave() |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 636 | */ |
| 637 | static void unixEnterMutex(void){ |
mistachkin | 93de653 | 2015-07-03 21:38:09 +0000 | [diff] [blame] | 638 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 639 | } |
| 640 | static void unixLeaveMutex(void){ |
mistachkin | 93de653 | 2015-07-03 21:38:09 +0000 | [diff] [blame] | 641 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 642 | } |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 643 | #ifdef SQLITE_DEBUG |
| 644 | static int unixMutexHeld(void) { |
mistachkin | 93de653 | 2015-07-03 21:38:09 +0000 | [diff] [blame] | 645 | return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 646 | } |
| 647 | #endif |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 648 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 649 | |
mistachkin | fb383e9 | 2015-04-16 03:24:38 +0000 | [diff] [blame] | 650 | #ifdef SQLITE_HAVE_OS_TRACE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 651 | /* |
| 652 | ** Helper function for printing out trace information from debugging |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 653 | ** binaries. This returns the string representation of the supplied |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 654 | ** integer lock-type. |
| 655 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 656 | static const char *azFileLock(int eFileLock){ |
| 657 | switch( eFileLock ){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 658 | case NO_LOCK: return "NONE"; |
| 659 | case SHARED_LOCK: return "SHARED"; |
| 660 | case RESERVED_LOCK: return "RESERVED"; |
| 661 | case PENDING_LOCK: return "PENDING"; |
| 662 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 663 | } |
| 664 | return "ERROR"; |
| 665 | } |
| 666 | #endif |
| 667 | |
| 668 | #ifdef SQLITE_LOCK_TRACE |
| 669 | /* |
| 670 | ** Print out information about all locking operations. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 671 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 672 | ** This routine is used for troubleshooting locks on multithreaded |
| 673 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 674 | ** command-line option on the compiler. This code is normally |
| 675 | ** turned off. |
| 676 | */ |
| 677 | static int lockTrace(int fd, int op, struct flock *p){ |
| 678 | char *zOpName, *zType; |
| 679 | int s; |
| 680 | int savedErrno; |
| 681 | if( op==F_GETLK ){ |
| 682 | zOpName = "GETLK"; |
| 683 | }else if( op==F_SETLK ){ |
| 684 | zOpName = "SETLK"; |
| 685 | }else{ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 686 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 687 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 688 | return s; |
| 689 | } |
| 690 | if( p->l_type==F_RDLCK ){ |
| 691 | zType = "RDLCK"; |
| 692 | }else if( p->l_type==F_WRLCK ){ |
| 693 | zType = "WRLCK"; |
| 694 | }else if( p->l_type==F_UNLCK ){ |
| 695 | zType = "UNLCK"; |
| 696 | }else{ |
| 697 | assert( 0 ); |
| 698 | } |
| 699 | assert( p->l_whence==SEEK_SET ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 700 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 701 | savedErrno = errno; |
| 702 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 703 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 704 | (int)p->l_pid, s); |
| 705 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 706 | struct flock l2; |
| 707 | l2 = *p; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 708 | osFcntl(fd, F_GETLK, &l2); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 709 | if( l2.l_type==F_RDLCK ){ |
| 710 | zType = "RDLCK"; |
| 711 | }else if( l2.l_type==F_WRLCK ){ |
| 712 | zType = "WRLCK"; |
| 713 | }else if( l2.l_type==F_UNLCK ){ |
| 714 | zType = "UNLCK"; |
| 715 | }else{ |
| 716 | assert( 0 ); |
| 717 | } |
| 718 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 719 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 720 | } |
| 721 | errno = savedErrno; |
| 722 | return s; |
| 723 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 724 | #undef osFcntl |
| 725 | #define osFcntl lockTrace |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 726 | #endif /* SQLITE_LOCK_TRACE */ |
| 727 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 728 | /* |
| 729 | ** Retry ftruncate() calls that fail due to EINTR |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 730 | ** |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 731 | ** All calls to ftruncate() within this file should be made through |
| 732 | ** this wrapper. On the Android platform, bypassing the logic below |
| 733 | ** could lead to a corrupt database. |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 734 | */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 735 | static int robust_ftruncate(int h, sqlite3_int64 sz){ |
| 736 | int rc; |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 737 | #ifdef __ANDROID__ |
| 738 | /* On Android, ftruncate() always uses 32-bit offsets, even if |
| 739 | ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to |
dan | 524a733 | 2014-09-06 17:06:13 +0000 | [diff] [blame] | 740 | ** truncate a file to any size larger than 2GiB. Silently ignore any |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 741 | ** such attempts. */ |
| 742 | if( sz>(sqlite3_int64)0x7FFFFFFF ){ |
| 743 | rc = SQLITE_OK; |
| 744 | }else |
| 745 | #endif |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 746 | do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR ); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 747 | return rc; |
| 748 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 749 | |
| 750 | /* |
| 751 | ** This routine translates a standard POSIX errno code into something |
| 752 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 753 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 754 | ** and a variety of "please close the file descriptor NOW" errors into |
| 755 | ** SQLITE_IOERR |
| 756 | ** |
| 757 | ** Errors during initialization of locks, or file system support for locks, |
| 758 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 759 | */ |
| 760 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
drh | 91c4def | 2015-11-25 14:00:07 +0000 | [diff] [blame] | 761 | assert( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
| 762 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 763 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 764 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 765 | switch (posixError) { |
drh | 91c4def | 2015-11-25 14:00:07 +0000 | [diff] [blame] | 766 | case EACCES: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 767 | case EAGAIN: |
| 768 | case ETIMEDOUT: |
| 769 | case EBUSY: |
| 770 | case EINTR: |
| 771 | case ENOLCK: |
| 772 | /* random NFS retry error, unless during file system support |
| 773 | * introspection, in which it actually means what it says */ |
| 774 | return SQLITE_BUSY; |
| 775 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 776 | case EPERM: |
| 777 | return SQLITE_PERM; |
| 778 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 779 | default: |
| 780 | return sqliteIOErr; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 785 | /****************************************************************************** |
| 786 | ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 787 | ** |
| 788 | ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 789 | ** the device number and the inode number. But this does not work on VxWorks. |
| 790 | ** On VxWorks, a unique file id must be based on the canonical filename. |
| 791 | ** |
| 792 | ** A pointer to an instance of the following structure can be used as a |
| 793 | ** unique file ID in VxWorks. Each instance of this structure contains |
| 794 | ** a copy of the canonical filename. There is also a reference count. |
| 795 | ** The structure is reclaimed when the number of pointers to it drops to |
| 796 | ** zero. |
| 797 | ** |
| 798 | ** There are never very many files open at one time and lookups are not |
| 799 | ** a performance-critical path, so it is sufficient to put these |
| 800 | ** structures on a linked list. |
| 801 | */ |
| 802 | struct vxworksFileId { |
| 803 | struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 804 | int nRef; /* Number of references to this one */ |
| 805 | int nName; /* Length of the zCanonicalName[] string */ |
| 806 | char *zCanonicalName; /* Canonical filename */ |
| 807 | }; |
| 808 | |
| 809 | #if OS_VXWORKS |
| 810 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 811 | ** All unique filenames are held on a linked list headed by this |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 812 | ** variable: |
| 813 | */ |
| 814 | static struct vxworksFileId *vxworksFileList = 0; |
| 815 | |
| 816 | /* |
| 817 | ** Simplify a filename into its canonical form |
| 818 | ** by making the following changes: |
| 819 | ** |
| 820 | ** * removing any trailing and duplicate / |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 821 | ** * convert /./ into just / |
| 822 | ** * convert /A/../ where A is any simple name into just / |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 823 | ** |
| 824 | ** Changes are made in-place. Return the new name length. |
| 825 | ** |
| 826 | ** The original filename is in z[0..n-1]. Return the number of |
| 827 | ** characters in the simplified name. |
| 828 | */ |
| 829 | static int vxworksSimplifyName(char *z, int n){ |
| 830 | int i, j; |
| 831 | while( n>1 && z[n-1]=='/' ){ n--; } |
| 832 | for(i=j=0; i<n; i++){ |
| 833 | if( z[i]=='/' ){ |
| 834 | if( z[i+1]=='/' ) continue; |
| 835 | if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 836 | i += 1; |
| 837 | continue; |
| 838 | } |
| 839 | if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 840 | while( j>0 && z[j-1]!='/' ){ j--; } |
| 841 | if( j>0 ){ j--; } |
| 842 | i += 2; |
| 843 | continue; |
| 844 | } |
| 845 | } |
| 846 | z[j++] = z[i]; |
| 847 | } |
| 848 | z[j] = 0; |
| 849 | return j; |
| 850 | } |
| 851 | |
| 852 | /* |
| 853 | ** Find a unique file ID for the given absolute pathname. Return |
| 854 | ** a pointer to the vxworksFileId object. This pointer is the unique |
| 855 | ** file ID. |
| 856 | ** |
| 857 | ** The nRef field of the vxworksFileId object is incremented before |
| 858 | ** the object is returned. A new vxworksFileId object is created |
| 859 | ** and added to the global list if necessary. |
| 860 | ** |
| 861 | ** If a memory allocation error occurs, return NULL. |
| 862 | */ |
| 863 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 864 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 865 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 866 | int n; /* Length of zAbsoluteName string */ |
| 867 | |
| 868 | assert( zAbsoluteName[0]=='/' ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 869 | n = (int)strlen(zAbsoluteName); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 870 | pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 871 | if( pNew==0 ) return 0; |
| 872 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 873 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 874 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 875 | |
| 876 | /* Search for an existing entry that matching the canonical name. |
| 877 | ** If found, increment the reference count and return a pointer to |
| 878 | ** the existing file ID. |
| 879 | */ |
| 880 | unixEnterMutex(); |
| 881 | for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 882 | if( pCandidate->nName==n |
| 883 | && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 884 | ){ |
| 885 | sqlite3_free(pNew); |
| 886 | pCandidate->nRef++; |
| 887 | unixLeaveMutex(); |
| 888 | return pCandidate; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | /* No match was found. We will make a new file ID */ |
| 893 | pNew->nRef = 1; |
| 894 | pNew->nName = n; |
| 895 | pNew->pNext = vxworksFileList; |
| 896 | vxworksFileList = pNew; |
| 897 | unixLeaveMutex(); |
| 898 | return pNew; |
| 899 | } |
| 900 | |
| 901 | /* |
| 902 | ** Decrement the reference count on a vxworksFileId object. Free |
| 903 | ** the object when the reference count reaches zero. |
| 904 | */ |
| 905 | static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 906 | unixEnterMutex(); |
| 907 | assert( pId->nRef>0 ); |
| 908 | pId->nRef--; |
| 909 | if( pId->nRef==0 ){ |
| 910 | struct vxworksFileId **pp; |
| 911 | for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 912 | assert( *pp==pId ); |
| 913 | *pp = pId->pNext; |
| 914 | sqlite3_free(pId); |
| 915 | } |
| 916 | unixLeaveMutex(); |
| 917 | } |
| 918 | #endif /* OS_VXWORKS */ |
| 919 | /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 920 | ******************************************************************************/ |
| 921 | |
| 922 | |
| 923 | /****************************************************************************** |
| 924 | *************************** Posix Advisory Locking **************************** |
| 925 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 926 | ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 927 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 928 | ** sets or clears a lock, that operation overrides any prior locks set |
| 929 | ** by the same process. It does not explicitly say so, but this implies |
| 930 | ** that it overrides locks set by the same process using a different |
| 931 | ** file descriptor. Consider this test case: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 932 | ** |
| 933 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 934 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 935 | ** |
| 936 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 937 | ** one is a hard or symbolic link to the other) then if you set |
| 938 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 939 | ** on fd2, it works. I would have expected the second lock to |
| 940 | ** fail since there was already a lock on the file due to fd1. |
| 941 | ** But not so. Since both locks came from the same process, the |
| 942 | ** second overrides the first, even though they were on different |
| 943 | ** file descriptors opened on different file names. |
| 944 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 945 | ** This means that we cannot use POSIX locks to synchronize file access |
| 946 | ** among competing threads of the same process. POSIX locks will work fine |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 947 | ** to synchronize access for threads in separate processes, but not |
| 948 | ** threads within the same process. |
| 949 | ** |
| 950 | ** To work around the problem, SQLite has to manage file locks internally |
| 951 | ** on its own. Whenever a new database is opened, we have to find the |
| 952 | ** specific inode of the database file (the inode is determined by the |
| 953 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 954 | ** and check for locks already existing on that inode. When locks are |
| 955 | ** created or removed, we have to look at our own internal record of the |
| 956 | ** locks to see if another thread has previously set a lock on that same |
| 957 | ** inode. |
| 958 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 959 | ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 960 | ** For VxWorks, we have to use the alternative unique ID system based on |
| 961 | ** canonical filename and implemented in the previous division.) |
| 962 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 963 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 964 | ** descriptor. It is now a structure that holds the integer file |
| 965 | ** descriptor and a pointer to a structure that describes the internal |
| 966 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 967 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 968 | ** point to the same locking structure. The locking structure keeps |
| 969 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 970 | ** field that tells us its internal lock status. cnt==0 means the |
| 971 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 972 | ** cnt>0 means there are cnt shared locks on the file. |
| 973 | ** |
| 974 | ** Any attempt to lock or unlock a file first checks the locking |
| 975 | ** structure. The fcntl() system call is only invoked to set a |
| 976 | ** POSIX lock if the internal lock structure transitions between |
| 977 | ** a locked and an unlocked state. |
| 978 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 979 | ** But wait: there are yet more problems with POSIX advisory locks. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 980 | ** |
| 981 | ** If you close a file descriptor that points to a file that has locks, |
| 982 | ** all locks on that file that are owned by the current process are |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 983 | ** released. To work around this problem, each unixInodeInfo object |
| 984 | ** maintains a count of the number of pending locks on tha inode. |
| 985 | ** When an attempt is made to close an unixFile, if there are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 986 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 987 | ** to close() the file descriptor is deferred until all of the locks clear. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 988 | ** The unixInodeInfo structure keeps a list of file descriptors that need to |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 989 | ** be closed and that list is walked (and cleared) when the last lock |
| 990 | ** clears. |
| 991 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 992 | ** Yet another problem: LinuxThreads do not play well with posix locks. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 993 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 994 | ** Many older versions of linux use the LinuxThreads library which is |
| 995 | ** not posix compliant. Under LinuxThreads, a lock created by thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 996 | ** A cannot be modified or overridden by a different thread B. |
| 997 | ** Only thread A can modify the lock. Locking behavior is correct |
| 998 | ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 999 | ** on linux - with NPTL a lock created by thread A can override locks |
| 1000 | ** in thread B. But there is no way to know at compile-time which |
| 1001 | ** threading library is being used. So there is no way to know at |
| 1002 | ** compile-time whether or not thread A can override locks on thread B. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1003 | ** One has to do a run-time check to discover the behavior of the |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1004 | ** current process. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1005 | ** |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1006 | ** SQLite used to support LinuxThreads. But support for LinuxThreads |
| 1007 | ** was dropped beginning with version 3.7.0. SQLite will still work with |
| 1008 | ** LinuxThreads provided that (1) there is no more than one connection |
| 1009 | ** per database file in the same process and (2) database connections |
| 1010 | ** do not move across threads. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1011 | */ |
| 1012 | |
| 1013 | /* |
| 1014 | ** An instance of the following structure serves as the key used |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1015 | ** to locate a particular unixInodeInfo object. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1016 | */ |
| 1017 | struct unixFileId { |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1018 | dev_t dev; /* Device number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1019 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1020 | struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1021 | #else |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1022 | ino_t ino; /* Inode number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1023 | #endif |
| 1024 | }; |
| 1025 | |
| 1026 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1027 | ** An instance of the following structure is allocated for each open |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1028 | ** inode. Or, on LinuxThreads, there is one of these structures for |
| 1029 | ** each inode opened by each thread. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1030 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1031 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1032 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1033 | ** object keeps a count of the number of unixFile pointing to it. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1034 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1035 | struct unixInodeInfo { |
| 1036 | struct unixFileId fileId; /* The lookup key */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1037 | int nShared; /* Number of SHARED locks held */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1038 | unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
| 1039 | unsigned char bProcessLock; /* An exclusive process lock is held */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1040 | int nRef; /* Number of pointers to this structure */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1041 | unixShmNode *pShmNode; /* Shared memory associated with this inode */ |
| 1042 | int nLock; /* Number of outstanding file locks */ |
| 1043 | UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
| 1044 | unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ |
| 1045 | unixInodeInfo *pPrev; /* .... doubly linked */ |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 1046 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1047 | unsigned long long sharedByte; /* for AFP simulated shared lock */ |
| 1048 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1049 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1050 | sem_t *pSem; /* Named POSIX semaphore */ |
| 1051 | char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1052 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1053 | }; |
| 1054 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1055 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1056 | ** A lists of all unixInodeInfo objects. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1057 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1058 | static unixInodeInfo *inodeList = 0; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1059 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1060 | /* |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1061 | ** |
drh | aaeaa18 | 2015-11-24 15:12:47 +0000 | [diff] [blame] | 1062 | ** This function - unixLogErrorAtLine(), is only ever called via the macro |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1063 | ** unixLogError(). |
| 1064 | ** |
| 1065 | ** It is invoked after an error occurs in an OS function and errno has been |
| 1066 | ** set. It logs a message using sqlite3_log() containing the current value of |
| 1067 | ** errno and, if possible, the human-readable equivalent from strerror() or |
| 1068 | ** strerror_r(). |
| 1069 | ** |
| 1070 | ** The first argument passed to the macro should be the error code that |
| 1071 | ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). |
| 1072 | ** The two subsequent arguments should be the name of the OS function that |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 1073 | ** failed (e.g. "unlink", "open") and the associated file-system path, |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1074 | ** if any. |
| 1075 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1076 | #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__) |
| 1077 | static int unixLogErrorAtLine( |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1078 | int errcode, /* SQLite error code */ |
| 1079 | const char *zFunc, /* Name of OS function that failed */ |
| 1080 | const char *zPath, /* File path associated with error */ |
| 1081 | int iLine /* Source line number where error occurred */ |
| 1082 | ){ |
| 1083 | char *zErr; /* Message from strerror() or equivalent */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1084 | int iErrno = errno; /* Saved syscall error number */ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1085 | |
| 1086 | /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use |
| 1087 | ** the strerror() function to obtain the human-readable error message |
| 1088 | ** equivalent to errno. Otherwise, use strerror_r(). |
| 1089 | */ |
| 1090 | #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) |
| 1091 | char aErr[80]; |
| 1092 | memset(aErr, 0, sizeof(aErr)); |
| 1093 | zErr = aErr; |
| 1094 | |
| 1095 | /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined, |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 1096 | ** assume that the system provides the GNU version of strerror_r() that |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1097 | ** returns a pointer to a buffer containing the error message. That pointer |
| 1098 | ** may point to aErr[], or it may point to some static storage somewhere. |
| 1099 | ** Otherwise, assume that the system provides the POSIX version of |
| 1100 | ** strerror_r(), which always writes an error message into aErr[]. |
| 1101 | ** |
| 1102 | ** If the code incorrectly assumes that it is the POSIX version that is |
| 1103 | ** available, the error message will often be an empty string. Not a |
| 1104 | ** huge problem. Incorrectly concluding that the GNU version is available |
| 1105 | ** could lead to a segfault though. |
| 1106 | */ |
| 1107 | #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) |
| 1108 | zErr = |
| 1109 | # endif |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1110 | strerror_r(iErrno, aErr, sizeof(aErr)-1); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1111 | |
| 1112 | #elif SQLITE_THREADSAFE |
| 1113 | /* This is a threadsafe build, but strerror_r() is not available. */ |
| 1114 | zErr = ""; |
| 1115 | #else |
| 1116 | /* Non-threadsafe build, use strerror(). */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1117 | zErr = strerror(iErrno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1118 | #endif |
| 1119 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1120 | if( zPath==0 ) zPath = ""; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1121 | sqlite3_log(errcode, |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1122 | "os_unix.c:%d: (%d) %s(%s) - %s", |
| 1123 | iLine, iErrno, zFunc, zPath, zErr |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1124 | ); |
| 1125 | |
| 1126 | return errcode; |
| 1127 | } |
| 1128 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1129 | /* |
| 1130 | ** Close a file descriptor. |
| 1131 | ** |
| 1132 | ** We assume that close() almost always works, since it is only in a |
| 1133 | ** very sick application or on a very sick platform that it might fail. |
| 1134 | ** If it does fail, simply leak the file descriptor, but do log the |
| 1135 | ** error. |
| 1136 | ** |
| 1137 | ** Note that it is not safe to retry close() after EINTR since the |
| 1138 | ** file descriptor might have already been reused by another thread. |
| 1139 | ** So we don't even try to recover from an EINTR. Just log the error |
| 1140 | ** and move on. |
| 1141 | */ |
| 1142 | static void robust_close(unixFile *pFile, int h, int lineno){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1143 | if( osClose(h) ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1144 | unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", |
| 1145 | pFile ? pFile->zPath : 0, lineno); |
| 1146 | } |
| 1147 | } |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1148 | |
| 1149 | /* |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 1150 | ** Set the pFile->lastErrno. Do this in a subroutine as that provides |
| 1151 | ** a convenient place to set a breakpoint. |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1152 | */ |
| 1153 | static void storeLastErrno(unixFile *pFile, int error){ |
| 1154 | pFile->lastErrno = error; |
| 1155 | } |
| 1156 | |
| 1157 | /* |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1158 | ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1159 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1160 | static void closePendingFds(unixFile *pFile){ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1161 | unixInodeInfo *pInode = pFile->pInode; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1162 | UnixUnusedFd *p; |
| 1163 | UnixUnusedFd *pNext; |
| 1164 | for(p=pInode->pUnused; p; p=pNext){ |
| 1165 | pNext = p->pNext; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1166 | robust_close(pFile, p->fd, __LINE__); |
| 1167 | sqlite3_free(p); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1168 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1169 | pInode->pUnused = 0; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1173 | ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1174 | ** |
| 1175 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1176 | ** when this function is called. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1177 | */ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1178 | static void releaseInodeInfo(unixFile *pFile){ |
| 1179 | unixInodeInfo *pInode = pFile->pInode; |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1180 | assert( unixMutexHeld() ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1181 | if( ALWAYS(pInode) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1182 | pInode->nRef--; |
| 1183 | if( pInode->nRef==0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1184 | assert( pInode->pShmNode==0 ); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1185 | closePendingFds(pFile); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1186 | if( pInode->pPrev ){ |
| 1187 | assert( pInode->pPrev->pNext==pInode ); |
| 1188 | pInode->pPrev->pNext = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1189 | }else{ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1190 | assert( inodeList==pInode ); |
| 1191 | inodeList = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1192 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1193 | if( pInode->pNext ){ |
| 1194 | assert( pInode->pNext->pPrev==pInode ); |
| 1195 | pInode->pNext->pPrev = pInode->pPrev; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1196 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1197 | sqlite3_free(pInode); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1198 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1203 | ** Given a file descriptor, locate the unixInodeInfo object that |
| 1204 | ** describes that file descriptor. Create a new one if necessary. The |
| 1205 | ** return value might be uninitialized if an error occurs. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1206 | ** |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1207 | ** The mutex entered using the unixEnterMutex() function must be held |
| 1208 | ** when this function is called. |
| 1209 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1210 | ** Return an appropriate error code. |
| 1211 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1212 | static int findInodeInfo( |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1213 | unixFile *pFile, /* Unix file with file desc used in the key */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1214 | unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1215 | ){ |
| 1216 | int rc; /* System call return code */ |
| 1217 | int fd; /* The file descriptor for pFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1218 | struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ |
| 1219 | struct stat statbuf; /* Low-level file information */ |
| 1220 | unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1221 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1222 | assert( unixMutexHeld() ); |
| 1223 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1224 | /* Get low-level information about the file that we can used to |
| 1225 | ** create a unique name for the file. |
| 1226 | */ |
| 1227 | fd = pFile->h; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1228 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1229 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1230 | storeLastErrno(pFile, errno); |
drh | 40fe8d3 | 2015-11-30 20:36:26 +0000 | [diff] [blame] | 1231 | #if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS) |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1232 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 1233 | #endif |
| 1234 | return SQLITE_IOERR; |
| 1235 | } |
| 1236 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1237 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1238 | /* On OS X on an msdos filesystem, the inode number is reported |
| 1239 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 1240 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 1241 | ** we always increase the file size to 1 by writing a single byte |
| 1242 | ** prior to accessing the inode number. The one byte written is |
| 1243 | ** an ASCII 'S' character which also happens to be the first byte |
| 1244 | ** in the header of every SQLite database. In this way, if there |
| 1245 | ** is a race condition such that another thread has already populated |
| 1246 | ** the first page of the database, no damage is done. |
| 1247 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1248 | if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 1249 | do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR ); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1250 | if( rc!=1 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1251 | storeLastErrno(pFile, errno); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1252 | return SQLITE_IOERR; |
| 1253 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1254 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1255 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1256 | storeLastErrno(pFile, errno); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1257 | return SQLITE_IOERR; |
| 1258 | } |
| 1259 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1260 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1261 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1262 | memset(&fileId, 0, sizeof(fileId)); |
| 1263 | fileId.dev = statbuf.st_dev; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1264 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1265 | fileId.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1266 | #else |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1267 | fileId.ino = statbuf.st_ino; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1268 | #endif |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1269 | pInode = inodeList; |
| 1270 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 1271 | pInode = pInode->pNext; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1272 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1273 | if( pInode==0 ){ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 1274 | pInode = sqlite3_malloc64( sizeof(*pInode) ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1275 | if( pInode==0 ){ |
| 1276 | return SQLITE_NOMEM; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1277 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1278 | memset(pInode, 0, sizeof(*pInode)); |
| 1279 | memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
| 1280 | pInode->nRef = 1; |
| 1281 | pInode->pNext = inodeList; |
| 1282 | pInode->pPrev = 0; |
| 1283 | if( inodeList ) inodeList->pPrev = pInode; |
| 1284 | inodeList = pInode; |
| 1285 | }else{ |
| 1286 | pInode->nRef++; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1287 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1288 | *ppInode = pInode; |
| 1289 | return SQLITE_OK; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1290 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1291 | |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1292 | /* |
| 1293 | ** Return TRUE if pFile has been renamed or unlinked since it was first opened. |
| 1294 | */ |
| 1295 | static int fileHasMoved(unixFile *pFile){ |
drh | 61ffea5 | 2014-08-12 12:19:25 +0000 | [diff] [blame] | 1296 | #if OS_VXWORKS |
| 1297 | return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId; |
| 1298 | #else |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1299 | struct stat buf; |
| 1300 | return pFile->pInode!=0 && |
drh | 61ffea5 | 2014-08-12 12:19:25 +0000 | [diff] [blame] | 1301 | (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino); |
drh | 91be7dc | 2014-08-11 13:53:30 +0000 | [diff] [blame] | 1302 | #endif |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1305 | |
| 1306 | /* |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1307 | ** Check a unixFile that is a database. Verify the following: |
| 1308 | ** |
| 1309 | ** (1) There is exactly one hard link on the file |
| 1310 | ** (2) The file is not a symbolic link |
| 1311 | ** (3) The file has not been renamed or unlinked |
| 1312 | ** |
| 1313 | ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right. |
| 1314 | */ |
| 1315 | static void verifyDbFile(unixFile *pFile){ |
| 1316 | struct stat buf; |
| 1317 | int rc; |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1318 | rc = osFstat(pFile->h, &buf); |
| 1319 | if( rc!=0 ){ |
| 1320 | sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1321 | return; |
| 1322 | } |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 1323 | if( buf.st_nlink==0 && (pFile->ctrlFlags & UNIXFILE_DELETE)==0 ){ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1324 | sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1325 | return; |
| 1326 | } |
| 1327 | if( buf.st_nlink>1 ){ |
| 1328 | sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1329 | return; |
| 1330 | } |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1331 | if( fileHasMoved(pFile) ){ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1332 | sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1333 | return; |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | |
| 1338 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1339 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1340 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1341 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1342 | ** 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] | 1343 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1344 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1345 | int rc = SQLITE_OK; |
| 1346 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1347 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1348 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1349 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1350 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1351 | assert( pFile ); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 1352 | assert( pFile->eFileLock<=SHARED_LOCK ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1353 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1354 | |
| 1355 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1356 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1357 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1360 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1361 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1362 | #ifndef __DJGPP__ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1363 | if( !reserved && !pFile->pInode->bProcessLock ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1364 | struct flock lock; |
| 1365 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1366 | lock.l_start = RESERVED_BYTE; |
| 1367 | lock.l_len = 1; |
| 1368 | lock.l_type = F_WRLCK; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1369 | if( osFcntl(pFile->h, F_GETLK, &lock) ){ |
| 1370 | rc = SQLITE_IOERR_CHECKRESERVEDLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1371 | storeLastErrno(pFile, errno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1372 | } else if( lock.l_type!=F_UNLCK ){ |
| 1373 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1374 | } |
| 1375 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1376 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1377 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1378 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1379 | OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1380 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1381 | *pResOut = reserved; |
| 1382 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1386 | ** Attempt to set a system-lock on the file pFile. The lock is |
| 1387 | ** described by pLock. |
| 1388 | ** |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1389 | ** If the pFile was opened read/write from unix-excl, then the only lock |
| 1390 | ** ever obtained is an exclusive lock, and it is obtained exactly once |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1391 | ** the first time any lock is attempted. All subsequent system locking |
| 1392 | ** operations become no-ops. Locking operations still happen internally, |
| 1393 | ** in order to coordinate access between separate database connections |
| 1394 | ** within this process, but all of that is handled in memory and the |
| 1395 | ** operating system does not participate. |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1396 | ** |
| 1397 | ** This function is a pass-through to fcntl(F_SETLK) if pFile is using |
| 1398 | ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl" |
| 1399 | ** and is read-only. |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1400 | ** |
| 1401 | ** Zero is returned if the call completes successfully, or -1 if a call |
| 1402 | ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()). |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1403 | */ |
| 1404 | static int unixFileLock(unixFile *pFile, struct flock *pLock){ |
| 1405 | int rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1406 | unixInodeInfo *pInode = pFile->pInode; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1407 | assert( unixMutexHeld() ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1408 | assert( pInode!=0 ); |
drh | 50358ad | 2015-12-02 01:04:33 +0000 | [diff] [blame] | 1409 | if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1410 | if( pInode->bProcessLock==0 ){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1411 | struct flock lock; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1412 | assert( pInode->nLock==0 ); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1413 | lock.l_whence = SEEK_SET; |
| 1414 | lock.l_start = SHARED_FIRST; |
| 1415 | lock.l_len = SHARED_SIZE; |
| 1416 | lock.l_type = F_WRLCK; |
| 1417 | rc = osFcntl(pFile->h, F_SETLK, &lock); |
| 1418 | if( rc<0 ) return rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1419 | pInode->bProcessLock = 1; |
| 1420 | pInode->nLock++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1421 | }else{ |
| 1422 | rc = 0; |
| 1423 | } |
| 1424 | }else{ |
| 1425 | rc = osFcntl(pFile->h, F_SETLK, pLock); |
| 1426 | } |
| 1427 | return rc; |
| 1428 | } |
| 1429 | |
| 1430 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1431 | ** Lock the file with the lock specified by parameter eFileLock - one |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1432 | ** of the following: |
| 1433 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1434 | ** (1) SHARED_LOCK |
| 1435 | ** (2) RESERVED_LOCK |
| 1436 | ** (3) PENDING_LOCK |
| 1437 | ** (4) EXCLUSIVE_LOCK |
| 1438 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1439 | ** Sometimes when requesting one lock state, additional lock states |
| 1440 | ** are inserted in between. The locking might fail on one of the later |
| 1441 | ** transitions leaving the lock state different from what it started but |
| 1442 | ** still short of its goal. The following chart shows the allowed |
| 1443 | ** transitions and the inserted intermediate states: |
| 1444 | ** |
| 1445 | ** UNLOCKED -> SHARED |
| 1446 | ** SHARED -> RESERVED |
| 1447 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1448 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1449 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1450 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1451 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1452 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1453 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1454 | static int unixLock(sqlite3_file *id, int eFileLock){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1455 | /* The following describes the implementation of the various locks and |
| 1456 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1457 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1458 | ** confusion with SQLite lock names). The algorithms are complicated |
| 1459 | ** slightly in order to be compatible with windows systems simultaneously |
| 1460 | ** accessing the same database file, in case that is ever required. |
| 1461 | ** |
| 1462 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1463 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1464 | ** range', a range of 510 bytes at a well known offset. |
| 1465 | ** |
| 1466 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 1467 | ** byte'. If this is successful, a random byte from the 'shared byte |
| 1468 | ** range' is read-locked and the lock on the 'pending byte' released. |
| 1469 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1470 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1471 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1472 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1473 | ** |
| 1474 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1475 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1476 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1477 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1478 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1479 | ** This property is used by the algorithm for rolling back a journal file |
| 1480 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1481 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1482 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1483 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1484 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1485 | ** within this range, this ensures that no other locks are held on the |
| 1486 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1487 | ** |
| 1488 | ** The reason a single byte cannot be used instead of the 'shared byte |
| 1489 | ** range' is that some versions of windows do not support read-locks. By |
| 1490 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 1491 | ** even if the locking primitive used is always a write-lock. |
| 1492 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1493 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1494 | unixFile *pFile = (unixFile*)id; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 1495 | unixInodeInfo *pInode; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1496 | struct flock lock; |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 1497 | int tErrno = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1498 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1499 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1500 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 1501 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 1502 | azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 1503 | osGetpid(0))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1504 | |
| 1505 | /* 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] | 1506 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1507 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1508 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1509 | if( pFile->eFileLock>=eFileLock ){ |
| 1510 | OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
| 1511 | azFileLock(eFileLock))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1512 | return SQLITE_OK; |
| 1513 | } |
| 1514 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1515 | /* Make sure the locking sequence is correct. |
| 1516 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 1517 | ** (2) SQLite never explicitly requests a pendig lock. |
| 1518 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1519 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1520 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 1521 | assert( eFileLock!=PENDING_LOCK ); |
| 1522 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1523 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1524 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1525 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1526 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1527 | pInode = pFile->pInode; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1528 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1529 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1530 | ** handle that precludes the requested lock, return BUSY. |
| 1531 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1532 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 1533 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1534 | ){ |
| 1535 | rc = SQLITE_BUSY; |
| 1536 | goto end_lock; |
| 1537 | } |
| 1538 | |
| 1539 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1540 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1541 | ** return SQLITE_OK. |
| 1542 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1543 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1544 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1545 | assert( eFileLock==SHARED_LOCK ); |
| 1546 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1547 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1548 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1549 | pInode->nShared++; |
| 1550 | pInode->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1551 | goto end_lock; |
| 1552 | } |
| 1553 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1554 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1555 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1556 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1557 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1558 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1559 | lock.l_len = 1L; |
| 1560 | lock.l_whence = SEEK_SET; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1561 | if( eFileLock==SHARED_LOCK |
| 1562 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1563 | ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1564 | lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1565 | lock.l_start = PENDING_BYTE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1566 | if( unixFileLock(pFile, &lock) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1567 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1568 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1569 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1570 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1571 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1572 | goto end_lock; |
| 1573 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | |
| 1577 | /* If control gets to this point, then actually go ahead and make |
| 1578 | ** operating system calls for the specified lock. |
| 1579 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1580 | if( eFileLock==SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1581 | assert( pInode->nShared==0 ); |
| 1582 | assert( pInode->eFileLock==0 ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1583 | assert( rc==SQLITE_OK ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1584 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1585 | /* Now get the read-lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1586 | lock.l_start = SHARED_FIRST; |
| 1587 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1588 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1589 | tErrno = errno; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1590 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1591 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1592 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1593 | /* Drop the temporary PENDING lock */ |
| 1594 | lock.l_start = PENDING_BYTE; |
| 1595 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1596 | lock.l_type = F_UNLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1597 | if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){ |
| 1598 | /* This could happen with a network mount */ |
| 1599 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1600 | rc = SQLITE_IOERR_UNLOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1601 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1602 | |
| 1603 | if( rc ){ |
| 1604 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1605 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1606 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1607 | goto end_lock; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1608 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1609 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1610 | pInode->nLock++; |
| 1611 | pInode->nShared = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1612 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1613 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1614 | /* We are trying for an exclusive lock but another thread in this |
| 1615 | ** same process is still holding a shared lock. */ |
| 1616 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1617 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1618 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1619 | ** assumed that there is a SHARED or greater lock on the file |
| 1620 | ** already. |
| 1621 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1622 | assert( 0!=pFile->eFileLock ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1623 | lock.l_type = F_WRLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1624 | |
| 1625 | assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK ); |
| 1626 | if( eFileLock==RESERVED_LOCK ){ |
| 1627 | lock.l_start = RESERVED_BYTE; |
| 1628 | lock.l_len = 1L; |
| 1629 | }else{ |
| 1630 | lock.l_start = SHARED_FIRST; |
| 1631 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1632 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1633 | |
| 1634 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1635 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1636 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1637 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1638 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1639 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1640 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1641 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1642 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1643 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 1644 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1645 | /* Set up the transaction-counter change checking flags when |
| 1646 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1647 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1648 | ** write operation (not a hot journal rollback). |
| 1649 | */ |
| 1650 | if( rc==SQLITE_OK |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1651 | && pFile->eFileLock<=SHARED_LOCK |
| 1652 | && eFileLock==RESERVED_LOCK |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1653 | ){ |
| 1654 | pFile->transCntrChng = 0; |
| 1655 | pFile->dbUpdate = 0; |
| 1656 | pFile->inNormalWrite = 1; |
| 1657 | } |
| 1658 | #endif |
| 1659 | |
| 1660 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1661 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1662 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1663 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1664 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 1665 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1666 | pInode->eFileLock = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1667 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1668 | |
| 1669 | end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1670 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1671 | OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), |
| 1672 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1673 | return rc; |
| 1674 | } |
| 1675 | |
| 1676 | /* |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1677 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1678 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1679 | */ |
| 1680 | static void setPendingFd(unixFile *pFile){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1681 | unixInodeInfo *pInode = pFile->pInode; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1682 | UnixUnusedFd *p = pFile->pUnused; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1683 | p->pNext = pInode->pUnused; |
| 1684 | pInode->pUnused = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1685 | pFile->h = -1; |
| 1686 | pFile->pUnused = 0; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1690 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1691 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1692 | ** |
| 1693 | ** If the locking level of the file descriptor is already at or below |
| 1694 | ** the requested locking level, this routine is a no-op. |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1695 | ** |
| 1696 | ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 1697 | ** the byte range is divided into 2 parts and the first part is unlocked then |
| 1698 | ** set to a read lock, then the other part is simply unlocked. This works |
| 1699 | ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 1700 | ** remove the write lock on a region when a read lock is set. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1701 | */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1702 | static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1703 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1704 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1705 | struct flock lock; |
| 1706 | int rc = SQLITE_OK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1707 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1708 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1709 | OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1710 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 1711 | osGetpid(0))); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1712 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1713 | assert( eFileLock<=SHARED_LOCK ); |
| 1714 | if( pFile->eFileLock<=eFileLock ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1715 | return SQLITE_OK; |
| 1716 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1717 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1718 | pInode = pFile->pInode; |
| 1719 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1720 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1721 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1722 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 1723 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1724 | /* When reducing a lock such that other processes can start |
| 1725 | ** reading the database file again, make sure that the |
| 1726 | ** transaction counter was updated if any part of the database |
| 1727 | ** file changed. If the transaction counter is not updated, |
| 1728 | ** other connections to the same file might not realize that |
| 1729 | ** the file has changed and hence might not know to flush their |
| 1730 | ** cache. The use of a stale cache can lead to database corruption. |
| 1731 | */ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1732 | pFile->inNormalWrite = 0; |
| 1733 | #endif |
| 1734 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1735 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 1736 | ** before establishing the readlock - to avoid a race condition we downgrade |
| 1737 | ** the lock in 2 blocks, so that part of the range will be covered by a |
| 1738 | ** write lock until the rest is covered by a read lock: |
| 1739 | ** 1: [WWWWW] |
| 1740 | ** 2: [....W] |
| 1741 | ** 3: [RRRRW] |
| 1742 | ** 4: [RRRR.] |
| 1743 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1744 | if( eFileLock==SHARED_LOCK ){ |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1745 | #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE |
drh | 87e79ae | 2011-03-08 13:06:41 +0000 | [diff] [blame] | 1746 | (void)handleNFSUnlock; |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1747 | assert( handleNFSUnlock==0 ); |
| 1748 | #endif |
| 1749 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1750 | if( handleNFSUnlock ){ |
drh | a712b4b | 2015-02-19 16:12:04 +0000 | [diff] [blame] | 1751 | int tErrno; /* Error code from system call errors */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1752 | off_t divSize = SHARED_SIZE - 1; |
| 1753 | |
| 1754 | lock.l_type = F_UNLCK; |
| 1755 | lock.l_whence = SEEK_SET; |
| 1756 | lock.l_start = SHARED_FIRST; |
| 1757 | lock.l_len = divSize; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 1758 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1759 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1760 | rc = SQLITE_IOERR_UNLOCK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 1761 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1762 | goto end_unlock; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1763 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1764 | lock.l_type = F_RDLCK; |
| 1765 | lock.l_whence = SEEK_SET; |
| 1766 | lock.l_start = SHARED_FIRST; |
| 1767 | lock.l_len = divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1768 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1769 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1770 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1771 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1772 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1773 | } |
| 1774 | goto end_unlock; |
| 1775 | } |
| 1776 | lock.l_type = F_UNLCK; |
| 1777 | lock.l_whence = SEEK_SET; |
| 1778 | lock.l_start = SHARED_FIRST+divSize; |
| 1779 | lock.l_len = SHARED_SIZE-divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1780 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1781 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1782 | rc = SQLITE_IOERR_UNLOCK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 1783 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1784 | goto end_unlock; |
| 1785 | } |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1786 | }else |
| 1787 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 1788 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1789 | lock.l_type = F_RDLCK; |
| 1790 | lock.l_whence = SEEK_SET; |
| 1791 | lock.l_start = SHARED_FIRST; |
| 1792 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1793 | if( unixFileLock(pFile, &lock) ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1794 | /* In theory, the call to unixFileLock() cannot fail because another |
| 1795 | ** process is holding an incompatible lock. If it does, this |
| 1796 | ** indicates that the other process is not following the locking |
| 1797 | ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning |
| 1798 | ** SQLITE_BUSY would confuse the upper layer (in practice it causes |
| 1799 | ** an assert to fail). */ |
| 1800 | rc = SQLITE_IOERR_RDLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1801 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1802 | goto end_unlock; |
| 1803 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1804 | } |
| 1805 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1806 | lock.l_type = F_UNLCK; |
| 1807 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1808 | lock.l_start = PENDING_BYTE; |
| 1809 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1810 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1811 | pInode->eFileLock = SHARED_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1812 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1813 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1814 | storeLastErrno(pFile, errno); |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 1815 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1816 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1817 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1818 | if( eFileLock==NO_LOCK ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1819 | /* Decrement the shared lock counter. Release the lock using an |
| 1820 | ** OS call only when all threads in this same process have released |
| 1821 | ** the lock. |
| 1822 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1823 | pInode->nShared--; |
| 1824 | if( pInode->nShared==0 ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1825 | lock.l_type = F_UNLCK; |
| 1826 | lock.l_whence = SEEK_SET; |
| 1827 | lock.l_start = lock.l_len = 0L; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1828 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1829 | pInode->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1830 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1831 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1832 | storeLastErrno(pFile, errno); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1833 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1834 | pFile->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1835 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1838 | /* Decrement the count of locks against this same file. When the |
| 1839 | ** count reaches zero, close any other file descriptors whose close |
| 1840 | ** was deferred because of outstanding locks. |
| 1841 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1842 | pInode->nLock--; |
| 1843 | assert( pInode->nLock>=0 ); |
| 1844 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1845 | closePendingFds(pFile); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1846 | } |
| 1847 | } |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 1848 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1849 | end_unlock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1850 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1851 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1852 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1856 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1857 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1858 | ** |
| 1859 | ** If the locking level of the file descriptor is already at or below |
| 1860 | ** the requested locking level, this routine is a no-op. |
| 1861 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1862 | static int unixUnlock(sqlite3_file *id, int eFileLock){ |
dan | f52a469 | 2013-10-31 18:49:58 +0000 | [diff] [blame] | 1863 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | a1afc74 | 2013-03-25 13:50:49 +0000 | [diff] [blame] | 1864 | assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 ); |
dan | f52a469 | 2013-10-31 18:49:58 +0000 | [diff] [blame] | 1865 | #endif |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1866 | return posixUnlock(id, eFileLock, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1869 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 1870 | static int unixMapfile(unixFile *pFd, i64 nByte); |
| 1871 | static void unixUnmapfile(unixFile *pFd); |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1872 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 1873 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1874 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1875 | ** This function performs the parts of the "close file" operation |
| 1876 | ** common to all locking schemes. It closes the directory and file |
| 1877 | ** handles, if they are valid, and sets all fields of the unixFile |
| 1878 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1879 | ** |
| 1880 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 1881 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 1882 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1883 | */ |
| 1884 | static int closeUnixFile(sqlite3_file *id){ |
| 1885 | unixFile *pFile = (unixFile*)id; |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1886 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 1887 | unixUnmapfile(pFile); |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 1888 | #endif |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1889 | if( pFile->h>=0 ){ |
| 1890 | robust_close(pFile, pFile->h, __LINE__); |
| 1891 | pFile->h = -1; |
| 1892 | } |
| 1893 | #if OS_VXWORKS |
| 1894 | if( pFile->pId ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 1895 | if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 1896 | osUnlink(pFile->pId->zCanonicalName); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1897 | } |
| 1898 | vxworksReleaseFileId(pFile->pId); |
| 1899 | pFile->pId = 0; |
| 1900 | } |
| 1901 | #endif |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 1902 | #ifdef SQLITE_UNLINK_AFTER_CLOSE |
| 1903 | if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
| 1904 | osUnlink(pFile->zPath); |
| 1905 | sqlite3_free(*(char**)&pFile->zPath); |
| 1906 | pFile->zPath = 0; |
| 1907 | } |
| 1908 | #endif |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1909 | OSTRACE(("CLOSE %-3d\n", pFile->h)); |
| 1910 | OpenCounter(-1); |
| 1911 | sqlite3_free(pFile->pUnused); |
| 1912 | memset(pFile, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1913 | return SQLITE_OK; |
| 1914 | } |
| 1915 | |
| 1916 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1917 | ** Close a file. |
| 1918 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1919 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1920 | int rc = SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1921 | unixFile *pFile = (unixFile *)id; |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1922 | verifyDbFile(pFile); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1923 | unixUnlock(id, NO_LOCK); |
| 1924 | unixEnterMutex(); |
| 1925 | |
| 1926 | /* unixFile.pInode is always valid here. Otherwise, a different close |
| 1927 | ** routine (e.g. nolockClose()) would be called instead. |
| 1928 | */ |
| 1929 | assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); |
| 1930 | if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){ |
| 1931 | /* If there are outstanding locks, do not actually close the file just |
| 1932 | ** yet because that would clear those locks. Instead, add the file |
| 1933 | ** descriptor to pInode->pUnused list. It will be automatically closed |
| 1934 | ** when the last lock is cleared. |
| 1935 | */ |
| 1936 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1937 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1938 | releaseInodeInfo(pFile); |
| 1939 | rc = closeUnixFile(id); |
| 1940 | unixLeaveMutex(); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1941 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1944 | /************** End of the posix advisory lock implementation ***************** |
| 1945 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1946 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1947 | /****************************************************************************** |
| 1948 | ****************************** No-op Locking ********************************** |
| 1949 | ** |
| 1950 | ** Of the various locking implementations available, this is by far the |
| 1951 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 1952 | ** file for reading or writing. |
| 1953 | ** |
| 1954 | ** This locking mode is appropriate for use on read-only databases |
| 1955 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 1956 | ** also be used if the application employs some external mechanism to |
| 1957 | ** prevent simultaneous access of the same database by two or more |
| 1958 | ** database connections. But there is a serious risk of database |
| 1959 | ** corruption if this locking mode is used in situations where multiple |
| 1960 | ** database connections are accessing the same database file at the same |
| 1961 | ** time and one or more of those connections are writing. |
| 1962 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1963 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1964 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 1965 | UNUSED_PARAMETER(NotUsed); |
| 1966 | *pResOut = 0; |
| 1967 | return SQLITE_OK; |
| 1968 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1969 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1970 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1971 | return SQLITE_OK; |
| 1972 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1973 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1974 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1975 | return SQLITE_OK; |
| 1976 | } |
| 1977 | |
| 1978 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1979 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1980 | */ |
| 1981 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1982 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | /******************* End of the no-op lock implementation ********************* |
| 1986 | ******************************************************************************/ |
| 1987 | |
| 1988 | /****************************************************************************** |
| 1989 | ************************* Begin dot-file Locking ****************************** |
| 1990 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 1991 | ** The dotfile locking implementation uses the existence of separate lock |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 1992 | ** files (really a directory) to control access to the database. This works |
| 1993 | ** on just about every filesystem imaginable. But there are serious downsides: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1994 | ** |
| 1995 | ** (1) There is zero concurrency. A single reader blocks all other |
| 1996 | ** connections from reading or writing the database. |
| 1997 | ** |
| 1998 | ** (2) An application crash or power loss can leave stale lock files |
| 1999 | ** sitting around that need to be cleared manually. |
| 2000 | ** |
| 2001 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 2002 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2003 | ** |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2004 | ** Dotfile locking works by creating a subdirectory in the same directory as |
| 2005 | ** the database and with the same name but with a ".lock" extension added. |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 2006 | ** The existence of a lock directory implies an EXCLUSIVE lock. All other |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2007 | ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2008 | */ |
| 2009 | |
| 2010 | /* |
| 2011 | ** The file suffix added to the data base filename in order to create the |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2012 | ** lock directory. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2013 | */ |
| 2014 | #define DOTLOCK_SUFFIX ".lock" |
| 2015 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2016 | /* |
| 2017 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2018 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2019 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2020 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2021 | ** |
| 2022 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 2023 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 2024 | ** is held on the file and false if the file is unlocked. |
| 2025 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2026 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 2027 | int rc = SQLITE_OK; |
| 2028 | int reserved = 0; |
| 2029 | unixFile *pFile = (unixFile*)id; |
| 2030 | |
| 2031 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2032 | |
| 2033 | assert( pFile ); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2034 | reserved = osAccess((const char*)pFile->lockingContext, 0)==0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2035 | OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2036 | *pResOut = reserved; |
| 2037 | return rc; |
| 2038 | } |
| 2039 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2040 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2041 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2042 | ** of the following: |
| 2043 | ** |
| 2044 | ** (1) SHARED_LOCK |
| 2045 | ** (2) RESERVED_LOCK |
| 2046 | ** (3) PENDING_LOCK |
| 2047 | ** (4) EXCLUSIVE_LOCK |
| 2048 | ** |
| 2049 | ** Sometimes when requesting one lock state, additional lock states |
| 2050 | ** are inserted in between. The locking might fail on one of the later |
| 2051 | ** transitions leaving the lock state different from what it started but |
| 2052 | ** still short of its goal. The following chart shows the allowed |
| 2053 | ** transitions and the inserted intermediate states: |
| 2054 | ** |
| 2055 | ** UNLOCKED -> SHARED |
| 2056 | ** SHARED -> RESERVED |
| 2057 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2058 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2059 | ** PENDING -> EXCLUSIVE |
| 2060 | ** |
| 2061 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2062 | ** routine to lower a locking level. |
| 2063 | ** |
| 2064 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 2065 | ** But we track the other locking levels internally. |
| 2066 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2067 | static int dotlockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2068 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2069 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2070 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2071 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2072 | |
| 2073 | /* If we have any lock, then the lock file already exists. All we have |
| 2074 | ** to do is adjust our internal record of the lock level. |
| 2075 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2076 | if( pFile->eFileLock > NO_LOCK ){ |
| 2077 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2078 | /* Always update the timestamp on the old file */ |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 2079 | #ifdef HAVE_UTIME |
| 2080 | utime(zLockFile, NULL); |
| 2081 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2082 | utimes(zLockFile, NULL); |
| 2083 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2084 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | /* grab an exclusive lock */ |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2088 | rc = osMkdir(zLockFile, 0777); |
| 2089 | if( rc<0 ){ |
| 2090 | /* failed to open/create the lock directory */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2091 | int tErrno = errno; |
| 2092 | if( EEXIST == tErrno ){ |
| 2093 | rc = SQLITE_BUSY; |
| 2094 | } else { |
| 2095 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2096 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2097 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2098 | } |
| 2099 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2100 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2101 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2102 | |
| 2103 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2104 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2105 | return rc; |
| 2106 | } |
| 2107 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2108 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2109 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2110 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2111 | ** |
| 2112 | ** If the locking level of the file descriptor is already at or below |
| 2113 | ** the requested locking level, this routine is a no-op. |
| 2114 | ** |
| 2115 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 2116 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2117 | static int dotlockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2118 | unixFile *pFile = (unixFile*)id; |
| 2119 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2120 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2121 | |
| 2122 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2123 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2124 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2125 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2126 | |
| 2127 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2128 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2129 | return SQLITE_OK; |
| 2130 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2131 | |
| 2132 | /* To downgrade to shared, simply update our internal notion of the |
| 2133 | ** lock state. No need to mess with the file on disk. |
| 2134 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2135 | if( eFileLock==SHARED_LOCK ){ |
| 2136 | pFile->eFileLock = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2137 | return SQLITE_OK; |
| 2138 | } |
| 2139 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2140 | /* To fully unlock the database, delete the lock file */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2141 | assert( eFileLock==NO_LOCK ); |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2142 | rc = osRmdir(zLockFile); |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2143 | if( rc<0 ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 2144 | int tErrno = errno; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2145 | if( tErrno==ENOENT ){ |
| 2146 | rc = SQLITE_OK; |
| 2147 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2148 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2149 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2150 | } |
| 2151 | return rc; |
| 2152 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2153 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2154 | return SQLITE_OK; |
| 2155 | } |
| 2156 | |
| 2157 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2158 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2159 | */ |
| 2160 | static int dotlockClose(sqlite3_file *id) { |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2161 | unixFile *pFile = (unixFile*)id; |
| 2162 | assert( id!=0 ); |
| 2163 | dotlockUnlock(id, NO_LOCK); |
| 2164 | sqlite3_free(pFile->lockingContext); |
| 2165 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2166 | } |
| 2167 | /****************** End of the dot-file lock implementation ******************* |
| 2168 | ******************************************************************************/ |
| 2169 | |
| 2170 | /****************************************************************************** |
| 2171 | ************************** Begin flock Locking ******************************** |
| 2172 | ** |
| 2173 | ** Use the flock() system call to do file locking. |
| 2174 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2175 | ** flock() locking is like dot-file locking in that the various |
| 2176 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 2177 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 2178 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 2179 | ** still works when you do this, but concurrency is reduced since |
| 2180 | ** only a single process can be reading the database at a time. |
| 2181 | ** |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 2182 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2183 | */ |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 2184 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2185 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2186 | /* |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2187 | ** Retry flock() calls that fail with EINTR |
| 2188 | */ |
| 2189 | #ifdef EINTR |
| 2190 | static int robust_flock(int fd, int op){ |
| 2191 | int rc; |
| 2192 | do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR ); |
| 2193 | return rc; |
| 2194 | } |
| 2195 | #else |
drh | 5c81927 | 2011-02-23 14:00:12 +0000 | [diff] [blame] | 2196 | # define robust_flock(a,b) flock(a,b) |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2197 | #endif |
| 2198 | |
| 2199 | |
| 2200 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2201 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2202 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2203 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2204 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2205 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2206 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 2207 | int rc = SQLITE_OK; |
| 2208 | int reserved = 0; |
| 2209 | unixFile *pFile = (unixFile*)id; |
| 2210 | |
| 2211 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2212 | |
| 2213 | assert( pFile ); |
| 2214 | |
| 2215 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2216 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2217 | reserved = 1; |
| 2218 | } |
| 2219 | |
| 2220 | /* Otherwise see if some other process holds it. */ |
| 2221 | if( !reserved ){ |
| 2222 | /* attempt to get the lock */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2223 | int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2224 | if( !lrc ){ |
| 2225 | /* got the lock, unlock it */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2226 | lrc = robust_flock(pFile->h, LOCK_UN); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2227 | if ( lrc ) { |
| 2228 | int tErrno = errno; |
| 2229 | /* unlock failed with an error */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2230 | lrc = SQLITE_IOERR_UNLOCK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2231 | storeLastErrno(pFile, tErrno); |
| 2232 | rc = lrc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2233 | } |
| 2234 | } else { |
| 2235 | int tErrno = errno; |
| 2236 | reserved = 1; |
| 2237 | /* someone else might have it reserved */ |
| 2238 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2239 | if( IS_LOCK_ERROR(lrc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2240 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2241 | rc = lrc; |
| 2242 | } |
| 2243 | } |
| 2244 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2245 | OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2246 | |
| 2247 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2248 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2249 | rc = SQLITE_OK; |
| 2250 | reserved=1; |
| 2251 | } |
| 2252 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2253 | *pResOut = reserved; |
| 2254 | return rc; |
| 2255 | } |
| 2256 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2257 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2258 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2259 | ** of the following: |
| 2260 | ** |
| 2261 | ** (1) SHARED_LOCK |
| 2262 | ** (2) RESERVED_LOCK |
| 2263 | ** (3) PENDING_LOCK |
| 2264 | ** (4) EXCLUSIVE_LOCK |
| 2265 | ** |
| 2266 | ** Sometimes when requesting one lock state, additional lock states |
| 2267 | ** are inserted in between. The locking might fail on one of the later |
| 2268 | ** transitions leaving the lock state different from what it started but |
| 2269 | ** still short of its goal. The following chart shows the allowed |
| 2270 | ** transitions and the inserted intermediate states: |
| 2271 | ** |
| 2272 | ** UNLOCKED -> SHARED |
| 2273 | ** SHARED -> RESERVED |
| 2274 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2275 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2276 | ** PENDING -> EXCLUSIVE |
| 2277 | ** |
| 2278 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 2279 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2280 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2281 | ** access the file. |
| 2282 | ** |
| 2283 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2284 | ** routine to lower a locking level. |
| 2285 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2286 | static int flockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2287 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2288 | unixFile *pFile = (unixFile*)id; |
| 2289 | |
| 2290 | assert( pFile ); |
| 2291 | |
| 2292 | /* if we already have a lock, it is exclusive. |
| 2293 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2294 | if (pFile->eFileLock > NO_LOCK) { |
| 2295 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2296 | return SQLITE_OK; |
| 2297 | } |
| 2298 | |
| 2299 | /* grab an exclusive lock */ |
| 2300 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2301 | if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2302 | int tErrno = errno; |
| 2303 | /* didn't get, must be busy */ |
| 2304 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2305 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2306 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2307 | } |
| 2308 | } else { |
| 2309 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2310 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2311 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2312 | OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), |
| 2313 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2314 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 2315 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 2316 | rc = SQLITE_BUSY; |
| 2317 | } |
| 2318 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2319 | return rc; |
| 2320 | } |
| 2321 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2322 | |
| 2323 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2324 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2325 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2326 | ** |
| 2327 | ** If the locking level of the file descriptor is already at or below |
| 2328 | ** the requested locking level, this routine is a no-op. |
| 2329 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2330 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2331 | unixFile *pFile = (unixFile*)id; |
| 2332 | |
| 2333 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2334 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2335 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2336 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2337 | |
| 2338 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2339 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2340 | return SQLITE_OK; |
| 2341 | } |
| 2342 | |
| 2343 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2344 | if (eFileLock==SHARED_LOCK) { |
| 2345 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2346 | return SQLITE_OK; |
| 2347 | } |
| 2348 | |
| 2349 | /* no, really, unlock. */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2350 | if( robust_flock(pFile->h, LOCK_UN) ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2351 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2352 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2353 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2354 | return SQLITE_IOERR_UNLOCK; |
| 2355 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2356 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2357 | return SQLITE_OK; |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | /* |
| 2362 | ** Close a file. |
| 2363 | */ |
| 2364 | static int flockClose(sqlite3_file *id) { |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2365 | assert( id!=0 ); |
| 2366 | flockUnlock(id, NO_LOCK); |
| 2367 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
| 2370 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 2371 | |
| 2372 | /******************* End of the flock lock implementation ********************* |
| 2373 | ******************************************************************************/ |
| 2374 | |
| 2375 | /****************************************************************************** |
| 2376 | ************************ Begin Named Semaphore Locking ************************ |
| 2377 | ** |
| 2378 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2379 | ** |
| 2380 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 2381 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 2382 | ** the database file at a time. This reduces potential concurrency, but |
| 2383 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2384 | */ |
| 2385 | #if OS_VXWORKS |
| 2386 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2387 | /* |
| 2388 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2389 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2390 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2391 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2392 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2393 | static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2394 | int rc = SQLITE_OK; |
| 2395 | int reserved = 0; |
| 2396 | unixFile *pFile = (unixFile*)id; |
| 2397 | |
| 2398 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2399 | |
| 2400 | assert( pFile ); |
| 2401 | |
| 2402 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2403 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2404 | reserved = 1; |
| 2405 | } |
| 2406 | |
| 2407 | /* Otherwise see if some other process holds it. */ |
| 2408 | if( !reserved ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2409 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2410 | |
| 2411 | if( sem_trywait(pSem)==-1 ){ |
| 2412 | int tErrno = errno; |
| 2413 | if( EAGAIN != tErrno ){ |
| 2414 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2415 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2416 | } else { |
| 2417 | /* someone else has the lock when we are in NO_LOCK */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2418 | reserved = (pFile->eFileLock < SHARED_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2419 | } |
| 2420 | }else{ |
| 2421 | /* we could have it if we want it */ |
| 2422 | sem_post(pSem); |
| 2423 | } |
| 2424 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2425 | OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2426 | |
| 2427 | *pResOut = reserved; |
| 2428 | return rc; |
| 2429 | } |
| 2430 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2431 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2432 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2433 | ** of the following: |
| 2434 | ** |
| 2435 | ** (1) SHARED_LOCK |
| 2436 | ** (2) RESERVED_LOCK |
| 2437 | ** (3) PENDING_LOCK |
| 2438 | ** (4) EXCLUSIVE_LOCK |
| 2439 | ** |
| 2440 | ** Sometimes when requesting one lock state, additional lock states |
| 2441 | ** are inserted in between. The locking might fail on one of the later |
| 2442 | ** transitions leaving the lock state different from what it started but |
| 2443 | ** still short of its goal. The following chart shows the allowed |
| 2444 | ** transitions and the inserted intermediate states: |
| 2445 | ** |
| 2446 | ** UNLOCKED -> SHARED |
| 2447 | ** SHARED -> RESERVED |
| 2448 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2449 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2450 | ** PENDING -> EXCLUSIVE |
| 2451 | ** |
| 2452 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 2453 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2454 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2455 | ** access the file. |
| 2456 | ** |
| 2457 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2458 | ** routine to lower a locking level. |
| 2459 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2460 | static int semXLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2461 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2462 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2463 | int rc = SQLITE_OK; |
| 2464 | |
| 2465 | /* if we already have a lock, it is exclusive. |
| 2466 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2467 | if (pFile->eFileLock > NO_LOCK) { |
| 2468 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2469 | rc = SQLITE_OK; |
| 2470 | goto sem_end_lock; |
| 2471 | } |
| 2472 | |
| 2473 | /* lock semaphore now but bail out when already locked. */ |
| 2474 | if( sem_trywait(pSem)==-1 ){ |
| 2475 | rc = SQLITE_BUSY; |
| 2476 | goto sem_end_lock; |
| 2477 | } |
| 2478 | |
| 2479 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2480 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2481 | |
| 2482 | sem_end_lock: |
| 2483 | return rc; |
| 2484 | } |
| 2485 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2486 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2487 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2488 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2489 | ** |
| 2490 | ** If the locking level of the file descriptor is already at or below |
| 2491 | ** the requested locking level, this routine is a no-op. |
| 2492 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2493 | static int semXUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2494 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2495 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2496 | |
| 2497 | assert( pFile ); |
| 2498 | assert( pSem ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2499 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2500 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2501 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2502 | |
| 2503 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2504 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2505 | return SQLITE_OK; |
| 2506 | } |
| 2507 | |
| 2508 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2509 | if (eFileLock==SHARED_LOCK) { |
| 2510 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2511 | return SQLITE_OK; |
| 2512 | } |
| 2513 | |
| 2514 | /* no, really unlock. */ |
| 2515 | if ( sem_post(pSem)==-1 ) { |
| 2516 | int rc, tErrno = errno; |
| 2517 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2518 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2519 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2520 | } |
| 2521 | return rc; |
| 2522 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2523 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2524 | return SQLITE_OK; |
| 2525 | } |
| 2526 | |
| 2527 | /* |
| 2528 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2529 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2530 | static int semXClose(sqlite3_file *id) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2531 | if( id ){ |
| 2532 | unixFile *pFile = (unixFile*)id; |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2533 | semXUnlock(id, NO_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2534 | assert( pFile ); |
| 2535 | unixEnterMutex(); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2536 | releaseInodeInfo(pFile); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2537 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2538 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2539 | } |
| 2540 | return SQLITE_OK; |
| 2541 | } |
| 2542 | |
| 2543 | #endif /* OS_VXWORKS */ |
| 2544 | /* |
| 2545 | ** Named semaphore locking is only available on VxWorks. |
| 2546 | ** |
| 2547 | *************** End of the named semaphore lock implementation **************** |
| 2548 | ******************************************************************************/ |
| 2549 | |
| 2550 | |
| 2551 | /****************************************************************************** |
| 2552 | *************************** Begin AFP Locking ********************************* |
| 2553 | ** |
| 2554 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2555 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2556 | ** |
| 2557 | ** Third-party implementations of AFP are available. But this code here |
| 2558 | ** only works on OSX. |
| 2559 | */ |
| 2560 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2561 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2562 | /* |
| 2563 | ** The afpLockingContext structure contains all afp lock specific state |
| 2564 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2565 | typedef struct afpLockingContext afpLockingContext; |
| 2566 | struct afpLockingContext { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2567 | int reserved; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2568 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2569 | }; |
| 2570 | |
| 2571 | struct ByteRangeLockPB2 |
| 2572 | { |
| 2573 | unsigned long long offset; /* offset to first byte to lock */ |
| 2574 | unsigned long long length; /* nbr of bytes to lock */ |
| 2575 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2576 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2577 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2578 | int fd; /* file desc to assoc this lock with */ |
| 2579 | }; |
| 2580 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2581 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2582 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2583 | /* |
| 2584 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2585 | ** AFP filesystem. |
| 2586 | ** |
| 2587 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2588 | */ |
| 2589 | static int afpSetLock( |
| 2590 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2591 | unixFile *pFile, /* Open file descriptor on path */ |
| 2592 | unsigned long long offset, /* First byte to be locked */ |
| 2593 | unsigned long long length, /* Number of bytes to lock */ |
| 2594 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2595 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2596 | struct ByteRangeLockPB2 pb; |
| 2597 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2598 | |
| 2599 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2600 | pb.startEndFlag = 0; |
| 2601 | pb.offset = offset; |
| 2602 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2603 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2604 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2605 | OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2606 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2607 | offset, length)); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2608 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2609 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2610 | int rc; |
| 2611 | int tErrno = errno; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2612 | OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2613 | path, tErrno, strerror(tErrno))); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2614 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2615 | rc = SQLITE_BUSY; |
| 2616 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2617 | rc = sqliteErrorFromPosixError(tErrno, |
| 2618 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2619 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2620 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2621 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2622 | } |
| 2623 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2624 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2625 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2626 | } |
| 2627 | } |
| 2628 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2629 | /* |
| 2630 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2631 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2632 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2633 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2634 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2635 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2636 | int rc = SQLITE_OK; |
| 2637 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2638 | unixFile *pFile = (unixFile*)id; |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2639 | afpLockingContext *context; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2640 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2641 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2642 | |
| 2643 | assert( pFile ); |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2644 | context = (afpLockingContext *) pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2645 | if( context->reserved ){ |
| 2646 | *pResOut = 1; |
| 2647 | return SQLITE_OK; |
| 2648 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2649 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2650 | |
| 2651 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2652 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2653 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | /* Otherwise see if some other process holds it. |
| 2657 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2658 | if( !reserved ){ |
| 2659 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2660 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2661 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2662 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2663 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2664 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2665 | } else { |
| 2666 | /* if we failed to get the lock then someone else must have it */ |
| 2667 | reserved = 1; |
| 2668 | } |
| 2669 | if( IS_LOCK_ERROR(lrc) ){ |
| 2670 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2671 | } |
| 2672 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2673 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2674 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2675 | OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2676 | |
| 2677 | *pResOut = reserved; |
| 2678 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2679 | } |
| 2680 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2681 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2682 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2683 | ** of the following: |
| 2684 | ** |
| 2685 | ** (1) SHARED_LOCK |
| 2686 | ** (2) RESERVED_LOCK |
| 2687 | ** (3) PENDING_LOCK |
| 2688 | ** (4) EXCLUSIVE_LOCK |
| 2689 | ** |
| 2690 | ** Sometimes when requesting one lock state, additional lock states |
| 2691 | ** are inserted in between. The locking might fail on one of the later |
| 2692 | ** transitions leaving the lock state different from what it started but |
| 2693 | ** still short of its goal. The following chart shows the allowed |
| 2694 | ** transitions and the inserted intermediate states: |
| 2695 | ** |
| 2696 | ** UNLOCKED -> SHARED |
| 2697 | ** SHARED -> RESERVED |
| 2698 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2699 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2700 | ** PENDING -> EXCLUSIVE |
| 2701 | ** |
| 2702 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2703 | ** routine to lower a locking level. |
| 2704 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2705 | static int afpLock(sqlite3_file *id, int eFileLock){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2706 | int rc = SQLITE_OK; |
| 2707 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2708 | unixInodeInfo *pInode = pFile->pInode; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2709 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2710 | |
| 2711 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2712 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 2713 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2714 | azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0))); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2715 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2716 | /* 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] | 2717 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2718 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2719 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2720 | if( pFile->eFileLock>=eFileLock ){ |
| 2721 | OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
| 2722 | azFileLock(eFileLock))); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2723 | return SQLITE_OK; |
| 2724 | } |
| 2725 | |
| 2726 | /* Make sure the locking sequence is correct |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2727 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 2728 | ** (2) SQLite never explicitly requests a pendig lock. |
| 2729 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2730 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2731 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 2732 | assert( eFileLock!=PENDING_LOCK ); |
| 2733 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2734 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2735 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2736 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2737 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2738 | pInode = pFile->pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2739 | |
| 2740 | /* If some thread using this PID has a lock via a different unixFile* |
| 2741 | ** handle that precludes the requested lock, return BUSY. |
| 2742 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2743 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 2744 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2745 | ){ |
| 2746 | rc = SQLITE_BUSY; |
| 2747 | goto afp_end_lock; |
| 2748 | } |
| 2749 | |
| 2750 | /* If a SHARED lock is requested, and some thread using this PID already |
| 2751 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 2752 | ** return SQLITE_OK. |
| 2753 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2754 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2755 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2756 | assert( eFileLock==SHARED_LOCK ); |
| 2757 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2758 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2759 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2760 | pInode->nShared++; |
| 2761 | pInode->nLock++; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2762 | goto afp_end_lock; |
| 2763 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2764 | |
| 2765 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2766 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2767 | ** be released. |
| 2768 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2769 | if( eFileLock==SHARED_LOCK |
| 2770 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2771 | ){ |
| 2772 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2773 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2774 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2775 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2776 | goto afp_end_lock; |
| 2777 | } |
| 2778 | } |
| 2779 | |
| 2780 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2781 | ** operating system calls for the specified lock. |
| 2782 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2783 | if( eFileLock==SHARED_LOCK ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2784 | int lrc1, lrc2, lrc1Errno = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2785 | long lk, mask; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2786 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2787 | assert( pInode->nShared==0 ); |
| 2788 | assert( pInode->eFileLock==0 ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2789 | |
| 2790 | mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2791 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2792 | /* note that the quality of the randomness doesn't matter that much */ |
| 2793 | lk = random(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2794 | pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2795 | lrc1 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2796 | SHARED_FIRST+pInode->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2797 | if( IS_LOCK_ERROR(lrc1) ){ |
| 2798 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2799 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2800 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2801 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2802 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2803 | if( IS_LOCK_ERROR(lrc1) ) { |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2804 | storeLastErrno(pFile, lrc1Errno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2805 | rc = lrc1; |
| 2806 | goto afp_end_lock; |
| 2807 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 2808 | rc = lrc2; |
| 2809 | goto afp_end_lock; |
| 2810 | } else if( lrc1 != SQLITE_OK ) { |
| 2811 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2812 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2813 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2814 | pInode->nLock++; |
| 2815 | pInode->nShared = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2816 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2817 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2818 | /* We are trying for an exclusive lock but another thread in this |
| 2819 | ** same process is still holding a shared lock. */ |
| 2820 | rc = SQLITE_BUSY; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2821 | }else{ |
| 2822 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 2823 | ** assumed that there is a SHARED or greater lock on the file |
| 2824 | ** already. |
| 2825 | */ |
| 2826 | int failed = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2827 | assert( 0!=pFile->eFileLock ); |
| 2828 | if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2829 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2830 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2831 | if( !failed ){ |
| 2832 | context->reserved = 1; |
| 2833 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2834 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2835 | if (!failed && eFileLock == EXCLUSIVE_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2836 | /* Acquire an EXCLUSIVE lock */ |
| 2837 | |
| 2838 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2839 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2840 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2841 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2842 | pInode->sharedByte, 1, 0)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2843 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2844 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2845 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2846 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2847 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2848 | SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2849 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 2850 | ** a critical I/O error |
| 2851 | */ |
| 2852 | rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : |
| 2853 | SQLITE_IOERR_LOCK; |
| 2854 | goto afp_end_lock; |
| 2855 | } |
| 2856 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2857 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2858 | } |
| 2859 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2860 | if( failed ){ |
| 2861 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2862 | } |
| 2863 | } |
| 2864 | |
| 2865 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2866 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2867 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2868 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 2869 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2870 | pInode->eFileLock = PENDING_LOCK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | afp_end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2874 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2875 | OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), |
| 2876 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2877 | return rc; |
| 2878 | } |
| 2879 | |
| 2880 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2881 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2882 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2883 | ** |
| 2884 | ** If the locking level of the file descriptor is already at or below |
| 2885 | ** the requested locking level, this routine is a no-op. |
| 2886 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2887 | static int afpUnlock(sqlite3_file *id, int eFileLock) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2888 | int rc = SQLITE_OK; |
| 2889 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2890 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2891 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 2892 | int skipShared = 0; |
| 2893 | #ifdef SQLITE_TEST |
| 2894 | int h = pFile->h; |
| 2895 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2896 | |
| 2897 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2898 | OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2899 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2900 | osGetpid(0))); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2901 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2902 | assert( eFileLock<=SHARED_LOCK ); |
| 2903 | if( pFile->eFileLock<=eFileLock ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2904 | return SQLITE_OK; |
| 2905 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2906 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2907 | pInode = pFile->pInode; |
| 2908 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2909 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2910 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2911 | SimulateIOErrorBenign(1); |
| 2912 | SimulateIOError( h=(-1) ) |
| 2913 | SimulateIOErrorBenign(0); |
| 2914 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 2915 | #ifdef SQLITE_DEBUG |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2916 | /* When reducing a lock such that other processes can start |
| 2917 | ** reading the database file again, make sure that the |
| 2918 | ** transaction counter was updated if any part of the database |
| 2919 | ** file changed. If the transaction counter is not updated, |
| 2920 | ** other connections to the same file might not realize that |
| 2921 | ** the file has changed and hence might not know to flush their |
| 2922 | ** cache. The use of a stale cache can lead to database corruption. |
| 2923 | */ |
| 2924 | assert( pFile->inNormalWrite==0 |
| 2925 | || pFile->dbUpdate==0 |
| 2926 | || pFile->transCntrChng==1 ); |
| 2927 | pFile->inNormalWrite = 0; |
| 2928 | #endif |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2929 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2930 | if( pFile->eFileLock==EXCLUSIVE_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2931 | rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2932 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2933 | /* only re-establish the shared lock if necessary */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2934 | int sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2935 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 2936 | } else { |
| 2937 | skipShared = 1; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2938 | } |
| 2939 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2940 | if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2941 | rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2942 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2943 | if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2944 | rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 2945 | if( !rc ){ |
| 2946 | context->reserved = 0; |
| 2947 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2948 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2949 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ |
| 2950 | pInode->eFileLock = SHARED_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2951 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2952 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2953 | if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2954 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2955 | /* Decrement the shared lock counter. Release the lock using an |
| 2956 | ** OS call only when all threads in this same process have released |
| 2957 | ** the lock. |
| 2958 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2959 | unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 2960 | pInode->nShared--; |
| 2961 | if( pInode->nShared==0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2962 | SimulateIOErrorBenign(1); |
| 2963 | SimulateIOError( h=(-1) ) |
| 2964 | SimulateIOErrorBenign(0); |
| 2965 | if( !skipShared ){ |
| 2966 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 2967 | } |
| 2968 | if( !rc ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2969 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2970 | pFile->eFileLock = NO_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2971 | } |
| 2972 | } |
| 2973 | if( rc==SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2974 | pInode->nLock--; |
| 2975 | assert( pInode->nLock>=0 ); |
| 2976 | if( pInode->nLock==0 ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 2977 | closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2978 | } |
| 2979 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2980 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2981 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2982 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2983 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2984 | return rc; |
| 2985 | } |
| 2986 | |
| 2987 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2988 | ** Close a file & cleanup AFP specific locking context |
| 2989 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2990 | static int afpClose(sqlite3_file *id) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2991 | int rc = SQLITE_OK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2992 | unixFile *pFile = (unixFile*)id; |
| 2993 | assert( id!=0 ); |
| 2994 | afpUnlock(id, NO_LOCK); |
| 2995 | unixEnterMutex(); |
| 2996 | if( pFile->pInode && pFile->pInode->nLock ){ |
| 2997 | /* If there are outstanding locks, do not actually close the file just |
| 2998 | ** yet because that would clear those locks. Instead, add the file |
| 2999 | ** descriptor to pInode->aPending. It will be automatically closed when |
| 3000 | ** the last lock is cleared. |
| 3001 | */ |
| 3002 | setPendingFd(pFile); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3003 | } |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 3004 | releaseInodeInfo(pFile); |
| 3005 | sqlite3_free(pFile->lockingContext); |
| 3006 | rc = closeUnixFile(id); |
| 3007 | unixLeaveMutex(); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3008 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3009 | } |
| 3010 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3011 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3012 | /* |
| 3013 | ** The code above is the AFP lock implementation. The code is specific |
| 3014 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 3015 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 3016 | ** VFS is not available. |
| 3017 | ** |
| 3018 | ********************* End of the AFP lock implementation ********************** |
| 3019 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3020 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3021 | /****************************************************************************** |
| 3022 | *************************** Begin NFS Locking ********************************/ |
| 3023 | |
| 3024 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 3025 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3026 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3027 | ** must be either NO_LOCK or SHARED_LOCK. |
| 3028 | ** |
| 3029 | ** If the locking level of the file descriptor is already at or below |
| 3030 | ** the requested locking level, this routine is a no-op. |
| 3031 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3032 | static int nfsUnlock(sqlite3_file *id, int eFileLock){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 3033 | return posixUnlock(id, eFileLock, 1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 3037 | /* |
| 3038 | ** The code above is the NFS lock implementation. The code is specific |
| 3039 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 3040 | ** is available. |
| 3041 | ** |
| 3042 | ********************* End of the NFS lock implementation ********************** |
| 3043 | ******************************************************************************/ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3044 | |
| 3045 | /****************************************************************************** |
| 3046 | **************** Non-locking sqlite3_file methods ***************************** |
| 3047 | ** |
| 3048 | ** The next division contains implementations for all methods of the |
| 3049 | ** sqlite3_file object other than the locking methods. The locking |
| 3050 | ** methods were defined in divisions above (one locking method per |
| 3051 | ** division). Those methods that are common to all locking modes |
| 3052 | ** are gather together into this division. |
| 3053 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3054 | |
| 3055 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3056 | ** Seek to the offset passed as the second argument, then read cnt |
| 3057 | ** bytes into pBuf. Return the number of bytes actually read. |
| 3058 | ** |
| 3059 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 3060 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 3061 | ** one system to another. Since SQLite does not define USE_PREAD |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3062 | ** in any form by default, we will not attempt to define _XOPEN_SOURCE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3063 | ** See tickets #2741 and #2681. |
| 3064 | ** |
| 3065 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 3066 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3067 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3068 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 3069 | int got; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3070 | int prior = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3071 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3072 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3073 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3074 | TIMER_START; |
drh | c1fd2cf | 2012-10-01 12:16:26 +0000 | [diff] [blame] | 3075 | assert( cnt==(cnt&0x1ffff) ); |
drh | 35a0379 | 2013-08-29 23:34:53 +0000 | [diff] [blame] | 3076 | assert( id->h>2 ); |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3077 | do{ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3078 | #if defined(USE_PREAD) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3079 | got = osPread(id->h, pBuf, cnt, offset); |
| 3080 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3081 | #elif defined(USE_PREAD64) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3082 | got = osPread64(id->h, pBuf, cnt, offset); |
| 3083 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3084 | #else |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3085 | newOffset = lseek(id->h, offset, SEEK_SET); |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3086 | SimulateIOError( newOffset = -1 ); |
| 3087 | if( newOffset<0 ){ |
| 3088 | storeLastErrno((unixFile*)id, errno); |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3089 | return -1; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3090 | } |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3091 | got = osRead(id->h, pBuf, cnt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3092 | #endif |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3093 | if( got==cnt ) break; |
| 3094 | if( got<0 ){ |
| 3095 | if( errno==EINTR ){ got = 1; continue; } |
| 3096 | prior = 0; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3097 | storeLastErrno((unixFile*)id, errno); |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3098 | break; |
| 3099 | }else if( got>0 ){ |
| 3100 | cnt -= got; |
| 3101 | offset += got; |
| 3102 | prior += got; |
| 3103 | pBuf = (void*)(got + (char*)pBuf); |
| 3104 | } |
| 3105 | }while( got>0 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3106 | TIMER_END; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3107 | OSTRACE(("READ %-3d %5d %7lld %llu\n", |
| 3108 | id->h, got+prior, offset-prior, TIMER_ELAPSED)); |
| 3109 | return got+prior; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3110 | } |
| 3111 | |
| 3112 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3113 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 3114 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 3115 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3116 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3117 | static int unixRead( |
| 3118 | sqlite3_file *id, |
| 3119 | void *pBuf, |
| 3120 | int amt, |
| 3121 | sqlite3_int64 offset |
| 3122 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3123 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3124 | int got; |
| 3125 | assert( id ); |
drh | 6cf9d8d | 2013-05-09 18:12:40 +0000 | [diff] [blame] | 3126 | assert( offset>=0 ); |
| 3127 | assert( amt>0 ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3128 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3129 | /* If this is a database file (not a journal, master-journal or temp |
| 3130 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3131 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3132 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3133 | || offset>=PENDING_BYTE+512 |
| 3134 | || offset+amt<=PENDING_BYTE |
| 3135 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3136 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3137 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3138 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 6c56963 | 2013-03-26 18:48:11 +0000 | [diff] [blame] | 3139 | /* Deal with as much of this read request as possible by transfering |
| 3140 | ** data from the memory mapping using memcpy(). */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3141 | if( offset<pFile->mmapSize ){ |
| 3142 | if( offset+amt <= pFile->mmapSize ){ |
| 3143 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt); |
| 3144 | return SQLITE_OK; |
| 3145 | }else{ |
| 3146 | int nCopy = pFile->mmapSize - offset; |
| 3147 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy); |
| 3148 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3149 | amt -= nCopy; |
| 3150 | offset += nCopy; |
| 3151 | } |
| 3152 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 3153 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3154 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3155 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3156 | if( got==amt ){ |
| 3157 | return SQLITE_OK; |
| 3158 | }else if( got<0 ){ |
| 3159 | /* lastErrno set by seekAndRead */ |
| 3160 | return SQLITE_IOERR_READ; |
| 3161 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3162 | storeLastErrno(pFile, 0); /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3163 | /* Unread parts of the buffer must be zero-filled */ |
| 3164 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 3165 | return SQLITE_IOERR_SHORT_READ; |
| 3166 | } |
| 3167 | } |
| 3168 | |
| 3169 | /* |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3170 | ** Attempt to seek the file-descriptor passed as the first argument to |
| 3171 | ** absolute offset iOff, then attempt to write nBuf bytes of data from |
| 3172 | ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise, |
| 3173 | ** return the actual number of bytes written (which may be less than |
| 3174 | ** nBuf). |
| 3175 | */ |
| 3176 | static int seekAndWriteFd( |
| 3177 | int fd, /* File descriptor to write to */ |
| 3178 | i64 iOff, /* File offset to begin writing at */ |
| 3179 | const void *pBuf, /* Copy data from this buffer to the file */ |
| 3180 | int nBuf, /* Size of buffer pBuf in bytes */ |
| 3181 | int *piErrno /* OUT: Error number if error occurs */ |
| 3182 | ){ |
| 3183 | int rc = 0; /* Value returned by system call */ |
| 3184 | |
| 3185 | assert( nBuf==(nBuf&0x1ffff) ); |
drh | 35a0379 | 2013-08-29 23:34:53 +0000 | [diff] [blame] | 3186 | assert( fd>2 ); |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3187 | assert( piErrno!=0 ); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3188 | nBuf &= 0x1ffff; |
| 3189 | TIMER_START; |
| 3190 | |
| 3191 | #if defined(USE_PREAD) |
drh | 2da47d3 | 2015-02-21 00:56:05 +0000 | [diff] [blame] | 3192 | do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR ); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3193 | #elif defined(USE_PREAD64) |
drh | 2da47d3 | 2015-02-21 00:56:05 +0000 | [diff] [blame] | 3194 | do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3195 | #else |
| 3196 | do{ |
| 3197 | i64 iSeek = lseek(fd, iOff, SEEK_SET); |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3198 | SimulateIOError( iSeek = -1 ); |
| 3199 | if( iSeek<0 ){ |
| 3200 | rc = -1; |
| 3201 | break; |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3202 | } |
| 3203 | rc = osWrite(fd, pBuf, nBuf); |
| 3204 | }while( rc<0 && errno==EINTR ); |
| 3205 | #endif |
| 3206 | |
| 3207 | TIMER_END; |
| 3208 | OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED)); |
| 3209 | |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3210 | if( rc<0 ) *piErrno = errno; |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3211 | return rc; |
| 3212 | } |
| 3213 | |
| 3214 | |
| 3215 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3216 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 3217 | ** Return the number of bytes actually read. Update the offset. |
| 3218 | ** |
| 3219 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 3220 | ** is set before returning. |
| 3221 | */ |
| 3222 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3223 | return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | |
| 3227 | /* |
| 3228 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 3229 | ** or some other error code on failure. |
| 3230 | */ |
| 3231 | static int unixWrite( |
| 3232 | sqlite3_file *id, |
| 3233 | const void *pBuf, |
| 3234 | int amt, |
| 3235 | sqlite3_int64 offset |
| 3236 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3237 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3238 | int wrote = 0; |
| 3239 | assert( id ); |
| 3240 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3241 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3242 | /* If this is a database file (not a journal, master-journal or temp |
| 3243 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3244 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3245 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3246 | || offset>=PENDING_BYTE+512 |
| 3247 | || offset+amt<=PENDING_BYTE |
| 3248 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3249 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3250 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3251 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3252 | /* If we are doing a normal write to a database file (as opposed to |
| 3253 | ** doing a hot-journal rollback or a write to some file other than a |
| 3254 | ** normal database file) then record the fact that the database |
| 3255 | ** has changed. If the transaction counter is modified, record that |
| 3256 | ** fact too. |
| 3257 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3258 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3259 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 3260 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3261 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3262 | char oldCntr[4]; |
| 3263 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3264 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3265 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3266 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3267 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 3268 | } |
| 3269 | } |
| 3270 | } |
| 3271 | #endif |
| 3272 | |
dan | fe33e39 | 2015-11-17 20:56:06 +0000 | [diff] [blame] | 3273 | #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3274 | /* Deal with as much of this write request as possible by transfering |
| 3275 | ** data from the memory mapping using memcpy(). */ |
| 3276 | if( offset<pFile->mmapSize ){ |
| 3277 | if( offset+amt <= pFile->mmapSize ){ |
| 3278 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt); |
| 3279 | return SQLITE_OK; |
| 3280 | }else{ |
| 3281 | int nCopy = pFile->mmapSize - offset; |
| 3282 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy); |
| 3283 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3284 | amt -= nCopy; |
| 3285 | offset += nCopy; |
| 3286 | } |
| 3287 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 3288 | #endif |
drh | 02bf8b4 | 2015-09-01 23:51:53 +0000 | [diff] [blame] | 3289 | |
| 3290 | while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3291 | amt -= wrote; |
| 3292 | offset += wrote; |
| 3293 | pBuf = &((char*)pBuf)[wrote]; |
| 3294 | } |
| 3295 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 3296 | SimulateDiskfullError(( wrote=0, amt=1 )); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3297 | |
drh | 02bf8b4 | 2015-09-01 23:51:53 +0000 | [diff] [blame] | 3298 | if( amt>wrote ){ |
drh | a21b83b | 2011-04-15 12:36:10 +0000 | [diff] [blame] | 3299 | if( wrote<0 && pFile->lastErrno!=ENOSPC ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3300 | /* lastErrno set by seekAndWrite */ |
| 3301 | return SQLITE_IOERR_WRITE; |
| 3302 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3303 | storeLastErrno(pFile, 0); /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3304 | return SQLITE_FULL; |
| 3305 | } |
| 3306 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3307 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3308 | return SQLITE_OK; |
| 3309 | } |
| 3310 | |
| 3311 | #ifdef SQLITE_TEST |
| 3312 | /* |
| 3313 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3314 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3315 | */ |
| 3316 | int sqlite3_sync_count = 0; |
| 3317 | int sqlite3_fullsync_count = 0; |
| 3318 | #endif |
| 3319 | |
| 3320 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3321 | ** We do not trust systems to provide a working fdatasync(). Some do. |
drh | 20f8e13 | 2011-08-31 21:01:55 +0000 | [diff] [blame] | 3322 | ** Others do no. To be safe, we will stick with the (slightly slower) |
| 3323 | ** fsync(). If you know that your system does support fdatasync() correctly, |
drh | f7a4a1b | 2015-01-10 18:02:45 +0000 | [diff] [blame] | 3324 | ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3325 | */ |
drh | f7a4a1b | 2015-01-10 18:02:45 +0000 | [diff] [blame] | 3326 | #if !defined(fdatasync) && !HAVE_FDATASYNC |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3327 | # define fdatasync fsync |
| 3328 | #endif |
| 3329 | |
| 3330 | /* |
| 3331 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 3332 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 3333 | ** only available on Mac OS X. But that could change. |
| 3334 | */ |
| 3335 | #ifdef F_FULLFSYNC |
| 3336 | # define HAVE_FULLFSYNC 1 |
| 3337 | #else |
| 3338 | # define HAVE_FULLFSYNC 0 |
| 3339 | #endif |
| 3340 | |
| 3341 | |
| 3342 | /* |
| 3343 | ** The fsync() system call does not work as advertised on many |
| 3344 | ** unix systems. The following procedure is an attempt to make |
| 3345 | ** it work better. |
| 3346 | ** |
| 3347 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 3348 | ** for testing when we want to run through the test suite quickly. |
| 3349 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 3350 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 3351 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3352 | ** |
| 3353 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 3354 | ** The idea behind dataOnly is that it should only write the file content |
| 3355 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 3356 | ** unchanged since the file size is part of the inode. However, |
| 3357 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 3358 | ** file size has changed. The only real difference between fdatasync() |
| 3359 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 3360 | ** inode if the mtime or owner or other inode attributes have changed. |
| 3361 | ** We only care about the file size, not the other file attributes, so |
| 3362 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 3363 | ** So, we always use fdatasync() if it is available, regardless of |
| 3364 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3365 | */ |
| 3366 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3367 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3368 | |
| 3369 | /* The following "ifdef/elif/else/" block has the same structure as |
| 3370 | ** the one below. It is replicated here solely to avoid cluttering |
| 3371 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 3372 | */ |
| 3373 | #ifdef SQLITE_NO_SYNC |
| 3374 | UNUSED_PARAMETER(fd); |
| 3375 | UNUSED_PARAMETER(fullSync); |
| 3376 | UNUSED_PARAMETER(dataOnly); |
| 3377 | #elif HAVE_FULLFSYNC |
| 3378 | UNUSED_PARAMETER(dataOnly); |
| 3379 | #else |
| 3380 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3381 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3382 | #endif |
| 3383 | |
| 3384 | /* Record the number of times that we do a normal fsync() and |
| 3385 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 3386 | ** gets called with the correct arguments. |
| 3387 | */ |
| 3388 | #ifdef SQLITE_TEST |
| 3389 | if( fullSync ) sqlite3_fullsync_count++; |
| 3390 | sqlite3_sync_count++; |
| 3391 | #endif |
| 3392 | |
| 3393 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
drh | 2c8fd12 | 2015-12-02 02:33:36 +0000 | [diff] [blame] | 3394 | ** no-op. But go ahead and call fstat() to validate the file |
| 3395 | ** descriptor as we need a method to provoke a failure during |
| 3396 | ** coverate testing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3397 | */ |
| 3398 | #ifdef SQLITE_NO_SYNC |
drh | 2c8fd12 | 2015-12-02 02:33:36 +0000 | [diff] [blame] | 3399 | { |
| 3400 | struct stat buf; |
| 3401 | rc = osFstat(fd, &buf); |
| 3402 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3403 | #elif HAVE_FULLFSYNC |
| 3404 | if( fullSync ){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3405 | rc = osFcntl(fd, F_FULLFSYNC, 0); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3406 | }else{ |
| 3407 | rc = 1; |
| 3408 | } |
| 3409 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3410 | ** It shouldn't be possible for fullfsync to fail on the local |
| 3411 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 3412 | ** isn't supported for this file system. So, attempt an fsync |
| 3413 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 3414 | ** It'd be better to detect fullfsync support once and avoid |
| 3415 | ** the fcntl call every time sync is called. |
| 3416 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3417 | if( rc ) rc = fsync(fd); |
| 3418 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3419 | #elif defined(__APPLE__) |
| 3420 | /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 3421 | ** so currently we default to the macro that redefines fdatasync to fsync |
| 3422 | */ |
| 3423 | rc = fsync(fd); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3424 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3425 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 3426 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3427 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3428 | rc = fsync(fd); |
| 3429 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3430 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3431 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 3432 | |
| 3433 | if( OS_VXWORKS && rc!= -1 ){ |
| 3434 | rc = 0; |
| 3435 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3436 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3437 | } |
| 3438 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3439 | /* |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3440 | ** Open a file descriptor to the directory containing file zFilename. |
| 3441 | ** If successful, *pFd is set to the opened file descriptor and |
| 3442 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 3443 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 3444 | ** value. |
| 3445 | ** |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3446 | ** The directory file descriptor is used for only one thing - to |
| 3447 | ** fsync() a directory to make sure file creation and deletion events |
| 3448 | ** are flushed to disk. Such fsyncs are not needed on newer |
| 3449 | ** journaling filesystems, but are required on older filesystems. |
| 3450 | ** |
| 3451 | ** This routine can be overridden using the xSetSysCall interface. |
| 3452 | ** The ability to override this routine was added in support of the |
| 3453 | ** chromium sandbox. Opening a directory is a security risk (we are |
| 3454 | ** told) so making it overrideable allows the chromium sandbox to |
| 3455 | ** replace this routine with a harmless no-op. To make this routine |
| 3456 | ** a no-op, replace it with a stub that returns SQLITE_OK but leaves |
| 3457 | ** *pFd set to a negative number. |
| 3458 | ** |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3459 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 3460 | ** the file descriptor *pFd using close(). |
| 3461 | */ |
| 3462 | static int openDirectory(const char *zFilename, int *pFd){ |
| 3463 | int ii; |
| 3464 | int fd = -1; |
| 3465 | char zDirname[MAX_PATHNAME+1]; |
| 3466 | |
| 3467 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
| 3468 | for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--); |
drh | adfa22e | 2015-12-02 02:08:30 +0000 | [diff] [blame] | 3469 | if( ii>1 ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3470 | zDirname[ii] = '\0'; |
| 3471 | fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0); |
| 3472 | if( fd>=0 ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3473 | OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); |
| 3474 | } |
| 3475 | } |
| 3476 | *pFd = fd; |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 3477 | if( fd>=0 ) return SQLITE_OK; |
| 3478 | return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname); |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
| 3481 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3482 | ** Make sure all writes to a particular file are committed to disk. |
| 3483 | ** |
| 3484 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 3485 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 3486 | ** file data is synced. |
| 3487 | ** |
| 3488 | ** Under Unix, also make sure that the directory entry for the file |
| 3489 | ** has been created by fsync-ing the directory that contains the file. |
| 3490 | ** If we do not do this and we encounter a power failure, the directory |
| 3491 | ** entry for the journal might not exist after we reboot. The next |
| 3492 | ** SQLite to access the file will not know that the journal exists (because |
| 3493 | ** the directory entry for the journal was never created) and the transaction |
| 3494 | ** will not roll back - possibly leading to database corruption. |
| 3495 | */ |
| 3496 | static int unixSync(sqlite3_file *id, int flags){ |
| 3497 | int rc; |
| 3498 | unixFile *pFile = (unixFile*)id; |
| 3499 | |
| 3500 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 3501 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 3502 | |
| 3503 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 3504 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 3505 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 3506 | ); |
| 3507 | |
| 3508 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 3509 | ** line is to test that doing so does not cause any problems. |
| 3510 | */ |
| 3511 | SimulateDiskfullError( return SQLITE_FULL ); |
| 3512 | |
| 3513 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3514 | OSTRACE(("SYNC %-3d\n", pFile->h)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3515 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 3516 | SimulateIOError( rc=1 ); |
| 3517 | if( rc ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3518 | storeLastErrno(pFile, errno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3519 | return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3520 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3521 | |
| 3522 | /* Also fsync the directory containing the file if the DIRSYNC flag |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 3523 | ** is set. This is a one-time occurrence. Many systems (examples: AIX) |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3524 | ** are unable to fsync a directory, so ignore errors on the fsync. |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3525 | */ |
| 3526 | if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){ |
| 3527 | int dirfd; |
| 3528 | OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3529 | HAVE_FULLFSYNC, isFullsync)); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3530 | rc = osOpenDirectory(pFile->zPath, &dirfd); |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 3531 | if( rc==SQLITE_OK ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3532 | full_fsync(dirfd, 0, 0); |
| 3533 | robust_close(pFile, dirfd, __LINE__); |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 3534 | }else{ |
| 3535 | assert( rc==SQLITE_CANTOPEN ); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 3536 | rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3537 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3538 | pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3539 | } |
| 3540 | return rc; |
| 3541 | } |
| 3542 | |
| 3543 | /* |
| 3544 | ** Truncate an open file to a specified size |
| 3545 | */ |
| 3546 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3547 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3548 | int rc; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3549 | assert( pFile ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3550 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3551 | |
| 3552 | /* If the user has configured a chunk-size for this file, truncate the |
| 3553 | ** file so that it consists of an integer number of chunks (i.e. the |
| 3554 | ** actual file size after the operation may be larger than the requested |
| 3555 | ** size). |
| 3556 | */ |
drh | b8af4b7 | 2012-04-05 20:04:39 +0000 | [diff] [blame] | 3557 | if( pFile->szChunk>0 ){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3558 | nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 3559 | } |
| 3560 | |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 3561 | rc = robust_ftruncate(pFile->h, nByte); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3562 | if( rc ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3563 | storeLastErrno(pFile, errno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3564 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3565 | }else{ |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3566 | #ifdef SQLITE_DEBUG |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3567 | /* If we are doing a normal write to a database file (as opposed to |
| 3568 | ** doing a hot-journal rollback or a write to some file other than a |
| 3569 | ** normal database file) and we truncate the file to zero length, |
| 3570 | ** that effectively updates the change counter. This might happen |
| 3571 | ** when restoring a database using the backup API from a zero-length |
| 3572 | ** source. |
| 3573 | */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3574 | if( pFile->inNormalWrite && nByte==0 ){ |
| 3575 | pFile->transCntrChng = 1; |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3576 | } |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3577 | #endif |
dan | c000331 | 2013-03-22 17:46:11 +0000 | [diff] [blame] | 3578 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3579 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | c000331 | 2013-03-22 17:46:11 +0000 | [diff] [blame] | 3580 | /* If the file was just truncated to a size smaller than the currently |
| 3581 | ** mapped region, reduce the effective mapping size as well. SQLite will |
| 3582 | ** use read() and write() to access data beyond this point from now on. |
| 3583 | */ |
| 3584 | if( nByte<pFile->mmapSize ){ |
| 3585 | pFile->mmapSize = nByte; |
| 3586 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3587 | #endif |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3588 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3589 | return SQLITE_OK; |
| 3590 | } |
| 3591 | } |
| 3592 | |
| 3593 | /* |
| 3594 | ** Determine the current size of a file in bytes |
| 3595 | */ |
| 3596 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 3597 | int rc; |
| 3598 | struct stat buf; |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 3599 | assert( id ); |
| 3600 | rc = osFstat(((unixFile*)id)->h, &buf); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3601 | SimulateIOError( rc=1 ); |
| 3602 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3603 | storeLastErrno((unixFile*)id, errno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3604 | return SQLITE_IOERR_FSTAT; |
| 3605 | } |
| 3606 | *pSize = buf.st_size; |
| 3607 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3608 | /* When opening a zero-size database, the findInodeInfo() procedure |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3609 | ** writes a single byte into that file in order to work around a bug |
| 3610 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 3611 | ** layers, we need to report this file size as zero even though it is |
| 3612 | ** really 1. Ticket #3260. |
| 3613 | */ |
| 3614 | if( *pSize==1 ) *pSize = 0; |
| 3615 | |
| 3616 | |
| 3617 | return SQLITE_OK; |
| 3618 | } |
| 3619 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3620 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3621 | /* |
| 3622 | ** Handler for proxy-locking file-control verbs. Defined below in the |
| 3623 | ** proxying locking division. |
| 3624 | */ |
| 3625 | static int proxyFileControl(sqlite3_file*,int,void*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3626 | #endif |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3627 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3628 | /* |
| 3629 | ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 3630 | ** file-control operation. Enlarge the database to nBytes in size |
| 3631 | ** (rounded up to the next chunk-size). If the database is already |
| 3632 | ** nBytes or larger, this routine is a no-op. |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3633 | */ |
| 3634 | static int fcntlSizeHint(unixFile *pFile, i64 nByte){ |
mistachkin | d589a54 | 2011-08-30 01:23:34 +0000 | [diff] [blame] | 3635 | if( pFile->szChunk>0 ){ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3636 | i64 nSize; /* Required file size */ |
| 3637 | struct stat buf; /* Used to hold return values of fstat() */ |
| 3638 | |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3639 | if( osFstat(pFile->h, &buf) ){ |
| 3640 | return SQLITE_IOERR_FSTAT; |
| 3641 | } |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3642 | |
| 3643 | nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; |
| 3644 | if( nSize>(i64)buf.st_size ){ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3645 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3646 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3647 | /* The code below is handling the return value of osFallocate() |
| 3648 | ** correctly. posix_fallocate() is defined to "returns zero on success, |
| 3649 | ** or an error number on failure". See the manpage for details. */ |
| 3650 | int err; |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3651 | do{ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3652 | err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size); |
| 3653 | }while( err==EINTR ); |
| 3654 | if( err ) return SQLITE_IOERR_WRITE; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3655 | #else |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3656 | /* If the OS does not have posix_fallocate(), fake it. Write a |
| 3657 | ** single byte to the last byte in each block that falls entirely |
| 3658 | ** within the extended region. Then, if required, a single byte |
| 3659 | ** at offset (nSize-1), to set the size of the file correctly. |
| 3660 | ** This is a similar technique to that used by glibc on systems |
| 3661 | ** that do not have a real fallocate() call. |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3662 | */ |
| 3663 | int nBlk = buf.st_blksize; /* File-system block size */ |
dan | ef3d66c | 2015-01-06 21:31:47 +0000 | [diff] [blame] | 3664 | int nWrite = 0; /* Number of bytes written by seekAndWrite */ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3665 | i64 iWrite; /* Next offset to write to */ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3666 | |
drh | 053378d | 2015-12-01 22:09:42 +0000 | [diff] [blame] | 3667 | iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1; |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3668 | assert( iWrite>=buf.st_size ); |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3669 | assert( ((iWrite+1)%nBlk)==0 ); |
drh | 053378d | 2015-12-01 22:09:42 +0000 | [diff] [blame] | 3670 | for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){ |
| 3671 | if( iWrite>=nSize ) iWrite = nSize - 1; |
dan | ef3d66c | 2015-01-06 21:31:47 +0000 | [diff] [blame] | 3672 | nWrite = seekAndWrite(pFile, iWrite, "", 1); |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3673 | if( nWrite!=1 ) return SQLITE_IOERR_WRITE; |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3674 | } |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3675 | #endif |
| 3676 | } |
| 3677 | } |
| 3678 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3679 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3680 | if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3681 | int rc; |
| 3682 | if( pFile->szChunk<=0 ){ |
| 3683 | if( robust_ftruncate(pFile->h, nByte) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3684 | storeLastErrno(pFile, errno); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3685 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
| 3686 | } |
| 3687 | } |
| 3688 | |
| 3689 | rc = unixMapfile(pFile, nByte); |
| 3690 | return rc; |
| 3691 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3692 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3693 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3694 | return SQLITE_OK; |
| 3695 | } |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3696 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3697 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3698 | ** If *pArg is initially negative then this is a query. Set *pArg to |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3699 | ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set. |
| 3700 | ** |
| 3701 | ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags. |
| 3702 | */ |
| 3703 | static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){ |
| 3704 | if( *pArg<0 ){ |
| 3705 | *pArg = (pFile->ctrlFlags & mask)!=0; |
| 3706 | }else if( (*pArg)==0 ){ |
| 3707 | pFile->ctrlFlags &= ~mask; |
| 3708 | }else{ |
| 3709 | pFile->ctrlFlags |= mask; |
| 3710 | } |
| 3711 | } |
| 3712 | |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3713 | /* Forward declaration */ |
| 3714 | static int unixGetTempname(int nBuf, char *zBuf); |
| 3715 | |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3716 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3717 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3718 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3719 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3720 | unixFile *pFile = (unixFile*)id; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3721 | switch( op ){ |
| 3722 | case SQLITE_FCNTL_LOCKSTATE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3723 | *(int*)pArg = pFile->eFileLock; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3724 | return SQLITE_OK; |
| 3725 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3726 | case SQLITE_FCNTL_LAST_ERRNO: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3727 | *(int*)pArg = pFile->lastErrno; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3728 | return SQLITE_OK; |
| 3729 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3730 | case SQLITE_FCNTL_CHUNK_SIZE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3731 | pFile->szChunk = *(int *)pArg; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3732 | return SQLITE_OK; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3733 | } |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3734 | case SQLITE_FCNTL_SIZE_HINT: { |
dan | da04ea4 | 2011-08-23 05:10:39 +0000 | [diff] [blame] | 3735 | int rc; |
| 3736 | SimulateIOErrorBenign(1); |
| 3737 | rc = fcntlSizeHint(pFile, *(i64 *)pArg); |
| 3738 | SimulateIOErrorBenign(0); |
| 3739 | return rc; |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3740 | } |
| 3741 | case SQLITE_FCNTL_PERSIST_WAL: { |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3742 | unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg); |
| 3743 | return SQLITE_OK; |
| 3744 | } |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3745 | case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { |
| 3746 | unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg); |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3747 | return SQLITE_OK; |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3748 | } |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 3749 | case SQLITE_FCNTL_VFSNAME: { |
| 3750 | *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName); |
| 3751 | return SQLITE_OK; |
| 3752 | } |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3753 | case SQLITE_FCNTL_TEMPFILENAME: { |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3754 | char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname ); |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3755 | if( zTFile ){ |
| 3756 | unixGetTempname(pFile->pVfs->mxPathname, zTFile); |
| 3757 | *(char**)pArg = zTFile; |
| 3758 | } |
| 3759 | return SQLITE_OK; |
| 3760 | } |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 3761 | case SQLITE_FCNTL_HAS_MOVED: { |
| 3762 | *(int*)pArg = fileHasMoved(pFile); |
| 3763 | return SQLITE_OK; |
| 3764 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3765 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3766 | case SQLITE_FCNTL_MMAP_SIZE: { |
drh | 34f7490 | 2013-04-03 13:09:18 +0000 | [diff] [blame] | 3767 | i64 newLimit = *(i64*)pArg; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3768 | int rc = SQLITE_OK; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3769 | if( newLimit>sqlite3GlobalConfig.mxMmap ){ |
| 3770 | newLimit = sqlite3GlobalConfig.mxMmap; |
| 3771 | } |
| 3772 | *(i64*)pArg = pFile->mmapSizeMax; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3773 | if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3774 | pFile->mmapSizeMax = newLimit; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3775 | if( pFile->mmapSize>0 ){ |
| 3776 | unixUnmapfile(pFile); |
| 3777 | rc = unixMapfile(pFile, -1); |
| 3778 | } |
dan | bcb8a86 | 2013-04-08 15:30:41 +0000 | [diff] [blame] | 3779 | } |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 3780 | return rc; |
dan | b2d3de3 | 2013-03-14 18:34:37 +0000 | [diff] [blame] | 3781 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3782 | #endif |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3783 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3784 | /* The pager calls this method to signal that it has done |
| 3785 | ** a rollback and that the database is therefore unchanged and |
| 3786 | ** it hence it is OK for the transaction change counter to be |
| 3787 | ** unchanged. |
| 3788 | */ |
| 3789 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 3790 | ((unixFile*)id)->dbUpdate = 0; |
| 3791 | return SQLITE_OK; |
| 3792 | } |
| 3793 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3794 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3795 | case SQLITE_FCNTL_SET_LOCKPROXYFILE: |
| 3796 | case SQLITE_FCNTL_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3797 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3798 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3799 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3800 | } |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 3801 | return SQLITE_NOTFOUND; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 3802 | } |
| 3803 | |
| 3804 | /* |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3805 | ** Return the sector size in bytes of the underlying block device for |
| 3806 | ** the specified file. This is almost always 512 bytes, but may be |
| 3807 | ** larger for some devices. |
| 3808 | ** |
| 3809 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 3810 | ** if two files are created in the same file-system directory (i.e. |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3811 | ** a database and its journal file) that the sector size will be the |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3812 | ** same for both. |
| 3813 | */ |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3814 | #ifndef __QNXNTO__ |
| 3815 | static int unixSectorSize(sqlite3_file *NotUsed){ |
| 3816 | UNUSED_PARAMETER(NotUsed); |
drh | 8942d41 | 2012-01-02 18:20:14 +0000 | [diff] [blame] | 3817 | return SQLITE_DEFAULT_SECTOR_SIZE; |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3818 | } |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3819 | #endif |
| 3820 | |
| 3821 | /* |
| 3822 | ** The following version of unixSectorSize() is optimized for QNX. |
| 3823 | */ |
| 3824 | #ifdef __QNXNTO__ |
| 3825 | #include <sys/dcmd_blk.h> |
| 3826 | #include <sys/statvfs.h> |
| 3827 | static int unixSectorSize(sqlite3_file *id){ |
| 3828 | unixFile *pFile = (unixFile*)id; |
| 3829 | if( pFile->sectorSize == 0 ){ |
| 3830 | struct statvfs fsInfo; |
| 3831 | |
| 3832 | /* Set defaults for non-supported filesystems */ |
| 3833 | pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 3834 | pFile->deviceCharacteristics = 0; |
| 3835 | if( fstatvfs(pFile->h, &fsInfo) == -1 ) { |
| 3836 | return pFile->sectorSize; |
| 3837 | } |
| 3838 | |
| 3839 | if( !strcmp(fsInfo.f_basetype, "tmp") ) { |
| 3840 | pFile->sectorSize = fsInfo.f_bsize; |
| 3841 | pFile->deviceCharacteristics = |
| 3842 | SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */ |
| 3843 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3844 | ** the write succeeds */ |
| 3845 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3846 | ** so it is ordered */ |
| 3847 | 0; |
| 3848 | }else if( strstr(fsInfo.f_basetype, "etfs") ){ |
| 3849 | pFile->sectorSize = fsInfo.f_bsize; |
| 3850 | pFile->deviceCharacteristics = |
| 3851 | /* etfs cluster size writes are atomic */ |
| 3852 | (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) | |
| 3853 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3854 | ** the write succeeds */ |
| 3855 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3856 | ** so it is ordered */ |
| 3857 | 0; |
| 3858 | }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){ |
| 3859 | pFile->sectorSize = fsInfo.f_bsize; |
| 3860 | pFile->deviceCharacteristics = |
| 3861 | SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */ |
| 3862 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3863 | ** the write succeeds */ |
| 3864 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3865 | ** so it is ordered */ |
| 3866 | 0; |
| 3867 | }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){ |
| 3868 | pFile->sectorSize = fsInfo.f_bsize; |
| 3869 | pFile->deviceCharacteristics = |
| 3870 | /* full bitset of atomics from max sector size and smaller */ |
| 3871 | ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 3872 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3873 | ** so it is ordered */ |
| 3874 | 0; |
| 3875 | }else if( strstr(fsInfo.f_basetype, "dos") ){ |
| 3876 | pFile->sectorSize = fsInfo.f_bsize; |
| 3877 | pFile->deviceCharacteristics = |
| 3878 | /* full bitset of atomics from max sector size and smaller */ |
| 3879 | ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 3880 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 3881 | ** so it is ordered */ |
| 3882 | 0; |
| 3883 | }else{ |
| 3884 | pFile->deviceCharacteristics = |
| 3885 | SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */ |
| 3886 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 3887 | ** the write succeeds */ |
| 3888 | 0; |
| 3889 | } |
| 3890 | } |
| 3891 | /* Last chance verification. If the sector size isn't a multiple of 512 |
| 3892 | ** then it isn't valid.*/ |
| 3893 | if( pFile->sectorSize % 512 != 0 ){ |
| 3894 | pFile->deviceCharacteristics = 0; |
| 3895 | pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 3896 | } |
| 3897 | return pFile->sectorSize; |
| 3898 | } |
| 3899 | #endif /* __QNXNTO__ */ |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3900 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3901 | /* |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3902 | ** Return the device characteristics for the file. |
| 3903 | ** |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3904 | ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default. |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3905 | ** However, that choice is controversial since technically the underlying |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3906 | ** file system does not always provide powersafe overwrites. (In other |
| 3907 | ** words, after a power-loss event, parts of the file that were never |
| 3908 | ** written might end up being altered.) However, non-PSOW behavior is very, |
| 3909 | ** very rare. And asserting PSOW makes a large reduction in the amount |
| 3910 | ** of required I/O for journaling, since a lot of padding is eliminated. |
| 3911 | ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control |
| 3912 | ** available to turn it off and URI query parameter available to turn it off. |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3913 | */ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3914 | static int unixDeviceCharacteristics(sqlite3_file *id){ |
| 3915 | unixFile *p = (unixFile*)id; |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3916 | int rc = 0; |
| 3917 | #ifdef __QNXNTO__ |
| 3918 | if( p->sectorSize==0 ) unixSectorSize(id); |
| 3919 | rc = p->deviceCharacteristics; |
| 3920 | #endif |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3921 | if( p->ctrlFlags & UNIXFILE_PSOW ){ |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3922 | rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE; |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3923 | } |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 3924 | return rc; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 3927 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3928 | |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 3929 | /* |
| 3930 | ** Return the system page size. |
| 3931 | ** |
| 3932 | ** This function should not be called directly by other code in this file. |
| 3933 | ** Instead, it should be called via macro osGetpagesize(). |
| 3934 | */ |
| 3935 | static int unixGetpagesize(void){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 3936 | #if OS_VXWORKS |
| 3937 | return 1024; |
| 3938 | #elif defined(_BSD_SOURCE) |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 3939 | return getpagesize(); |
| 3940 | #else |
| 3941 | return (int)sysconf(_SC_PAGESIZE); |
| 3942 | #endif |
| 3943 | } |
| 3944 | |
| 3945 | #endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */ |
| 3946 | |
| 3947 | #ifndef SQLITE_OMIT_WAL |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3948 | |
| 3949 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3950 | ** Object used to represent an shared memory buffer. |
| 3951 | ** |
| 3952 | ** When multiple threads all reference the same wal-index, each thread |
| 3953 | ** has its own unixShm object, but they all point to a single instance |
| 3954 | ** of this unixShmNode object. In other words, each wal-index is opened |
| 3955 | ** only once per process. |
| 3956 | ** |
| 3957 | ** Each unixShmNode object is connected to a single unixInodeInfo object. |
| 3958 | ** We could coalesce this object into unixInodeInfo, but that would mean |
| 3959 | ** every open file that does not use shared memory (in other words, most |
| 3960 | ** open files) would have to carry around this extra information. So |
| 3961 | ** the unixInodeInfo object contains a pointer to this unixShmNode object |
| 3962 | ** and the unixShmNode object is created only when needed. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3963 | ** |
| 3964 | ** unixMutexHeld() must be true when creating or destroying |
| 3965 | ** this object or while reading or writing the following fields: |
| 3966 | ** |
| 3967 | ** nRef |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3968 | ** |
| 3969 | ** The following fields are read-only after the object is created: |
| 3970 | ** |
| 3971 | ** fid |
| 3972 | ** zFilename |
| 3973 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3974 | ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3975 | ** unixMutexHeld() is true when reading or writing any other field |
| 3976 | ** in this structure. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3977 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3978 | struct unixShmNode { |
| 3979 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3980 | sqlite3_mutex *mutex; /* Mutex to access this object */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3981 | char *zFilename; /* Name of the mmapped file */ |
| 3982 | int h; /* Open file descriptor */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3983 | int szRegion; /* Size of shared-memory regions */ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 3984 | u16 nRegion; /* Size of array apRegion */ |
| 3985 | u8 isReadonly; /* True if read-only */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3986 | char **apRegion; /* Array of mapped shared-memory regions */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3987 | int nRef; /* Number of unixShm objects pointing to this */ |
| 3988 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3989 | #ifdef SQLITE_DEBUG |
| 3990 | u8 exclMask; /* Mask of exclusive locks held */ |
| 3991 | u8 sharedMask; /* Mask of shared locks held */ |
| 3992 | u8 nextShmId; /* Next available unixShm.id value */ |
| 3993 | #endif |
| 3994 | }; |
| 3995 | |
| 3996 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3997 | ** Structure used internally by this VFS to record the state of an |
| 3998 | ** open shared memory connection. |
| 3999 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4000 | ** The following fields are initialized when this object is created and |
| 4001 | ** are read-only thereafter: |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4002 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4003 | ** unixShm.pFile |
| 4004 | ** unixShm.id |
| 4005 | ** |
| 4006 | ** All other fields are read/write. The unixShm.pFile->mutex must be held |
| 4007 | ** while accessing any read/write fields. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4008 | */ |
| 4009 | struct unixShm { |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4010 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 4011 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4012 | u8 hasMutex; /* True if holding the unixShmNode mutex */ |
drh | fd53231 | 2011-08-31 18:35:34 +0000 | [diff] [blame] | 4013 | u8 id; /* Id of this connection within its unixShmNode */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4014 | u16 sharedMask; /* Mask of shared locks held */ |
| 4015 | u16 exclMask; /* Mask of exclusive locks held */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4016 | }; |
| 4017 | |
| 4018 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4019 | ** Constants used for locking |
| 4020 | */ |
drh | bd9676c | 2010-06-23 17:58:38 +0000 | [diff] [blame] | 4021 | #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
drh | 4222441 | 2010-05-31 14:28:25 +0000 | [diff] [blame] | 4022 | #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4023 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4024 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4025 | ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4026 | ** |
| 4027 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 4028 | ** otherwise. |
| 4029 | */ |
| 4030 | static int unixShmSystemLock( |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4031 | unixFile *pFile, /* Open connection to the WAL file */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4032 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4033 | int ofst, /* First byte of the locking range */ |
| 4034 | int n /* Number of bytes to lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4035 | ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4036 | unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */ |
| 4037 | struct flock f; /* The posix advisory locking structure */ |
| 4038 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4039 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4040 | /* Access to the unixShmNode object is serialized by the caller */ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4041 | pShmNode = pFile->pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4042 | assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4043 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4044 | /* Shared locks never span more than one byte */ |
| 4045 | assert( n==1 || lockType!=F_RDLCK ); |
| 4046 | |
| 4047 | /* Locks are within range */ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4048 | assert( n>=1 && n<SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4049 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4050 | if( pShmNode->h>=0 ){ |
| 4051 | /* Initialize the locking parameters */ |
| 4052 | memset(&f, 0, sizeof(f)); |
| 4053 | f.l_type = lockType; |
| 4054 | f.l_whence = SEEK_SET; |
| 4055 | f.l_start = ofst; |
| 4056 | f.l_len = n; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4057 | |
drh | dcfb965 | 2015-12-02 00:05:26 +0000 | [diff] [blame] | 4058 | rc = osFcntl(pShmNode->h, F_SETLK, &f); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4059 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 4060 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4061 | |
| 4062 | /* Update the global lock state and do debug tracing */ |
| 4063 | #ifdef SQLITE_DEBUG |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4064 | { u16 mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4065 | OSTRACE(("SHM-LOCK ")); |
drh | 693e671 | 2014-01-24 22:58:00 +0000 | [diff] [blame] | 4066 | mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4067 | if( rc==SQLITE_OK ){ |
| 4068 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4069 | OSTRACE(("unlock %d ok", ofst)); |
| 4070 | pShmNode->exclMask &= ~mask; |
| 4071 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4072 | }else if( lockType==F_RDLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4073 | OSTRACE(("read-lock %d ok", ofst)); |
| 4074 | pShmNode->exclMask &= ~mask; |
| 4075 | pShmNode->sharedMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4076 | }else{ |
| 4077 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4078 | OSTRACE(("write-lock %d ok", ofst)); |
| 4079 | pShmNode->exclMask |= mask; |
| 4080 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4081 | } |
| 4082 | }else{ |
| 4083 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4084 | OSTRACE(("unlock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4085 | }else if( lockType==F_RDLCK ){ |
| 4086 | OSTRACE(("read-lock failed")); |
| 4087 | }else{ |
| 4088 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4089 | OSTRACE(("write-lock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4090 | } |
| 4091 | } |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 4092 | OSTRACE((" - afterwards %03x,%03x\n", |
| 4093 | pShmNode->sharedMask, pShmNode->exclMask)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4094 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4095 | #endif |
| 4096 | |
| 4097 | return rc; |
| 4098 | } |
| 4099 | |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4100 | /* |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4101 | ** Return the minimum number of 32KB shm regions that should be mapped at |
| 4102 | ** a time, assuming that each mapping must be an integer multiple of the |
| 4103 | ** current system page-size. |
| 4104 | ** |
| 4105 | ** Usually, this is 1. The exception seems to be systems that are configured |
| 4106 | ** to use 64KB pages - in this case each mapping must cover at least two |
| 4107 | ** shm regions. |
| 4108 | */ |
| 4109 | static int unixShmRegionPerMap(void){ |
| 4110 | int shmsz = 32*1024; /* SHM region size */ |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 4111 | int pgsz = osGetpagesize(); /* System page size */ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4112 | assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */ |
| 4113 | if( pgsz<shmsz ) return 1; |
| 4114 | return pgsz/shmsz; |
| 4115 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4116 | |
| 4117 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4118 | ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4119 | ** |
| 4120 | ** This is not a VFS shared-memory method; it is a utility function called |
| 4121 | ** by VFS shared-memory methods. |
| 4122 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4123 | static void unixShmPurge(unixFile *pFd){ |
| 4124 | unixShmNode *p = pFd->pInode->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4125 | assert( unixMutexHeld() ); |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 4126 | if( p && ALWAYS(p->nRef==0) ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4127 | int nShmPerMap = unixShmRegionPerMap(); |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4128 | int i; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4129 | assert( p->pInode==pFd->pInode ); |
drh | df3aa16 | 2011-06-24 11:29:51 +0000 | [diff] [blame] | 4130 | sqlite3_mutex_free(p->mutex); |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4131 | for(i=0; i<p->nRegion; i+=nShmPerMap){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4132 | if( p->h>=0 ){ |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 4133 | osMunmap(p->apRegion[i], p->szRegion); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4134 | }else{ |
| 4135 | sqlite3_free(p->apRegion[i]); |
| 4136 | } |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4137 | } |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4138 | sqlite3_free(p->apRegion); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4139 | if( p->h>=0 ){ |
| 4140 | robust_close(pFd, p->h, __LINE__); |
| 4141 | p->h = -1; |
| 4142 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4143 | p->pInode->pShmNode = 0; |
| 4144 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4145 | } |
| 4146 | } |
| 4147 | |
| 4148 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4149 | ** Open a shared-memory area associated with open database file pDbFd. |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4150 | ** This particular implementation uses mmapped files. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4151 | ** |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4152 | ** The file used to implement shared-memory is in the same directory |
| 4153 | ** as the open database file and has the same name as the open database |
| 4154 | ** file with the "-shm" suffix added. For example, if the database file |
| 4155 | ** is "/home/user1/config.db" then the file that is created and mmapped |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4156 | ** for shared memory will be called "/home/user1/config.db-shm". |
| 4157 | ** |
| 4158 | ** Another approach to is to use files in /dev/shm or /dev/tmp or an |
| 4159 | ** some other tmpfs mount. But if a file in a different directory |
| 4160 | ** from the database file is used, then differing access permissions |
| 4161 | ** or a chroot() might cause two different processes on the same |
| 4162 | ** database to end up using different files for shared memory - |
| 4163 | ** meaning that their memory would not really be shared - resulting |
| 4164 | ** in database corruption. Nevertheless, this tmpfs file usage |
| 4165 | ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" |
| 4166 | ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time |
| 4167 | ** option results in an incompatible build of SQLite; builds of SQLite |
| 4168 | ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the |
| 4169 | ** same database file at the same time, database corruption will likely |
| 4170 | ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered |
| 4171 | ** "unsupported" and may go away in a future SQLite release. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4172 | ** |
| 4173 | ** When opening a new shared-memory file, if no other instances of that |
| 4174 | ** file are currently open, in this process or in other processes, then |
| 4175 | ** the file must be truncated to zero length or have its header cleared. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4176 | ** |
| 4177 | ** If the original database file (pDbFd) is using the "unix-excl" VFS |
| 4178 | ** that means that an exclusive lock is held on the database file and |
| 4179 | ** that no other processes are able to read or write the database. In |
| 4180 | ** that case, we do not really need shared memory. No shared memory |
| 4181 | ** file is created. The shared memory will be simulated with heap memory. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4182 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4183 | static int unixOpenSharedMemory(unixFile *pDbFd){ |
| 4184 | struct unixShm *p = 0; /* The connection to be opened */ |
| 4185 | struct unixShmNode *pShmNode; /* The underlying mmapped file */ |
| 4186 | int rc; /* Result code */ |
| 4187 | unixInodeInfo *pInode; /* The inode of fd */ |
| 4188 | char *zShmFilename; /* Name of the file used for SHM */ |
| 4189 | int nShmFilename; /* Size of the SHM filename in bytes */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4190 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4191 | /* Allocate space for the new unixShm object. */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4192 | p = sqlite3_malloc64( sizeof(*p) ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4193 | if( p==0 ) return SQLITE_NOMEM; |
| 4194 | memset(p, 0, sizeof(*p)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4195 | assert( pDbFd->pShm==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4196 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4197 | /* Check to see if a unixShmNode object already exists. Reuse an existing |
| 4198 | ** one if present. Create a new one if necessary. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4199 | */ |
| 4200 | unixEnterMutex(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4201 | pInode = pDbFd->pInode; |
| 4202 | pShmNode = pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4203 | if( pShmNode==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4204 | struct stat sStat; /* fstat() info for database file */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4205 | #ifndef SQLITE_SHM_DIRECTORY |
| 4206 | const char *zBasePath = pDbFd->zPath; |
| 4207 | #endif |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4208 | |
| 4209 | /* Call fstat() to figure out the permissions on the database file. If |
| 4210 | ** a new *-shm file is created, an attempt will be made to create it |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 4211 | ** with the same permissions. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4212 | */ |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 4213 | if( osFstat(pDbFd->h, &sStat) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4214 | rc = SQLITE_IOERR_FSTAT; |
| 4215 | goto shm_open_err; |
| 4216 | } |
| 4217 | |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4218 | #ifdef SQLITE_SHM_DIRECTORY |
drh | 52bcde0 | 2012-01-03 14:50:45 +0000 | [diff] [blame] | 4219 | nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4220 | #else |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4221 | nShmFilename = 6 + (int)strlen(zBasePath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4222 | #endif |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4223 | pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4224 | if( pShmNode==0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4225 | rc = SQLITE_NOMEM; |
| 4226 | goto shm_open_err; |
| 4227 | } |
drh | 9cb5a0d | 2012-01-05 21:19:54 +0000 | [diff] [blame] | 4228 | memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename); |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4229 | zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1]; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4230 | #ifdef SQLITE_SHM_DIRECTORY |
| 4231 | sqlite3_snprintf(nShmFilename, zShmFilename, |
| 4232 | SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", |
| 4233 | (u32)sStat.st_ino, (u32)sStat.st_dev); |
| 4234 | #else |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4235 | sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", zBasePath); |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 4236 | sqlite3FileSuffix3(pDbFd->zPath, zShmFilename); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4237 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4238 | pShmNode->h = -1; |
| 4239 | pDbFd->pInode->pShmNode = pShmNode; |
| 4240 | pShmNode->pInode = pDbFd->pInode; |
| 4241 | pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 4242 | if( pShmNode->mutex==0 ){ |
| 4243 | rc = SQLITE_NOMEM; |
| 4244 | goto shm_open_err; |
| 4245 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4246 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4247 | if( pInode->bProcessLock==0 ){ |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 4248 | int openFlags = O_RDWR | O_CREAT; |
drh | 9291372 | 2011-12-23 00:07:33 +0000 | [diff] [blame] | 4249 | if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 4250 | openFlags = O_RDONLY; |
| 4251 | pShmNode->isReadonly = 1; |
| 4252 | } |
| 4253 | pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777)); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4254 | if( pShmNode->h<0 ){ |
drh | c96d1e7 | 2012-02-11 18:51:34 +0000 | [diff] [blame] | 4255 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename); |
| 4256 | goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4257 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4258 | |
| 4259 | /* If this process is running as root, make sure that the SHM file |
| 4260 | ** is owned by the same user that owns the original database. Otherwise, |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 4261 | ** the original owner will not be able to connect. |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4262 | */ |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 4263 | robustFchown(pShmNode->h, sStat.st_uid, sStat.st_gid); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4264 | |
| 4265 | /* Check to see if another process is holding the dead-man switch. |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4266 | ** If not, truncate the file to zero length. |
| 4267 | */ |
| 4268 | rc = SQLITE_OK; |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4269 | if( unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4270 | if( robust_ftruncate(pShmNode->h, 0) ){ |
| 4271 | rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4272 | } |
| 4273 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4274 | if( rc==SQLITE_OK ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4275 | rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1); |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4276 | } |
| 4277 | if( rc ) goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4278 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4279 | } |
| 4280 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4281 | /* Make the new connection a child of the unixShmNode */ |
| 4282 | p->pShmNode = pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4283 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4284 | p->id = pShmNode->nextShmId++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4285 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4286 | pShmNode->nRef++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4287 | pDbFd->pShm = p; |
| 4288 | unixLeaveMutex(); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 4289 | |
| 4290 | /* The reference count on pShmNode has already been incremented under |
| 4291 | ** the cover of the unixEnterMutex() mutex and the pointer from the |
| 4292 | ** new (struct unixShm) object to the pShmNode has been set. All that is |
| 4293 | ** left to do is to link the new object into the linked list starting |
| 4294 | ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex |
| 4295 | ** mutex. |
| 4296 | */ |
| 4297 | sqlite3_mutex_enter(pShmNode->mutex); |
| 4298 | p->pNext = pShmNode->pFirst; |
| 4299 | pShmNode->pFirst = p; |
| 4300 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4301 | return SQLITE_OK; |
| 4302 | |
| 4303 | /* Jump here on any error */ |
| 4304 | shm_open_err: |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4305 | unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4306 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4307 | unixLeaveMutex(); |
| 4308 | return rc; |
| 4309 | } |
| 4310 | |
| 4311 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4312 | ** This function is called to obtain a pointer to region iRegion of the |
| 4313 | ** shared-memory associated with the database file fd. Shared-memory regions |
| 4314 | ** are numbered starting from zero. Each shared-memory region is szRegion |
| 4315 | ** bytes in size. |
| 4316 | ** |
| 4317 | ** If an error occurs, an error code is returned and *pp is set to NULL. |
| 4318 | ** |
| 4319 | ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory |
| 4320 | ** region has not been allocated (by any client, including one running in a |
| 4321 | ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 4322 | ** bExtend is non-zero and the requested shared-memory region has not yet |
| 4323 | ** been allocated, it is allocated by this function. |
| 4324 | ** |
| 4325 | ** If the shared-memory region has already been allocated or is allocated by |
| 4326 | ** this call as described above, then it is mapped into this processes |
| 4327 | ** address space (if it is not already), *pp is set to point to the mapped |
| 4328 | ** memory and SQLITE_OK returned. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4329 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4330 | static int unixShmMap( |
| 4331 | sqlite3_file *fd, /* Handle open on database file */ |
| 4332 | int iRegion, /* Region to retrieve */ |
| 4333 | int szRegion, /* Size of regions */ |
| 4334 | int bExtend, /* True to extend file if necessary */ |
| 4335 | void volatile **pp /* OUT: Mapped memory */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4336 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4337 | unixFile *pDbFd = (unixFile*)fd; |
| 4338 | unixShm *p; |
| 4339 | unixShmNode *pShmNode; |
| 4340 | int rc = SQLITE_OK; |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4341 | int nShmPerMap = unixShmRegionPerMap(); |
| 4342 | int nReqRegion; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4343 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4344 | /* If the shared-memory file has not yet been opened, open it now. */ |
| 4345 | if( pDbFd->pShm==0 ){ |
| 4346 | rc = unixOpenSharedMemory(pDbFd); |
| 4347 | if( rc!=SQLITE_OK ) return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4348 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4349 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4350 | p = pDbFd->pShm; |
| 4351 | pShmNode = p->pShmNode; |
| 4352 | sqlite3_mutex_enter(pShmNode->mutex); |
| 4353 | assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4354 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4355 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4356 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4357 | |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4358 | /* Minimum number of regions required to be mapped. */ |
| 4359 | nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap; |
| 4360 | |
| 4361 | if( pShmNode->nRegion<nReqRegion ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4362 | char **apNew; /* New apRegion[] array */ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4363 | int nByte = nReqRegion*szRegion; /* Minimum required file size */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4364 | struct stat sStat; /* Used by fstat() */ |
| 4365 | |
| 4366 | pShmNode->szRegion = szRegion; |
| 4367 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4368 | if( pShmNode->h>=0 ){ |
| 4369 | /* The requested region is not mapped into this processes address space. |
| 4370 | ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 4371 | ** large enough to contain the requested region). |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4372 | */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4373 | if( osFstat(pShmNode->h, &sStat) ){ |
| 4374 | rc = SQLITE_IOERR_SHMSIZE; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4375 | goto shmpage_out; |
| 4376 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4377 | |
| 4378 | if( sStat.st_size<nByte ){ |
| 4379 | /* The requested memory region does not exist. If bExtend is set to |
| 4380 | ** false, exit early. *pp will be set to NULL and SQLITE_OK returned. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4381 | */ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4382 | if( !bExtend ){ |
drh | 0fbb50e | 2012-11-13 10:54:12 +0000 | [diff] [blame] | 4383 | goto shmpage_out; |
| 4384 | } |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4385 | |
| 4386 | /* Alternatively, if bExtend is true, extend the file. Do this by |
| 4387 | ** writing a single byte to the end of each (OS) page being |
| 4388 | ** allocated or extended. Technically, we need only write to the |
| 4389 | ** last page in order to extend the file. But writing to all new |
| 4390 | ** pages forces the OS to allocate them immediately, which reduces |
| 4391 | ** the chances of SIGBUS while accessing the mapped region later on. |
| 4392 | */ |
| 4393 | else{ |
| 4394 | static const int pgsz = 4096; |
| 4395 | int iPg; |
| 4396 | |
| 4397 | /* Write to the last byte of each newly allocated or extended page */ |
| 4398 | assert( (nByte % pgsz)==0 ); |
| 4399 | for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){ |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 4400 | int x = 0; |
| 4401 | if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, &x)!=1 ){ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4402 | const char *zFile = pShmNode->zFilename; |
| 4403 | rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile); |
| 4404 | goto shmpage_out; |
| 4405 | } |
| 4406 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4407 | } |
| 4408 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4409 | } |
| 4410 | |
| 4411 | /* Map the requested memory region into this processes address space. */ |
| 4412 | apNew = (char **)sqlite3_realloc( |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4413 | pShmNode->apRegion, nReqRegion*sizeof(char *) |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4414 | ); |
| 4415 | if( !apNew ){ |
| 4416 | rc = SQLITE_IOERR_NOMEM; |
| 4417 | goto shmpage_out; |
| 4418 | } |
| 4419 | pShmNode->apRegion = apNew; |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4420 | while( pShmNode->nRegion<nReqRegion ){ |
| 4421 | int nMap = szRegion*nShmPerMap; |
| 4422 | int i; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4423 | void *pMem; |
| 4424 | if( pShmNode->h>=0 ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4425 | pMem = osMmap(0, nMap, |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4426 | pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, |
drh | 5a05be1 | 2012-10-09 18:51:44 +0000 | [diff] [blame] | 4427 | MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4428 | ); |
| 4429 | if( pMem==MAP_FAILED ){ |
drh | 50990db | 2011-04-13 20:26:13 +0000 | [diff] [blame] | 4430 | rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4431 | goto shmpage_out; |
| 4432 | } |
| 4433 | }else{ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4434 | pMem = sqlite3_malloc64(szRegion); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4435 | if( pMem==0 ){ |
| 4436 | rc = SQLITE_NOMEM; |
| 4437 | goto shmpage_out; |
| 4438 | } |
| 4439 | memset(pMem, 0, szRegion); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4440 | } |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4441 | |
| 4442 | for(i=0; i<nShmPerMap; i++){ |
| 4443 | pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i]; |
| 4444 | } |
| 4445 | pShmNode->nRegion += nShmPerMap; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4446 | } |
| 4447 | } |
| 4448 | |
| 4449 | shmpage_out: |
| 4450 | if( pShmNode->nRegion>iRegion ){ |
| 4451 | *pp = pShmNode->apRegion[iRegion]; |
| 4452 | }else{ |
| 4453 | *pp = 0; |
| 4454 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4455 | if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4456 | sqlite3_mutex_leave(pShmNode->mutex); |
| 4457 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4458 | } |
| 4459 | |
| 4460 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4461 | ** Change the lock state for a shared-memory segment. |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4462 | ** |
| 4463 | ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little |
| 4464 | ** different here than in posix. In xShmLock(), one can go from unlocked |
| 4465 | ** to shared and back or from unlocked to exclusive and back. But one may |
| 4466 | ** not go from shared to exclusive or from exclusive to shared. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4467 | */ |
| 4468 | static int unixShmLock( |
| 4469 | sqlite3_file *fd, /* Database file holding the shared memory */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4470 | int ofst, /* First lock to acquire or release */ |
| 4471 | int n, /* Number of locks to acquire or release */ |
| 4472 | int flags /* What to do with the lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4473 | ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4474 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 4475 | unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 4476 | unixShm *pX; /* For looping over all siblings */ |
| 4477 | unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 4478 | int rc = SQLITE_OK; /* Result code */ |
| 4479 | u16 mask; /* Mask of locks to take or release */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4480 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4481 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4482 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4483 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4484 | assert( n>=1 ); |
| 4485 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 4486 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 4487 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 4488 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 4489 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4490 | assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4491 | assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4492 | |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4493 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4494 | assert( n>1 || mask==(1<<ofst) ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4495 | sqlite3_mutex_enter(pShmNode->mutex); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4496 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 4497 | u16 allMask = 0; /* Mask of locks held by siblings */ |
| 4498 | |
| 4499 | /* See if any siblings hold this same lock */ |
| 4500 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 4501 | if( pX==p ) continue; |
| 4502 | assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
| 4503 | allMask |= pX->sharedMask; |
| 4504 | } |
| 4505 | |
| 4506 | /* Unlock the system-level locks */ |
| 4507 | if( (mask & allMask)==0 ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4508 | rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4509 | }else{ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4510 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4511 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4512 | |
| 4513 | /* Undo the local locks */ |
| 4514 | if( rc==SQLITE_OK ){ |
| 4515 | p->exclMask &= ~mask; |
| 4516 | p->sharedMask &= ~mask; |
| 4517 | } |
| 4518 | }else if( flags & SQLITE_SHM_SHARED ){ |
| 4519 | u16 allShared = 0; /* Union of locks held by connections other than "p" */ |
| 4520 | |
| 4521 | /* Find out which shared locks are already held by sibling connections. |
| 4522 | ** If any sibling already holds an exclusive lock, go ahead and return |
| 4523 | ** SQLITE_BUSY. |
| 4524 | */ |
| 4525 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4526 | if( (pX->exclMask & mask)!=0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4527 | rc = SQLITE_BUSY; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4528 | break; |
| 4529 | } |
| 4530 | allShared |= pX->sharedMask; |
| 4531 | } |
| 4532 | |
| 4533 | /* Get shared locks at the system level, if necessary */ |
| 4534 | if( rc==SQLITE_OK ){ |
| 4535 | if( (allShared & mask)==0 ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4536 | rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4537 | }else{ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4538 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4539 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4540 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4541 | |
| 4542 | /* Get the local shared locks */ |
| 4543 | if( rc==SQLITE_OK ){ |
| 4544 | p->sharedMask |= mask; |
| 4545 | } |
| 4546 | }else{ |
| 4547 | /* Make sure no sibling connections hold locks that will block this |
| 4548 | ** lock. If any do, return SQLITE_BUSY right away. |
| 4549 | */ |
| 4550 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4551 | if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ |
| 4552 | rc = SQLITE_BUSY; |
| 4553 | break; |
| 4554 | } |
| 4555 | } |
| 4556 | |
| 4557 | /* Get the exclusive locks at the system level. Then if successful |
| 4558 | ** also mark the local connection as being locked. |
| 4559 | */ |
| 4560 | if( rc==SQLITE_OK ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4561 | rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4562 | if( rc==SQLITE_OK ){ |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4563 | assert( (p->sharedMask & mask)==0 ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4564 | p->exclMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4565 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4566 | } |
| 4567 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4568 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 4569 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 4570 | p->id, osGetpid(0), p->sharedMask, p->exclMask)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4571 | return rc; |
| 4572 | } |
| 4573 | |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4574 | /* |
| 4575 | ** Implement a memory barrier or memory fence on shared memory. |
| 4576 | ** |
| 4577 | ** All loads and stores begun before the barrier must complete before |
| 4578 | ** any load or store begun after the barrier. |
| 4579 | */ |
| 4580 | static void unixShmBarrier( |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4581 | sqlite3_file *fd /* Database file holding the shared memory */ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4582 | ){ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 4583 | UNUSED_PARAMETER(fd); |
drh | 22c733d | 2015-09-24 12:40:43 +0000 | [diff] [blame] | 4584 | sqlite3MemoryBarrier(); /* compiler-defined memory barrier */ |
| 4585 | unixEnterMutex(); /* Also mutex, for redundancy */ |
drh | b29ad85 | 2010-06-01 00:03:57 +0000 | [diff] [blame] | 4586 | unixLeaveMutex(); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4587 | } |
| 4588 | |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4589 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4590 | ** Close a connection to shared-memory. Delete the underlying |
| 4591 | ** storage if deleteFlag is true. |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 4592 | ** |
| 4593 | ** If there is no shared memory associated with the connection then this |
| 4594 | ** routine is a harmless no-op. |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4595 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4596 | static int unixShmUnmap( |
| 4597 | sqlite3_file *fd, /* The underlying database file */ |
| 4598 | int deleteFlag /* Delete shared-memory if true */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4599 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4600 | unixShm *p; /* The connection to be closed */ |
| 4601 | unixShmNode *pShmNode; /* The underlying shared-memory file */ |
| 4602 | unixShm **pp; /* For looping over sibling connections */ |
| 4603 | unixFile *pDbFd; /* The underlying database file */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4604 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4605 | pDbFd = (unixFile*)fd; |
| 4606 | p = pDbFd->pShm; |
| 4607 | if( p==0 ) return SQLITE_OK; |
| 4608 | pShmNode = p->pShmNode; |
| 4609 | |
| 4610 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4611 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4612 | |
| 4613 | /* Remove connection p from the set of connections associated |
| 4614 | ** with pShmNode */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4615 | sqlite3_mutex_enter(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4616 | for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 4617 | *pp = p->pNext; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4618 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4619 | /* Free the connection p */ |
| 4620 | sqlite3_free(p); |
| 4621 | pDbFd->pShm = 0; |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4622 | sqlite3_mutex_leave(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4623 | |
| 4624 | /* If pShmNode->nRef has reached 0, then close the underlying |
| 4625 | ** shared-memory file, too */ |
| 4626 | unixEnterMutex(); |
| 4627 | assert( pShmNode->nRef>0 ); |
| 4628 | pShmNode->nRef--; |
| 4629 | if( pShmNode->nRef==0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4630 | if( deleteFlag && pShmNode->h>=0 ){ |
| 4631 | osUnlink(pShmNode->zFilename); |
| 4632 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4633 | unixShmPurge(pDbFd); |
| 4634 | } |
| 4635 | unixLeaveMutex(); |
| 4636 | |
| 4637 | return SQLITE_OK; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4638 | } |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4639 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4640 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4641 | #else |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 4642 | # define unixShmMap 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4643 | # define unixShmLock 0 |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4644 | # define unixShmBarrier 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4645 | # define unixShmUnmap 0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4646 | #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 4647 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 4648 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4649 | /* |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4650 | ** If it is currently memory mapped, unmap file pFd. |
dan | d306e1a | 2013-03-20 18:25:49 +0000 | [diff] [blame] | 4651 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4652 | static void unixUnmapfile(unixFile *pFd){ |
| 4653 | assert( pFd->nFetchOut==0 ); |
| 4654 | if( pFd->pMapRegion ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4655 | osMunmap(pFd->pMapRegion, pFd->mmapSizeActual); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4656 | pFd->pMapRegion = 0; |
| 4657 | pFd->mmapSize = 0; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4658 | pFd->mmapSizeActual = 0; |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4659 | } |
| 4660 | } |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4661 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4662 | /* |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4663 | ** Attempt to set the size of the memory mapping maintained by file |
| 4664 | ** descriptor pFd to nNew bytes. Any existing mapping is discarded. |
| 4665 | ** |
| 4666 | ** If successful, this function sets the following variables: |
| 4667 | ** |
| 4668 | ** unixFile.pMapRegion |
| 4669 | ** unixFile.mmapSize |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4670 | ** unixFile.mmapSizeActual |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4671 | ** |
| 4672 | ** If unsuccessful, an error message is logged via sqlite3_log() and |
| 4673 | ** the three variables above are zeroed. In this case SQLite should |
| 4674 | ** continue accessing the database using the xRead() and xWrite() |
| 4675 | ** methods. |
| 4676 | */ |
| 4677 | static void unixRemapfile( |
| 4678 | unixFile *pFd, /* File descriptor object */ |
| 4679 | i64 nNew /* Required mapping size */ |
| 4680 | ){ |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4681 | const char *zErr = "mmap"; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4682 | int h = pFd->h; /* File descriptor open on db file */ |
| 4683 | u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4684 | i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */ |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4685 | u8 *pNew = 0; /* Location of new mapping */ |
| 4686 | int flags = PROT_READ; /* Flags to pass to mmap() */ |
| 4687 | |
| 4688 | assert( pFd->nFetchOut==0 ); |
| 4689 | assert( nNew>pFd->mmapSize ); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4690 | assert( nNew<=pFd->mmapSizeMax ); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4691 | assert( nNew>0 ); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4692 | assert( pFd->mmapSizeActual>=pFd->mmapSize ); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4693 | assert( MAP_FAILED!=0 ); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4694 | |
dan | fe33e39 | 2015-11-17 20:56:06 +0000 | [diff] [blame] | 4695 | #ifdef SQLITE_MMAP_READWRITE |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4696 | if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE; |
dan | fe33e39 | 2015-11-17 20:56:06 +0000 | [diff] [blame] | 4697 | #endif |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4698 | |
| 4699 | if( pOrig ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4700 | #if HAVE_MREMAP |
| 4701 | i64 nReuse = pFd->mmapSize; |
| 4702 | #else |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 4703 | const int szSyspage = osGetpagesize(); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4704 | i64 nReuse = (pFd->mmapSize & ~(szSyspage-1)); |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4705 | #endif |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4706 | u8 *pReq = &pOrig[nReuse]; |
| 4707 | |
| 4708 | /* Unmap any pages of the existing mapping that cannot be reused. */ |
| 4709 | if( nReuse!=nOrig ){ |
| 4710 | osMunmap(pReq, nOrig-nReuse); |
| 4711 | } |
| 4712 | |
| 4713 | #if HAVE_MREMAP |
| 4714 | pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4715 | zErr = "mremap"; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4716 | #else |
| 4717 | pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse); |
| 4718 | if( pNew!=MAP_FAILED ){ |
| 4719 | if( pNew!=pReq ){ |
| 4720 | osMunmap(pNew, nNew - nReuse); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4721 | pNew = 0; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4722 | }else{ |
| 4723 | pNew = pOrig; |
| 4724 | } |
| 4725 | } |
| 4726 | #endif |
| 4727 | |
dan | 48ccef8 | 2013-04-02 20:55:01 +0000 | [diff] [blame] | 4728 | /* The attempt to extend the existing mapping failed. Free it. */ |
| 4729 | if( pNew==MAP_FAILED || pNew==0 ){ |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4730 | osMunmap(pOrig, nReuse); |
| 4731 | } |
| 4732 | } |
| 4733 | |
| 4734 | /* If pNew is still NULL, try to create an entirely new mapping. */ |
| 4735 | if( pNew==0 ){ |
| 4736 | pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4737 | } |
| 4738 | |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4739 | if( pNew==MAP_FAILED ){ |
| 4740 | pNew = 0; |
| 4741 | nNew = 0; |
| 4742 | unixLogError(SQLITE_OK, zErr, pFd->zPath); |
| 4743 | |
| 4744 | /* If the mmap() above failed, assume that all subsequent mmap() calls |
| 4745 | ** will probably fail too. Fall back to using xRead/xWrite exclusively |
| 4746 | ** in this case. */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4747 | pFd->mmapSizeMax = 0; |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 4748 | } |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4749 | pFd->pMapRegion = (void *)pNew; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4750 | pFd->mmapSize = pFd->mmapSizeActual = nNew; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4751 | } |
| 4752 | |
| 4753 | /* |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4754 | ** Memory map or remap the file opened by file-descriptor pFd (if the file |
| 4755 | ** is already mapped, the existing mapping is replaced by the new). Or, if |
| 4756 | ** there already exists a mapping for this file, and there are still |
| 4757 | ** outstanding xFetch() references to it, this function is a no-op. |
| 4758 | ** |
| 4759 | ** If parameter nByte is non-negative, then it is the requested size of |
| 4760 | ** the mapping to create. Otherwise, if nByte is less than zero, then the |
| 4761 | ** requested size is the size of the file on disk. The actual size of the |
| 4762 | ** created mapping is either the requested size or the value configured |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 4763 | ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller. |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4764 | ** |
| 4765 | ** SQLITE_OK is returned if no error occurs (even if the mapping is not |
| 4766 | ** recreated as a result of outstanding references) or an SQLite error |
| 4767 | ** code otherwise. |
| 4768 | */ |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 4769 | static int unixMapfile(unixFile *pFd, i64 nMap){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4770 | assert( nMap>=0 || pFd->nFetchOut==0 ); |
drh | 333e6ca | 2015-12-02 15:44:39 +0000 | [diff] [blame^] | 4771 | assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4772 | if( pFd->nFetchOut>0 ) return SQLITE_OK; |
| 4773 | |
| 4774 | if( nMap<0 ){ |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 4775 | struct stat statbuf; /* Low-level file information */ |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 4776 | if( osFstat(pFd->h, &statbuf) ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4777 | return SQLITE_IOERR_FSTAT; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 4778 | } |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 4779 | nMap = statbuf.st_size; |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4780 | } |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4781 | if( nMap>pFd->mmapSizeMax ){ |
| 4782 | nMap = pFd->mmapSizeMax; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 4783 | } |
| 4784 | |
drh | 333e6ca | 2015-12-02 15:44:39 +0000 | [diff] [blame^] | 4785 | assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4786 | if( nMap!=pFd->mmapSize ){ |
drh | 333e6ca | 2015-12-02 15:44:39 +0000 | [diff] [blame^] | 4787 | unixRemapfile(pFd, nMap); |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4790 | return SQLITE_OK; |
| 4791 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 4792 | #endif /* SQLITE_MAX_MMAP_SIZE>0 */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4793 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4794 | /* |
| 4795 | ** If possible, return a pointer to a mapping of file fd starting at offset |
| 4796 | ** iOff. The mapping must be valid for at least nAmt bytes. |
| 4797 | ** |
| 4798 | ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK. |
| 4799 | ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK. |
| 4800 | ** Finally, if an error does occur, return an SQLite error code. The final |
| 4801 | ** value of *pp is undefined in this case. |
| 4802 | ** |
| 4803 | ** If this function does return a pointer, the caller must eventually |
| 4804 | ** release the reference by calling unixUnfetch(). |
| 4805 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4806 | static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4807 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4808 | unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 4809 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4810 | *pp = 0; |
| 4811 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4812 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 4813 | if( pFd->mmapSizeMax>0 ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4814 | if( pFd->pMapRegion==0 ){ |
| 4815 | int rc = unixMapfile(pFd, -1); |
| 4816 | if( rc!=SQLITE_OK ) return rc; |
| 4817 | } |
| 4818 | if( pFd->mmapSize >= iOff+nAmt ){ |
| 4819 | *pp = &((u8 *)pFd->pMapRegion)[iOff]; |
| 4820 | pFd->nFetchOut++; |
| 4821 | } |
| 4822 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 4823 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4824 | return SQLITE_OK; |
| 4825 | } |
| 4826 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4827 | /* |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 4828 | ** If the third argument is non-NULL, then this function releases a |
| 4829 | ** reference obtained by an earlier call to unixFetch(). The second |
| 4830 | ** argument passed to this function must be the same as the corresponding |
| 4831 | ** argument that was passed to the unixFetch() invocation. |
| 4832 | ** |
| 4833 | ** Or, if the third argument is NULL, then this function is being called |
| 4834 | ** to inform the VFS layer that, according to POSIX, any existing mapping |
| 4835 | ** may now be invalid and should be unmapped. |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4836 | */ |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 4837 | static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){ |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 4838 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 4839 | unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
dan | 9871c59 | 2014-01-10 16:40:21 +0000 | [diff] [blame] | 4840 | UNUSED_PARAMETER(iOff); |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 4841 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4842 | /* If p==0 (unmap the entire file) then there must be no outstanding |
| 4843 | ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference), |
| 4844 | ** then there must be at least one outstanding. */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4845 | assert( (p==0)==(pFd->nFetchOut==0) ); |
| 4846 | |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 4847 | /* If p!=0, it must match the iOff value. */ |
| 4848 | assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] ); |
| 4849 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4850 | if( p ){ |
| 4851 | pFd->nFetchOut--; |
| 4852 | }else{ |
| 4853 | unixUnmapfile(pFd); |
| 4854 | } |
| 4855 | |
| 4856 | assert( pFd->nFetchOut>=0 ); |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 4857 | #else |
| 4858 | UNUSED_PARAMETER(fd); |
| 4859 | UNUSED_PARAMETER(p); |
dan | 9871c59 | 2014-01-10 16:40:21 +0000 | [diff] [blame] | 4860 | UNUSED_PARAMETER(iOff); |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 4861 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4862 | return SQLITE_OK; |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4863 | } |
| 4864 | |
| 4865 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4866 | ** Here ends the implementation of all sqlite3_file methods. |
| 4867 | ** |
| 4868 | ********************** End sqlite3_file Methods ******************************* |
| 4869 | ******************************************************************************/ |
| 4870 | |
| 4871 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4872 | ** This division contains definitions of sqlite3_io_methods objects that |
| 4873 | ** implement various file locking strategies. It also contains definitions |
| 4874 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 4875 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 4876 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 4877 | ** the correct finder-function for that VFS. |
| 4878 | ** |
| 4879 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 4880 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 4881 | ** looks at the filesystem type and tries to guess the best locking |
| 4882 | ** strategy from that. |
| 4883 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 4884 | ** For finder-function F, two objects are created: |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4885 | ** |
| 4886 | ** (1) The real finder-function named "FImpt()". |
| 4887 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4888 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4889 | ** |
| 4890 | ** |
| 4891 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 4892 | ** objects. We have to do this instead of letting pAppData point |
| 4893 | ** directly at the finder-function since C90 rules prevent a void* |
| 4894 | ** from be cast into a function pointer. |
| 4895 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4896 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4897 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4898 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4899 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 4900 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 4901 | ** |
| 4902 | ** * An I/O method finder function called FINDER that returns a pointer |
| 4903 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4904 | */ |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 4905 | #define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP) \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4906 | static const sqlite3_io_methods METHOD = { \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4907 | VERSION, /* iVersion */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4908 | CLOSE, /* xClose */ \ |
| 4909 | unixRead, /* xRead */ \ |
| 4910 | unixWrite, /* xWrite */ \ |
| 4911 | unixTruncate, /* xTruncate */ \ |
| 4912 | unixSync, /* xSync */ \ |
| 4913 | unixFileSize, /* xFileSize */ \ |
| 4914 | LOCK, /* xLock */ \ |
| 4915 | UNLOCK, /* xUnlock */ \ |
| 4916 | CKLOCK, /* xCheckReservedLock */ \ |
| 4917 | unixFileControl, /* xFileControl */ \ |
| 4918 | unixSectorSize, /* xSectorSize */ \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4919 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 4920 | SHMMAP, /* xShmMap */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4921 | unixShmLock, /* xShmLock */ \ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4922 | unixShmBarrier, /* xShmBarrier */ \ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4923 | unixShmUnmap, /* xShmUnmap */ \ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4924 | unixFetch, /* xFetch */ \ |
| 4925 | unixUnfetch, /* xUnfetch */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4926 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4927 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 4928 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4929 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4930 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4931 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4932 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4933 | |
| 4934 | /* |
| 4935 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 4936 | ** locking strategies. Functions that return pointers to these methods |
| 4937 | ** are also created. |
| 4938 | */ |
| 4939 | IOMETHODS( |
| 4940 | posixIoFinder, /* Finder function name */ |
| 4941 | posixIoMethods, /* sqlite3_io_methods object name */ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4942 | 3, /* shared memory and mmap are enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4943 | unixClose, /* xClose method */ |
| 4944 | unixLock, /* xLock method */ |
| 4945 | unixUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 4946 | unixCheckReservedLock, /* xCheckReservedLock method */ |
| 4947 | unixShmMap /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4948 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4949 | IOMETHODS( |
| 4950 | nolockIoFinder, /* Finder function name */ |
| 4951 | nolockIoMethods, /* sqlite3_io_methods object name */ |
drh | 142341c | 2014-09-19 19:00:48 +0000 | [diff] [blame] | 4952 | 3, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4953 | nolockClose, /* xClose method */ |
| 4954 | nolockLock, /* xLock method */ |
| 4955 | nolockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 4956 | nolockCheckReservedLock, /* xCheckReservedLock method */ |
| 4957 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4958 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4959 | IOMETHODS( |
| 4960 | dotlockIoFinder, /* Finder function name */ |
| 4961 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4962 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4963 | dotlockClose, /* xClose method */ |
| 4964 | dotlockLock, /* xLock method */ |
| 4965 | dotlockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 4966 | dotlockCheckReservedLock, /* xCheckReservedLock method */ |
| 4967 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4968 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4969 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 4970 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4971 | IOMETHODS( |
| 4972 | flockIoFinder, /* Finder function name */ |
| 4973 | flockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4974 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4975 | flockClose, /* xClose method */ |
| 4976 | flockLock, /* xLock method */ |
| 4977 | flockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 4978 | flockCheckReservedLock, /* xCheckReservedLock method */ |
| 4979 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4980 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4981 | #endif |
| 4982 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4983 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4984 | IOMETHODS( |
| 4985 | semIoFinder, /* Finder function name */ |
| 4986 | semIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 4987 | 1, /* shared memory is disabled */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4988 | semXClose, /* xClose method */ |
| 4989 | semXLock, /* xLock method */ |
| 4990 | semXUnlock, /* xUnlock method */ |
| 4991 | semXCheckReservedLock, /* xCheckReservedLock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 4992 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4993 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4994 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4995 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4996 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4997 | IOMETHODS( |
| 4998 | afpIoFinder, /* Finder function name */ |
| 4999 | afpIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5000 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5001 | afpClose, /* xClose method */ |
| 5002 | afpLock, /* xLock method */ |
| 5003 | afpUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5004 | afpCheckReservedLock, /* xCheckReservedLock method */ |
| 5005 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5006 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5007 | #endif |
| 5008 | |
| 5009 | /* |
| 5010 | ** The proxy locking method is a "super-method" in the sense that it |
| 5011 | ** opens secondary file descriptors for the conch and lock files and |
| 5012 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 5013 | ** secondary files. For this reason, the division that implements |
| 5014 | ** proxy locking is located much further down in the file. But we need |
| 5015 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 5016 | ** for proxy locking here. So we forward declare the I/O methods. |
| 5017 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5018 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5019 | static int proxyClose(sqlite3_file*); |
| 5020 | static int proxyLock(sqlite3_file*, int); |
| 5021 | static int proxyUnlock(sqlite3_file*, int); |
| 5022 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5023 | IOMETHODS( |
| 5024 | proxyIoFinder, /* Finder function name */ |
| 5025 | proxyIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5026 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5027 | proxyClose, /* xClose method */ |
| 5028 | proxyLock, /* xLock method */ |
| 5029 | proxyUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5030 | proxyCheckReservedLock, /* xCheckReservedLock method */ |
| 5031 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5032 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5033 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5034 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5035 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 5036 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 5037 | IOMETHODS( |
| 5038 | nfsIoFinder, /* Finder function name */ |
| 5039 | nfsIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5040 | 1, /* shared memory is disabled */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5041 | unixClose, /* xClose method */ |
| 5042 | unixLock, /* xLock method */ |
| 5043 | nfsUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5044 | unixCheckReservedLock, /* xCheckReservedLock method */ |
| 5045 | 0 /* xShmMap method */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5046 | ) |
| 5047 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5048 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5049 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5050 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5051 | ** This "finder" function attempts to determine the best locking strategy |
| 5052 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5053 | ** object that implements that strategy. |
| 5054 | ** |
| 5055 | ** This is for MacOSX only. |
| 5056 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5057 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5058 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5059 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5060 | ){ |
| 5061 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5062 | const char *zFilesystem; /* Filesystem type name */ |
| 5063 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5064 | } aMap[] = { |
| 5065 | { "hfs", &posixIoMethods }, |
| 5066 | { "ufs", &posixIoMethods }, |
| 5067 | { "afpfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5068 | { "smbfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5069 | { "webdav", &nolockIoMethods }, |
| 5070 | { 0, 0 } |
| 5071 | }; |
| 5072 | int i; |
| 5073 | struct statfs fsInfo; |
| 5074 | struct flock lockInfo; |
| 5075 | |
| 5076 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5077 | /* If filePath==NULL that means we are dealing with a transient file |
| 5078 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5079 | return &nolockIoMethods; |
| 5080 | } |
| 5081 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 5082 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 5083 | return &nolockIoMethods; |
| 5084 | } |
| 5085 | for(i=0; aMap[i].zFilesystem; i++){ |
| 5086 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 5087 | return aMap[i].pMethods; |
| 5088 | } |
| 5089 | } |
| 5090 | } |
| 5091 | |
| 5092 | /* Default case. Handles, amongst others, "nfs". |
| 5093 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 5094 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5095 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5096 | lockInfo.l_len = 1; |
| 5097 | lockInfo.l_start = 0; |
| 5098 | lockInfo.l_whence = SEEK_SET; |
| 5099 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5100 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5101 | if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 5102 | return &nfsIoMethods; |
| 5103 | } else { |
| 5104 | return &posixIoMethods; |
| 5105 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5106 | }else{ |
| 5107 | return &dotlockIoMethods; |
| 5108 | } |
| 5109 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5110 | static const sqlite3_io_methods |
| 5111 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5112 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5113 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5114 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5115 | #if OS_VXWORKS |
| 5116 | /* |
| 5117 | ** This "finder" function for VxWorks checks to see if posix advisory |
| 5118 | ** locking works. If it does, then that is what is used. If it does not |
| 5119 | ** work, then fallback to named semaphore locking. |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5120 | */ |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5121 | static const sqlite3_io_methods *vxworksIoFinderImpl( |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5122 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5123 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5124 | ){ |
| 5125 | struct flock lockInfo; |
| 5126 | |
| 5127 | if( !filePath ){ |
| 5128 | /* If filePath==NULL that means we are dealing with a transient file |
| 5129 | ** that does not need to be locked. */ |
| 5130 | return &nolockIoMethods; |
| 5131 | } |
| 5132 | |
| 5133 | /* Test if fcntl() is supported and use POSIX style locks. |
| 5134 | ** Otherwise fall back to the named semaphore method. |
| 5135 | */ |
| 5136 | lockInfo.l_len = 1; |
| 5137 | lockInfo.l_start = 0; |
| 5138 | lockInfo.l_whence = SEEK_SET; |
| 5139 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5140 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5141 | return &posixIoMethods; |
| 5142 | }else{ |
| 5143 | return &semIoMethods; |
| 5144 | } |
| 5145 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5146 | static const sqlite3_io_methods |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5147 | *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5148 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5149 | #endif /* OS_VXWORKS */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5150 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5151 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5152 | ** An abstract type for a pointer to an IO method finder function: |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5153 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5154 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5155 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5156 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5157 | /**************************************************************************** |
| 5158 | **************************** sqlite3_vfs methods **************************** |
| 5159 | ** |
| 5160 | ** This division contains the implementation of methods on the |
| 5161 | ** sqlite3_vfs object. |
| 5162 | */ |
| 5163 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 5164 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5165 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5166 | */ |
| 5167 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5168 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5169 | int h, /* Open file descriptor of file being opened */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 5170 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5171 | const char *zFilename, /* Name of the file being opened */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5172 | int ctrlFlags /* Zero or more UNIXFILE_* values */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5173 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5174 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5175 | unixFile *pNew = (unixFile *)pId; |
| 5176 | int rc = SQLITE_OK; |
| 5177 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5178 | assert( pNew->pInode==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 5179 | |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 5180 | /* Usually the path zFilename should not be a relative pathname. The |
| 5181 | ** exception is when opening the proxy "conch" file in builds that |
| 5182 | ** include the special Apple locking styles. |
| 5183 | */ |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 5184 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | f7f55ed | 2010-10-05 18:22:47 +0000 | [diff] [blame] | 5185 | assert( zFilename==0 || zFilename[0]=='/' |
| 5186 | || pVfs->pAppData==(void*)&autolockIoFinder ); |
| 5187 | #else |
| 5188 | assert( zFilename==0 || zFilename[0]=='/' ); |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 5189 | #endif |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 5190 | |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5191 | /* No locking occurs in temporary files */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5192 | assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5193 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5194 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5195 | pNew->h = h; |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 5196 | pNew->pVfs = pVfs; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5197 | pNew->zPath = zFilename; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5198 | pNew->ctrlFlags = (u8)ctrlFlags; |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5199 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | ede01a9 | 2013-05-17 12:10:52 +0000 | [diff] [blame] | 5200 | pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap; |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5201 | #endif |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5202 | if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0), |
| 5203 | "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 5204 | pNew->ctrlFlags |= UNIXFILE_PSOW; |
drh | bec7c97 | 2011-12-23 00:25:02 +0000 | [diff] [blame] | 5205 | } |
drh | 503a686 | 2013-03-01 01:07:17 +0000 | [diff] [blame] | 5206 | if( strcmp(pVfs->zName,"unix-excl")==0 ){ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 5207 | pNew->ctrlFlags |= UNIXFILE_EXCL; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 5208 | } |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 5209 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5210 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 5211 | pNew->pId = vxworksFindFileId(zFilename); |
| 5212 | if( pNew->pId==0 ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5213 | ctrlFlags |= UNIXFILE_NOLOCK; |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 5214 | rc = SQLITE_NOMEM; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5215 | } |
| 5216 | #endif |
| 5217 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5218 | if( ctrlFlags & UNIXFILE_NOLOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5219 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5220 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5221 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5222 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5223 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 5224 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 5225 | ** zFilename remains valid until file is closed, to support */ |
| 5226 | pNew->lockingContext = (void*)zFilename; |
| 5227 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5228 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5229 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5230 | if( pLockingStyle == &posixIoMethods |
| 5231 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 5232 | || pLockingStyle == &nfsIoMethods |
| 5233 | #endif |
| 5234 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5235 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5236 | rc = findInodeInfo(pNew, &pNew->pInode); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5237 | if( rc!=SQLITE_OK ){ |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 5238 | /* If an error occurred in findInodeInfo(), close the file descriptor |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5239 | ** immediately, before releasing the mutex. findInodeInfo() may fail |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5240 | ** in two scenarios: |
| 5241 | ** |
| 5242 | ** (a) A call to fstat() failed. |
| 5243 | ** (b) A malloc failed. |
| 5244 | ** |
| 5245 | ** Scenario (b) may only occur if the process is holding no other |
| 5246 | ** file descriptors open on the same file. If there were other file |
| 5247 | ** descriptors on this file, then no malloc would be required by |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5248 | ** findInodeInfo(). If this is the case, it is quite safe to close |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5249 | ** handle h - as it is guaranteed that no posix locks will be released |
| 5250 | ** by doing so. |
| 5251 | ** |
| 5252 | ** If scenario (a) caused the error then things are not so safe. The |
| 5253 | ** implicit assumption here is that if fstat() fails, things are in |
| 5254 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 5255 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5256 | robust_close(pNew, h, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5257 | h = -1; |
| 5258 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5259 | unixLeaveMutex(); |
| 5260 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5261 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5262 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 5263 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5264 | /* AFP locking uses the file path so it needs to be included in |
| 5265 | ** the afpLockingContext. |
| 5266 | */ |
| 5267 | afpLockingContext *pCtx; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5268 | pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) ); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5269 | if( pCtx==0 ){ |
| 5270 | rc = SQLITE_NOMEM; |
| 5271 | }else{ |
| 5272 | /* NB: zFilename exists and remains valid until the file is closed |
| 5273 | ** according to requirement F11141. So we do not need to make a |
| 5274 | ** copy of the filename. */ |
| 5275 | pCtx->dbPath = zFilename; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5276 | pCtx->reserved = 0; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5277 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5278 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5279 | rc = findInodeInfo(pNew, &pNew->pInode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5280 | if( rc!=SQLITE_OK ){ |
| 5281 | sqlite3_free(pNew->lockingContext); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5282 | robust_close(pNew, h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5283 | h = -1; |
| 5284 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5285 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5286 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5287 | } |
| 5288 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5289 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5290 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 5291 | /* Dotfile locking uses the file path so it needs to be included in |
| 5292 | ** the dotlockLockingContext |
| 5293 | */ |
| 5294 | char *zLockFile; |
| 5295 | int nFilename; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5296 | assert( zFilename!=0 ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5297 | nFilename = (int)strlen(zFilename) + 6; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5298 | zLockFile = (char *)sqlite3_malloc64(nFilename); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5299 | if( zLockFile==0 ){ |
| 5300 | rc = SQLITE_NOMEM; |
| 5301 | }else{ |
| 5302 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5303 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5304 | pNew->lockingContext = zLockFile; |
| 5305 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5306 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5307 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5308 | else if( pLockingStyle == &semIoMethods ){ |
| 5309 | /* Named semaphore locking uses the file path so it needs to be |
| 5310 | ** included in the semLockingContext |
| 5311 | */ |
| 5312 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5313 | rc = findInodeInfo(pNew, &pNew->pInode); |
| 5314 | if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 5315 | char *zSemName = pNew->pInode->aSemName; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5316 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 5317 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5318 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 5319 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5320 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5321 | pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 5322 | if( pNew->pInode->pSem == SEM_FAILED ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5323 | rc = SQLITE_NOMEM; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5324 | pNew->pInode->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5325 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5326 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5327 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5328 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5329 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 5330 | |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 5331 | storeLastErrno(pNew, 0); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5332 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5333 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5334 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
drh | 309e655 | 2010-02-05 18:00:26 +0000 | [diff] [blame] | 5335 | h = -1; |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5336 | osUnlink(zFilename); |
drh | c579754 | 2013-04-27 12:13:29 +0000 | [diff] [blame] | 5337 | pNew->ctrlFlags |= UNIXFILE_DELETE; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5338 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5339 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5340 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5341 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5342 | }else{ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5343 | pNew->pMethod = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5344 | OpenCounter(+1); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 5345 | verifyDbFile(pNew); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5346 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5347 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 5348 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 5349 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5350 | /* |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5351 | ** Return the name of a directory in which to put temporary files. |
| 5352 | ** If no suitable temporary file directory can be found, return NULL. |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5353 | */ |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 5354 | static const char *unixTempFileDir(void){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5355 | static const char *azDirs[] = { |
| 5356 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5357 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5358 | "/var/tmp", |
| 5359 | "/usr/tmp", |
| 5360 | "/tmp", |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5361 | "." |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5362 | }; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5363 | unsigned int i; |
| 5364 | struct stat buf; |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5365 | const char *zDir = sqlite3_temp_directory; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5366 | |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5367 | if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR"); |
| 5368 | if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); |
drh | 19515c8 | 2010-06-19 23:53:11 +0000 | [diff] [blame] | 5369 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){ |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5370 | if( zDir==0 ) continue; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5371 | if( osStat(zDir, &buf) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5372 | if( !S_ISDIR(buf.st_mode) ) continue; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5373 | if( osAccess(zDir, 07) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5374 | break; |
| 5375 | } |
| 5376 | return zDir; |
| 5377 | } |
| 5378 | |
| 5379 | /* |
| 5380 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 5381 | ** by the calling process and must be big enough to hold at least |
| 5382 | ** pVfs->mxPathname bytes. |
| 5383 | */ |
| 5384 | static int unixGetTempname(int nBuf, char *zBuf){ |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5385 | const char *zDir; |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5386 | int iLimit = 0; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5387 | |
| 5388 | /* It's odd to simulate an io-error here, but really this is just |
| 5389 | ** using the io-error infrastructure to test that SQLite handles this |
| 5390 | ** function failing. |
| 5391 | */ |
| 5392 | SimulateIOError( return SQLITE_IOERR ); |
| 5393 | |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 5394 | zDir = unixTempFileDir(); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5395 | do{ |
drh | 970942e | 2015-11-25 23:13:14 +0000 | [diff] [blame] | 5396 | u64 r; |
| 5397 | sqlite3_randomness(sizeof(r), &r); |
| 5398 | assert( nBuf>2 ); |
| 5399 | zBuf[nBuf-2] = 0; |
| 5400 | sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c", |
| 5401 | zDir, r, 0); |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5402 | if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5403 | }while( osAccess(zBuf,0)==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5404 | return SQLITE_OK; |
| 5405 | } |
| 5406 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5407 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 5408 | /* |
| 5409 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 5410 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 5411 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 5412 | */ |
| 5413 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 5414 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 5415 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5416 | /* |
| 5417 | ** Search for an unused file descriptor that was opened on the database |
| 5418 | ** file (not a journal or master-journal file) identified by pathname |
| 5419 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 5420 | ** argument to this function. |
| 5421 | ** |
| 5422 | ** Such a file descriptor may exist if a database connection was closed |
| 5423 | ** but the associated file descriptor could not be closed because some |
| 5424 | ** other file descriptor open on the same file is holding a file-lock. |
| 5425 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 5426 | ** describing "Posix Advisory Locking" at the start of this file for |
| 5427 | ** further details. Also, ticket #4018. |
| 5428 | ** |
| 5429 | ** If a suitable file descriptor is found, then it is returned. If no |
| 5430 | ** such file descriptor is located, -1 is returned. |
| 5431 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5432 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 5433 | UnixUnusedFd *pUnused = 0; |
| 5434 | |
| 5435 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 5436 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 5437 | ** but because no way to test it is currently available. It is better |
| 5438 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 5439 | ** feature. */ |
| 5440 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5441 | struct stat sStat; /* Results of stat() call */ |
| 5442 | |
| 5443 | /* A stat() call may fail for various reasons. If this happens, it is |
| 5444 | ** almost certain that an open() call on the same path will also fail. |
| 5445 | ** For this reason, if an error occurs in the stat() call here, it is |
| 5446 | ** ignored and -1 is returned. The caller will try to open a new file |
| 5447 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 5448 | ** |
| 5449 | ** Even if a subsequent open() call does succeed, the consequences of |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5450 | ** not searching for a reusable file descriptor are not dire. */ |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 5451 | if( 0==osStat(zPath, &sStat) ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 5452 | unixInodeInfo *pInode; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5453 | |
| 5454 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5455 | pInode = inodeList; |
| 5456 | while( pInode && (pInode->fileId.dev!=sStat.st_dev |
| 5457 | || pInode->fileId.ino!=sStat.st_ino) ){ |
| 5458 | pInode = pInode->pNext; |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 5459 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5460 | if( pInode ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5461 | UnixUnusedFd **pp; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5462 | for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5463 | pUnused = *pp; |
| 5464 | if( pUnused ){ |
| 5465 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5466 | } |
| 5467 | } |
| 5468 | unixLeaveMutex(); |
| 5469 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5470 | #endif /* if !OS_VXWORKS */ |
| 5471 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5472 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5473 | |
| 5474 | /* |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5475 | ** This function is called by unixOpen() to determine the unix permissions |
drh | f65bc91 | 2010-07-14 20:51:34 +0000 | [diff] [blame] | 5476 | ** to create new files with. If no error occurs, then SQLITE_OK is returned |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5477 | ** and a value suitable for passing as the third argument to open(2) is |
| 5478 | ** written to *pMode. If an IO error occurs, an SQLite error code is |
| 5479 | ** returned and the value of *pMode is not modified. |
| 5480 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5481 | ** In most cases, this routine sets *pMode to 0, which will become |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 5482 | ** an indication to robust_open() to create the file using |
| 5483 | ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask. |
| 5484 | ** But if the file being opened is a WAL or regular journal file, then |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5485 | ** this function queries the file-system for the permissions on the |
| 5486 | ** corresponding database file and sets *pMode to this value. Whenever |
| 5487 | ** possible, WAL and journal files are created using the same permissions |
| 5488 | ** as the associated database file. |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 5489 | ** |
| 5490 | ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the |
| 5491 | ** original filename is unavailable. But 8_3_NAMES is only used for |
| 5492 | ** FAT filesystems and permissions do not matter there, so just use |
| 5493 | ** the default permissions. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5494 | */ |
| 5495 | static int findCreateFileMode( |
| 5496 | const char *zPath, /* Path of file (possibly) being created */ |
| 5497 | int flags, /* Flags passed as 4th argument to xOpen() */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5498 | mode_t *pMode, /* OUT: Permissions to open file with */ |
| 5499 | uid_t *pUid, /* OUT: uid to set on the file */ |
| 5500 | gid_t *pGid /* OUT: gid to set on the file */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5501 | ){ |
| 5502 | int rc = SQLITE_OK; /* Return Code */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 5503 | *pMode = 0; |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5504 | *pUid = 0; |
| 5505 | *pGid = 0; |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5506 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5507 | char zDb[MAX_PATHNAME+1]; /* Database file path */ |
| 5508 | int nDb; /* Number of valid bytes in zDb */ |
| 5509 | struct stat sStat; /* Output of stat() on database file */ |
| 5510 | |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5511 | /* zPath is a path to a WAL or journal file. The following block derives |
| 5512 | ** the path to the associated database file from zPath. This block handles |
| 5513 | ** the following naming conventions: |
| 5514 | ** |
| 5515 | ** "<path to db>-journal" |
| 5516 | ** "<path to db>-wal" |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 5517 | ** "<path to db>-journalNN" |
| 5518 | ** "<path to db>-walNN" |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5519 | ** |
drh | d337c5b | 2011-10-20 18:23:35 +0000 | [diff] [blame] | 5520 | ** where NN is a decimal number. The NN naming schemes are |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5521 | ** used by the test_multiplex.c module. |
| 5522 | */ |
| 5523 | nDb = sqlite3Strlen30(zPath) - 1; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5524 | #ifdef SQLITE_ENABLE_8_3_NAMES |
dan | 28a67fd | 2011-12-12 19:48:43 +0000 | [diff] [blame] | 5525 | while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--; |
drh | d337c5b | 2011-10-20 18:23:35 +0000 | [diff] [blame] | 5526 | if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5527 | #else |
| 5528 | while( zPath[nDb]!='-' ){ |
| 5529 | assert( nDb>0 ); |
| 5530 | assert( zPath[nDb]!='\n' ); |
| 5531 | nDb--; |
| 5532 | } |
| 5533 | #endif |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5534 | memcpy(zDb, zPath, nDb); |
| 5535 | zDb[nDb] = '\0'; |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5536 | |
drh | 58384f1 | 2011-07-28 00:14:45 +0000 | [diff] [blame] | 5537 | if( 0==osStat(zDb, &sStat) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5538 | *pMode = sStat.st_mode & 0777; |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5539 | *pUid = sStat.st_uid; |
| 5540 | *pGid = sStat.st_gid; |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5541 | }else{ |
| 5542 | rc = SQLITE_IOERR_FSTAT; |
| 5543 | } |
| 5544 | }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 5545 | *pMode = 0600; |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5546 | } |
| 5547 | return rc; |
| 5548 | } |
| 5549 | |
| 5550 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5551 | ** Open the file zPath. |
| 5552 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5553 | ** Previously, the SQLite OS layer used three functions in place of this |
| 5554 | ** one: |
| 5555 | ** |
| 5556 | ** sqlite3OsOpenReadWrite(); |
| 5557 | ** sqlite3OsOpenReadOnly(); |
| 5558 | ** sqlite3OsOpenExclusive(); |
| 5559 | ** |
| 5560 | ** These calls correspond to the following combinations of flags: |
| 5561 | ** |
| 5562 | ** ReadWrite() -> (READWRITE | CREATE) |
| 5563 | ** ReadOnly() -> (READONLY) |
| 5564 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 5565 | ** |
| 5566 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 5567 | ** true, the file was configured to be automatically deleted when the |
| 5568 | ** file handle closed. To achieve the same effect using this new |
| 5569 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 5570 | ** OpenExclusive(). |
| 5571 | */ |
| 5572 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5573 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 5574 | const char *zPath, /* Pathname of file to be opened */ |
| 5575 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 5576 | int flags, /* Input flags to control the opening */ |
| 5577 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5578 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5579 | unixFile *p = (unixFile *)pFile; |
| 5580 | int fd = -1; /* File descriptor returned by open() */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5581 | int openFlags = 0; /* Flags to pass to open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5582 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5583 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5584 | int rc = SQLITE_OK; /* Function Return Code */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5585 | int ctrlFlags = 0; /* UNIXFILE_* flags */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5586 | |
| 5587 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 5588 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 5589 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 5590 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 5591 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5592 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5593 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 5594 | #endif |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 5595 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 5596 | struct statfs fsInfo; |
| 5597 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5598 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5599 | /* If creating a master or main-file journal, this function will open |
| 5600 | ** a file-descriptor on the directory too. The first time unixSync() |
| 5601 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 5602 | */ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5603 | int syncDir = (isCreate && ( |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5604 | eType==SQLITE_OPEN_MASTER_JOURNAL |
| 5605 | || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 5606 | || eType==SQLITE_OPEN_WAL |
| 5607 | )); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5608 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5609 | /* If argument zPath is a NULL pointer, this function is required to open |
| 5610 | ** a temporary file. Use this buffer to store the file name in. |
| 5611 | */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5612 | char zTmpname[MAX_PATHNAME+2]; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5613 | const char *zName = zPath; |
| 5614 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5615 | /* Check the following statements are true: |
| 5616 | ** |
| 5617 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 5618 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 5619 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5620 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5621 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5622 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5623 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5624 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5625 | assert(isDelete==0 || isCreate); |
| 5626 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5627 | /* The main DB, main journal, WAL file and master journal are never |
| 5628 | ** automatically deleted. Nor are they ever temporary files. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5629 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 5630 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 5631 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5632 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5633 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5634 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 5635 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 5636 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 5637 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5638 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5639 | ); |
| 5640 | |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 5641 | /* Detect a pid change and reset the PRNG. There is a race condition |
| 5642 | ** here such that two or more threads all trying to open databases at |
| 5643 | ** the same instant might all reset the PRNG. But multiple resets |
| 5644 | ** are harmless. |
| 5645 | */ |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 5646 | if( randomnessPid!=osGetpid(0) ){ |
| 5647 | randomnessPid = osGetpid(0); |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 5648 | sqlite3_randomness(0,0); |
| 5649 | } |
| 5650 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5651 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5652 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5653 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5654 | UnixUnusedFd *pUnused; |
| 5655 | pUnused = findReusableFd(zName, flags); |
| 5656 | if( pUnused ){ |
| 5657 | fd = pUnused->fd; |
| 5658 | }else{ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5659 | pUnused = sqlite3_malloc64(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5660 | if( !pUnused ){ |
| 5661 | return SQLITE_NOMEM; |
| 5662 | } |
| 5663 | } |
| 5664 | p->pUnused = pUnused; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5665 | |
| 5666 | /* Database filenames are double-zero terminated if they are not |
| 5667 | ** URIs with parameters. Hence, they can always be passed into |
| 5668 | ** sqlite3_uri_parameter(). */ |
| 5669 | assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 ); |
| 5670 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5671 | }else if( !zName ){ |
| 5672 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 5673 | assert(isDelete && !syncDir); |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5674 | rc = unixGetTempname(pVfs->mxPathname, zTmpname); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5675 | if( rc!=SQLITE_OK ){ |
| 5676 | return rc; |
| 5677 | } |
| 5678 | zName = zTmpname; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5679 | |
| 5680 | /* Generated temporary filenames are always double-zero terminated |
| 5681 | ** for use by sqlite3_uri_parameter(). */ |
| 5682 | assert( zName[strlen(zName)+1]==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5683 | } |
| 5684 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5685 | /* Determine the value of the flags parameter passed to POSIX function |
| 5686 | ** open(). These must be calculated even if open() is not called, as |
| 5687 | ** they may be stored as part of the file handle and used by the |
| 5688 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5689 | if( isReadonly ) openFlags |= O_RDONLY; |
| 5690 | if( isReadWrite ) openFlags |= O_RDWR; |
| 5691 | if( isCreate ) openFlags |= O_CREAT; |
| 5692 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 5693 | openFlags |= (O_LARGEFILE|O_BINARY); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5694 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5695 | if( fd<0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5696 | mode_t openMode; /* Permissions to create file with */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5697 | uid_t uid; /* Userid for the file */ |
| 5698 | gid_t gid; /* Groupid for the file */ |
| 5699 | rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5700 | if( rc!=SQLITE_OK ){ |
| 5701 | assert( !p->pUnused ); |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5702 | assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5703 | return rc; |
| 5704 | } |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5705 | fd = robust_open(zName, openFlags, openMode); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5706 | OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); |
drh | 5a2d970 | 2015-11-26 02:21:05 +0000 | [diff] [blame] | 5707 | assert( !isExclusive || (openFlags & O_CREAT)!=0 ); |
| 5708 | if( fd<0 && errno!=EISDIR && isReadWrite ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5709 | /* Failed to open the file for read/write access. Try read-only. */ |
| 5710 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5711 | openFlags &= ~(O_RDWR|O_CREAT); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5712 | flags |= SQLITE_OPEN_READONLY; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5713 | openFlags |= O_RDONLY; |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 5714 | isReadonly = 1; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 5715 | fd = robust_open(zName, openFlags, openMode); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5716 | } |
| 5717 | if( fd<0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5718 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5719 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5720 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5721 | |
| 5722 | /* If this process is running as root and if creating a new rollback |
| 5723 | ** journal or WAL file, set the ownership of the journal or WAL to be |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 5724 | ** the same as the original database. |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5725 | */ |
| 5726 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 5727 | robustFchown(fd, uid, gid); |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5728 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5729 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5730 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5731 | if( pOutFlags ){ |
| 5732 | *pOutFlags = flags; |
| 5733 | } |
| 5734 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5735 | if( p->pUnused ){ |
| 5736 | p->pUnused->fd = fd; |
| 5737 | p->pUnused->flags = flags; |
| 5738 | } |
| 5739 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5740 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5741 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5742 | zPath = zName; |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 5743 | #elif defined(SQLITE_UNLINK_AFTER_CLOSE) |
| 5744 | zPath = sqlite3_mprintf("%s", zName); |
| 5745 | if( zPath==0 ){ |
| 5746 | robust_close(p, fd, __LINE__); |
| 5747 | return SQLITE_NOMEM; |
| 5748 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5749 | #else |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5750 | osUnlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5751 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5752 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 5753 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5754 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5755 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 5756 | } |
| 5757 | #endif |
| 5758 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5759 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5760 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5761 | |
| 5762 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5763 | if( fstatfs(fd, &fsInfo) == -1 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 5764 | storeLastErrno(p, errno); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5765 | robust_close(p, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5766 | return SQLITE_IOERR_ACCESS; |
| 5767 | } |
| 5768 | if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 5769 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 5770 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 5771 | if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) { |
| 5772 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 5773 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5774 | #endif |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5775 | |
| 5776 | /* Set up appropriate ctrlFlags */ |
| 5777 | if( isDelete ) ctrlFlags |= UNIXFILE_DELETE; |
| 5778 | if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY; |
| 5779 | if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK; |
| 5780 | if( syncDir ) ctrlFlags |= UNIXFILE_DIRSYNC; |
| 5781 | if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI; |
| 5782 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5783 | #if SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5784 | #if SQLITE_PREFER_PROXY_LOCKING |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5785 | isAutoProxy = 1; |
| 5786 | #endif |
| 5787 | if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5788 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 5789 | int useProxy = 0; |
| 5790 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5791 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 5792 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5793 | if( envforce!=NULL ){ |
| 5794 | useProxy = atoi(envforce)>0; |
| 5795 | }else{ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5796 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 5797 | } |
| 5798 | if( useProxy ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5799 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5800 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5801 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5802 | if( rc!=SQLITE_OK ){ |
| 5803 | /* Use unixClose to clean up the resources added in fillInUnixFile |
| 5804 | ** and clear all the structure's references. Specifically, |
| 5805 | ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 5806 | */ |
| 5807 | unixClose(pFile); |
| 5808 | return rc; |
| 5809 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5810 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5811 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5812 | } |
| 5813 | } |
| 5814 | #endif |
| 5815 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5816 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
| 5817 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5818 | open_finished: |
| 5819 | if( rc!=SQLITE_OK ){ |
| 5820 | sqlite3_free(p->pUnused); |
| 5821 | } |
| 5822 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5823 | } |
| 5824 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5825 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5826 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5827 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 5828 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5829 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5830 | static int unixDelete( |
| 5831 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 5832 | const char *zPath, /* Name of file to be deleted */ |
| 5833 | int dirSync /* If true, fsync() directory after deleting file */ |
| 5834 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5835 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5836 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5837 | SimulateIOError(return SQLITE_IOERR_DELETE); |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 5838 | if( osUnlink(zPath)==(-1) ){ |
drh | bd94554 | 2014-08-13 11:39:42 +0000 | [diff] [blame] | 5839 | if( errno==ENOENT |
| 5840 | #if OS_VXWORKS |
drh | 19541f3 | 2014-09-01 13:37:55 +0000 | [diff] [blame] | 5841 | || osAccess(zPath,0)!=0 |
drh | bd94554 | 2014-08-13 11:39:42 +0000 | [diff] [blame] | 5842 | #endif |
| 5843 | ){ |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 5844 | rc = SQLITE_IOERR_DELETE_NOENT; |
| 5845 | }else{ |
drh | b430816 | 2012-11-09 21:40:02 +0000 | [diff] [blame] | 5846 | rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath); |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 5847 | } |
drh | b430816 | 2012-11-09 21:40:02 +0000 | [diff] [blame] | 5848 | return rc; |
drh | 5d4feff | 2010-07-14 01:45:22 +0000 | [diff] [blame] | 5849 | } |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 5850 | #ifndef SQLITE_DISABLE_DIRSYNC |
drh | e349519 | 2012-01-05 16:07:30 +0000 | [diff] [blame] | 5851 | if( (dirSync & 1)!=0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5852 | int fd; |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 5853 | rc = osOpenDirectory(zPath, &fd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5854 | if( rc==SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5855 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5856 | if( fsync(fd)==-1 ) |
| 5857 | #else |
| 5858 | if( fsync(fd) ) |
| 5859 | #endif |
| 5860 | { |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5861 | rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5862 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5863 | robust_close(0, fd, __LINE__); |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 5864 | }else{ |
| 5865 | assert( rc==SQLITE_CANTOPEN ); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 5866 | rc = SQLITE_OK; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5867 | } |
| 5868 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 5869 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5870 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5871 | } |
| 5872 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5873 | /* |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 5874 | ** Test the existence of or access permissions of file zPath. The |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 5875 | ** test performed depends on the value of flags: |
| 5876 | ** |
| 5877 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 5878 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 5879 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 5880 | ** |
| 5881 | ** Otherwise return 0. |
| 5882 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5883 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5884 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 5885 | const char *zPath, /* Path of the file to examine */ |
| 5886 | int flags, /* What do we want to learn about the zPath file? */ |
| 5887 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5888 | ){ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5889 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5890 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 5891 | assert( pResOut!=0 ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5892 | |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 5893 | /* The spec says there are three possible values for flags. But only |
| 5894 | ** two of them are actually used */ |
| 5895 | assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE ); |
| 5896 | |
| 5897 | if( flags==SQLITE_ACCESS_EXISTS ){ |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 5898 | struct stat buf; |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 5899 | *pResOut = (0==osStat(zPath, &buf) && buf.st_size>0); |
| 5900 | }else{ |
| 5901 | *pResOut = osAccess(zPath, W_OK|R_OK)==0; |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 5902 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 5903 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5904 | } |
| 5905 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5906 | |
| 5907 | /* |
| 5908 | ** Turn a relative pathname into a full pathname. The relative path |
| 5909 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 5910 | ** zPath. |
| 5911 | ** |
| 5912 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 5913 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 5914 | ** this buffer before returning. |
| 5915 | */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 5916 | static int unixFullPathname( |
| 5917 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 5918 | const char *zPath, /* Possibly relative input path */ |
| 5919 | int nOut, /* Size of output buffer in bytes */ |
| 5920 | char *zOut /* Output buffer */ |
| 5921 | ){ |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 5922 | int nByte; |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 5923 | |
| 5924 | /* It's odd to simulate an io-error here, but really this is just |
| 5925 | ** using the io-error infrastructure to test that SQLite handles this |
| 5926 | ** function failing. This function could fail if, for example, the |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5927 | ** current working directory has been unlinked. |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 5928 | */ |
| 5929 | SimulateIOError( return SQLITE_ERROR ); |
| 5930 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 5931 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 5932 | UNUSED_PARAMETER(pVfs); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5933 | |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 5934 | /* Attempt to resolve the path as if it were a symbolic link. If it is |
| 5935 | ** a symbolic link, the resolved path is stored in buffer zOut[]. Or, if |
| 5936 | ** the identified file is not a symbolic link or does not exist, then |
| 5937 | ** zPath is copied directly into zOut. Either way, nByte is left set to |
| 5938 | ** the size of the string copied into zOut[] in bytes. */ |
| 5939 | nByte = osReadlink(zPath, zOut, nOut-1); |
| 5940 | if( nByte<0 ){ |
| 5941 | if( errno!=EINVAL && errno!=ENOENT ){ |
| 5942 | return unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zPath); |
| 5943 | } |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 5944 | sqlite3_snprintf(nOut, zOut, "%s", zPath); |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 5945 | nByte = sqlite3Strlen30(zOut); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5946 | }else{ |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 5947 | zOut[nByte] = '\0'; |
| 5948 | } |
| 5949 | |
| 5950 | /* If buffer zOut[] now contains an absolute path there is nothing more |
| 5951 | ** to do. If it contains a relative path, do the following: |
| 5952 | ** |
| 5953 | ** * move the relative path string so that it is at the end of th |
| 5954 | ** zOut[] buffer. |
| 5955 | ** * Call getcwd() to read the path of the current working directory |
| 5956 | ** into the start of the zOut[] buffer. |
| 5957 | ** * Append a '/' character to the cwd string and move the |
| 5958 | ** relative path back within the buffer so that it immediately |
| 5959 | ** follows the '/'. |
| 5960 | ** |
| 5961 | ** This code is written so that if the combination of the CWD and relative |
| 5962 | ** path are larger than the allocated size of zOut[] the CWD is silently |
| 5963 | ** truncated to make it fit. This is Ok, as SQLite refuses to open any |
| 5964 | ** file for which this function returns a full path larger than (nOut-8) |
| 5965 | ** bytes in size. */ |
drh | 025d2f7 | 2015-11-30 22:22:23 +0000 | [diff] [blame] | 5966 | testcase( nByte==nOut-5 ); |
| 5967 | testcase( nByte==nOut-4 ); |
| 5968 | if( zOut[0]!='/' && nByte<nOut-4 ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5969 | int nCwd; |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 5970 | int nRem = nOut-nByte-1; |
| 5971 | memmove(&zOut[nRem], zOut, nByte+1); |
| 5972 | zOut[nRem-1] = '\0'; |
| 5973 | if( osGetcwd(zOut, nRem-1)==0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 5974 | return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5975 | } |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 5976 | nCwd = sqlite3Strlen30(zOut); |
| 5977 | assert( nCwd<=nRem-1 ); |
| 5978 | zOut[nCwd] = '/'; |
| 5979 | memmove(&zOut[nCwd+1], &zOut[nRem], nByte+1); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5980 | } |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 5981 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5982 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5983 | } |
| 5984 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 5985 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5986 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 5987 | /* |
| 5988 | ** Interfaces for opening a shared library, finding entry points |
| 5989 | ** within the shared library, and closing the shared library. |
| 5990 | */ |
| 5991 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5992 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 5993 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 5994 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 5995 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 5996 | |
| 5997 | /* |
| 5998 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 5999 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 6000 | ** message is available, it is written to zBufOut. If no error message |
| 6001 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 6002 | ** error message. |
| 6003 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6004 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
dan | 3239053 | 2010-11-29 18:36:22 +0000 | [diff] [blame] | 6005 | const char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6006 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6007 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6008 | zErr = dlerror(); |
| 6009 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6010 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6011 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6012 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6013 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6014 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 6015 | /* |
| 6016 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 6017 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 6018 | ** returns a void* which is really a pointer to a function. So how do we |
| 6019 | ** use dlsym() with -pedantic-errors? |
| 6020 | ** |
| 6021 | ** Variable x below is defined to be a pointer to a function taking |
| 6022 | ** parameters void* and const char* and returning a pointer to a function. |
| 6023 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 6024 | ** (That assignment requires a cast.) Then we call the function that |
| 6025 | ** x points to. |
| 6026 | ** |
| 6027 | ** This work-around is unlikely to work correctly on any system where |
| 6028 | ** you really cannot cast a function pointer into void*. But then, on the |
| 6029 | ** other hand, dlsym() will not work on such a system either, so we have |
| 6030 | ** not really lost anything. |
| 6031 | */ |
| 6032 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6033 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6034 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 6035 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6036 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6037 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 6038 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6039 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6040 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6041 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 6042 | #define unixDlOpen 0 |
| 6043 | #define unixDlError 0 |
| 6044 | #define unixDlSym 0 |
| 6045 | #define unixDlClose 0 |
| 6046 | #endif |
| 6047 | |
| 6048 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6049 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6050 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6051 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 6052 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 6053 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6054 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6055 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 6056 | ** errors. The reports issued by valgrind are incorrect - we would |
| 6057 | ** prefer that the randomness be increased by making use of the |
| 6058 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 6059 | ** some users. Rather than argue, it seems easier just to initialize |
| 6060 | ** the whole array and silence valgrind, even if that means less randomness |
| 6061 | ** in the random seed. |
| 6062 | ** |
| 6063 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 6064 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6065 | ** tests repeatable. |
| 6066 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6067 | memset(zBuf, 0, nBuf); |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6068 | randomnessPid = osGetpid(0); |
drh | 6a412b8 | 2015-04-30 12:31:49 +0000 | [diff] [blame] | 6069 | #if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6070 | { |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 6071 | int fd, got; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 6072 | fd = robust_open("/dev/urandom", O_RDONLY, 0); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6073 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 6074 | time_t t; |
| 6075 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6076 | memcpy(zBuf, &t, sizeof(t)); |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 6077 | memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid)); |
| 6078 | assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf ); |
| 6079 | nBuf = sizeof(t) + sizeof(randomnessPid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6080 | }else{ |
drh | c18b404 | 2012-02-10 03:10:27 +0000 | [diff] [blame] | 6081 | do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6082 | robust_close(0, fd, __LINE__); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6083 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6084 | } |
| 6085 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 6086 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6087 | } |
| 6088 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6089 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6090 | /* |
| 6091 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6092 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 6093 | ** The return value is the number of microseconds of sleep actually |
| 6094 | ** requested from the underlying operating system, a number which |
| 6095 | ** might be greater than or equal to the argument, but not less |
| 6096 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6097 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6098 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6099 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 6100 | struct timespec sp; |
| 6101 | |
| 6102 | sp.tv_sec = microseconds / 1000000; |
| 6103 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 6104 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6105 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6106 | return microseconds; |
| 6107 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6108 | usleep(microseconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6109 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6110 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6111 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6112 | int seconds = (microseconds+999999)/1000000; |
| 6113 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6114 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 6115 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 6116 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 6117 | } |
| 6118 | |
| 6119 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6120 | ** The following variable, if set to a non-zero value, is interpreted as |
| 6121 | ** the number of seconds since 1970 and is used to set the result of |
| 6122 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6123 | */ |
| 6124 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6125 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6126 | #endif |
| 6127 | |
| 6128 | /* |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6129 | ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 6130 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 6131 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 6132 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 6133 | ** proleptic Gregorian calendar. |
| 6134 | ** |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6135 | ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
| 6136 | ** cannot be found. |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6137 | */ |
| 6138 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 6139 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6140 | int rc = SQLITE_OK; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6141 | #if defined(NO_GETTOD) |
| 6142 | time_t t; |
| 6143 | time(&t); |
dan | 15eac4e | 2010-11-22 17:26:07 +0000 | [diff] [blame] | 6144 | *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6145 | #elif OS_VXWORKS |
| 6146 | struct timespec sNow; |
| 6147 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 6148 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 6149 | #else |
| 6150 | struct timeval sNow; |
drh | 970942e | 2015-11-25 23:13:14 +0000 | [diff] [blame] | 6151 | (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */ |
| 6152 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6153 | #endif |
| 6154 | |
| 6155 | #ifdef SQLITE_TEST |
| 6156 | if( sqlite3_current_time ){ |
| 6157 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 6158 | } |
| 6159 | #endif |
| 6160 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6161 | return rc; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6162 | } |
| 6163 | |
drh | 5337dac | 2015-11-25 15:15:03 +0000 | [diff] [blame] | 6164 | #if 0 /* Not used */ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6165 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6166 | ** Find the current time (in Universal Coordinated Time). Write the |
| 6167 | ** current time and date as a Julian Day number into *prNow and |
| 6168 | ** return 0. Return 1 if the time and date cannot be found. |
| 6169 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6170 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | b87a666 | 2011-10-13 01:01:14 +0000 | [diff] [blame] | 6171 | sqlite3_int64 i = 0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6172 | int rc; |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 6173 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6174 | rc = unixCurrentTimeInt64(0, &i); |
drh | 0dcb0a7 | 2010-05-03 18:22:52 +0000 | [diff] [blame] | 6175 | *prNow = i/86400000.0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6176 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6177 | } |
drh | 5337dac | 2015-11-25 15:15:03 +0000 | [diff] [blame] | 6178 | #else |
| 6179 | # define unixCurrentTime 0 |
| 6180 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6181 | |
drh | 5337dac | 2015-11-25 15:15:03 +0000 | [diff] [blame] | 6182 | #if 0 /* Not used */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6183 | /* |
| 6184 | ** We added the xGetLastError() method with the intention of providing |
| 6185 | ** better low-level error messages when operating-system problems come up |
| 6186 | ** during SQLite operation. But so far, none of that has been implemented |
| 6187 | ** in the core. So this routine is never called. For now, it is merely |
| 6188 | ** a place-holder. |
| 6189 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6190 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 6191 | UNUSED_PARAMETER(NotUsed); |
| 6192 | UNUSED_PARAMETER(NotUsed2); |
| 6193 | UNUSED_PARAMETER(NotUsed3); |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 6194 | return 0; |
| 6195 | } |
drh | 5337dac | 2015-11-25 15:15:03 +0000 | [diff] [blame] | 6196 | #else |
| 6197 | # define unixGetLastError 0 |
| 6198 | #endif |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 6199 | |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6200 | |
| 6201 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6202 | ************************ End of sqlite3_vfs methods *************************** |
| 6203 | ******************************************************************************/ |
| 6204 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6205 | /****************************************************************************** |
| 6206 | ************************** Begin Proxy Locking ******************************** |
| 6207 | ** |
| 6208 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 6209 | ** other locking methods on secondary lock files. Proxy locking is a |
| 6210 | ** meta-layer over top of the primitive locking implemented above. For |
| 6211 | ** this reason, the division that implements of proxy locking is deferred |
| 6212 | ** until late in the file (here) after all of the other I/O methods have |
| 6213 | ** been defined - so that the primitive locking methods are available |
| 6214 | ** as services to help with the implementation of proxy locking. |
| 6215 | ** |
| 6216 | **** |
| 6217 | ** |
| 6218 | ** The default locking schemes in SQLite use byte-range locks on the |
| 6219 | ** database file to coordinate safe, concurrent access by multiple readers |
| 6220 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 6221 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 6222 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 6223 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 6224 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 6225 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 6226 | ** address in the shared range is taken for a SHARED lock, the entire |
| 6227 | ** shared range is taken for an EXCLUSIVE lock): |
| 6228 | ** |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 6229 | ** PENDING_BYTE 0x40000000 |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6230 | ** RESERVED_BYTE 0x40000001 |
| 6231 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 6232 | ** |
| 6233 | ** This works well on the local file system, but shows a nearly 100x |
| 6234 | ** slowdown in read performance on AFP because the AFP client disables |
| 6235 | ** the read cache when byte-range locks are present. Enabling the read |
| 6236 | ** cache exposes a cache coherency problem that is present on all OS X |
| 6237 | ** supported network file systems. NFS and AFP both observe the |
| 6238 | ** close-to-open semantics for ensuring cache coherency |
| 6239 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 6240 | ** address the requirements for concurrent database access by multiple |
| 6241 | ** readers and writers |
| 6242 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 6243 | ** |
| 6244 | ** To address the performance and cache coherency issues, proxy file locking |
| 6245 | ** changes the way database access is controlled by limiting access to a |
| 6246 | ** single host at a time and moving file locks off of the database file |
| 6247 | ** and onto a proxy file on the local file system. |
| 6248 | ** |
| 6249 | ** |
| 6250 | ** Using proxy locks |
| 6251 | ** ----------------- |
| 6252 | ** |
| 6253 | ** C APIs |
| 6254 | ** |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6255 | ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6256 | ** <proxy_path> | ":auto:"); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6257 | ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE, |
| 6258 | ** &<proxy_path>); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6259 | ** |
| 6260 | ** |
| 6261 | ** SQL pragmas |
| 6262 | ** |
| 6263 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 6264 | ** PRAGMA [database.]lock_proxy_file |
| 6265 | ** |
| 6266 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 6267 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 6268 | ** a proxy path based on the user's temp dir |
| 6269 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 6270 | ** actual proxy file name is generated from the name and path of the |
| 6271 | ** database file. For example: |
| 6272 | ** |
| 6273 | ** For database path "/Users/me/foo.db" |
| 6274 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 6275 | ** |
| 6276 | ** Once a lock proxy is configured for a database connection, it can not |
| 6277 | ** be removed, however it may be switched to a different proxy path via |
| 6278 | ** the above APIs (assuming the conch file is not being held by another |
| 6279 | ** connection or process). |
| 6280 | ** |
| 6281 | ** |
| 6282 | ** How proxy locking works |
| 6283 | ** ----------------------- |
| 6284 | ** |
| 6285 | ** Proxy file locking relies primarily on two new supporting files: |
| 6286 | ** |
| 6287 | ** * conch file to limit access to the database file to a single host |
| 6288 | ** at a time |
| 6289 | ** |
| 6290 | ** * proxy file to act as a proxy for the advisory locks normally |
| 6291 | ** taken on the database |
| 6292 | ** |
| 6293 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 6294 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 6295 | ** contents and comparing the host's unique host ID (see below) and lock |
| 6296 | ** proxy path against the values stored in the conch. The conch file is |
| 6297 | ** stored in the same directory as the database file and the file name |
| 6298 | ** is patterned after the database file name as ".<databasename>-conch". |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 6299 | ** If the conch file does not exist, or its contents do not match the |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6300 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 6301 | ** lock and the conch file contents is updated with the host ID and proxy |
| 6302 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 6303 | ** is held by another process (with a shared lock), the exclusive lock |
| 6304 | ** will fail and SQLITE_BUSY is returned. |
| 6305 | ** |
| 6306 | ** The proxy file - a single-byte file used for all advisory file locks |
| 6307 | ** normally taken on the database file. This allows for safe sharing |
| 6308 | ** of the database file for multiple readers and writers on the same |
| 6309 | ** host (the conch ensures that they all use the same local lock file). |
| 6310 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6311 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 6312 | ** only taken when the first request to lock database file is made. |
| 6313 | ** This matches the semantics of the traditional locking behavior, where |
| 6314 | ** opening a connection to a database file does not take a lock on it. |
| 6315 | ** The shared lock and an open file descriptor are maintained until |
| 6316 | ** the connection to the database is closed. |
| 6317 | ** |
| 6318 | ** The proxy file and the lock file are never deleted so they only need |
| 6319 | ** to be created the first time they are used. |
| 6320 | ** |
| 6321 | ** Configuration options |
| 6322 | ** --------------------- |
| 6323 | ** |
| 6324 | ** SQLITE_PREFER_PROXY_LOCKING |
| 6325 | ** |
| 6326 | ** Database files accessed on non-local file systems are |
| 6327 | ** automatically configured for proxy locking, lock files are |
| 6328 | ** named automatically using the same logic as |
| 6329 | ** PRAGMA lock_proxy_file=":auto:" |
| 6330 | ** |
| 6331 | ** SQLITE_PROXY_DEBUG |
| 6332 | ** |
| 6333 | ** Enables the logging of error messages during host id file |
| 6334 | ** retrieval and creation |
| 6335 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6336 | ** LOCKPROXYDIR |
| 6337 | ** |
| 6338 | ** Overrides the default directory used for lock proxy files that |
| 6339 | ** are named automatically via the ":auto:" setting |
| 6340 | ** |
| 6341 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 6342 | ** |
| 6343 | ** Permissions to use when creating a directory for storing the |
| 6344 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 6345 | ** |
| 6346 | ** |
| 6347 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 6348 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 6349 | ** force proxy locking to be used for every database file opened, and 0 |
| 6350 | ** will force automatic proxy locking to be disabled for all database |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6351 | ** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6352 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 6353 | */ |
| 6354 | |
| 6355 | /* |
| 6356 | ** Proxy locking is only available on MacOSX |
| 6357 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6358 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6359 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6360 | /* |
| 6361 | ** The proxyLockingContext has the path and file structures for the remote |
| 6362 | ** and local proxy files in it |
| 6363 | */ |
| 6364 | typedef struct proxyLockingContext proxyLockingContext; |
| 6365 | struct proxyLockingContext { |
| 6366 | unixFile *conchFile; /* Open conch file */ |
| 6367 | char *conchFilePath; /* Name of the conch file */ |
| 6368 | unixFile *lockProxy; /* Open proxy lock file */ |
| 6369 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 6370 | char *dbPath; /* Name of the open file */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6371 | int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6372 | int nFails; /* Number of conch taking failures */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6373 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 6374 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 6375 | }; |
| 6376 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6377 | /* |
| 6378 | ** The proxy lock file path for the database at dbPath is written into lPath, |
| 6379 | ** which must point to valid, writable memory large enough for a maxLen length |
| 6380 | ** file path. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6381 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6382 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 6383 | int len; |
| 6384 | int dbLen; |
| 6385 | int i; |
| 6386 | |
| 6387 | #ifdef LOCKPROXYDIR |
| 6388 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 6389 | #else |
| 6390 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 6391 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6392 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6393 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6394 | lPath, errno, osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6395 | return SQLITE_IOERR_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6396 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6397 | len = strlcat(lPath, "sqliteplocks", maxLen); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6398 | } |
| 6399 | # else |
| 6400 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 6401 | # endif |
| 6402 | #endif |
| 6403 | |
| 6404 | if( lPath[len-1]!='/' ){ |
| 6405 | len = strlcat(lPath, "/", maxLen); |
| 6406 | } |
| 6407 | |
| 6408 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6409 | dbLen = (int)strlen(dbPath); |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6410 | for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6411 | char c = dbPath[i]; |
| 6412 | lPath[i+len] = (c=='/')?'_':c; |
| 6413 | } |
| 6414 | lPath[i+len]='\0'; |
| 6415 | strlcat(lPath, ":auto:", maxLen); |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6416 | OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6417 | return SQLITE_OK; |
| 6418 | } |
| 6419 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6420 | /* |
| 6421 | ** Creates the lock file and any missing directories in lockPath |
| 6422 | */ |
| 6423 | static int proxyCreateLockPath(const char *lockPath){ |
| 6424 | int i, len; |
| 6425 | char buf[MAXPATHLEN]; |
| 6426 | int start = 0; |
| 6427 | |
| 6428 | assert(lockPath!=NULL); |
| 6429 | /* try to create all the intermediate directories */ |
| 6430 | len = (int)strlen(lockPath); |
| 6431 | buf[0] = lockPath[0]; |
| 6432 | for( i=1; i<len; i++ ){ |
| 6433 | if( lockPath[i] == '/' && (i - start > 0) ){ |
| 6434 | /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 6435 | if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 6436 | || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 6437 | buf[i]='\0'; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 6438 | if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6439 | int err=errno; |
| 6440 | if( err!=EEXIST ) { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6441 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6442 | "'%s' proxy lock path=%s pid=%d\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6443 | buf, strerror(err), lockPath, osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6444 | return err; |
| 6445 | } |
| 6446 | } |
| 6447 | } |
| 6448 | start=i+1; |
| 6449 | } |
| 6450 | buf[i] = lockPath[i]; |
| 6451 | } |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 6452 | OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6453 | return 0; |
| 6454 | } |
| 6455 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6456 | /* |
| 6457 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 6458 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 6459 | ** |
| 6460 | ** The caller is responsible not only for closing the file descriptor |
| 6461 | ** but also for freeing the memory associated with the file descriptor. |
| 6462 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6463 | static int proxyCreateUnixFile( |
| 6464 | const char *path, /* path for the new unixFile */ |
| 6465 | unixFile **ppFile, /* unixFile created and returned by ref */ |
| 6466 | int islockfile /* if non zero missing dirs will be created */ |
| 6467 | ) { |
| 6468 | int fd = -1; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6469 | unixFile *pNew; |
| 6470 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6471 | int openFlags = O_RDWR | O_CREAT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6472 | sqlite3_vfs dummyVfs; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6473 | int terrno = 0; |
| 6474 | UnixUnusedFd *pUnused = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6475 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6476 | /* 1. first try to open/create the file |
| 6477 | ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 6478 | ** the parent directories and then try again. |
| 6479 | ** 3. if that fails, try to open the file read-only |
| 6480 | ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 6481 | */ |
| 6482 | pUnused = findReusableFd(path, openFlags); |
| 6483 | if( pUnused ){ |
| 6484 | fd = pUnused->fd; |
| 6485 | }else{ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 6486 | pUnused = sqlite3_malloc64(sizeof(*pUnused)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6487 | if( !pUnused ){ |
| 6488 | return SQLITE_NOMEM; |
| 6489 | } |
| 6490 | } |
| 6491 | if( fd<0 ){ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6492 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6493 | terrno = errno; |
| 6494 | if( fd<0 && errno==ENOENT && islockfile ){ |
| 6495 | if( proxyCreateLockPath(path) == SQLITE_OK ){ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6496 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6497 | } |
| 6498 | } |
| 6499 | } |
| 6500 | if( fd<0 ){ |
| 6501 | openFlags = O_RDONLY; |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6502 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6503 | terrno = errno; |
| 6504 | } |
| 6505 | if( fd<0 ){ |
| 6506 | if( islockfile ){ |
| 6507 | return SQLITE_BUSY; |
| 6508 | } |
| 6509 | switch (terrno) { |
| 6510 | case EACCES: |
| 6511 | return SQLITE_PERM; |
| 6512 | case EIO: |
| 6513 | return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 6514 | default: |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6515 | return SQLITE_CANTOPEN_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6516 | } |
| 6517 | } |
| 6518 | |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 6519 | pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6520 | if( pNew==NULL ){ |
| 6521 | rc = SQLITE_NOMEM; |
| 6522 | goto end_create_proxy; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6523 | } |
| 6524 | memset(pNew, 0, sizeof(unixFile)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6525 | pNew->openFlags = openFlags; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 6526 | memset(&dummyVfs, 0, sizeof(dummyVfs)); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6527 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 6528 | dummyVfs.zName = "dummy"; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6529 | pUnused->fd = fd; |
| 6530 | pUnused->flags = openFlags; |
| 6531 | pNew->pUnused = pUnused; |
| 6532 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6533 | rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6534 | if( rc==SQLITE_OK ){ |
| 6535 | *ppFile = pNew; |
| 6536 | return SQLITE_OK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6537 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6538 | end_create_proxy: |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6539 | robust_close(pNew, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6540 | sqlite3_free(pNew); |
| 6541 | sqlite3_free(pUnused); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6542 | return rc; |
| 6543 | } |
| 6544 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6545 | #ifdef SQLITE_TEST |
| 6546 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 6547 | int sqlite3_hostid_num = 0; |
| 6548 | #endif |
| 6549 | |
| 6550 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 6551 | |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 6552 | #ifdef HAVE_GETHOSTUUID |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6553 | /* Not always defined in the headers as it ought to be */ |
| 6554 | extern int gethostuuid(uuid_t id, const struct timespec *wait); |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 6555 | #endif |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6556 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6557 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 6558 | ** bytes of writable memory. |
| 6559 | */ |
| 6560 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6561 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 6562 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 6563 | #ifdef HAVE_GETHOSTUUID |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 6564 | { |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6565 | struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 6566 | if( gethostuuid(pHostID, &timeout) ){ |
| 6567 | int err = errno; |
| 6568 | if( pError ){ |
| 6569 | *pError = err; |
| 6570 | } |
| 6571 | return SQLITE_IOERR; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6572 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6573 | } |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6574 | #else |
| 6575 | UNUSED_PARAMETER(pError); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 6576 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6577 | #ifdef SQLITE_TEST |
| 6578 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 6579 | if( sqlite3_hostid_num != 0){ |
| 6580 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 6581 | } |
| 6582 | #endif |
| 6583 | |
| 6584 | return SQLITE_OK; |
| 6585 | } |
| 6586 | |
| 6587 | /* The conch file contains the header, host id and lock file path |
| 6588 | */ |
| 6589 | #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 6590 | #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 6591 | #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 6592 | #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 6593 | |
| 6594 | /* |
| 6595 | ** Takes an open conch file, copies the contents to a new path and then moves |
| 6596 | ** it back. The newly created file's file descriptor is assigned to the |
| 6597 | ** conch file structure and finally the original conch file descriptor is |
| 6598 | ** closed. Returns zero if successful. |
| 6599 | */ |
| 6600 | static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 6601 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6602 | unixFile *conchFile = pCtx->conchFile; |
| 6603 | char tPath[MAXPATHLEN]; |
| 6604 | char buf[PROXY_MAXCONCHLEN]; |
| 6605 | char *cPath = pCtx->conchFilePath; |
| 6606 | size_t readLen = 0; |
| 6607 | size_t pathLen = 0; |
| 6608 | char errmsg[64] = ""; |
| 6609 | int fd = -1; |
| 6610 | int rc = -1; |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6611 | UNUSED_PARAMETER(myHostID); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6612 | |
| 6613 | /* create a new path by replace the trailing '-conch' with '-break' */ |
| 6614 | pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 6615 | if( pathLen>MAXPATHLEN || pathLen<6 || |
| 6616 | (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6617 | sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6618 | goto end_breaklock; |
| 6619 | } |
| 6620 | /* read the conch content */ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6621 | readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6622 | if( readLen<PROXY_PATHINDEX ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6623 | sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6624 | goto end_breaklock; |
| 6625 | } |
| 6626 | /* write it out to the temporary break file */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6627 | fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6628 | if( fd<0 ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6629 | sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6630 | goto end_breaklock; |
| 6631 | } |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6632 | if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6633 | sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6634 | goto end_breaklock; |
| 6635 | } |
| 6636 | if( rename(tPath, cPath) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 6637 | sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6638 | goto end_breaklock; |
| 6639 | } |
| 6640 | rc = 0; |
| 6641 | fprintf(stderr, "broke stale lock on %s\n", cPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6642 | robust_close(pFile, conchFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6643 | conchFile->h = fd; |
| 6644 | conchFile->openFlags = O_RDWR | O_CREAT; |
| 6645 | |
| 6646 | end_breaklock: |
| 6647 | if( rc ){ |
| 6648 | if( fd>=0 ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 6649 | osUnlink(tPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6650 | robust_close(pFile, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6651 | } |
| 6652 | fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 6653 | } |
| 6654 | return rc; |
| 6655 | } |
| 6656 | |
| 6657 | /* Take the requested lock on the conch file and break a stale lock if the |
| 6658 | ** host id matches. |
| 6659 | */ |
| 6660 | static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 6661 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6662 | unixFile *conchFile = pCtx->conchFile; |
| 6663 | int rc = SQLITE_OK; |
| 6664 | int nTries = 0; |
| 6665 | struct timespec conchModTime; |
| 6666 | |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6667 | memset(&conchModTime, 0, sizeof(conchModTime)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6668 | do { |
| 6669 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 6670 | nTries ++; |
| 6671 | if( rc==SQLITE_BUSY ){ |
| 6672 | /* If the lock failed (busy): |
| 6673 | * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 6674 | * 2nd try: fail if the mod time changed or host id is different, wait |
| 6675 | * 10 sec and try again |
| 6676 | * 3rd try: break the lock unless the mod time has changed. |
| 6677 | */ |
| 6678 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6679 | if( osFstat(conchFile->h, &buf) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6680 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6681 | return SQLITE_IOERR_LOCK; |
| 6682 | } |
| 6683 | |
| 6684 | if( nTries==1 ){ |
| 6685 | conchModTime = buf.st_mtimespec; |
| 6686 | usleep(500000); /* wait 0.5 sec and try the lock again*/ |
| 6687 | continue; |
| 6688 | } |
| 6689 | |
| 6690 | assert( nTries>1 ); |
| 6691 | if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 6692 | conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 6693 | return SQLITE_BUSY; |
| 6694 | } |
| 6695 | |
| 6696 | if( nTries==2 ){ |
| 6697 | char tBuf[PROXY_MAXCONCHLEN]; |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6698 | int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6699 | if( len<0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6700 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6701 | return SQLITE_IOERR_LOCK; |
| 6702 | } |
| 6703 | if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 6704 | /* don't break the lock if the host id doesn't match */ |
| 6705 | if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 6706 | return SQLITE_BUSY; |
| 6707 | } |
| 6708 | }else{ |
| 6709 | /* don't break the lock on short read or a version mismatch */ |
| 6710 | return SQLITE_BUSY; |
| 6711 | } |
| 6712 | usleep(10000000); /* wait 10 sec and try the lock again */ |
| 6713 | continue; |
| 6714 | } |
| 6715 | |
| 6716 | assert( nTries==3 ); |
| 6717 | if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 6718 | rc = SQLITE_OK; |
| 6719 | if( lockType==EXCLUSIVE_LOCK ){ |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 6720 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6721 | } |
| 6722 | if( !rc ){ |
| 6723 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 6724 | } |
| 6725 | } |
| 6726 | } |
| 6727 | } while( rc==SQLITE_BUSY && nTries<3 ); |
| 6728 | |
| 6729 | return rc; |
| 6730 | } |
| 6731 | |
| 6732 | /* Takes the conch by taking a shared lock and read the contents conch, if |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6733 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 6734 | ** lockPath means that the lockPath in the conch file will be used if the |
| 6735 | ** host IDs match, or a new lock path will be generated automatically |
| 6736 | ** and written to the conch file. |
| 6737 | */ |
| 6738 | static int proxyTakeConch(unixFile *pFile){ |
| 6739 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6740 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6741 | if( pCtx->conchHeld!=0 ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6742 | return SQLITE_OK; |
| 6743 | }else{ |
| 6744 | unixFile *conchFile = pCtx->conchFile; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6745 | uuid_t myHostID; |
| 6746 | int pError = 0; |
| 6747 | char readBuf[PROXY_MAXCONCHLEN]; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6748 | char lockPath[MAXPATHLEN]; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6749 | char *tempLockPath = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6750 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6751 | int createConch = 0; |
| 6752 | int hostIdMatch = 0; |
| 6753 | int readLen = 0; |
| 6754 | int tryOldLockPath = 0; |
| 6755 | int forceNewLockPath = 0; |
| 6756 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6757 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 6758 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6759 | osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6760 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6761 | rc = proxyGetHostID(myHostID, &pError); |
| 6762 | if( (rc&0xff)==SQLITE_IOERR ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6763 | storeLastErrno(pFile, pError); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6764 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6765 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6766 | rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6767 | if( rc!=SQLITE_OK ){ |
| 6768 | goto end_takeconch; |
| 6769 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6770 | /* read the existing conch file */ |
| 6771 | readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 6772 | if( readLen<0 ){ |
| 6773 | /* I/O error: lastErrno set by seekAndRead */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6774 | storeLastErrno(pFile, conchFile->lastErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6775 | rc = SQLITE_IOERR_READ; |
| 6776 | goto end_takeconch; |
| 6777 | }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 6778 | readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 6779 | /* a short read or version format mismatch means we need to create a new |
| 6780 | ** conch file. |
| 6781 | */ |
| 6782 | createConch = 1; |
| 6783 | } |
| 6784 | /* if the host id matches and the lock path already exists in the conch |
| 6785 | ** we'll try to use the path there, if we can't open that path, we'll |
| 6786 | ** retry with a new auto-generated path |
| 6787 | */ |
| 6788 | do { /* in case we need to try again for an :auto: named lock file */ |
| 6789 | |
| 6790 | if( !createConch && !forceNewLockPath ){ |
| 6791 | hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 6792 | PROXY_HOSTIDLEN); |
| 6793 | /* if the conch has data compare the contents */ |
| 6794 | if( !pCtx->lockProxyPath ){ |
| 6795 | /* for auto-named local lock file, just check the host ID and we'll |
| 6796 | ** use the local lock file path that's already in there |
| 6797 | */ |
| 6798 | if( hostIdMatch ){ |
| 6799 | size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 6800 | |
| 6801 | if( pathLen>=MAXPATHLEN ){ |
| 6802 | pathLen=MAXPATHLEN-1; |
| 6803 | } |
| 6804 | memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 6805 | lockPath[pathLen] = 0; |
| 6806 | tempLockPath = lockPath; |
| 6807 | tryOldLockPath = 1; |
| 6808 | /* create a copy of the lock path if the conch is taken */ |
| 6809 | goto end_takeconch; |
| 6810 | } |
| 6811 | }else if( hostIdMatch |
| 6812 | && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 6813 | readLen-PROXY_PATHINDEX) |
| 6814 | ){ |
| 6815 | /* conch host and lock path match */ |
| 6816 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6817 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6818 | } |
| 6819 | |
| 6820 | /* if the conch isn't writable and doesn't match, we can't take it */ |
| 6821 | if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 6822 | rc = SQLITE_BUSY; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6823 | goto end_takeconch; |
| 6824 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6825 | |
| 6826 | /* either the conch didn't match or we need to create a new one */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6827 | if( !pCtx->lockProxyPath ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6828 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 6829 | tempLockPath = lockPath; |
| 6830 | /* create a copy of the lock path _only_ if the conch is taken */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6831 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6832 | |
| 6833 | /* update conch with host and path (this will fail if other process |
| 6834 | ** has a shared lock already), if the host id matches, use the big |
| 6835 | ** stick. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6836 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6837 | futimes(conchFile->h, NULL); |
| 6838 | if( hostIdMatch && !createConch ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 6839 | if( conchFile->pInode && conchFile->pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6840 | /* We are trying for an exclusive lock but another thread in this |
| 6841 | ** same process is still holding a shared lock. */ |
| 6842 | rc = SQLITE_BUSY; |
| 6843 | } else { |
| 6844 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6845 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6846 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6847 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6848 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6849 | if( rc==SQLITE_OK ){ |
| 6850 | char writeBuffer[PROXY_MAXCONCHLEN]; |
| 6851 | int writeSize = 0; |
| 6852 | |
| 6853 | writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 6854 | memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 6855 | if( pCtx->lockProxyPath!=NULL ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6856 | strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, |
| 6857 | MAXPATHLEN); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6858 | }else{ |
| 6859 | strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 6860 | } |
| 6861 | writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6862 | robust_ftruncate(conchFile->h, writeSize); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6863 | rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
| 6864 | fsync(conchFile->h); |
| 6865 | /* If we created a new conch file (not just updated the contents of a |
| 6866 | ** valid conch file), try to match the permissions of the database |
| 6867 | */ |
| 6868 | if( rc==SQLITE_OK && createConch ){ |
| 6869 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 6870 | int err = osFstat(pFile->h, &buf); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6871 | if( err==0 ){ |
| 6872 | mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 6873 | S_IROTH|S_IWOTH); |
| 6874 | /* try to match the database file R/W permissions, ignore failure */ |
| 6875 | #ifndef SQLITE_PROXY_DEBUG |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6876 | osFchmod(conchFile->h, cmode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6877 | #else |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6878 | do{ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 6879 | rc = osFchmod(conchFile->h, cmode); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 6880 | }while( rc==(-1) && errno==EINTR ); |
| 6881 | if( rc!=0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6882 | int code = errno; |
| 6883 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 6884 | cmode, code, strerror(code)); |
| 6885 | } else { |
| 6886 | fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 6887 | } |
| 6888 | }else{ |
| 6889 | int code = errno; |
| 6890 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 6891 | err, code, strerror(code)); |
| 6892 | #endif |
| 6893 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6894 | } |
| 6895 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6896 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 6897 | |
| 6898 | end_takeconch: |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6899 | OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6900 | if( rc==SQLITE_OK && pFile->openFlags ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6901 | int fd; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6902 | if( pFile->h>=0 ){ |
drh | e84009f | 2011-03-02 17:54:32 +0000 | [diff] [blame] | 6903 | robust_close(pFile, pFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6904 | } |
| 6905 | pFile->h = -1; |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6906 | fd = robust_open(pCtx->dbPath, pFile->openFlags, 0); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6907 | OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6908 | if( fd>=0 ){ |
| 6909 | pFile->h = fd; |
| 6910 | }else{ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6911 | rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6912 | during locking */ |
| 6913 | } |
| 6914 | } |
| 6915 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 6916 | char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 6917 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 6918 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 6919 | /* we couldn't create the proxy lock file with the old lock file path |
| 6920 | ** so try again via auto-naming |
| 6921 | */ |
| 6922 | forceNewLockPath = 1; |
| 6923 | tryOldLockPath = 0; |
dan | 2b0ef47 | 2010-02-16 12:18:47 +0000 | [diff] [blame] | 6924 | continue; /* go back to the do {} while start point, try again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6925 | } |
| 6926 | } |
| 6927 | if( rc==SQLITE_OK ){ |
| 6928 | /* Need to make a copy of path if we extracted the value |
| 6929 | ** from the conch file or the path was allocated on the stack |
| 6930 | */ |
| 6931 | if( tempLockPath ){ |
| 6932 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 6933 | if( !pCtx->lockProxyPath ){ |
| 6934 | rc = SQLITE_NOMEM; |
| 6935 | } |
| 6936 | } |
| 6937 | } |
| 6938 | if( rc==SQLITE_OK ){ |
| 6939 | pCtx->conchHeld = 1; |
| 6940 | |
| 6941 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 6942 | afpLockingContext *afpCtx; |
| 6943 | afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 6944 | afpCtx->dbPath = pCtx->lockProxyPath; |
| 6945 | } |
| 6946 | } else { |
| 6947 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 6948 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6949 | OSTRACE(("TAKECONCH %d %s\n", conchFile->h, |
| 6950 | rc==SQLITE_OK?"ok":"failed")); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6951 | return rc; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6952 | } while (1); /* in case we need to retry the :auto: lock file - |
| 6953 | ** we should never get here except via the 'continue' call. */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6954 | } |
| 6955 | } |
| 6956 | |
| 6957 | /* |
| 6958 | ** If pFile holds a lock on a conch file, then release that lock. |
| 6959 | */ |
| 6960 | static int proxyReleaseConch(unixFile *pFile){ |
drh | 1c5bb4d | 2010-05-10 17:29:28 +0000 | [diff] [blame] | 6961 | int rc = SQLITE_OK; /* Subroutine return code */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6962 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 6963 | unixFile *conchFile; /* Name of the conch file */ |
| 6964 | |
| 6965 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6966 | conchFile = pCtx->conchFile; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6967 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6968 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6969 | osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6970 | if( pCtx->conchHeld>0 ){ |
| 6971 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 6972 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6973 | pCtx->conchHeld = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6974 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| 6975 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6976 | return rc; |
| 6977 | } |
| 6978 | |
| 6979 | /* |
| 6980 | ** Given the name of a database file, compute the name of its conch file. |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 6981 | ** Store the conch filename in memory obtained from sqlite3_malloc64(). |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6982 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 6983 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 6984 | ** |
| 6985 | ** The caller is responsible for ensuring that the allocated memory |
| 6986 | ** space is eventually freed. |
| 6987 | ** |
| 6988 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 6989 | */ |
| 6990 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 6991 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6992 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6993 | char *conchPath; /* buffer in which to construct conch name */ |
| 6994 | |
| 6995 | /* Allocate space for the conch filename and initialize the name to |
| 6996 | ** the name of the original database file. */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 6997 | *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6998 | if( conchPath==0 ){ |
| 6999 | return SQLITE_NOMEM; |
| 7000 | } |
| 7001 | memcpy(conchPath, dbPath, len+1); |
| 7002 | |
| 7003 | /* now insert a "." before the last / character */ |
| 7004 | for( i=(len-1); i>=0; i-- ){ |
| 7005 | if( conchPath[i]=='/' ){ |
| 7006 | i++; |
| 7007 | break; |
| 7008 | } |
| 7009 | } |
| 7010 | conchPath[i]='.'; |
| 7011 | while ( i<len ){ |
| 7012 | conchPath[i+1]=dbPath[i]; |
| 7013 | i++; |
| 7014 | } |
| 7015 | |
| 7016 | /* append the "-conch" suffix to the file */ |
| 7017 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7018 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7019 | |
| 7020 | return SQLITE_OK; |
| 7021 | } |
| 7022 | |
| 7023 | |
| 7024 | /* Takes a fully configured proxy locking-style unix file and switches |
| 7025 | ** the local lock file path |
| 7026 | */ |
| 7027 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 7028 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 7029 | char *oldPath = pCtx->lockProxyPath; |
| 7030 | int rc = SQLITE_OK; |
| 7031 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7032 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7033 | return SQLITE_BUSY; |
| 7034 | } |
| 7035 | |
| 7036 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 7037 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 7038 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 7039 | return SQLITE_OK; |
| 7040 | }else{ |
| 7041 | unixFile *lockProxy = pCtx->lockProxy; |
| 7042 | pCtx->lockProxy=NULL; |
| 7043 | pCtx->conchHeld = 0; |
| 7044 | if( lockProxy!=NULL ){ |
| 7045 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 7046 | if( rc ) return rc; |
| 7047 | sqlite3_free(lockProxy); |
| 7048 | } |
| 7049 | sqlite3_free(oldPath); |
| 7050 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 7051 | } |
| 7052 | |
| 7053 | return rc; |
| 7054 | } |
| 7055 | |
| 7056 | /* |
| 7057 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 7058 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 7059 | ** |
| 7060 | ** This routine find the filename associated with pFile and writes it |
| 7061 | ** int dbPath. |
| 7062 | */ |
| 7063 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7064 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7065 | if( pFile->pMethod == &afpIoMethods ){ |
| 7066 | /* afp style keeps a reference to the db path in the filePath field |
| 7067 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7068 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7069 | strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, |
| 7070 | MAXPATHLEN); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7071 | } else |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7072 | #endif |
| 7073 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 7074 | /* dot lock style uses the locking context to store the dot lock |
| 7075 | ** file path */ |
| 7076 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 7077 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 7078 | }else{ |
| 7079 | /* all other styles use the locking context to store the db file path */ |
| 7080 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7081 | strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7082 | } |
| 7083 | return SQLITE_OK; |
| 7084 | } |
| 7085 | |
| 7086 | /* |
| 7087 | ** Takes an already filled in unix file and alters it so all file locking |
| 7088 | ** will be performed on the local proxy lock file. The following fields |
| 7089 | ** are preserved in the locking context so that they can be restored and |
| 7090 | ** the unix structure properly cleaned up at close time: |
| 7091 | ** ->lockingContext |
| 7092 | ** ->pMethod |
| 7093 | */ |
| 7094 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 7095 | proxyLockingContext *pCtx; |
| 7096 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 7097 | char *lockPath=NULL; |
| 7098 | int rc = SQLITE_OK; |
| 7099 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7100 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7101 | return SQLITE_BUSY; |
| 7102 | } |
| 7103 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 7104 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 7105 | lockPath=NULL; |
| 7106 | }else{ |
| 7107 | lockPath=(char *)path; |
| 7108 | } |
| 7109 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7110 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 7111 | (lockPath ? lockPath : ":auto:"), osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7112 | |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 7113 | pCtx = sqlite3_malloc64( sizeof(*pCtx) ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7114 | if( pCtx==0 ){ |
| 7115 | return SQLITE_NOMEM; |
| 7116 | } |
| 7117 | memset(pCtx, 0, sizeof(*pCtx)); |
| 7118 | |
| 7119 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 7120 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7121 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 7122 | if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 7123 | /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 7124 | ** (c) the file system is read-only, then enable no-locking access. |
| 7125 | ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 7126 | ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 7127 | */ |
| 7128 | struct statfs fsInfo; |
| 7129 | struct stat conchInfo; |
| 7130 | int goLockless = 0; |
| 7131 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7132 | if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7133 | int err = errno; |
| 7134 | if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 7135 | goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 7136 | } |
| 7137 | } |
| 7138 | if( goLockless ){ |
| 7139 | pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 7140 | rc = SQLITE_OK; |
| 7141 | } |
| 7142 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7143 | } |
| 7144 | if( rc==SQLITE_OK && lockPath ){ |
| 7145 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 7146 | } |
| 7147 | |
| 7148 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7149 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 7150 | if( pCtx->dbPath==NULL ){ |
| 7151 | rc = SQLITE_NOMEM; |
| 7152 | } |
| 7153 | } |
| 7154 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7155 | /* all memory is allocated, proxys are created and assigned, |
| 7156 | ** switch the locking context and pMethod then return. |
| 7157 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7158 | pCtx->oldLockingContext = pFile->lockingContext; |
| 7159 | pFile->lockingContext = pCtx; |
| 7160 | pCtx->pOldMethod = pFile->pMethod; |
| 7161 | pFile->pMethod = &proxyIoMethods; |
| 7162 | }else{ |
| 7163 | if( pCtx->conchFile ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7164 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7165 | sqlite3_free(pCtx->conchFile); |
| 7166 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7167 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7168 | sqlite3_free(pCtx->conchFilePath); |
| 7169 | sqlite3_free(pCtx); |
| 7170 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7171 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 7172 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7173 | return rc; |
| 7174 | } |
| 7175 | |
| 7176 | |
| 7177 | /* |
| 7178 | ** This routine handles sqlite3_file_control() calls that are specific |
| 7179 | ** to proxy locking. |
| 7180 | */ |
| 7181 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 7182 | switch( op ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7183 | case SQLITE_FCNTL_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7184 | unixFile *pFile = (unixFile*)id; |
| 7185 | if( pFile->pMethod == &proxyIoMethods ){ |
| 7186 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 7187 | proxyTakeConch(pFile); |
| 7188 | if( pCtx->lockProxyPath ){ |
| 7189 | *(const char **)pArg = pCtx->lockProxyPath; |
| 7190 | }else{ |
| 7191 | *(const char **)pArg = ":auto: (not held)"; |
| 7192 | } |
| 7193 | } else { |
| 7194 | *(const char **)pArg = NULL; |
| 7195 | } |
| 7196 | return SQLITE_OK; |
| 7197 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7198 | case SQLITE_FCNTL_SET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7199 | unixFile *pFile = (unixFile*)id; |
| 7200 | int rc = SQLITE_OK; |
| 7201 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 7202 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 7203 | if( isProxyStyle ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7204 | /* turn off proxy locking - not supported. If support is added for |
| 7205 | ** switching proxy locking mode off then it will need to fail if |
| 7206 | ** the journal mode is WAL mode. |
| 7207 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7208 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 7209 | }else{ |
| 7210 | /* turn off proxy locking - already off - NOOP */ |
| 7211 | rc = SQLITE_OK; |
| 7212 | } |
| 7213 | }else{ |
| 7214 | const char *proxyPath = (const char *)pArg; |
| 7215 | if( isProxyStyle ){ |
| 7216 | proxyLockingContext *pCtx = |
| 7217 | (proxyLockingContext*)pFile->lockingContext; |
| 7218 | if( !strcmp(pArg, ":auto:") |
| 7219 | || (pCtx->lockProxyPath && |
| 7220 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 7221 | ){ |
| 7222 | rc = SQLITE_OK; |
| 7223 | }else{ |
| 7224 | rc = switchLockProxyPath(pFile, proxyPath); |
| 7225 | } |
| 7226 | }else{ |
| 7227 | /* turn on proxy file locking */ |
| 7228 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 7229 | } |
| 7230 | } |
| 7231 | return rc; |
| 7232 | } |
| 7233 | default: { |
| 7234 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 7235 | } |
| 7236 | } |
| 7237 | /*NOTREACHED*/ |
| 7238 | return SQLITE_ERROR; |
| 7239 | } |
| 7240 | |
| 7241 | /* |
| 7242 | ** Within this division (the proxying locking implementation) the procedures |
| 7243 | ** above this point are all utilities. The lock-related methods of the |
| 7244 | ** proxy-locking sqlite3_io_method object follow. |
| 7245 | */ |
| 7246 | |
| 7247 | |
| 7248 | /* |
| 7249 | ** This routine checks if there is a RESERVED lock held on the specified |
| 7250 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 7251 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 7252 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 7253 | */ |
| 7254 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 7255 | unixFile *pFile = (unixFile*)id; |
| 7256 | int rc = proxyTakeConch(pFile); |
| 7257 | if( rc==SQLITE_OK ){ |
| 7258 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7259 | if( pCtx->conchHeld>0 ){ |
| 7260 | unixFile *proxy = pCtx->lockProxy; |
| 7261 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 7262 | }else{ /* conchHeld < 0 is lockless */ |
| 7263 | pResOut=0; |
| 7264 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7265 | } |
| 7266 | return rc; |
| 7267 | } |
| 7268 | |
| 7269 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7270 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7271 | ** of the following: |
| 7272 | ** |
| 7273 | ** (1) SHARED_LOCK |
| 7274 | ** (2) RESERVED_LOCK |
| 7275 | ** (3) PENDING_LOCK |
| 7276 | ** (4) EXCLUSIVE_LOCK |
| 7277 | ** |
| 7278 | ** Sometimes when requesting one lock state, additional lock states |
| 7279 | ** are inserted in between. The locking might fail on one of the later |
| 7280 | ** transitions leaving the lock state different from what it started but |
| 7281 | ** still short of its goal. The following chart shows the allowed |
| 7282 | ** transitions and the inserted intermediate states: |
| 7283 | ** |
| 7284 | ** UNLOCKED -> SHARED |
| 7285 | ** SHARED -> RESERVED |
| 7286 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 7287 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 7288 | ** PENDING -> EXCLUSIVE |
| 7289 | ** |
| 7290 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 7291 | ** routine to lower a locking level. |
| 7292 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7293 | static int proxyLock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7294 | unixFile *pFile = (unixFile*)id; |
| 7295 | int rc = proxyTakeConch(pFile); |
| 7296 | if( rc==SQLITE_OK ){ |
| 7297 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7298 | if( pCtx->conchHeld>0 ){ |
| 7299 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7300 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); |
| 7301 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7302 | }else{ |
| 7303 | /* conchHeld < 0 is lockless */ |
| 7304 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7305 | } |
| 7306 | return rc; |
| 7307 | } |
| 7308 | |
| 7309 | |
| 7310 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7311 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7312 | ** must be either NO_LOCK or SHARED_LOCK. |
| 7313 | ** |
| 7314 | ** If the locking level of the file descriptor is already at or below |
| 7315 | ** the requested locking level, this routine is a no-op. |
| 7316 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7317 | static int proxyUnlock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7318 | unixFile *pFile = (unixFile*)id; |
| 7319 | int rc = proxyTakeConch(pFile); |
| 7320 | if( rc==SQLITE_OK ){ |
| 7321 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7322 | if( pCtx->conchHeld>0 ){ |
| 7323 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7324 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); |
| 7325 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7326 | }else{ |
| 7327 | /* conchHeld < 0 is lockless */ |
| 7328 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7329 | } |
| 7330 | return rc; |
| 7331 | } |
| 7332 | |
| 7333 | /* |
| 7334 | ** Close a file that uses proxy locks. |
| 7335 | */ |
| 7336 | static int proxyClose(sqlite3_file *id) { |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 7337 | if( ALWAYS(id) ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7338 | unixFile *pFile = (unixFile*)id; |
| 7339 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7340 | unixFile *lockProxy = pCtx->lockProxy; |
| 7341 | unixFile *conchFile = pCtx->conchFile; |
| 7342 | int rc = SQLITE_OK; |
| 7343 | |
| 7344 | if( lockProxy ){ |
| 7345 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 7346 | if( rc ) return rc; |
| 7347 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 7348 | if( rc ) return rc; |
| 7349 | sqlite3_free(lockProxy); |
| 7350 | pCtx->lockProxy = 0; |
| 7351 | } |
| 7352 | if( conchFile ){ |
| 7353 | if( pCtx->conchHeld ){ |
| 7354 | rc = proxyReleaseConch(pFile); |
| 7355 | if( rc ) return rc; |
| 7356 | } |
| 7357 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 7358 | if( rc ) return rc; |
| 7359 | sqlite3_free(conchFile); |
| 7360 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7361 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7362 | sqlite3_free(pCtx->conchFilePath); |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7363 | sqlite3DbFree(0, pCtx->dbPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7364 | /* restore the original locking context and pMethod then close it */ |
| 7365 | pFile->lockingContext = pCtx->oldLockingContext; |
| 7366 | pFile->pMethod = pCtx->pOldMethod; |
| 7367 | sqlite3_free(pCtx); |
| 7368 | return pFile->pMethod->xClose(id); |
| 7369 | } |
| 7370 | return SQLITE_OK; |
| 7371 | } |
| 7372 | |
| 7373 | |
| 7374 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7375 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7376 | /* |
| 7377 | ** The proxy locking style is intended for use with AFP filesystems. |
| 7378 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 7379 | ** restricted to MacOSX. |
| 7380 | ** |
| 7381 | ** |
| 7382 | ******************* End of the proxy lock implementation ********************** |
| 7383 | ******************************************************************************/ |
| 7384 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7385 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7386 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7387 | ** |
| 7388 | ** This routine registers all VFS implementations for unix-like operating |
| 7389 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 7390 | ** should be the only routines in this file that are visible from other |
| 7391 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7392 | ** |
| 7393 | ** This routine is called once during SQLite initialization and by a |
| 7394 | ** single thread. The memory allocation and mutex subsystems have not |
| 7395 | ** necessarily been initialized when this routine is called, and so they |
| 7396 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7397 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7398 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7399 | /* |
| 7400 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7401 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 7402 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 7403 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 7404 | ** and so we have to go through the intermediate pointer to avoid problems |
| 7405 | ** when compiling with -pedantic-errors on GCC.) |
| 7406 | ** |
| 7407 | ** The FINDER parameter to this macro is the name of the pointer to the |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7408 | ** finder-function. The finder-function returns a pointer to the |
| 7409 | ** sqlite_io_methods object that implements the desired locking |
| 7410 | ** behaviors. See the division above that contains the IOMETHODS |
| 7411 | ** macro for addition information on finder-functions. |
| 7412 | ** |
| 7413 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 7414 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 7415 | ** more than that; it looks at the filesystem type that hosts the |
| 7416 | ** database file and tries to choose an locking method appropriate for |
| 7417 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7418 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7419 | #define UNIXVFS(VFSNAME, FINDER) { \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7420 | 3, /* iVersion */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7421 | sizeof(unixFile), /* szOsFile */ \ |
| 7422 | MAX_PATHNAME, /* mxPathname */ \ |
| 7423 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7424 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7425 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7426 | unixOpen, /* xOpen */ \ |
| 7427 | unixDelete, /* xDelete */ \ |
| 7428 | unixAccess, /* xAccess */ \ |
| 7429 | unixFullPathname, /* xFullPathname */ \ |
| 7430 | unixDlOpen, /* xDlOpen */ \ |
| 7431 | unixDlError, /* xDlError */ \ |
| 7432 | unixDlSym, /* xDlSym */ \ |
| 7433 | unixDlClose, /* xDlClose */ \ |
| 7434 | unixRandomness, /* xRandomness */ \ |
| 7435 | unixSleep, /* xSleep */ \ |
| 7436 | unixCurrentTime, /* xCurrentTime */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 7437 | unixGetLastError, /* xGetLastError */ \ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 7438 | unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7439 | unixSetSystemCall, /* xSetSystemCall */ \ |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 7440 | unixGetSystemCall, /* xGetSystemCall */ \ |
| 7441 | unixNextSystemCall, /* xNextSystemCall */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7442 | } |
| 7443 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7444 | /* |
| 7445 | ** All default VFSes for unix are contained in the following array. |
| 7446 | ** |
| 7447 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 7448 | ** by the SQLite core when the VFS is registered. So the following |
| 7449 | ** array cannot be const. |
| 7450 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7451 | static sqlite3_vfs aVfs[] = { |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7452 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7453 | UNIXVFS("unix", autolockIoFinder ), |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7454 | #elif OS_VXWORKS |
| 7455 | UNIXVFS("unix", vxworksIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7456 | #else |
| 7457 | UNIXVFS("unix", posixIoFinder ), |
| 7458 | #endif |
| 7459 | UNIXVFS("unix-none", nolockIoFinder ), |
| 7460 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7461 | UNIXVFS("unix-excl", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7462 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7463 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7464 | #endif |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7465 | #if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7466 | UNIXVFS("unix-posix", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7467 | #endif |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7468 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 7469 | UNIXVFS("unix-flock", flockIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 7470 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7471 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7472 | UNIXVFS("unix-afp", afpIoFinder ), |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7473 | UNIXVFS("unix-nfs", nfsIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7474 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7475 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7476 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7477 | unsigned int i; /* Loop counter */ |
| 7478 | |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 7479 | /* Double-check that the aSyscall[] array has been constructed |
| 7480 | ** correctly. See ticket [bb3a86e890c8e96ab] */ |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 7481 | assert( ArraySize(aSyscall)==27 ); |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 7482 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7483 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7484 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7485 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7486 | } |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7487 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7488 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7489 | |
| 7490 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7491 | ** Shutdown the operating system interface. |
| 7492 | ** |
| 7493 | ** Some operating systems might need to do some cleanup in this routine, |
| 7494 | ** to release dynamically allocated objects. But not on unix. |
| 7495 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7496 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7497 | int sqlite3_os_end(void){ |
| 7498 | return SQLITE_OK; |
| 7499 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 7500 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 7501 | #endif /* SQLITE_OS_UNIX */ |