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 | e32a256 | 2016-03-04 02:38:00 +0000 | [diff] [blame] | 74 | /* Use pread() and pwrite() if they are available */ |
drh | 79a2ca3 | 2016-03-04 03:14:39 +0000 | [diff] [blame] | 75 | #if defined(__APPLE__) |
| 76 | # define HAVE_PREAD 1 |
| 77 | # define HAVE_PWRITE 1 |
| 78 | #endif |
drh | e32a256 | 2016-03-04 02:38:00 +0000 | [diff] [blame] | 79 | #if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64) |
| 80 | # undef USE_PREAD |
drh | e32a256 | 2016-03-04 02:38:00 +0000 | [diff] [blame] | 81 | # define USE_PREAD64 1 |
drh | e32a256 | 2016-03-04 02:38:00 +0000 | [diff] [blame] | 82 | #elif defined(HAVE_PREAD) && defined(HAVE_PWRITE) |
drh | 79a2ca3 | 2016-03-04 03:14:39 +0000 | [diff] [blame] | 83 | # undef USE_PREAD64 |
| 84 | # define USE_PREAD 1 |
drh | e32a256 | 2016-03-04 02:38:00 +0000 | [diff] [blame] | 85 | #endif |
| 86 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 87 | /* |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 88 | ** standard include files. |
| 89 | */ |
| 90 | #include <sys/types.h> |
| 91 | #include <sys/stat.h> |
| 92 | #include <fcntl.h> |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 93 | #include <sys/ioctl.h> |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 94 | #include <unistd.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 95 | #include <time.h> |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 96 | #include <sys/time.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 97 | #include <errno.h> |
dan | 32c12fe | 2013-05-02 17:37:31 +0000 | [diff] [blame] | 98 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | 91be7dc | 2014-08-11 13:53:30 +0000 | [diff] [blame] | 99 | # include <sys/mman.h> |
drh | b469f46 | 2010-12-22 21:48:50 +0000 | [diff] [blame] | 100 | #endif |
drh | 1da88f0 | 2011-12-17 16:09:16 +0000 | [diff] [blame] | 101 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 102 | #if SQLITE_ENABLE_LOCKING_STYLE |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 103 | # include <sys/ioctl.h> |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 104 | # include <sys/file.h> |
| 105 | # include <sys/param.h> |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 106 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 107 | |
drh | e4079e1 | 2019-09-27 16:33:27 +0000 | [diff] [blame] | 108 | /* |
| 109 | ** Try to determine if gethostuuid() is available based on standard |
| 110 | ** macros. This might sometimes compute the wrong value for some |
| 111 | ** obscure platforms. For those cases, simply compile with one of |
| 112 | ** the following: |
| 113 | ** |
| 114 | ** -DHAVE_GETHOSTUUID=0 |
| 115 | ** -DHAVE_GETHOSTUUID=1 |
| 116 | ** |
| 117 | ** None if this matters except when building on Apple products with |
| 118 | ** -DSQLITE_ENABLE_LOCKING_STYLE. |
| 119 | */ |
| 120 | #ifndef HAVE_GETHOSTUUID |
| 121 | # define HAVE_GETHOSTUUID 0 |
| 122 | # if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \ |
| 123 | (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000)) |
| 124 | # if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \ |
| 125 | && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0)) |
| 126 | # undef HAVE_GETHOSTUUID |
| 127 | # define HAVE_GETHOSTUUID 1 |
| 128 | # else |
| 129 | # warning "gethostuuid() is disabled." |
| 130 | # endif |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 131 | # endif |
| 132 | #endif |
| 133 | |
| 134 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 135 | #if OS_VXWORKS |
| 136 | # include <sys/ioctl.h> |
| 137 | # include <semaphore.h> |
| 138 | # include <limits.h> |
| 139 | #endif /* OS_VXWORKS */ |
| 140 | |
| 141 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 84a2bf6 | 2010-03-05 13:41:06 +0000 | [diff] [blame] | 142 | # include <sys/mount.h> |
| 143 | #endif |
| 144 | |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 145 | #ifdef HAVE_UTIME |
| 146 | # include <utime.h> |
| 147 | #endif |
| 148 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 149 | /* |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 150 | ** Allowed values of unixFile.fsFlags |
| 151 | */ |
| 152 | #define SQLITE_FSFLAGS_IS_MSDOS 0x1 |
| 153 | |
| 154 | /* |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 155 | ** If we are to be thread-safe, include the pthreads header. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 156 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 157 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 158 | # include <pthread.h> |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 159 | #endif |
| 160 | |
| 161 | /* |
| 162 | ** Default permissions when creating a new file |
| 163 | */ |
| 164 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 165 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 166 | #endif |
| 167 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 168 | /* |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 169 | ** Default permissions when creating auto proxy dir |
| 170 | */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 171 | #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 172 | # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 |
| 173 | #endif |
| 174 | |
| 175 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 176 | ** Maximum supported path-length. |
| 177 | */ |
| 178 | #define MAX_PATHNAME 512 |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 179 | |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 180 | /* |
| 181 | ** Maximum supported symbolic links |
| 182 | */ |
| 183 | #define SQLITE_MAX_SYMLINKS 100 |
| 184 | |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 185 | /* Always cast the getpid() return type for compatibility with |
| 186 | ** kernel modules in VxWorks. */ |
| 187 | #define osGetpid(X) (pid_t)getpid() |
| 188 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 189 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 190 | ** Only set the lastErrno if the error code is a real error and not |
| 191 | ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 192 | */ |
| 193 | #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |
| 194 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 195 | /* Forward references */ |
| 196 | typedef struct unixShm unixShm; /* Connection shared memory */ |
| 197 | typedef struct unixShmNode unixShmNode; /* Shared memory instance */ |
| 198 | typedef struct unixInodeInfo unixInodeInfo; /* An i-node */ |
| 199 | typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 200 | |
| 201 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 202 | ** Sometimes, after a file handle is closed by SQLite, the file descriptor |
| 203 | ** cannot be closed immediately. In these cases, instances of the following |
| 204 | ** structure are used to store the file descriptor while waiting for an |
| 205 | ** opportunity to either close or reuse it. |
| 206 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 207 | struct UnixUnusedFd { |
| 208 | int fd; /* File descriptor to close */ |
| 209 | int flags; /* Flags this file descriptor was opened with */ |
| 210 | UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ |
| 211 | }; |
| 212 | |
| 213 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 214 | ** The unixFile structure is subclass of sqlite3_file specific to the unix |
| 215 | ** VFS implementations. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 216 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 217 | typedef struct unixFile unixFile; |
| 218 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 219 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 220 | sqlite3_vfs *pVfs; /* The VFS that created this unixFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 221 | unixInodeInfo *pInode; /* Info about locks on this inode */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 222 | int h; /* The file descriptor */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 223 | unsigned char eFileLock; /* The type of lock held on this fd */ |
drh | 3ee3484 | 2012-02-11 21:21:17 +0000 | [diff] [blame] | 224 | unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 225 | int lastErrno; /* The unix errno from last I/O error */ |
| 226 | void *lockingContext; /* Locking style specific state */ |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 227 | UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 228 | const char *zPath; /* Name of the file */ |
| 229 | unixShm *pShm; /* Shared memory segment information */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 230 | int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 231 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 232 | int nFetchOut; /* Number of outstanding xFetch refs */ |
| 233 | sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 234 | sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */ |
| 235 | sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 236 | void *pMapRegion; /* Memory mapped region */ |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 237 | #endif |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 238 | int sectorSize; /* Device sector size */ |
| 239 | int deviceCharacteristics; /* Precomputed device characteristics */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 240 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 241 | int openFlags; /* The flags specified at open() */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 242 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 243 | #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 244 | unsigned fsFlags; /* cached details from statfs() */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 245 | #endif |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 246 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 247 | unsigned iBusyTimeout; /* Wait this many millisec on locks */ |
| 248 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 249 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 250 | struct vxworksFileId *pId; /* Unique file ID */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 251 | #endif |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 252 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 253 | /* The next group of variables are used to track whether or not the |
| 254 | ** transaction counter in bytes 24-27 of database files are updated |
| 255 | ** whenever any part of the database changes. An assertion fault will |
| 256 | ** occur if a file is updated without also updating the transaction |
| 257 | ** counter. This test is made to avoid new problems similar to the |
| 258 | ** one described by ticket #3584. |
| 259 | */ |
| 260 | unsigned char transCntrChng; /* True if the transaction counter changed */ |
| 261 | unsigned char dbUpdate; /* True if any part of database file changed */ |
| 262 | unsigned char inNormalWrite; /* True if in a normal write operation */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 263 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 264 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 265 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 266 | #ifdef SQLITE_TEST |
| 267 | /* In test mode, increase the size of this structure a bit so that |
| 268 | ** it is larger than the struct CrashFile defined in test6.c. |
| 269 | */ |
| 270 | char aPadding[32]; |
| 271 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 272 | }; |
| 273 | |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 274 | /* This variable holds the process id (pid) from when the xRandomness() |
| 275 | ** method was called. If xOpen() is called from a different process id, |
| 276 | ** indicating that a fork() has occurred, the PRNG will be reset. |
| 277 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 278 | static pid_t randomnessPid = 0; |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 279 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 280 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 281 | ** Allowed values for the unixFile.ctrlFlags bitmask: |
| 282 | */ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 283 | #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ |
| 284 | #define UNIXFILE_RDONLY 0x02 /* Connection is read only */ |
| 285 | #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ |
dan | ee140c4 | 2011-08-25 13:46:32 +0000 | [diff] [blame] | 286 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 287 | # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ |
| 288 | #else |
| 289 | # define UNIXFILE_DIRSYNC 0x00 |
| 290 | #endif |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 291 | #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 292 | #define UNIXFILE_DELETE 0x20 /* Delete on close */ |
| 293 | #define UNIXFILE_URI 0x40 /* Filename might have query parameters */ |
| 294 | #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 295 | |
| 296 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 297 | ** Include code that is common to all os_*.c files |
| 298 | */ |
| 299 | #include "os_common.h" |
| 300 | |
| 301 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 302 | ** Define various macros that are missing from some systems. |
| 303 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 304 | #ifndef O_LARGEFILE |
| 305 | # define O_LARGEFILE 0 |
| 306 | #endif |
| 307 | #ifdef SQLITE_DISABLE_LFS |
| 308 | # undef O_LARGEFILE |
| 309 | # define O_LARGEFILE 0 |
| 310 | #endif |
| 311 | #ifndef O_NOFOLLOW |
| 312 | # define O_NOFOLLOW 0 |
| 313 | #endif |
| 314 | #ifndef O_BINARY |
| 315 | # define O_BINARY 0 |
| 316 | #endif |
| 317 | |
| 318 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 319 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 320 | ** testing and debugging only. |
| 321 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 322 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 323 | #define threadid pthread_self() |
| 324 | #else |
| 325 | #define threadid 0 |
| 326 | #endif |
| 327 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 328 | /* |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 329 | ** HAVE_MREMAP defaults to true on Linux and false everywhere else. |
| 330 | */ |
| 331 | #if !defined(HAVE_MREMAP) |
| 332 | # if defined(__linux__) && defined(_GNU_SOURCE) |
| 333 | # define HAVE_MREMAP 1 |
| 334 | # else |
| 335 | # define HAVE_MREMAP 0 |
| 336 | # endif |
| 337 | #endif |
| 338 | |
| 339 | /* |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 340 | ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek() |
| 341 | ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined. |
| 342 | */ |
| 343 | #ifdef __ANDROID__ |
| 344 | # define lseek lseek64 |
| 345 | #endif |
| 346 | |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 347 | #ifdef __linux__ |
| 348 | /* |
| 349 | ** Linux-specific IOCTL magic numbers used for controlling F2FS |
| 350 | */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 351 | #define F2FS_IOCTL_MAGIC 0xf5 |
| 352 | #define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1) |
| 353 | #define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2) |
| 354 | #define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3) |
| 355 | #define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5) |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame] | 356 | #define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, u32) |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame] | 357 | #define F2FS_FEATURE_ATOMIC_WRITE 0x0004 |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 358 | #endif /* __linux__ */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 359 | |
| 360 | |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 361 | /* |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 362 | ** Different Unix systems declare open() in different ways. Same use |
| 363 | ** open(const char*,int,mode_t). Others use open(const char*,int,...). |
| 364 | ** The difference is important when using a pointer to the function. |
| 365 | ** |
| 366 | ** The safest way to deal with the problem is to always use this wrapper |
| 367 | ** which always has the same well-defined interface. |
| 368 | */ |
| 369 | static int posixOpen(const char *zFile, int flags, int mode){ |
| 370 | return open(zFile, flags, mode); |
| 371 | } |
| 372 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 373 | /* Forward reference */ |
| 374 | static int openDirectory(const char*, int*); |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 375 | static int unixGetpagesize(void); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 376 | |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 377 | /* |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 378 | ** Many system calls are accessed through pointer-to-functions so that |
| 379 | ** they may be overridden at runtime to facilitate fault injection during |
| 380 | ** testing and sandboxing. The following array holds the names and pointers |
| 381 | ** to all overrideable system calls. |
| 382 | */ |
| 383 | static struct unix_syscall { |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 384 | const char *zName; /* Name of the system call */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 385 | sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ |
| 386 | sqlite3_syscall_ptr pDefault; /* Default value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 387 | } aSyscall[] = { |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 388 | { "open", (sqlite3_syscall_ptr)posixOpen, 0 }, |
| 389 | #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent) |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 390 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 391 | { "close", (sqlite3_syscall_ptr)close, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 392 | #define osClose ((int(*)(int))aSyscall[1].pCurrent) |
| 393 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 394 | { "access", (sqlite3_syscall_ptr)access, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 395 | #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent) |
| 396 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 397 | { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 398 | #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent) |
| 399 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 400 | { "stat", (sqlite3_syscall_ptr)stat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 401 | #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) |
| 402 | |
| 403 | /* |
| 404 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 405 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 406 | ** that always succeeds. This means that locking does not occur under |
| 407 | ** DJGPP. But it is DOS - what did you expect? |
| 408 | */ |
| 409 | #ifdef __DJGPP__ |
| 410 | { "fstat", 0, 0 }, |
| 411 | #define osFstat(a,b,c) 0 |
| 412 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 413 | { "fstat", (sqlite3_syscall_ptr)fstat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 414 | #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent) |
| 415 | #endif |
| 416 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 417 | { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 418 | #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent) |
| 419 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 420 | { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 421 | #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 422 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 423 | { "read", (sqlite3_syscall_ptr)read, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 424 | #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) |
| 425 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 426 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 427 | { "pread", (sqlite3_syscall_ptr)pread, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 428 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 429 | { "pread", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 430 | #endif |
| 431 | #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) |
| 432 | |
| 433 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 434 | { "pread64", (sqlite3_syscall_ptr)pread64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 435 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 436 | { "pread64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 437 | #endif |
drh | f9986d9 | 2016-04-18 13:09:55 +0000 | [diff] [blame] | 438 | #define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 439 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 440 | { "write", (sqlite3_syscall_ptr)write, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 441 | #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) |
| 442 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 443 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 444 | { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 445 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 446 | { "pwrite", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 447 | #endif |
| 448 | #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 449 | aSyscall[12].pCurrent) |
| 450 | |
| 451 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 452 | { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 453 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 454 | { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 455 | #endif |
drh | f9986d9 | 2016-04-18 13:09:55 +0000 | [diff] [blame] | 456 | #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off64_t))\ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 457 | aSyscall[13].pCurrent) |
| 458 | |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 459 | { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 460 | #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 461 | |
| 462 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 463 | { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 464 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 465 | { "fallocate", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 466 | #endif |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 467 | #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 468 | |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 469 | { "unlink", (sqlite3_syscall_ptr)unlink, 0 }, |
| 470 | #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent) |
| 471 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 472 | { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 }, |
| 473 | #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent) |
| 474 | |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 475 | { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 }, |
| 476 | #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent) |
| 477 | |
| 478 | { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 }, |
| 479 | #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent) |
| 480 | |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 481 | #if defined(HAVE_FCHOWN) |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 482 | { "fchown", (sqlite3_syscall_ptr)fchown, 0 }, |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 483 | #else |
| 484 | { "fchown", (sqlite3_syscall_ptr)0, 0 }, |
| 485 | #endif |
dan | d3eaebd | 2012-02-13 08:50:23 +0000 | [diff] [blame] | 486 | #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent) |
drh | 23c4b97 | 2012-02-11 23:55:15 +0000 | [diff] [blame] | 487 | |
drh | 26f625f | 2018-02-19 16:34:31 +0000 | [diff] [blame] | 488 | #if defined(HAVE_FCHOWN) |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 489 | { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 }, |
drh | 26f625f | 2018-02-19 16:34:31 +0000 | [diff] [blame] | 490 | #else |
| 491 | { "geteuid", (sqlite3_syscall_ptr)0, 0 }, |
| 492 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 493 | #define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent) |
| 494 | |
dan | 4dd5144 | 2013-08-26 14:30:25 +0000 | [diff] [blame] | 495 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 496 | { "mmap", (sqlite3_syscall_ptr)mmap, 0 }, |
| 497 | #else |
| 498 | { "mmap", (sqlite3_syscall_ptr)0, 0 }, |
| 499 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 500 | #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] | 501 | |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 502 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 503 | { "munmap", (sqlite3_syscall_ptr)munmap, 0 }, |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 504 | #else |
drh | a829992 | 2016-01-08 22:31:00 +0000 | [diff] [blame] | 505 | { "munmap", (sqlite3_syscall_ptr)0, 0 }, |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 506 | #endif |
drh | 62be1fa | 2017-12-09 01:02:33 +0000 | [diff] [blame] | 507 | #define osMunmap ((int(*)(void*,size_t))aSyscall[23].pCurrent) |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 508 | |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 509 | #if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 510 | { "mremap", (sqlite3_syscall_ptr)mremap, 0 }, |
| 511 | #else |
| 512 | { "mremap", (sqlite3_syscall_ptr)0, 0 }, |
| 513 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 514 | #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent) |
| 515 | |
drh | 24dbeae | 2016-01-08 22:18:00 +0000 | [diff] [blame] | 516 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 517 | { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 }, |
drh | 24dbeae | 2016-01-08 22:18:00 +0000 | [diff] [blame] | 518 | #else |
| 519 | { "getpagesize", (sqlite3_syscall_ptr)0, 0 }, |
| 520 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 521 | #define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent) |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 522 | |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 523 | #if defined(HAVE_READLINK) |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 524 | { "readlink", (sqlite3_syscall_ptr)readlink, 0 }, |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 525 | #else |
| 526 | { "readlink", (sqlite3_syscall_ptr)0, 0 }, |
| 527 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 528 | #define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent) |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 529 | |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 530 | #if defined(HAVE_LSTAT) |
| 531 | { "lstat", (sqlite3_syscall_ptr)lstat, 0 }, |
| 532 | #else |
| 533 | { "lstat", (sqlite3_syscall_ptr)0, 0 }, |
| 534 | #endif |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 535 | #define osLstat ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent) |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 536 | |
drh | b5d013e | 2017-10-25 16:14:12 +0000 | [diff] [blame] | 537 | #if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) |
dan | 16f39b6 | 2018-09-18 19:40:18 +0000 | [diff] [blame] | 538 | # ifdef __ANDROID__ |
| 539 | { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 }, |
dan | ec9b2a1 | 2019-07-15 07:58:28 +0000 | [diff] [blame] | 540 | #define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent) |
dan | 16f39b6 | 2018-09-18 19:40:18 +0000 | [diff] [blame] | 541 | # else |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 542 | { "ioctl", (sqlite3_syscall_ptr)ioctl, 0 }, |
dan | ec9b2a1 | 2019-07-15 07:58:28 +0000 | [diff] [blame] | 543 | #define osIoctl ((int(*)(int,unsigned long,...))aSyscall[28].pCurrent) |
dan | 16f39b6 | 2018-09-18 19:40:18 +0000 | [diff] [blame] | 544 | # endif |
drh | b5d013e | 2017-10-25 16:14:12 +0000 | [diff] [blame] | 545 | #else |
| 546 | { "ioctl", (sqlite3_syscall_ptr)0, 0 }, |
| 547 | #endif |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 548 | |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 549 | }; /* End of the overrideable system calls */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 550 | |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 551 | |
| 552 | /* |
| 553 | ** On some systems, calls to fchown() will trigger a message in a security |
| 554 | ** log if they come from non-root processes. So avoid calling fchown() if |
| 555 | ** we are not running as root. |
| 556 | */ |
| 557 | static int robustFchown(int fd, uid_t uid, gid_t gid){ |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 558 | #if defined(HAVE_FCHOWN) |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 559 | return osGeteuid() ? 0 : osFchown(fd,uid,gid); |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 560 | #else |
| 561 | return 0; |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 562 | #endif |
| 563 | } |
| 564 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 565 | /* |
| 566 | ** This is the xSetSystemCall() method of sqlite3_vfs for all of the |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 567 | ** "unix" VFSes. Return SQLITE_OK opon successfully updating the |
| 568 | ** system call pointer, or SQLITE_NOTFOUND if there is no configurable |
| 569 | ** system call named zName. |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 570 | */ |
| 571 | static int unixSetSystemCall( |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 572 | sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ |
| 573 | const char *zName, /* Name of system call to override */ |
| 574 | sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 575 | ){ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 576 | unsigned int i; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 577 | int rc = SQLITE_NOTFOUND; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 578 | |
| 579 | UNUSED_PARAMETER(pNotUsed); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 580 | if( zName==0 ){ |
| 581 | /* If no zName is given, restore all system calls to their default |
| 582 | ** settings and return NULL |
| 583 | */ |
dan | 51438a7 | 2011-04-02 17:00:47 +0000 | [diff] [blame] | 584 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 585 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 586 | if( aSyscall[i].pDefault ){ |
| 587 | aSyscall[i].pCurrent = aSyscall[i].pDefault; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | }else{ |
| 591 | /* If zName is specified, operate on only the one system call |
| 592 | ** specified. |
| 593 | */ |
| 594 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 595 | if( strcmp(zName, aSyscall[i].zName)==0 ){ |
| 596 | if( aSyscall[i].pDefault==0 ){ |
| 597 | aSyscall[i].pDefault = aSyscall[i].pCurrent; |
| 598 | } |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 599 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 600 | if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault; |
| 601 | aSyscall[i].pCurrent = pNewFunc; |
| 602 | break; |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | return rc; |
| 607 | } |
| 608 | |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 609 | /* |
| 610 | ** Return the value of a system call. Return NULL if zName is not a |
| 611 | ** recognized system call name. NULL is also returned if the system call |
| 612 | ** is currently undefined. |
| 613 | */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 614 | static sqlite3_syscall_ptr unixGetSystemCall( |
| 615 | sqlite3_vfs *pNotUsed, |
| 616 | const char *zName |
| 617 | ){ |
| 618 | unsigned int i; |
| 619 | |
| 620 | UNUSED_PARAMETER(pNotUsed); |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 621 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 622 | if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent; |
| 623 | } |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | ** Return the name of the first system call after zName. If zName==NULL |
| 629 | ** then return the name of the first system call. Return NULL if zName |
| 630 | ** is the last system call or if zName is not the name of a valid |
| 631 | ** system call. |
| 632 | */ |
| 633 | static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){ |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 634 | int i = -1; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 635 | |
| 636 | UNUSED_PARAMETER(p); |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 637 | if( zName ){ |
| 638 | for(i=0; i<ArraySize(aSyscall)-1; i++){ |
| 639 | if( strcmp(zName, aSyscall[i].zName)==0 ) break; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 640 | } |
| 641 | } |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 642 | for(i++; i<ArraySize(aSyscall); i++){ |
| 643 | if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 644 | } |
| 645 | return 0; |
| 646 | } |
| 647 | |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 648 | /* |
drh | 77a3fdc | 2013-08-30 14:24:12 +0000 | [diff] [blame] | 649 | ** Do not accept any file descriptor less than this value, in order to avoid |
| 650 | ** opening database file using file descriptors that are commonly used for |
| 651 | ** standard input, output, and error. |
| 652 | */ |
| 653 | #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR |
| 654 | # define SQLITE_MINIMUM_FILE_DESCRIPTOR 3 |
| 655 | #endif |
| 656 | |
| 657 | /* |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 658 | ** Invoke open(). Do so multiple times, until it either succeeds or |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 659 | ** fails for some reason other than EINTR. |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 660 | ** |
| 661 | ** If the file creation mode "m" is 0 then set it to the default for |
| 662 | ** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally |
| 663 | ** 0644) as modified by the system umask. If m is not 0, then |
| 664 | ** make the file creation mode be exactly m ignoring the umask. |
| 665 | ** |
| 666 | ** The m parameter will be non-zero only when creating -wal, -journal, |
| 667 | ** and -shm files. We want those files to have *exactly* the same |
| 668 | ** permissions as their original database, unadulterated by the umask. |
| 669 | ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a |
| 670 | ** transaction crashes and leaves behind hot journals, then any |
| 671 | ** process that is able to write to the database will also be able to |
| 672 | ** recover the hot journals. |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 673 | */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 674 | static int robust_open(const char *z, int f, mode_t m){ |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 675 | int fd; |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 676 | mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS; |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 677 | while(1){ |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 678 | #if defined(O_CLOEXEC) |
| 679 | fd = osOpen(z,f|O_CLOEXEC,m2); |
| 680 | #else |
| 681 | fd = osOpen(z,f,m2); |
| 682 | #endif |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 683 | if( fd<0 ){ |
| 684 | if( errno==EINTR ) continue; |
| 685 | break; |
| 686 | } |
drh | 77a3fdc | 2013-08-30 14:24:12 +0000 | [diff] [blame] | 687 | if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break; |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 688 | osClose(fd); |
| 689 | sqlite3_log(SQLITE_WARNING, |
| 690 | "attempt to open \"%s\" as file descriptor %d", z, fd); |
| 691 | fd = -1; |
| 692 | if( osOpen("/dev/null", f, m)<0 ) break; |
| 693 | } |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 694 | if( fd>=0 ){ |
| 695 | if( m!=0 ){ |
| 696 | struct stat statbuf; |
dan | b83c21e | 2013-03-05 15:27:34 +0000 | [diff] [blame] | 697 | if( osFstat(fd, &statbuf)==0 |
| 698 | && statbuf.st_size==0 |
drh | cfc1769 | 2013-03-06 01:41:53 +0000 | [diff] [blame] | 699 | && (statbuf.st_mode&0777)!=m |
dan | b83c21e | 2013-03-05 15:27:34 +0000 | [diff] [blame] | 700 | ){ |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 701 | osFchmod(fd, m); |
| 702 | } |
| 703 | } |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 704 | #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0) |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 705 | osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 706 | #endif |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 707 | } |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 708 | return fd; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 709 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 710 | |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 711 | /* |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 712 | ** Helper functions to obtain and relinquish the global mutex. The |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 713 | ** global mutex is used to protect the unixInodeInfo and |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 714 | ** vxworksFileId objects used by this file, all of which may be |
| 715 | ** shared by multiple threads. |
| 716 | ** |
| 717 | ** Function unixMutexHeld() is used to assert() that the global mutex |
| 718 | ** is held when required. This function is only used as part of assert() |
| 719 | ** statements. e.g. |
| 720 | ** |
| 721 | ** unixEnterMutex() |
| 722 | ** assert( unixMutexHeld() ); |
| 723 | ** unixEnterLeave() |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 724 | ** |
| 725 | ** To prevent deadlock, the global unixBigLock must must be acquired |
| 726 | ** before the unixInodeInfo.pLockMutex mutex, if both are held. It is |
| 727 | ** OK to get the pLockMutex without holding unixBigLock first, but if |
| 728 | ** that happens, the unixBigLock mutex must not be acquired until after |
| 729 | ** pLockMutex is released. |
| 730 | ** |
| 731 | ** OK: enter(unixBigLock), enter(pLockInfo) |
| 732 | ** OK: enter(unixBigLock) |
| 733 | ** OK: enter(pLockInfo) |
| 734 | ** ERROR: enter(pLockInfo), enter(unixBigLock) |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 735 | */ |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame] | 736 | static sqlite3_mutex *unixBigLock = 0; |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 737 | static void unixEnterMutex(void){ |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 738 | assert( sqlite3_mutex_notheld(unixBigLock) ); /* Not a recursive mutex */ |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame] | 739 | sqlite3_mutex_enter(unixBigLock); |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 740 | } |
| 741 | static void unixLeaveMutex(void){ |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 742 | assert( sqlite3_mutex_held(unixBigLock) ); |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame] | 743 | sqlite3_mutex_leave(unixBigLock); |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 744 | } |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 745 | #ifdef SQLITE_DEBUG |
| 746 | static int unixMutexHeld(void) { |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame] | 747 | return sqlite3_mutex_held(unixBigLock); |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 748 | } |
| 749 | #endif |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 750 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 751 | |
mistachkin | fb383e9 | 2015-04-16 03:24:38 +0000 | [diff] [blame] | 752 | #ifdef SQLITE_HAVE_OS_TRACE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 753 | /* |
| 754 | ** Helper function for printing out trace information from debugging |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 755 | ** binaries. This returns the string representation of the supplied |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 756 | ** integer lock-type. |
| 757 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 758 | static const char *azFileLock(int eFileLock){ |
| 759 | switch( eFileLock ){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 760 | case NO_LOCK: return "NONE"; |
| 761 | case SHARED_LOCK: return "SHARED"; |
| 762 | case RESERVED_LOCK: return "RESERVED"; |
| 763 | case PENDING_LOCK: return "PENDING"; |
| 764 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 765 | } |
| 766 | return "ERROR"; |
| 767 | } |
| 768 | #endif |
| 769 | |
| 770 | #ifdef SQLITE_LOCK_TRACE |
| 771 | /* |
| 772 | ** Print out information about all locking operations. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 773 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 774 | ** This routine is used for troubleshooting locks on multithreaded |
| 775 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 776 | ** command-line option on the compiler. This code is normally |
| 777 | ** turned off. |
| 778 | */ |
| 779 | static int lockTrace(int fd, int op, struct flock *p){ |
| 780 | char *zOpName, *zType; |
| 781 | int s; |
| 782 | int savedErrno; |
| 783 | if( op==F_GETLK ){ |
| 784 | zOpName = "GETLK"; |
| 785 | }else if( op==F_SETLK ){ |
| 786 | zOpName = "SETLK"; |
| 787 | }else{ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 788 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 789 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 790 | return s; |
| 791 | } |
| 792 | if( p->l_type==F_RDLCK ){ |
| 793 | zType = "RDLCK"; |
| 794 | }else if( p->l_type==F_WRLCK ){ |
| 795 | zType = "WRLCK"; |
| 796 | }else if( p->l_type==F_UNLCK ){ |
| 797 | zType = "UNLCK"; |
| 798 | }else{ |
| 799 | assert( 0 ); |
| 800 | } |
| 801 | assert( p->l_whence==SEEK_SET ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 802 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 803 | savedErrno = errno; |
| 804 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 805 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 806 | (int)p->l_pid, s); |
| 807 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 808 | struct flock l2; |
| 809 | l2 = *p; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 810 | osFcntl(fd, F_GETLK, &l2); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 811 | if( l2.l_type==F_RDLCK ){ |
| 812 | zType = "RDLCK"; |
| 813 | }else if( l2.l_type==F_WRLCK ){ |
| 814 | zType = "WRLCK"; |
| 815 | }else if( l2.l_type==F_UNLCK ){ |
| 816 | zType = "UNLCK"; |
| 817 | }else{ |
| 818 | assert( 0 ); |
| 819 | } |
| 820 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 821 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 822 | } |
| 823 | errno = savedErrno; |
| 824 | return s; |
| 825 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 826 | #undef osFcntl |
| 827 | #define osFcntl lockTrace |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 828 | #endif /* SQLITE_LOCK_TRACE */ |
| 829 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 830 | /* |
| 831 | ** Retry ftruncate() calls that fail due to EINTR |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 832 | ** |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 833 | ** All calls to ftruncate() within this file should be made through |
| 834 | ** this wrapper. On the Android platform, bypassing the logic below |
| 835 | ** could lead to a corrupt database. |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 836 | */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 837 | static int robust_ftruncate(int h, sqlite3_int64 sz){ |
| 838 | int rc; |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 839 | #ifdef __ANDROID__ |
| 840 | /* On Android, ftruncate() always uses 32-bit offsets, even if |
| 841 | ** _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] | 842 | ** truncate a file to any size larger than 2GiB. Silently ignore any |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 843 | ** such attempts. */ |
| 844 | if( sz>(sqlite3_int64)0x7FFFFFFF ){ |
| 845 | rc = SQLITE_OK; |
| 846 | }else |
| 847 | #endif |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 848 | do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR ); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 849 | return rc; |
| 850 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 851 | |
| 852 | /* |
| 853 | ** This routine translates a standard POSIX errno code into something |
| 854 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 855 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 856 | ** and a variety of "please close the file descriptor NOW" errors into |
| 857 | ** SQLITE_IOERR |
| 858 | ** |
| 859 | ** Errors during initialization of locks, or file system support for locks, |
| 860 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 861 | */ |
| 862 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
drh | 91c4def | 2015-11-25 14:00:07 +0000 | [diff] [blame] | 863 | assert( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
| 864 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 865 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 866 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 867 | switch (posixError) { |
drh | 91c4def | 2015-11-25 14:00:07 +0000 | [diff] [blame] | 868 | case EACCES: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 869 | case EAGAIN: |
| 870 | case ETIMEDOUT: |
| 871 | case EBUSY: |
| 872 | case EINTR: |
| 873 | case ENOLCK: |
| 874 | /* random NFS retry error, unless during file system support |
| 875 | * introspection, in which it actually means what it says */ |
| 876 | return SQLITE_BUSY; |
| 877 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 878 | case EPERM: |
| 879 | return SQLITE_PERM; |
| 880 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 881 | default: |
| 882 | return sqliteIOErr; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 887 | /****************************************************************************** |
| 888 | ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 889 | ** |
| 890 | ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 891 | ** the device number and the inode number. But this does not work on VxWorks. |
| 892 | ** On VxWorks, a unique file id must be based on the canonical filename. |
| 893 | ** |
| 894 | ** A pointer to an instance of the following structure can be used as a |
| 895 | ** unique file ID in VxWorks. Each instance of this structure contains |
| 896 | ** a copy of the canonical filename. There is also a reference count. |
| 897 | ** The structure is reclaimed when the number of pointers to it drops to |
| 898 | ** zero. |
| 899 | ** |
| 900 | ** There are never very many files open at one time and lookups are not |
| 901 | ** a performance-critical path, so it is sufficient to put these |
| 902 | ** structures on a linked list. |
| 903 | */ |
| 904 | struct vxworksFileId { |
| 905 | struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 906 | int nRef; /* Number of references to this one */ |
| 907 | int nName; /* Length of the zCanonicalName[] string */ |
| 908 | char *zCanonicalName; /* Canonical filename */ |
| 909 | }; |
| 910 | |
| 911 | #if OS_VXWORKS |
| 912 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 913 | ** All unique filenames are held on a linked list headed by this |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 914 | ** variable: |
| 915 | */ |
| 916 | static struct vxworksFileId *vxworksFileList = 0; |
| 917 | |
| 918 | /* |
| 919 | ** Simplify a filename into its canonical form |
| 920 | ** by making the following changes: |
| 921 | ** |
| 922 | ** * removing any trailing and duplicate / |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 923 | ** * convert /./ into just / |
| 924 | ** * convert /A/../ where A is any simple name into just / |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 925 | ** |
| 926 | ** Changes are made in-place. Return the new name length. |
| 927 | ** |
| 928 | ** The original filename is in z[0..n-1]. Return the number of |
| 929 | ** characters in the simplified name. |
| 930 | */ |
| 931 | static int vxworksSimplifyName(char *z, int n){ |
| 932 | int i, j; |
| 933 | while( n>1 && z[n-1]=='/' ){ n--; } |
| 934 | for(i=j=0; i<n; i++){ |
| 935 | if( z[i]=='/' ){ |
| 936 | if( z[i+1]=='/' ) continue; |
| 937 | if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 938 | i += 1; |
| 939 | continue; |
| 940 | } |
| 941 | if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 942 | while( j>0 && z[j-1]!='/' ){ j--; } |
| 943 | if( j>0 ){ j--; } |
| 944 | i += 2; |
| 945 | continue; |
| 946 | } |
| 947 | } |
| 948 | z[j++] = z[i]; |
| 949 | } |
| 950 | z[j] = 0; |
| 951 | return j; |
| 952 | } |
| 953 | |
| 954 | /* |
| 955 | ** Find a unique file ID for the given absolute pathname. Return |
| 956 | ** a pointer to the vxworksFileId object. This pointer is the unique |
| 957 | ** file ID. |
| 958 | ** |
| 959 | ** The nRef field of the vxworksFileId object is incremented before |
| 960 | ** the object is returned. A new vxworksFileId object is created |
| 961 | ** and added to the global list if necessary. |
| 962 | ** |
| 963 | ** If a memory allocation error occurs, return NULL. |
| 964 | */ |
| 965 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 966 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 967 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 968 | int n; /* Length of zAbsoluteName string */ |
| 969 | |
| 970 | assert( zAbsoluteName[0]=='/' ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 971 | n = (int)strlen(zAbsoluteName); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 972 | pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 973 | if( pNew==0 ) return 0; |
| 974 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 975 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 976 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 977 | |
| 978 | /* Search for an existing entry that matching the canonical name. |
| 979 | ** If found, increment the reference count and return a pointer to |
| 980 | ** the existing file ID. |
| 981 | */ |
| 982 | unixEnterMutex(); |
| 983 | for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 984 | if( pCandidate->nName==n |
| 985 | && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 986 | ){ |
| 987 | sqlite3_free(pNew); |
| 988 | pCandidate->nRef++; |
| 989 | unixLeaveMutex(); |
| 990 | return pCandidate; |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | /* No match was found. We will make a new file ID */ |
| 995 | pNew->nRef = 1; |
| 996 | pNew->nName = n; |
| 997 | pNew->pNext = vxworksFileList; |
| 998 | vxworksFileList = pNew; |
| 999 | unixLeaveMutex(); |
| 1000 | return pNew; |
| 1001 | } |
| 1002 | |
| 1003 | /* |
| 1004 | ** Decrement the reference count on a vxworksFileId object. Free |
| 1005 | ** the object when the reference count reaches zero. |
| 1006 | */ |
| 1007 | static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 1008 | unixEnterMutex(); |
| 1009 | assert( pId->nRef>0 ); |
| 1010 | pId->nRef--; |
| 1011 | if( pId->nRef==0 ){ |
| 1012 | struct vxworksFileId **pp; |
| 1013 | for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 1014 | assert( *pp==pId ); |
| 1015 | *pp = pId->pNext; |
| 1016 | sqlite3_free(pId); |
| 1017 | } |
| 1018 | unixLeaveMutex(); |
| 1019 | } |
| 1020 | #endif /* OS_VXWORKS */ |
| 1021 | /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 1022 | ******************************************************************************/ |
| 1023 | |
| 1024 | |
| 1025 | /****************************************************************************** |
| 1026 | *************************** Posix Advisory Locking **************************** |
| 1027 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1028 | ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1029 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 1030 | ** sets or clears a lock, that operation overrides any prior locks set |
| 1031 | ** by the same process. It does not explicitly say so, but this implies |
| 1032 | ** that it overrides locks set by the same process using a different |
| 1033 | ** file descriptor. Consider this test case: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1034 | ** |
| 1035 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1036 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 1037 | ** |
| 1038 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 1039 | ** one is a hard or symbolic link to the other) then if you set |
| 1040 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 1041 | ** on fd2, it works. I would have expected the second lock to |
| 1042 | ** fail since there was already a lock on the file due to fd1. |
| 1043 | ** But not so. Since both locks came from the same process, the |
| 1044 | ** second overrides the first, even though they were on different |
| 1045 | ** file descriptors opened on different file names. |
| 1046 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1047 | ** This means that we cannot use POSIX locks to synchronize file access |
| 1048 | ** among competing threads of the same process. POSIX locks will work fine |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1049 | ** to synchronize access for threads in separate processes, but not |
| 1050 | ** threads within the same process. |
| 1051 | ** |
| 1052 | ** To work around the problem, SQLite has to manage file locks internally |
| 1053 | ** on its own. Whenever a new database is opened, we have to find the |
| 1054 | ** specific inode of the database file (the inode is determined by the |
| 1055 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 1056 | ** and check for locks already existing on that inode. When locks are |
| 1057 | ** created or removed, we have to look at our own internal record of the |
| 1058 | ** locks to see if another thread has previously set a lock on that same |
| 1059 | ** inode. |
| 1060 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1061 | ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 1062 | ** For VxWorks, we have to use the alternative unique ID system based on |
| 1063 | ** canonical filename and implemented in the previous division.) |
| 1064 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1065 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1066 | ** descriptor. It is now a structure that holds the integer file |
| 1067 | ** descriptor and a pointer to a structure that describes the internal |
| 1068 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1069 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1070 | ** point to the same locking structure. The locking structure keeps |
| 1071 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 1072 | ** field that tells us its internal lock status. cnt==0 means the |
| 1073 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 1074 | ** cnt>0 means there are cnt shared locks on the file. |
| 1075 | ** |
| 1076 | ** Any attempt to lock or unlock a file first checks the locking |
| 1077 | ** structure. The fcntl() system call is only invoked to set a |
| 1078 | ** POSIX lock if the internal lock structure transitions between |
| 1079 | ** a locked and an unlocked state. |
| 1080 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1081 | ** But wait: there are yet more problems with POSIX advisory locks. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1082 | ** |
| 1083 | ** If you close a file descriptor that points to a file that has locks, |
| 1084 | ** all locks on that file that are owned by the current process are |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1085 | ** released. To work around this problem, each unixInodeInfo object |
| 1086 | ** maintains a count of the number of pending locks on tha inode. |
| 1087 | ** When an attempt is made to close an unixFile, if there are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1088 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1089 | ** to close() the file descriptor is deferred until all of the locks clear. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1090 | ** The unixInodeInfo structure keeps a list of file descriptors that need to |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1091 | ** be closed and that list is walked (and cleared) when the last lock |
| 1092 | ** clears. |
| 1093 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1094 | ** Yet another problem: LinuxThreads do not play well with posix locks. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1095 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1096 | ** Many older versions of linux use the LinuxThreads library which is |
| 1097 | ** not posix compliant. Under LinuxThreads, a lock created by thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1098 | ** A cannot be modified or overridden by a different thread B. |
| 1099 | ** Only thread A can modify the lock. Locking behavior is correct |
| 1100 | ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 1101 | ** on linux - with NPTL a lock created by thread A can override locks |
| 1102 | ** in thread B. But there is no way to know at compile-time which |
| 1103 | ** threading library is being used. So there is no way to know at |
| 1104 | ** compile-time whether or not thread A can override locks on thread B. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1105 | ** 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] | 1106 | ** current process. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1107 | ** |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1108 | ** SQLite used to support LinuxThreads. But support for LinuxThreads |
| 1109 | ** was dropped beginning with version 3.7.0. SQLite will still work with |
| 1110 | ** LinuxThreads provided that (1) there is no more than one connection |
| 1111 | ** per database file in the same process and (2) database connections |
| 1112 | ** do not move across threads. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1113 | */ |
| 1114 | |
| 1115 | /* |
| 1116 | ** An instance of the following structure serves as the key used |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1117 | ** to locate a particular unixInodeInfo object. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1118 | */ |
| 1119 | struct unixFileId { |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1120 | dev_t dev; /* Device number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1121 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1122 | struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1123 | #else |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 1124 | /* We are told that some versions of Android contain a bug that |
| 1125 | ** sizes ino_t at only 32-bits instead of 64-bits. (See |
| 1126 | ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c) |
| 1127 | ** To work around this, always allocate 64-bits for the inode number. |
| 1128 | ** On small machines that only have 32-bit inodes, this wastes 4 bytes, |
| 1129 | ** but that should not be a big deal. */ |
| 1130 | /* WAS: ino_t ino; */ |
| 1131 | u64 ino; /* Inode number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1132 | #endif |
| 1133 | }; |
| 1134 | |
| 1135 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1136 | ** An instance of the following structure is allocated for each open |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1137 | ** inode. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1138 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1139 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1140 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1141 | ** object keeps a count of the number of unixFile pointing to it. |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1142 | ** |
| 1143 | ** Mutex rules: |
| 1144 | ** |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 1145 | ** (1) Only the pLockMutex mutex must be held in order to read or write |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1146 | ** any of the locking fields: |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1147 | ** nShared, nLock, eFileLock, bProcessLock, pUnused |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1148 | ** |
| 1149 | ** (2) When nRef>0, then the following fields are unchanging and can |
| 1150 | ** be read (but not written) without holding any mutex: |
| 1151 | ** fileId, pLockMutex |
| 1152 | ** |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1153 | ** (3) With the exceptions above, all the fields may only be read |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1154 | ** or written while holding the global unixBigLock mutex. |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 1155 | ** |
| 1156 | ** Deadlock prevention: The global unixBigLock mutex may not |
| 1157 | ** be acquired while holding the pLockMutex mutex. If both unixBigLock |
| 1158 | ** and pLockMutex are needed, then unixBigLock must be acquired first. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1159 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1160 | struct unixInodeInfo { |
| 1161 | struct unixFileId fileId; /* The lookup key */ |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1162 | sqlite3_mutex *pLockMutex; /* Hold this mutex for... */ |
| 1163 | int nShared; /* Number of SHARED locks held */ |
| 1164 | int nLock; /* Number of outstanding file locks */ |
| 1165 | unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
| 1166 | unsigned char bProcessLock; /* An exclusive process lock is held */ |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1167 | UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1168 | int nRef; /* Number of pointers to this structure */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1169 | unixShmNode *pShmNode; /* Shared memory associated with this inode */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1170 | unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ |
| 1171 | unixInodeInfo *pPrev; /* .... doubly linked */ |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 1172 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1173 | unsigned long long sharedByte; /* for AFP simulated shared lock */ |
| 1174 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1175 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1176 | sem_t *pSem; /* Named POSIX semaphore */ |
| 1177 | char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1178 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1179 | }; |
| 1180 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1181 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1182 | ** A lists of all unixInodeInfo objects. |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1183 | ** |
| 1184 | ** Must hold unixBigLock in order to read or write this variable. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1185 | */ |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 1186 | static unixInodeInfo *inodeList = 0; /* All unixInodeInfo objects */ |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 1187 | |
| 1188 | #ifdef SQLITE_DEBUG |
| 1189 | /* |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1190 | ** True if the inode mutex (on the unixFile.pFileMutex field) is held, or not. |
| 1191 | ** This routine is used only within assert() to help verify correct mutex |
| 1192 | ** usage. |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 1193 | */ |
| 1194 | int unixFileMutexHeld(unixFile *pFile){ |
| 1195 | assert( pFile->pInode ); |
| 1196 | return sqlite3_mutex_held(pFile->pInode->pLockMutex); |
| 1197 | } |
| 1198 | int unixFileMutexNotheld(unixFile *pFile){ |
| 1199 | assert( pFile->pInode ); |
| 1200 | return sqlite3_mutex_notheld(pFile->pInode->pLockMutex); |
| 1201 | } |
| 1202 | #endif |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1203 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1204 | /* |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1205 | ** |
drh | aaeaa18 | 2015-11-24 15:12:47 +0000 | [diff] [blame] | 1206 | ** This function - unixLogErrorAtLine(), is only ever called via the macro |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1207 | ** unixLogError(). |
| 1208 | ** |
| 1209 | ** It is invoked after an error occurs in an OS function and errno has been |
| 1210 | ** set. It logs a message using sqlite3_log() containing the current value of |
| 1211 | ** errno and, if possible, the human-readable equivalent from strerror() or |
| 1212 | ** strerror_r(). |
| 1213 | ** |
| 1214 | ** The first argument passed to the macro should be the error code that |
| 1215 | ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). |
| 1216 | ** The two subsequent arguments should be the name of the OS function that |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 1217 | ** failed (e.g. "unlink", "open") and the associated file-system path, |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1218 | ** if any. |
| 1219 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1220 | #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__) |
| 1221 | static int unixLogErrorAtLine( |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1222 | int errcode, /* SQLite error code */ |
| 1223 | const char *zFunc, /* Name of OS function that failed */ |
| 1224 | const char *zPath, /* File path associated with error */ |
| 1225 | int iLine /* Source line number where error occurred */ |
| 1226 | ){ |
| 1227 | char *zErr; /* Message from strerror() or equivalent */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1228 | int iErrno = errno; /* Saved syscall error number */ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1229 | |
| 1230 | /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use |
| 1231 | ** the strerror() function to obtain the human-readable error message |
| 1232 | ** equivalent to errno. Otherwise, use strerror_r(). |
| 1233 | */ |
| 1234 | #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) |
| 1235 | char aErr[80]; |
| 1236 | memset(aErr, 0, sizeof(aErr)); |
| 1237 | zErr = aErr; |
| 1238 | |
| 1239 | /* 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] | 1240 | ** assume that the system provides the GNU version of strerror_r() that |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1241 | ** returns a pointer to a buffer containing the error message. That pointer |
| 1242 | ** may point to aErr[], or it may point to some static storage somewhere. |
| 1243 | ** Otherwise, assume that the system provides the POSIX version of |
| 1244 | ** strerror_r(), which always writes an error message into aErr[]. |
| 1245 | ** |
| 1246 | ** If the code incorrectly assumes that it is the POSIX version that is |
| 1247 | ** available, the error message will often be an empty string. Not a |
| 1248 | ** huge problem. Incorrectly concluding that the GNU version is available |
| 1249 | ** could lead to a segfault though. |
| 1250 | */ |
| 1251 | #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) |
| 1252 | zErr = |
| 1253 | # endif |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1254 | strerror_r(iErrno, aErr, sizeof(aErr)-1); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1255 | |
| 1256 | #elif SQLITE_THREADSAFE |
| 1257 | /* This is a threadsafe build, but strerror_r() is not available. */ |
| 1258 | zErr = ""; |
| 1259 | #else |
| 1260 | /* Non-threadsafe build, use strerror(). */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1261 | zErr = strerror(iErrno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1262 | #endif |
| 1263 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1264 | if( zPath==0 ) zPath = ""; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1265 | sqlite3_log(errcode, |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1266 | "os_unix.c:%d: (%d) %s(%s) - %s", |
| 1267 | iLine, iErrno, zFunc, zPath, zErr |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1268 | ); |
| 1269 | |
| 1270 | return errcode; |
| 1271 | } |
| 1272 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1273 | /* |
| 1274 | ** Close a file descriptor. |
| 1275 | ** |
| 1276 | ** We assume that close() almost always works, since it is only in a |
| 1277 | ** very sick application or on a very sick platform that it might fail. |
| 1278 | ** If it does fail, simply leak the file descriptor, but do log the |
| 1279 | ** error. |
| 1280 | ** |
| 1281 | ** Note that it is not safe to retry close() after EINTR since the |
| 1282 | ** file descriptor might have already been reused by another thread. |
| 1283 | ** So we don't even try to recover from an EINTR. Just log the error |
| 1284 | ** and move on. |
| 1285 | */ |
| 1286 | static void robust_close(unixFile *pFile, int h, int lineno){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1287 | if( osClose(h) ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1288 | unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", |
| 1289 | pFile ? pFile->zPath : 0, lineno); |
| 1290 | } |
| 1291 | } |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1292 | |
| 1293 | /* |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 1294 | ** Set the pFile->lastErrno. Do this in a subroutine as that provides |
| 1295 | ** a convenient place to set a breakpoint. |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1296 | */ |
| 1297 | static void storeLastErrno(unixFile *pFile, int error){ |
| 1298 | pFile->lastErrno = error; |
| 1299 | } |
| 1300 | |
| 1301 | /* |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1302 | ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1303 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1304 | static void closePendingFds(unixFile *pFile){ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1305 | unixInodeInfo *pInode = pFile->pInode; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1306 | UnixUnusedFd *p; |
| 1307 | UnixUnusedFd *pNext; |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1308 | assert( unixFileMutexHeld(pFile) ); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1309 | for(p=pInode->pUnused; p; p=pNext){ |
| 1310 | pNext = p->pNext; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1311 | robust_close(pFile, p->fd, __LINE__); |
| 1312 | sqlite3_free(p); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1313 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1314 | pInode->pUnused = 0; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1318 | ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1319 | ** |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1320 | ** The global mutex must be held when this routine is called, but the mutex |
| 1321 | ** on the inode being deleted must NOT be held. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1322 | */ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1323 | static void releaseInodeInfo(unixFile *pFile){ |
| 1324 | unixInodeInfo *pInode = pFile->pInode; |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1325 | assert( unixMutexHeld() ); |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 1326 | assert( unixFileMutexNotheld(pFile) ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1327 | if( ALWAYS(pInode) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1328 | pInode->nRef--; |
| 1329 | if( pInode->nRef==0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1330 | assert( pInode->pShmNode==0 ); |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1331 | sqlite3_mutex_enter(pInode->pLockMutex); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1332 | closePendingFds(pFile); |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1333 | sqlite3_mutex_leave(pInode->pLockMutex); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1334 | if( pInode->pPrev ){ |
| 1335 | assert( pInode->pPrev->pNext==pInode ); |
| 1336 | pInode->pPrev->pNext = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1337 | }else{ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1338 | assert( inodeList==pInode ); |
| 1339 | inodeList = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1340 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1341 | if( pInode->pNext ){ |
| 1342 | assert( pInode->pNext->pPrev==pInode ); |
| 1343 | pInode->pNext->pPrev = pInode->pPrev; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1344 | } |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1345 | sqlite3_mutex_free(pInode->pLockMutex); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1346 | sqlite3_free(pInode); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1347 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1352 | ** Given a file descriptor, locate the unixInodeInfo object that |
| 1353 | ** describes that file descriptor. Create a new one if necessary. The |
| 1354 | ** return value might be uninitialized if an error occurs. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1355 | ** |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1356 | ** The global mutex must held when calling this routine. |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1357 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1358 | ** Return an appropriate error code. |
| 1359 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1360 | static int findInodeInfo( |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1361 | unixFile *pFile, /* Unix file with file desc used in the key */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1362 | unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1363 | ){ |
| 1364 | int rc; /* System call return code */ |
| 1365 | int fd; /* The file descriptor for pFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1366 | struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ |
| 1367 | struct stat statbuf; /* Low-level file information */ |
| 1368 | unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1369 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1370 | assert( unixMutexHeld() ); |
| 1371 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1372 | /* Get low-level information about the file that we can used to |
| 1373 | ** create a unique name for the file. |
| 1374 | */ |
| 1375 | fd = pFile->h; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1376 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1377 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1378 | storeLastErrno(pFile, errno); |
drh | 40fe8d3 | 2015-11-30 20:36:26 +0000 | [diff] [blame] | 1379 | #if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS) |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1380 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 1381 | #endif |
| 1382 | return SQLITE_IOERR; |
| 1383 | } |
| 1384 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1385 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1386 | /* On OS X on an msdos filesystem, the inode number is reported |
| 1387 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 1388 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 1389 | ** we always increase the file size to 1 by writing a single byte |
| 1390 | ** prior to accessing the inode number. The one byte written is |
| 1391 | ** an ASCII 'S' character which also happens to be the first byte |
| 1392 | ** in the header of every SQLite database. In this way, if there |
| 1393 | ** is a race condition such that another thread has already populated |
| 1394 | ** the first page of the database, no damage is done. |
| 1395 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1396 | if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 1397 | do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR ); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1398 | if( rc!=1 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1399 | storeLastErrno(pFile, errno); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1400 | return SQLITE_IOERR; |
| 1401 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1402 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1403 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1404 | storeLastErrno(pFile, errno); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1405 | return SQLITE_IOERR; |
| 1406 | } |
| 1407 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1408 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1409 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1410 | memset(&fileId, 0, sizeof(fileId)); |
| 1411 | fileId.dev = statbuf.st_dev; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1412 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1413 | fileId.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1414 | #else |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 1415 | fileId.ino = (u64)statbuf.st_ino; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1416 | #endif |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1417 | assert( unixMutexHeld() ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1418 | pInode = inodeList; |
| 1419 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 1420 | pInode = pInode->pNext; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1421 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1422 | if( pInode==0 ){ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 1423 | pInode = sqlite3_malloc64( sizeof(*pInode) ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1424 | if( pInode==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 1425 | return SQLITE_NOMEM_BKPT; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1426 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1427 | memset(pInode, 0, sizeof(*pInode)); |
| 1428 | memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
drh | 6886d6d | 2018-07-23 22:55:10 +0000 | [diff] [blame] | 1429 | if( sqlite3GlobalConfig.bCoreMutex ){ |
| 1430 | pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 1431 | if( pInode->pLockMutex==0 ){ |
| 1432 | sqlite3_free(pInode); |
| 1433 | return SQLITE_NOMEM_BKPT; |
| 1434 | } |
| 1435 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1436 | pInode->nRef = 1; |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1437 | assert( unixMutexHeld() ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1438 | pInode->pNext = inodeList; |
| 1439 | pInode->pPrev = 0; |
| 1440 | if( inodeList ) inodeList->pPrev = pInode; |
| 1441 | inodeList = pInode; |
| 1442 | }else{ |
| 1443 | pInode->nRef++; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1444 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1445 | *ppInode = pInode; |
| 1446 | return SQLITE_OK; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1447 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1448 | |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1449 | /* |
| 1450 | ** Return TRUE if pFile has been renamed or unlinked since it was first opened. |
| 1451 | */ |
| 1452 | static int fileHasMoved(unixFile *pFile){ |
drh | 61ffea5 | 2014-08-12 12:19:25 +0000 | [diff] [blame] | 1453 | #if OS_VXWORKS |
| 1454 | return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId; |
| 1455 | #else |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1456 | struct stat buf; |
| 1457 | return pFile->pInode!=0 && |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 1458 | (osStat(pFile->zPath, &buf)!=0 |
| 1459 | || (u64)buf.st_ino!=pFile->pInode->fileId.ino); |
drh | 91be7dc | 2014-08-11 13:53:30 +0000 | [diff] [blame] | 1460 | #endif |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1463 | |
| 1464 | /* |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1465 | ** Check a unixFile that is a database. Verify the following: |
| 1466 | ** |
| 1467 | ** (1) There is exactly one hard link on the file |
| 1468 | ** (2) The file is not a symbolic link |
| 1469 | ** (3) The file has not been renamed or unlinked |
| 1470 | ** |
| 1471 | ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right. |
| 1472 | */ |
| 1473 | static void verifyDbFile(unixFile *pFile){ |
| 1474 | struct stat buf; |
| 1475 | int rc; |
drh | 86151e8 | 2015-12-08 14:37:16 +0000 | [diff] [blame] | 1476 | |
| 1477 | /* These verifications occurs for the main database only */ |
| 1478 | if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return; |
| 1479 | |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1480 | rc = osFstat(pFile->h, &buf); |
| 1481 | if( rc!=0 ){ |
| 1482 | sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1483 | return; |
| 1484 | } |
drh | 6369bc3 | 2016-03-21 16:06:42 +0000 | [diff] [blame] | 1485 | if( buf.st_nlink==0 ){ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1486 | sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1487 | return; |
| 1488 | } |
| 1489 | if( buf.st_nlink>1 ){ |
| 1490 | sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1491 | return; |
| 1492 | } |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1493 | if( fileHasMoved(pFile) ){ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1494 | sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1495 | return; |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | |
| 1500 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1501 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1502 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1503 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1504 | ** 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] | 1505 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1506 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1507 | int rc = SQLITE_OK; |
| 1508 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1509 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1510 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1511 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1512 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1513 | assert( pFile ); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 1514 | assert( pFile->eFileLock<=SHARED_LOCK ); |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1515 | sqlite3_mutex_enter(pFile->pInode->pLockMutex); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1516 | |
| 1517 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1518 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1519 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1522 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1523 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1524 | #ifndef __DJGPP__ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1525 | if( !reserved && !pFile->pInode->bProcessLock ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1526 | struct flock lock; |
| 1527 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1528 | lock.l_start = RESERVED_BYTE; |
| 1529 | lock.l_len = 1; |
| 1530 | lock.l_type = F_WRLCK; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1531 | if( osFcntl(pFile->h, F_GETLK, &lock) ){ |
| 1532 | rc = SQLITE_IOERR_CHECKRESERVEDLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1533 | storeLastErrno(pFile, errno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1534 | } else if( lock.l_type!=F_UNLCK ){ |
| 1535 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1536 | } |
| 1537 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1538 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1539 | |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1540 | sqlite3_mutex_leave(pFile->pInode->pLockMutex); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1541 | OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1542 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1543 | *pResOut = reserved; |
| 1544 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | /* |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 1548 | ** Set a posix-advisory-lock. |
| 1549 | ** |
| 1550 | ** There are two versions of this routine. If compiled with |
| 1551 | ** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter |
| 1552 | ** which is a pointer to a unixFile. If the unixFile->iBusyTimeout |
| 1553 | ** value is set, then it is the number of milliseconds to wait before |
| 1554 | ** failing the lock. The iBusyTimeout value is always reset back to |
| 1555 | ** zero on each call. |
| 1556 | ** |
| 1557 | ** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking |
| 1558 | ** attempt to set the lock. |
| 1559 | */ |
| 1560 | #ifndef SQLITE_ENABLE_SETLK_TIMEOUT |
| 1561 | # define osSetPosixAdvisoryLock(h,x,t) osFcntl(h,F_SETLK,x) |
| 1562 | #else |
| 1563 | static int osSetPosixAdvisoryLock( |
| 1564 | int h, /* The file descriptor on which to take the lock */ |
| 1565 | struct flock *pLock, /* The description of the lock */ |
| 1566 | unixFile *pFile /* Structure holding timeout value */ |
| 1567 | ){ |
| 1568 | int rc = osFcntl(h,F_SETLK,pLock); |
drh | fd72563 | 2018-03-26 20:43:05 +0000 | [diff] [blame] | 1569 | while( rc<0 && pFile->iBusyTimeout>0 ){ |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 1570 | /* On systems that support some kind of blocking file lock with a timeout, |
| 1571 | ** make appropriate changes here to invoke that blocking file lock. On |
| 1572 | ** generic posix, however, there is no such API. So we simply try the |
| 1573 | ** lock once every millisecond until either the timeout expires, or until |
| 1574 | ** the lock is obtained. */ |
drh | fd72563 | 2018-03-26 20:43:05 +0000 | [diff] [blame] | 1575 | usleep(1000); |
| 1576 | rc = osFcntl(h,F_SETLK,pLock); |
| 1577 | pFile->iBusyTimeout--; |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 1578 | } |
| 1579 | return rc; |
| 1580 | } |
| 1581 | #endif /* SQLITE_ENABLE_SETLK_TIMEOUT */ |
| 1582 | |
| 1583 | |
| 1584 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1585 | ** Attempt to set a system-lock on the file pFile. The lock is |
| 1586 | ** described by pLock. |
| 1587 | ** |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1588 | ** If the pFile was opened read/write from unix-excl, then the only lock |
| 1589 | ** ever obtained is an exclusive lock, and it is obtained exactly once |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1590 | ** the first time any lock is attempted. All subsequent system locking |
| 1591 | ** operations become no-ops. Locking operations still happen internally, |
| 1592 | ** in order to coordinate access between separate database connections |
| 1593 | ** within this process, but all of that is handled in memory and the |
| 1594 | ** operating system does not participate. |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1595 | ** |
| 1596 | ** This function is a pass-through to fcntl(F_SETLK) if pFile is using |
| 1597 | ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl" |
| 1598 | ** and is read-only. |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1599 | ** |
| 1600 | ** Zero is returned if the call completes successfully, or -1 if a call |
| 1601 | ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()). |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1602 | */ |
| 1603 | static int unixFileLock(unixFile *pFile, struct flock *pLock){ |
| 1604 | int rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1605 | unixInodeInfo *pInode = pFile->pInode; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1606 | assert( pInode!=0 ); |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1607 | assert( sqlite3_mutex_held(pInode->pLockMutex) ); |
drh | 50358ad | 2015-12-02 01:04:33 +0000 | [diff] [blame] | 1608 | if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1609 | if( pInode->bProcessLock==0 ){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1610 | struct flock lock; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1611 | assert( pInode->nLock==0 ); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1612 | lock.l_whence = SEEK_SET; |
| 1613 | lock.l_start = SHARED_FIRST; |
| 1614 | lock.l_len = SHARED_SIZE; |
| 1615 | lock.l_type = F_WRLCK; |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 1616 | rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1617 | if( rc<0 ) return rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1618 | pInode->bProcessLock = 1; |
| 1619 | pInode->nLock++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1620 | }else{ |
| 1621 | rc = 0; |
| 1622 | } |
| 1623 | }else{ |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 1624 | rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1625 | } |
| 1626 | return rc; |
| 1627 | } |
| 1628 | |
| 1629 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1630 | ** Lock the file with the lock specified by parameter eFileLock - one |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1631 | ** of the following: |
| 1632 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1633 | ** (1) SHARED_LOCK |
| 1634 | ** (2) RESERVED_LOCK |
| 1635 | ** (3) PENDING_LOCK |
| 1636 | ** (4) EXCLUSIVE_LOCK |
| 1637 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1638 | ** Sometimes when requesting one lock state, additional lock states |
| 1639 | ** are inserted in between. The locking might fail on one of the later |
| 1640 | ** transitions leaving the lock state different from what it started but |
| 1641 | ** still short of its goal. The following chart shows the allowed |
| 1642 | ** transitions and the inserted intermediate states: |
| 1643 | ** |
| 1644 | ** UNLOCKED -> SHARED |
| 1645 | ** SHARED -> RESERVED |
| 1646 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1647 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1648 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1649 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1650 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1651 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1652 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1653 | static int unixLock(sqlite3_file *id, int eFileLock){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1654 | /* The following describes the implementation of the various locks and |
| 1655 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1656 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1657 | ** confusion with SQLite lock names). The algorithms are complicated |
drh | f878e6e | 2016-04-07 13:45:20 +0000 | [diff] [blame] | 1658 | ** slightly in order to be compatible with Windows95 systems simultaneously |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1659 | ** accessing the same database file, in case that is ever required. |
| 1660 | ** |
| 1661 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1662 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1663 | ** range', a range of 510 bytes at a well known offset. |
| 1664 | ** |
| 1665 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
drh | f878e6e | 2016-04-07 13:45:20 +0000 | [diff] [blame] | 1666 | ** byte'. If this is successful, 'shared byte range' is read-locked |
| 1667 | ** and the lock on the 'pending byte' released. (Legacy note: When |
| 1668 | ** SQLite was first developed, Windows95 systems were still very common, |
| 1669 | ** and Widnows95 lacks a shared-lock capability. So on Windows95, a |
| 1670 | ** single randomly selected by from the 'shared byte range' is locked. |
| 1671 | ** Windows95 is now pretty much extinct, but this work-around for the |
| 1672 | ** lack of shared-locks on Windows95 lives on, for backwards |
| 1673 | ** compatibility.) |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1674 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1675 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1676 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1677 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1678 | ** |
| 1679 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1680 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1681 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1682 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1683 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1684 | ** This property is used by the algorithm for rolling back a journal file |
| 1685 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1686 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1687 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1688 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1689 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1690 | ** within this range, this ensures that no other locks are held on the |
| 1691 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1692 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1693 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1694 | unixFile *pFile = (unixFile*)id; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 1695 | unixInodeInfo *pInode; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1696 | struct flock lock; |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 1697 | int tErrno = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1698 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1699 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1700 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 1701 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 1702 | azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 1703 | osGetpid(0))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1704 | |
| 1705 | /* 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] | 1706 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1707 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1708 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1709 | if( pFile->eFileLock>=eFileLock ){ |
| 1710 | OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
| 1711 | azFileLock(eFileLock))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1712 | return SQLITE_OK; |
| 1713 | } |
| 1714 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1715 | /* Make sure the locking sequence is correct. |
| 1716 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 1717 | ** (2) SQLite never explicitly requests a pendig lock. |
| 1718 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1719 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1720 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 1721 | assert( eFileLock!=PENDING_LOCK ); |
| 1722 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1723 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1724 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1725 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1726 | pInode = pFile->pInode; |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1727 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1728 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1729 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1730 | ** handle that precludes the requested lock, return BUSY. |
| 1731 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1732 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 1733 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1734 | ){ |
| 1735 | rc = SQLITE_BUSY; |
| 1736 | goto end_lock; |
| 1737 | } |
| 1738 | |
| 1739 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1740 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1741 | ** return SQLITE_OK. |
| 1742 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1743 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1744 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1745 | assert( eFileLock==SHARED_LOCK ); |
| 1746 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1747 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1748 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1749 | pInode->nShared++; |
| 1750 | pInode->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1751 | goto end_lock; |
| 1752 | } |
| 1753 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1754 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1755 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1756 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1757 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1758 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1759 | lock.l_len = 1L; |
| 1760 | lock.l_whence = SEEK_SET; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1761 | if( eFileLock==SHARED_LOCK |
| 1762 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1763 | ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1764 | lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1765 | lock.l_start = PENDING_BYTE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1766 | if( unixFileLock(pFile, &lock) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1767 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1768 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1769 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1770 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1771 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1772 | goto end_lock; |
| 1773 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | |
| 1777 | /* If control gets to this point, then actually go ahead and make |
| 1778 | ** operating system calls for the specified lock. |
| 1779 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1780 | if( eFileLock==SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1781 | assert( pInode->nShared==0 ); |
| 1782 | assert( pInode->eFileLock==0 ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1783 | assert( rc==SQLITE_OK ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1784 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1785 | /* Now get the read-lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1786 | lock.l_start = SHARED_FIRST; |
| 1787 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1788 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1789 | tErrno = errno; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1790 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1791 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1792 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1793 | /* Drop the temporary PENDING lock */ |
| 1794 | lock.l_start = PENDING_BYTE; |
| 1795 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1796 | lock.l_type = F_UNLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1797 | if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){ |
| 1798 | /* This could happen with a network mount */ |
| 1799 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1800 | rc = SQLITE_IOERR_UNLOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1801 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1802 | |
| 1803 | if( rc ){ |
| 1804 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1805 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1806 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1807 | goto end_lock; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1808 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1809 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1810 | pInode->nLock++; |
| 1811 | pInode->nShared = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1812 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1813 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1814 | /* We are trying for an exclusive lock but another thread in this |
| 1815 | ** same process is still holding a shared lock. */ |
| 1816 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1817 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1818 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1819 | ** assumed that there is a SHARED or greater lock on the file |
| 1820 | ** already. |
| 1821 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1822 | assert( 0!=pFile->eFileLock ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1823 | lock.l_type = F_WRLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1824 | |
| 1825 | assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK ); |
| 1826 | if( eFileLock==RESERVED_LOCK ){ |
| 1827 | lock.l_start = RESERVED_BYTE; |
| 1828 | lock.l_len = 1L; |
| 1829 | }else{ |
| 1830 | lock.l_start = SHARED_FIRST; |
| 1831 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1832 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1833 | |
| 1834 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1835 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1836 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1837 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1838 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1839 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1840 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1841 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1842 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1843 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 1844 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1845 | /* Set up the transaction-counter change checking flags when |
| 1846 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1847 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1848 | ** write operation (not a hot journal rollback). |
| 1849 | */ |
| 1850 | if( rc==SQLITE_OK |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1851 | && pFile->eFileLock<=SHARED_LOCK |
| 1852 | && eFileLock==RESERVED_LOCK |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1853 | ){ |
| 1854 | pFile->transCntrChng = 0; |
| 1855 | pFile->dbUpdate = 0; |
| 1856 | pFile->inNormalWrite = 1; |
| 1857 | } |
| 1858 | #endif |
| 1859 | |
| 1860 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1861 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1862 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1863 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1864 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 1865 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1866 | pInode->eFileLock = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1867 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1868 | |
| 1869 | end_lock: |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1870 | sqlite3_mutex_leave(pInode->pLockMutex); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1871 | OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), |
| 1872 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1873 | return rc; |
| 1874 | } |
| 1875 | |
| 1876 | /* |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1877 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1878 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1879 | */ |
| 1880 | static void setPendingFd(unixFile *pFile){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1881 | unixInodeInfo *pInode = pFile->pInode; |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 1882 | UnixUnusedFd *p = pFile->pPreallocatedUnused; |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1883 | assert( unixFileMutexHeld(pFile) ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1884 | p->pNext = pInode->pUnused; |
| 1885 | pInode->pUnused = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1886 | pFile->h = -1; |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 1887 | pFile->pPreallocatedUnused = 0; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1891 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1892 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1893 | ** |
| 1894 | ** If the locking level of the file descriptor is already at or below |
| 1895 | ** the requested locking level, this routine is a no-op. |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1896 | ** |
| 1897 | ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 1898 | ** the byte range is divided into 2 parts and the first part is unlocked then |
| 1899 | ** set to a read lock, then the other part is simply unlocked. This works |
| 1900 | ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 1901 | ** remove the write lock on a region when a read lock is set. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1902 | */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1903 | static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1904 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1905 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1906 | struct flock lock; |
| 1907 | int rc = SQLITE_OK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1908 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1909 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1910 | 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] | 1911 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 1912 | osGetpid(0))); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1913 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1914 | assert( eFileLock<=SHARED_LOCK ); |
| 1915 | if( pFile->eFileLock<=eFileLock ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1916 | return SQLITE_OK; |
| 1917 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1918 | pInode = pFile->pInode; |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1919 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1920 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1921 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1922 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1923 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 1924 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1925 | /* When reducing a lock such that other processes can start |
| 1926 | ** reading the database file again, make sure that the |
| 1927 | ** transaction counter was updated if any part of the database |
| 1928 | ** file changed. If the transaction counter is not updated, |
| 1929 | ** other connections to the same file might not realize that |
| 1930 | ** the file has changed and hence might not know to flush their |
| 1931 | ** cache. The use of a stale cache can lead to database corruption. |
| 1932 | */ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1933 | pFile->inNormalWrite = 0; |
| 1934 | #endif |
| 1935 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1936 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 1937 | ** before establishing the readlock - to avoid a race condition we downgrade |
| 1938 | ** the lock in 2 blocks, so that part of the range will be covered by a |
| 1939 | ** write lock until the rest is covered by a read lock: |
| 1940 | ** 1: [WWWWW] |
| 1941 | ** 2: [....W] |
| 1942 | ** 3: [RRRRW] |
| 1943 | ** 4: [RRRR.] |
| 1944 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1945 | if( eFileLock==SHARED_LOCK ){ |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1946 | #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE |
drh | 87e79ae | 2011-03-08 13:06:41 +0000 | [diff] [blame] | 1947 | (void)handleNFSUnlock; |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1948 | assert( handleNFSUnlock==0 ); |
| 1949 | #endif |
| 1950 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1951 | if( handleNFSUnlock ){ |
drh | a712b4b | 2015-02-19 16:12:04 +0000 | [diff] [blame] | 1952 | int tErrno; /* Error code from system call errors */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1953 | off_t divSize = SHARED_SIZE - 1; |
| 1954 | |
| 1955 | lock.l_type = F_UNLCK; |
| 1956 | lock.l_whence = SEEK_SET; |
| 1957 | lock.l_start = SHARED_FIRST; |
| 1958 | lock.l_len = divSize; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 1959 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1960 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1961 | rc = SQLITE_IOERR_UNLOCK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 1962 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1963 | goto end_unlock; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1964 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1965 | lock.l_type = F_RDLCK; |
| 1966 | lock.l_whence = SEEK_SET; |
| 1967 | lock.l_start = SHARED_FIRST; |
| 1968 | lock.l_len = divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1969 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1970 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1971 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1972 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1973 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1974 | } |
| 1975 | goto end_unlock; |
| 1976 | } |
| 1977 | lock.l_type = F_UNLCK; |
| 1978 | lock.l_whence = SEEK_SET; |
| 1979 | lock.l_start = SHARED_FIRST+divSize; |
| 1980 | lock.l_len = SHARED_SIZE-divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1981 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1982 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1983 | rc = SQLITE_IOERR_UNLOCK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 1984 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1985 | goto end_unlock; |
| 1986 | } |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1987 | }else |
| 1988 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 1989 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1990 | lock.l_type = F_RDLCK; |
| 1991 | lock.l_whence = SEEK_SET; |
| 1992 | lock.l_start = SHARED_FIRST; |
| 1993 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1994 | if( unixFileLock(pFile, &lock) ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1995 | /* In theory, the call to unixFileLock() cannot fail because another |
| 1996 | ** process is holding an incompatible lock. If it does, this |
| 1997 | ** indicates that the other process is not following the locking |
| 1998 | ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning |
| 1999 | ** SQLITE_BUSY would confuse the upper layer (in practice it causes |
| 2000 | ** an assert to fail). */ |
| 2001 | rc = SQLITE_IOERR_RDLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2002 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2003 | goto end_unlock; |
| 2004 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 2005 | } |
| 2006 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2007 | lock.l_type = F_UNLCK; |
| 2008 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2009 | lock.l_start = PENDING_BYTE; |
| 2010 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2011 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2012 | pInode->eFileLock = SHARED_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 2013 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2014 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2015 | storeLastErrno(pFile, errno); |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 2016 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 2017 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2018 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2019 | if( eFileLock==NO_LOCK ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2020 | /* Decrement the shared lock counter. Release the lock using an |
| 2021 | ** OS call only when all threads in this same process have released |
| 2022 | ** the lock. |
| 2023 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2024 | pInode->nShared--; |
| 2025 | if( pInode->nShared==0 ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2026 | lock.l_type = F_UNLCK; |
| 2027 | lock.l_whence = SEEK_SET; |
| 2028 | lock.l_start = lock.l_len = 0L; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2029 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2030 | pInode->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 2031 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2032 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2033 | storeLastErrno(pFile, errno); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2034 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2035 | pFile->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 2036 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2039 | /* Decrement the count of locks against this same file. When the |
| 2040 | ** count reaches zero, close any other file descriptors whose close |
| 2041 | ** was deferred because of outstanding locks. |
| 2042 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2043 | pInode->nLock--; |
| 2044 | assert( pInode->nLock>=0 ); |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 2045 | if( pInode->nLock==0 ) closePendingFds(pFile); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2046 | } |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2047 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2048 | end_unlock: |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 2049 | sqlite3_mutex_leave(pInode->pLockMutex); |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 2050 | if( rc==SQLITE_OK ){ |
| 2051 | pFile->eFileLock = eFileLock; |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 2052 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 2053 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2057 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2058 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2059 | ** |
| 2060 | ** If the locking level of the file descriptor is already at or below |
| 2061 | ** the requested locking level, this routine is a no-op. |
| 2062 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2063 | static int unixUnlock(sqlite3_file *id, int eFileLock){ |
dan | f52a469 | 2013-10-31 18:49:58 +0000 | [diff] [blame] | 2064 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | a1afc74 | 2013-03-25 13:50:49 +0000 | [diff] [blame] | 2065 | assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 ); |
dan | f52a469 | 2013-10-31 18:49:58 +0000 | [diff] [blame] | 2066 | #endif |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 2067 | return posixUnlock(id, eFileLock, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 2070 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 2071 | static int unixMapfile(unixFile *pFd, i64 nByte); |
| 2072 | static void unixUnmapfile(unixFile *pFd); |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 2073 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 2074 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2075 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2076 | ** This function performs the parts of the "close file" operation |
| 2077 | ** common to all locking schemes. It closes the directory and file |
| 2078 | ** handles, if they are valid, and sets all fields of the unixFile |
| 2079 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2080 | ** |
| 2081 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 2082 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 2083 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2084 | */ |
| 2085 | static int closeUnixFile(sqlite3_file *id){ |
| 2086 | unixFile *pFile = (unixFile*)id; |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 2087 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 2088 | unixUnmapfile(pFile); |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 2089 | #endif |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2090 | if( pFile->h>=0 ){ |
| 2091 | robust_close(pFile, pFile->h, __LINE__); |
| 2092 | pFile->h = -1; |
| 2093 | } |
| 2094 | #if OS_VXWORKS |
| 2095 | if( pFile->pId ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 2096 | if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 2097 | osUnlink(pFile->pId->zCanonicalName); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2098 | } |
| 2099 | vxworksReleaseFileId(pFile->pId); |
| 2100 | pFile->pId = 0; |
| 2101 | } |
| 2102 | #endif |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 2103 | #ifdef SQLITE_UNLINK_AFTER_CLOSE |
| 2104 | if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
| 2105 | osUnlink(pFile->zPath); |
| 2106 | sqlite3_free(*(char**)&pFile->zPath); |
| 2107 | pFile->zPath = 0; |
| 2108 | } |
| 2109 | #endif |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2110 | OSTRACE(("CLOSE %-3d\n", pFile->h)); |
| 2111 | OpenCounter(-1); |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 2112 | sqlite3_free(pFile->pPreallocatedUnused); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2113 | memset(pFile, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2114 | return SQLITE_OK; |
| 2115 | } |
| 2116 | |
| 2117 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 2118 | ** Close a file. |
| 2119 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2120 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2121 | int rc = SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2122 | unixFile *pFile = (unixFile *)id; |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 2123 | unixInodeInfo *pInode = pFile->pInode; |
| 2124 | |
| 2125 | assert( pInode!=0 ); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 2126 | verifyDbFile(pFile); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2127 | unixUnlock(id, NO_LOCK); |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 2128 | assert( unixFileMutexNotheld(pFile) ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2129 | unixEnterMutex(); |
| 2130 | |
| 2131 | /* unixFile.pInode is always valid here. Otherwise, a different close |
| 2132 | ** routine (e.g. nolockClose()) would be called instead. |
| 2133 | */ |
| 2134 | assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 2135 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | 3fcef1a | 2018-08-16 16:24:24 +0000 | [diff] [blame] | 2136 | if( pInode->nLock ){ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2137 | /* If there are outstanding locks, do not actually close the file just |
| 2138 | ** yet because that would clear those locks. Instead, add the file |
| 2139 | ** descriptor to pInode->pUnused list. It will be automatically closed |
| 2140 | ** when the last lock is cleared. |
| 2141 | */ |
| 2142 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 2143 | } |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 2144 | sqlite3_mutex_leave(pInode->pLockMutex); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2145 | releaseInodeInfo(pFile); |
| 2146 | rc = closeUnixFile(id); |
| 2147 | unixLeaveMutex(); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2148 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2151 | /************** End of the posix advisory lock implementation ***************** |
| 2152 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2153 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2154 | /****************************************************************************** |
| 2155 | ****************************** No-op Locking ********************************** |
| 2156 | ** |
| 2157 | ** Of the various locking implementations available, this is by far the |
| 2158 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 2159 | ** file for reading or writing. |
| 2160 | ** |
| 2161 | ** This locking mode is appropriate for use on read-only databases |
| 2162 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 2163 | ** also be used if the application employs some external mechanism to |
| 2164 | ** prevent simultaneous access of the same database by two or more |
| 2165 | ** database connections. But there is a serious risk of database |
| 2166 | ** corruption if this locking mode is used in situations where multiple |
| 2167 | ** database connections are accessing the same database file at the same |
| 2168 | ** time and one or more of those connections are writing. |
| 2169 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2170 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2171 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 2172 | UNUSED_PARAMETER(NotUsed); |
| 2173 | *pResOut = 0; |
| 2174 | return SQLITE_OK; |
| 2175 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2176 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 2177 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 2178 | return SQLITE_OK; |
| 2179 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2180 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 2181 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 2182 | return SQLITE_OK; |
| 2183 | } |
| 2184 | |
| 2185 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2186 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2187 | */ |
| 2188 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2189 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | /******************* End of the no-op lock implementation ********************* |
| 2193 | ******************************************************************************/ |
| 2194 | |
| 2195 | /****************************************************************************** |
| 2196 | ************************* Begin dot-file Locking ****************************** |
| 2197 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 2198 | ** The dotfile locking implementation uses the existence of separate lock |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2199 | ** files (really a directory) to control access to the database. This works |
| 2200 | ** on just about every filesystem imaginable. But there are serious downsides: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2201 | ** |
| 2202 | ** (1) There is zero concurrency. A single reader blocks all other |
| 2203 | ** connections from reading or writing the database. |
| 2204 | ** |
| 2205 | ** (2) An application crash or power loss can leave stale lock files |
| 2206 | ** sitting around that need to be cleared manually. |
| 2207 | ** |
| 2208 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 2209 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2210 | ** |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2211 | ** Dotfile locking works by creating a subdirectory in the same directory as |
| 2212 | ** the database and with the same name but with a ".lock" extension added. |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 2213 | ** The existence of a lock directory implies an EXCLUSIVE lock. All other |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2214 | ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2215 | */ |
| 2216 | |
| 2217 | /* |
| 2218 | ** 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] | 2219 | ** lock directory. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2220 | */ |
| 2221 | #define DOTLOCK_SUFFIX ".lock" |
| 2222 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2223 | /* |
| 2224 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2225 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2226 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2227 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2228 | ** |
| 2229 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 2230 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 2231 | ** is held on the file and false if the file is unlocked. |
| 2232 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2233 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 2234 | int rc = SQLITE_OK; |
| 2235 | int reserved = 0; |
| 2236 | unixFile *pFile = (unixFile*)id; |
| 2237 | |
| 2238 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2239 | |
| 2240 | assert( pFile ); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2241 | reserved = osAccess((const char*)pFile->lockingContext, 0)==0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2242 | OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2243 | *pResOut = reserved; |
| 2244 | return rc; |
| 2245 | } |
| 2246 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2247 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2248 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2249 | ** of the following: |
| 2250 | ** |
| 2251 | ** (1) SHARED_LOCK |
| 2252 | ** (2) RESERVED_LOCK |
| 2253 | ** (3) PENDING_LOCK |
| 2254 | ** (4) EXCLUSIVE_LOCK |
| 2255 | ** |
| 2256 | ** Sometimes when requesting one lock state, additional lock states |
| 2257 | ** are inserted in between. The locking might fail on one of the later |
| 2258 | ** transitions leaving the lock state different from what it started but |
| 2259 | ** still short of its goal. The following chart shows the allowed |
| 2260 | ** transitions and the inserted intermediate states: |
| 2261 | ** |
| 2262 | ** UNLOCKED -> SHARED |
| 2263 | ** SHARED -> RESERVED |
| 2264 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2265 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2266 | ** PENDING -> EXCLUSIVE |
| 2267 | ** |
| 2268 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2269 | ** routine to lower a locking level. |
| 2270 | ** |
| 2271 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 2272 | ** But we track the other locking levels internally. |
| 2273 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2274 | static int dotlockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2275 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2276 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2277 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2278 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2279 | |
| 2280 | /* If we have any lock, then the lock file already exists. All we have |
| 2281 | ** to do is adjust our internal record of the lock level. |
| 2282 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2283 | if( pFile->eFileLock > NO_LOCK ){ |
| 2284 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2285 | /* Always update the timestamp on the old file */ |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 2286 | #ifdef HAVE_UTIME |
| 2287 | utime(zLockFile, NULL); |
| 2288 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2289 | utimes(zLockFile, NULL); |
| 2290 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2291 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | /* grab an exclusive lock */ |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2295 | rc = osMkdir(zLockFile, 0777); |
| 2296 | if( rc<0 ){ |
| 2297 | /* failed to open/create the lock directory */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2298 | int tErrno = errno; |
| 2299 | if( EEXIST == tErrno ){ |
| 2300 | rc = SQLITE_BUSY; |
| 2301 | } else { |
| 2302 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2303 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2304 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2305 | } |
| 2306 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2307 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2308 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2309 | |
| 2310 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2311 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2312 | return rc; |
| 2313 | } |
| 2314 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2315 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2316 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2317 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2318 | ** |
| 2319 | ** If the locking level of the file descriptor is already at or below |
| 2320 | ** the requested locking level, this routine is a no-op. |
| 2321 | ** |
| 2322 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 2323 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2324 | static int dotlockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2325 | unixFile *pFile = (unixFile*)id; |
| 2326 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2327 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2328 | |
| 2329 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2330 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2331 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2332 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2333 | |
| 2334 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2335 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2336 | return SQLITE_OK; |
| 2337 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2338 | |
| 2339 | /* To downgrade to shared, simply update our internal notion of the |
| 2340 | ** lock state. No need to mess with the file on disk. |
| 2341 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2342 | if( eFileLock==SHARED_LOCK ){ |
| 2343 | pFile->eFileLock = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2344 | return SQLITE_OK; |
| 2345 | } |
| 2346 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2347 | /* To fully unlock the database, delete the lock file */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2348 | assert( eFileLock==NO_LOCK ); |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2349 | rc = osRmdir(zLockFile); |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2350 | if( rc<0 ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 2351 | int tErrno = errno; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2352 | if( tErrno==ENOENT ){ |
| 2353 | rc = SQLITE_OK; |
| 2354 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2355 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2356 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2357 | } |
| 2358 | return rc; |
| 2359 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2360 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2361 | return SQLITE_OK; |
| 2362 | } |
| 2363 | |
| 2364 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2365 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2366 | */ |
| 2367 | static int dotlockClose(sqlite3_file *id) { |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2368 | unixFile *pFile = (unixFile*)id; |
| 2369 | assert( id!=0 ); |
| 2370 | dotlockUnlock(id, NO_LOCK); |
| 2371 | sqlite3_free(pFile->lockingContext); |
| 2372 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2373 | } |
| 2374 | /****************** End of the dot-file lock implementation ******************* |
| 2375 | ******************************************************************************/ |
| 2376 | |
| 2377 | /****************************************************************************** |
| 2378 | ************************** Begin flock Locking ******************************** |
| 2379 | ** |
| 2380 | ** Use the flock() system call to do file locking. |
| 2381 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2382 | ** flock() locking is like dot-file locking in that the various |
| 2383 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 2384 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 2385 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 2386 | ** still works when you do this, but concurrency is reduced since |
| 2387 | ** only a single process can be reading the database at a time. |
| 2388 | ** |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 2389 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2390 | */ |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 2391 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2392 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2393 | /* |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2394 | ** Retry flock() calls that fail with EINTR |
| 2395 | */ |
| 2396 | #ifdef EINTR |
| 2397 | static int robust_flock(int fd, int op){ |
| 2398 | int rc; |
| 2399 | do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR ); |
| 2400 | return rc; |
| 2401 | } |
| 2402 | #else |
drh | 5c81927 | 2011-02-23 14:00:12 +0000 | [diff] [blame] | 2403 | # define robust_flock(a,b) flock(a,b) |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2404 | #endif |
| 2405 | |
| 2406 | |
| 2407 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2408 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2409 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2410 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2411 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2412 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2413 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 2414 | int rc = SQLITE_OK; |
| 2415 | int reserved = 0; |
| 2416 | unixFile *pFile = (unixFile*)id; |
| 2417 | |
| 2418 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2419 | |
| 2420 | assert( pFile ); |
| 2421 | |
| 2422 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2423 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2424 | reserved = 1; |
| 2425 | } |
| 2426 | |
| 2427 | /* Otherwise see if some other process holds it. */ |
| 2428 | if( !reserved ){ |
| 2429 | /* attempt to get the lock */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2430 | int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2431 | if( !lrc ){ |
| 2432 | /* got the lock, unlock it */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2433 | lrc = robust_flock(pFile->h, LOCK_UN); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2434 | if ( lrc ) { |
| 2435 | int tErrno = errno; |
| 2436 | /* unlock failed with an error */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2437 | lrc = SQLITE_IOERR_UNLOCK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2438 | storeLastErrno(pFile, tErrno); |
| 2439 | rc = lrc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2440 | } |
| 2441 | } else { |
| 2442 | int tErrno = errno; |
| 2443 | reserved = 1; |
| 2444 | /* someone else might have it reserved */ |
| 2445 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2446 | if( IS_LOCK_ERROR(lrc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2447 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2448 | rc = lrc; |
| 2449 | } |
| 2450 | } |
| 2451 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2452 | OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2453 | |
| 2454 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
drh | 2e23381 | 2017-08-22 15:21:54 +0000 | [diff] [blame] | 2455 | if( (rc & 0xff) == SQLITE_IOERR ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2456 | rc = SQLITE_OK; |
| 2457 | reserved=1; |
| 2458 | } |
| 2459 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2460 | *pResOut = reserved; |
| 2461 | return rc; |
| 2462 | } |
| 2463 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2464 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2465 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2466 | ** of the following: |
| 2467 | ** |
| 2468 | ** (1) SHARED_LOCK |
| 2469 | ** (2) RESERVED_LOCK |
| 2470 | ** (3) PENDING_LOCK |
| 2471 | ** (4) EXCLUSIVE_LOCK |
| 2472 | ** |
| 2473 | ** Sometimes when requesting one lock state, additional lock states |
| 2474 | ** are inserted in between. The locking might fail on one of the later |
| 2475 | ** transitions leaving the lock state different from what it started but |
| 2476 | ** still short of its goal. The following chart shows the allowed |
| 2477 | ** transitions and the inserted intermediate states: |
| 2478 | ** |
| 2479 | ** UNLOCKED -> SHARED |
| 2480 | ** SHARED -> RESERVED |
| 2481 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2482 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2483 | ** PENDING -> EXCLUSIVE |
| 2484 | ** |
| 2485 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 2486 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2487 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2488 | ** access the file. |
| 2489 | ** |
| 2490 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2491 | ** routine to lower a locking level. |
| 2492 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2493 | static int flockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2494 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2495 | unixFile *pFile = (unixFile*)id; |
| 2496 | |
| 2497 | assert( pFile ); |
| 2498 | |
| 2499 | /* if we already have a lock, it is exclusive. |
| 2500 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2501 | if (pFile->eFileLock > NO_LOCK) { |
| 2502 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2503 | return SQLITE_OK; |
| 2504 | } |
| 2505 | |
| 2506 | /* grab an exclusive lock */ |
| 2507 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2508 | if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2509 | int tErrno = errno; |
| 2510 | /* didn't get, must be busy */ |
| 2511 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2512 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2513 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2514 | } |
| 2515 | } else { |
| 2516 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2517 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2518 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2519 | OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), |
| 2520 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2521 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
drh | 2e23381 | 2017-08-22 15:21:54 +0000 | [diff] [blame] | 2522 | if( (rc & 0xff) == SQLITE_IOERR ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2523 | rc = SQLITE_BUSY; |
| 2524 | } |
| 2525 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2526 | return rc; |
| 2527 | } |
| 2528 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2529 | |
| 2530 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2531 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2532 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2533 | ** |
| 2534 | ** If the locking level of the file descriptor is already at or below |
| 2535 | ** the requested locking level, this routine is a no-op. |
| 2536 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2537 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2538 | unixFile *pFile = (unixFile*)id; |
| 2539 | |
| 2540 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2541 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2542 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2543 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2544 | |
| 2545 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2546 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2547 | return SQLITE_OK; |
| 2548 | } |
| 2549 | |
| 2550 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2551 | if (eFileLock==SHARED_LOCK) { |
| 2552 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2553 | return SQLITE_OK; |
| 2554 | } |
| 2555 | |
| 2556 | /* no, really, unlock. */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2557 | if( robust_flock(pFile->h, LOCK_UN) ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2558 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2559 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2560 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2561 | return SQLITE_IOERR_UNLOCK; |
| 2562 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2563 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2564 | return SQLITE_OK; |
| 2565 | } |
| 2566 | } |
| 2567 | |
| 2568 | /* |
| 2569 | ** Close a file. |
| 2570 | */ |
| 2571 | static int flockClose(sqlite3_file *id) { |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2572 | assert( id!=0 ); |
| 2573 | flockUnlock(id, NO_LOCK); |
| 2574 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
| 2577 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 2578 | |
| 2579 | /******************* End of the flock lock implementation ********************* |
| 2580 | ******************************************************************************/ |
| 2581 | |
| 2582 | /****************************************************************************** |
| 2583 | ************************ Begin Named Semaphore Locking ************************ |
| 2584 | ** |
| 2585 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2586 | ** |
| 2587 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 2588 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 2589 | ** the database file at a time. This reduces potential concurrency, but |
| 2590 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2591 | */ |
| 2592 | #if OS_VXWORKS |
| 2593 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2594 | /* |
| 2595 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2596 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2597 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2598 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2599 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2600 | static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2601 | int rc = SQLITE_OK; |
| 2602 | int reserved = 0; |
| 2603 | unixFile *pFile = (unixFile*)id; |
| 2604 | |
| 2605 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2606 | |
| 2607 | assert( pFile ); |
| 2608 | |
| 2609 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2610 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2611 | reserved = 1; |
| 2612 | } |
| 2613 | |
| 2614 | /* Otherwise see if some other process holds it. */ |
| 2615 | if( !reserved ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2616 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2617 | |
| 2618 | if( sem_trywait(pSem)==-1 ){ |
| 2619 | int tErrno = errno; |
| 2620 | if( EAGAIN != tErrno ){ |
| 2621 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2622 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2623 | } else { |
| 2624 | /* someone else has the lock when we are in NO_LOCK */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2625 | reserved = (pFile->eFileLock < SHARED_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2626 | } |
| 2627 | }else{ |
| 2628 | /* we could have it if we want it */ |
| 2629 | sem_post(pSem); |
| 2630 | } |
| 2631 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2632 | OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2633 | |
| 2634 | *pResOut = reserved; |
| 2635 | return rc; |
| 2636 | } |
| 2637 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2638 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2639 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2640 | ** of the following: |
| 2641 | ** |
| 2642 | ** (1) SHARED_LOCK |
| 2643 | ** (2) RESERVED_LOCK |
| 2644 | ** (3) PENDING_LOCK |
| 2645 | ** (4) EXCLUSIVE_LOCK |
| 2646 | ** |
| 2647 | ** Sometimes when requesting one lock state, additional lock states |
| 2648 | ** are inserted in between. The locking might fail on one of the later |
| 2649 | ** transitions leaving the lock state different from what it started but |
| 2650 | ** still short of its goal. The following chart shows the allowed |
| 2651 | ** transitions and the inserted intermediate states: |
| 2652 | ** |
| 2653 | ** UNLOCKED -> SHARED |
| 2654 | ** SHARED -> RESERVED |
| 2655 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2656 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2657 | ** PENDING -> EXCLUSIVE |
| 2658 | ** |
| 2659 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 2660 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2661 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2662 | ** access the file. |
| 2663 | ** |
| 2664 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2665 | ** routine to lower a locking level. |
| 2666 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2667 | static int semXLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2668 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2669 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2670 | int rc = SQLITE_OK; |
| 2671 | |
| 2672 | /* if we already have a lock, it is exclusive. |
| 2673 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2674 | if (pFile->eFileLock > NO_LOCK) { |
| 2675 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2676 | rc = SQLITE_OK; |
| 2677 | goto sem_end_lock; |
| 2678 | } |
| 2679 | |
| 2680 | /* lock semaphore now but bail out when already locked. */ |
| 2681 | if( sem_trywait(pSem)==-1 ){ |
| 2682 | rc = SQLITE_BUSY; |
| 2683 | goto sem_end_lock; |
| 2684 | } |
| 2685 | |
| 2686 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2687 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2688 | |
| 2689 | sem_end_lock: |
| 2690 | return rc; |
| 2691 | } |
| 2692 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2693 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2694 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2695 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2696 | ** |
| 2697 | ** If the locking level of the file descriptor is already at or below |
| 2698 | ** the requested locking level, this routine is a no-op. |
| 2699 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2700 | static int semXUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2701 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2702 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2703 | |
| 2704 | assert( pFile ); |
| 2705 | assert( pSem ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2706 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2707 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2708 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2709 | |
| 2710 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2711 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2712 | return SQLITE_OK; |
| 2713 | } |
| 2714 | |
| 2715 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2716 | if (eFileLock==SHARED_LOCK) { |
| 2717 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2718 | return SQLITE_OK; |
| 2719 | } |
| 2720 | |
| 2721 | /* no, really unlock. */ |
| 2722 | if ( sem_post(pSem)==-1 ) { |
| 2723 | int rc, tErrno = errno; |
| 2724 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2725 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2726 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2727 | } |
| 2728 | return rc; |
| 2729 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2730 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2731 | return SQLITE_OK; |
| 2732 | } |
| 2733 | |
| 2734 | /* |
| 2735 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2736 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2737 | static int semXClose(sqlite3_file *id) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2738 | if( id ){ |
| 2739 | unixFile *pFile = (unixFile*)id; |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2740 | semXUnlock(id, NO_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2741 | assert( pFile ); |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 2742 | assert( unixFileMutexNotheld(pFile) ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2743 | unixEnterMutex(); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2744 | releaseInodeInfo(pFile); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2745 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2746 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2747 | } |
| 2748 | return SQLITE_OK; |
| 2749 | } |
| 2750 | |
| 2751 | #endif /* OS_VXWORKS */ |
| 2752 | /* |
| 2753 | ** Named semaphore locking is only available on VxWorks. |
| 2754 | ** |
| 2755 | *************** End of the named semaphore lock implementation **************** |
| 2756 | ******************************************************************************/ |
| 2757 | |
| 2758 | |
| 2759 | /****************************************************************************** |
| 2760 | *************************** Begin AFP Locking ********************************* |
| 2761 | ** |
| 2762 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2763 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2764 | ** |
| 2765 | ** Third-party implementations of AFP are available. But this code here |
| 2766 | ** only works on OSX. |
| 2767 | */ |
| 2768 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2769 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2770 | /* |
| 2771 | ** The afpLockingContext structure contains all afp lock specific state |
| 2772 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2773 | typedef struct afpLockingContext afpLockingContext; |
| 2774 | struct afpLockingContext { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2775 | int reserved; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2776 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2777 | }; |
| 2778 | |
| 2779 | struct ByteRangeLockPB2 |
| 2780 | { |
| 2781 | unsigned long long offset; /* offset to first byte to lock */ |
| 2782 | unsigned long long length; /* nbr of bytes to lock */ |
| 2783 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2784 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2785 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2786 | int fd; /* file desc to assoc this lock with */ |
| 2787 | }; |
| 2788 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2789 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2790 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2791 | /* |
| 2792 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2793 | ** AFP filesystem. |
| 2794 | ** |
| 2795 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2796 | */ |
| 2797 | static int afpSetLock( |
| 2798 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2799 | unixFile *pFile, /* Open file descriptor on path */ |
| 2800 | unsigned long long offset, /* First byte to be locked */ |
| 2801 | unsigned long long length, /* Number of bytes to lock */ |
| 2802 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2803 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2804 | struct ByteRangeLockPB2 pb; |
| 2805 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2806 | |
| 2807 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2808 | pb.startEndFlag = 0; |
| 2809 | pb.offset = offset; |
| 2810 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2811 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2812 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2813 | OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2814 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2815 | offset, length)); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2816 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2817 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2818 | int rc; |
| 2819 | int tErrno = errno; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2820 | OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2821 | path, tErrno, strerror(tErrno))); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2822 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2823 | rc = SQLITE_BUSY; |
| 2824 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2825 | rc = sqliteErrorFromPosixError(tErrno, |
| 2826 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2827 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2828 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2829 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2830 | } |
| 2831 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2832 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2833 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2834 | } |
| 2835 | } |
| 2836 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2837 | /* |
| 2838 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2839 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2840 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2841 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2842 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2843 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2844 | int rc = SQLITE_OK; |
| 2845 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2846 | unixFile *pFile = (unixFile*)id; |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2847 | afpLockingContext *context; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2848 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2849 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2850 | |
| 2851 | assert( pFile ); |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2852 | context = (afpLockingContext *) pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2853 | if( context->reserved ){ |
| 2854 | *pResOut = 1; |
| 2855 | return SQLITE_OK; |
| 2856 | } |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 2857 | sqlite3_mutex_enter(pFile->pInode->pLockMutex); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2858 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2859 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2860 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
| 2863 | /* Otherwise see if some other process holds it. |
| 2864 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2865 | if( !reserved ){ |
| 2866 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2867 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2868 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2869 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2870 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2871 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2872 | } else { |
| 2873 | /* if we failed to get the lock then someone else must have it */ |
| 2874 | reserved = 1; |
| 2875 | } |
| 2876 | if( IS_LOCK_ERROR(lrc) ){ |
| 2877 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2878 | } |
| 2879 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2880 | |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 2881 | sqlite3_mutex_leave(pFile->pInode->pLockMutex); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2882 | OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2883 | |
| 2884 | *pResOut = reserved; |
| 2885 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2888 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2889 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2890 | ** of the following: |
| 2891 | ** |
| 2892 | ** (1) SHARED_LOCK |
| 2893 | ** (2) RESERVED_LOCK |
| 2894 | ** (3) PENDING_LOCK |
| 2895 | ** (4) EXCLUSIVE_LOCK |
| 2896 | ** |
| 2897 | ** Sometimes when requesting one lock state, additional lock states |
| 2898 | ** are inserted in between. The locking might fail on one of the later |
| 2899 | ** transitions leaving the lock state different from what it started but |
| 2900 | ** still short of its goal. The following chart shows the allowed |
| 2901 | ** transitions and the inserted intermediate states: |
| 2902 | ** |
| 2903 | ** UNLOCKED -> SHARED |
| 2904 | ** SHARED -> RESERVED |
| 2905 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2906 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2907 | ** PENDING -> EXCLUSIVE |
| 2908 | ** |
| 2909 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2910 | ** routine to lower a locking level. |
| 2911 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2912 | static int afpLock(sqlite3_file *id, int eFileLock){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2913 | int rc = SQLITE_OK; |
| 2914 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2915 | unixInodeInfo *pInode = pFile->pInode; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2916 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2917 | |
| 2918 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2919 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 2920 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2921 | azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0))); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2922 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2923 | /* 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] | 2924 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2925 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2926 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2927 | if( pFile->eFileLock>=eFileLock ){ |
| 2928 | OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
| 2929 | azFileLock(eFileLock))); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2930 | return SQLITE_OK; |
| 2931 | } |
| 2932 | |
| 2933 | /* Make sure the locking sequence is correct |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2934 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 2935 | ** (2) SQLite never explicitly requests a pendig lock. |
| 2936 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2937 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2938 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 2939 | assert( eFileLock!=PENDING_LOCK ); |
| 2940 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2941 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2942 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2943 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2944 | pInode = pFile->pInode; |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 2945 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2946 | |
| 2947 | /* If some thread using this PID has a lock via a different unixFile* |
| 2948 | ** handle that precludes the requested lock, return BUSY. |
| 2949 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2950 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 2951 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2952 | ){ |
| 2953 | rc = SQLITE_BUSY; |
| 2954 | goto afp_end_lock; |
| 2955 | } |
| 2956 | |
| 2957 | /* If a SHARED lock is requested, and some thread using this PID already |
| 2958 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 2959 | ** return SQLITE_OK. |
| 2960 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2961 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2962 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2963 | assert( eFileLock==SHARED_LOCK ); |
| 2964 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2965 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2966 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2967 | pInode->nShared++; |
| 2968 | pInode->nLock++; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2969 | goto afp_end_lock; |
| 2970 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2971 | |
| 2972 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2973 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2974 | ** be released. |
| 2975 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2976 | if( eFileLock==SHARED_LOCK |
| 2977 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2978 | ){ |
| 2979 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2980 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2981 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2982 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2983 | goto afp_end_lock; |
| 2984 | } |
| 2985 | } |
| 2986 | |
| 2987 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2988 | ** operating system calls for the specified lock. |
| 2989 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2990 | if( eFileLock==SHARED_LOCK ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2991 | int lrc1, lrc2, lrc1Errno = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2992 | long lk, mask; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2993 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2994 | assert( pInode->nShared==0 ); |
| 2995 | assert( pInode->eFileLock==0 ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2996 | |
| 2997 | mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2998 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2999 | /* note that the quality of the randomness doesn't matter that much */ |
| 3000 | lk = random(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3001 | pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3002 | lrc1 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3003 | SHARED_FIRST+pInode->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3004 | if( IS_LOCK_ERROR(lrc1) ){ |
| 3005 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3006 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3007 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3008 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3009 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3010 | if( IS_LOCK_ERROR(lrc1) ) { |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3011 | storeLastErrno(pFile, lrc1Errno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3012 | rc = lrc1; |
| 3013 | goto afp_end_lock; |
| 3014 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 3015 | rc = lrc2; |
| 3016 | goto afp_end_lock; |
| 3017 | } else if( lrc1 != SQLITE_OK ) { |
| 3018 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3019 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3020 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3021 | pInode->nLock++; |
| 3022 | pInode->nShared = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3023 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3024 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3025 | /* We are trying for an exclusive lock but another thread in this |
| 3026 | ** same process is still holding a shared lock. */ |
| 3027 | rc = SQLITE_BUSY; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3028 | }else{ |
| 3029 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 3030 | ** assumed that there is a SHARED or greater lock on the file |
| 3031 | ** already. |
| 3032 | */ |
| 3033 | int failed = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3034 | assert( 0!=pFile->eFileLock ); |
| 3035 | if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3036 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3037 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3038 | if( !failed ){ |
| 3039 | context->reserved = 1; |
| 3040 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3041 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3042 | if (!failed && eFileLock == EXCLUSIVE_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3043 | /* Acquire an EXCLUSIVE lock */ |
| 3044 | |
| 3045 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3046 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3047 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3048 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3049 | pInode->sharedByte, 1, 0)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3050 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3051 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3052 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3053 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3054 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3055 | SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3056 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 3057 | ** a critical I/O error |
| 3058 | */ |
drh | 2e23381 | 2017-08-22 15:21:54 +0000 | [diff] [blame] | 3059 | rc = ((failed & 0xff) == SQLITE_IOERR) ? failed2 : |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3060 | SQLITE_IOERR_LOCK; |
| 3061 | goto afp_end_lock; |
| 3062 | } |
| 3063 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3064 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3065 | } |
| 3066 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3067 | if( failed ){ |
| 3068 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3069 | } |
| 3070 | } |
| 3071 | |
| 3072 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3073 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3074 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3075 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 3076 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3077 | pInode->eFileLock = PENDING_LOCK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
| 3080 | afp_end_lock: |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 3081 | sqlite3_mutex_leave(pInode->pLockMutex); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3082 | OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), |
| 3083 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3084 | return rc; |
| 3085 | } |
| 3086 | |
| 3087 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3088 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3089 | ** must be either NO_LOCK or SHARED_LOCK. |
| 3090 | ** |
| 3091 | ** If the locking level of the file descriptor is already at or below |
| 3092 | ** the requested locking level, this routine is a no-op. |
| 3093 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3094 | static int afpUnlock(sqlite3_file *id, int eFileLock) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3095 | int rc = SQLITE_OK; |
| 3096 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3097 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3098 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 3099 | int skipShared = 0; |
| 3100 | #ifdef SQLITE_TEST |
| 3101 | int h = pFile->h; |
| 3102 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3103 | |
| 3104 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3105 | 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] | 3106 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 3107 | osGetpid(0))); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3108 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3109 | assert( eFileLock<=SHARED_LOCK ); |
| 3110 | if( pFile->eFileLock<=eFileLock ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3111 | return SQLITE_OK; |
| 3112 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3113 | pInode = pFile->pInode; |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 3114 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3115 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3116 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3117 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3118 | SimulateIOErrorBenign(1); |
| 3119 | SimulateIOError( h=(-1) ) |
| 3120 | SimulateIOErrorBenign(0); |
| 3121 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3122 | #ifdef SQLITE_DEBUG |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3123 | /* When reducing a lock such that other processes can start |
| 3124 | ** reading the database file again, make sure that the |
| 3125 | ** transaction counter was updated if any part of the database |
| 3126 | ** file changed. If the transaction counter is not updated, |
| 3127 | ** other connections to the same file might not realize that |
| 3128 | ** the file has changed and hence might not know to flush their |
| 3129 | ** cache. The use of a stale cache can lead to database corruption. |
| 3130 | */ |
| 3131 | assert( pFile->inNormalWrite==0 |
| 3132 | || pFile->dbUpdate==0 |
| 3133 | || pFile->transCntrChng==1 ); |
| 3134 | pFile->inNormalWrite = 0; |
| 3135 | #endif |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3136 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3137 | if( pFile->eFileLock==EXCLUSIVE_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3138 | rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3139 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3140 | /* only re-establish the shared lock if necessary */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3141 | int sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3142 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 3143 | } else { |
| 3144 | skipShared = 1; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3145 | } |
| 3146 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3147 | if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3148 | rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3149 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3150 | if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3151 | rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 3152 | if( !rc ){ |
| 3153 | context->reserved = 0; |
| 3154 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3155 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3156 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ |
| 3157 | pInode->eFileLock = SHARED_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3158 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3159 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3160 | if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3161 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3162 | /* Decrement the shared lock counter. Release the lock using an |
| 3163 | ** OS call only when all threads in this same process have released |
| 3164 | ** the lock. |
| 3165 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3166 | unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 3167 | pInode->nShared--; |
| 3168 | if( pInode->nShared==0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3169 | SimulateIOErrorBenign(1); |
| 3170 | SimulateIOError( h=(-1) ) |
| 3171 | SimulateIOErrorBenign(0); |
| 3172 | if( !skipShared ){ |
| 3173 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 3174 | } |
| 3175 | if( !rc ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3176 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3177 | pFile->eFileLock = NO_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3178 | } |
| 3179 | } |
| 3180 | if( rc==SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3181 | pInode->nLock--; |
| 3182 | assert( pInode->nLock>=0 ); |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 3183 | if( pInode->nLock==0 ) closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3184 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3185 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3186 | |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 3187 | sqlite3_mutex_leave(pInode->pLockMutex); |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 3188 | if( rc==SQLITE_OK ){ |
| 3189 | pFile->eFileLock = eFileLock; |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 3190 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3191 | return rc; |
| 3192 | } |
| 3193 | |
| 3194 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3195 | ** Close a file & cleanup AFP specific locking context |
| 3196 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3197 | static int afpClose(sqlite3_file *id) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3198 | int rc = SQLITE_OK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 3199 | unixFile *pFile = (unixFile*)id; |
| 3200 | assert( id!=0 ); |
| 3201 | afpUnlock(id, NO_LOCK); |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 3202 | assert( unixFileMutexNotheld(pFile) ); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 3203 | unixEnterMutex(); |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 3204 | if( pFile->pInode ){ |
| 3205 | unixInodeInfo *pInode = pFile->pInode; |
| 3206 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | cb4e4b0 | 2018-09-06 19:36:29 +0000 | [diff] [blame] | 3207 | if( pInode->nLock ){ |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 3208 | /* If there are outstanding locks, do not actually close the file just |
| 3209 | ** yet because that would clear those locks. Instead, add the file |
| 3210 | ** descriptor to pInode->aPending. It will be automatically closed when |
| 3211 | ** the last lock is cleared. |
| 3212 | */ |
| 3213 | setPendingFd(pFile); |
| 3214 | } |
| 3215 | sqlite3_mutex_leave(pInode->pLockMutex); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3216 | } |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 3217 | releaseInodeInfo(pFile); |
| 3218 | sqlite3_free(pFile->lockingContext); |
| 3219 | rc = closeUnixFile(id); |
| 3220 | unixLeaveMutex(); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3221 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3222 | } |
| 3223 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3224 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3225 | /* |
| 3226 | ** The code above is the AFP lock implementation. The code is specific |
| 3227 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 3228 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 3229 | ** VFS is not available. |
| 3230 | ** |
| 3231 | ********************* End of the AFP lock implementation ********************** |
| 3232 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3233 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3234 | /****************************************************************************** |
| 3235 | *************************** Begin NFS Locking ********************************/ |
| 3236 | |
| 3237 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 3238 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3239 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3240 | ** must be either NO_LOCK or SHARED_LOCK. |
| 3241 | ** |
| 3242 | ** If the locking level of the file descriptor is already at or below |
| 3243 | ** the requested locking level, this routine is a no-op. |
| 3244 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3245 | static int nfsUnlock(sqlite3_file *id, int eFileLock){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 3246 | return posixUnlock(id, eFileLock, 1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 3250 | /* |
| 3251 | ** The code above is the NFS lock implementation. The code is specific |
| 3252 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 3253 | ** is available. |
| 3254 | ** |
| 3255 | ********************* End of the NFS lock implementation ********************** |
| 3256 | ******************************************************************************/ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3257 | |
| 3258 | /****************************************************************************** |
| 3259 | **************** Non-locking sqlite3_file methods ***************************** |
| 3260 | ** |
| 3261 | ** The next division contains implementations for all methods of the |
| 3262 | ** sqlite3_file object other than the locking methods. The locking |
| 3263 | ** methods were defined in divisions above (one locking method per |
| 3264 | ** division). Those methods that are common to all locking modes |
| 3265 | ** are gather together into this division. |
| 3266 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3267 | |
| 3268 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3269 | ** Seek to the offset passed as the second argument, then read cnt |
| 3270 | ** bytes into pBuf. Return the number of bytes actually read. |
| 3271 | ** |
| 3272 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 3273 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 3274 | ** one system to another. Since SQLite does not define USE_PREAD |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3275 | ** in any form by default, we will not attempt to define _XOPEN_SOURCE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3276 | ** See tickets #2741 and #2681. |
| 3277 | ** |
| 3278 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 3279 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3280 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3281 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 3282 | int got; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3283 | int prior = 0; |
drh | a46cadc | 2016-03-04 03:02:06 +0000 | [diff] [blame] | 3284 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
| 3285 | i64 newOffset; |
| 3286 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3287 | TIMER_START; |
drh | c1fd2cf | 2012-10-01 12:16:26 +0000 | [diff] [blame] | 3288 | assert( cnt==(cnt&0x1ffff) ); |
drh | 35a0379 | 2013-08-29 23:34:53 +0000 | [diff] [blame] | 3289 | assert( id->h>2 ); |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3290 | do{ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3291 | #if defined(USE_PREAD) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3292 | got = osPread(id->h, pBuf, cnt, offset); |
| 3293 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3294 | #elif defined(USE_PREAD64) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3295 | got = osPread64(id->h, pBuf, cnt, offset); |
| 3296 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3297 | #else |
drh | a46cadc | 2016-03-04 03:02:06 +0000 | [diff] [blame] | 3298 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 3299 | SimulateIOError( newOffset = -1 ); |
| 3300 | if( newOffset<0 ){ |
| 3301 | storeLastErrno((unixFile*)id, errno); |
| 3302 | return -1; |
| 3303 | } |
| 3304 | got = osRead(id->h, pBuf, cnt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3305 | #endif |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3306 | if( got==cnt ) break; |
| 3307 | if( got<0 ){ |
| 3308 | if( errno==EINTR ){ got = 1; continue; } |
| 3309 | prior = 0; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3310 | storeLastErrno((unixFile*)id, errno); |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3311 | break; |
| 3312 | }else if( got>0 ){ |
| 3313 | cnt -= got; |
| 3314 | offset += got; |
| 3315 | prior += got; |
| 3316 | pBuf = (void*)(got + (char*)pBuf); |
| 3317 | } |
| 3318 | }while( got>0 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3319 | TIMER_END; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3320 | OSTRACE(("READ %-3d %5d %7lld %llu\n", |
| 3321 | id->h, got+prior, offset-prior, TIMER_ELAPSED)); |
| 3322 | return got+prior; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3323 | } |
| 3324 | |
| 3325 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3326 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 3327 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 3328 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3329 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3330 | static int unixRead( |
| 3331 | sqlite3_file *id, |
| 3332 | void *pBuf, |
| 3333 | int amt, |
| 3334 | sqlite3_int64 offset |
| 3335 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3336 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3337 | int got; |
| 3338 | assert( id ); |
drh | 6cf9d8d | 2013-05-09 18:12:40 +0000 | [diff] [blame] | 3339 | assert( offset>=0 ); |
| 3340 | assert( amt>0 ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3341 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3342 | /* If this is a database file (not a journal, master-journal or temp |
| 3343 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3344 | #if 0 |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 3345 | assert( pFile->pPreallocatedUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3346 | || offset>=PENDING_BYTE+512 |
| 3347 | || offset+amt<=PENDING_BYTE |
| 3348 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3349 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3350 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3351 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 6c56963 | 2013-03-26 18:48:11 +0000 | [diff] [blame] | 3352 | /* Deal with as much of this read request as possible by transfering |
| 3353 | ** data from the memory mapping using memcpy(). */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3354 | if( offset<pFile->mmapSize ){ |
| 3355 | if( offset+amt <= pFile->mmapSize ){ |
| 3356 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt); |
| 3357 | return SQLITE_OK; |
| 3358 | }else{ |
| 3359 | int nCopy = pFile->mmapSize - offset; |
| 3360 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy); |
| 3361 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3362 | amt -= nCopy; |
| 3363 | offset += nCopy; |
| 3364 | } |
| 3365 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 3366 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3367 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3368 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3369 | if( got==amt ){ |
| 3370 | return SQLITE_OK; |
| 3371 | }else if( got<0 ){ |
| 3372 | /* lastErrno set by seekAndRead */ |
| 3373 | return SQLITE_IOERR_READ; |
| 3374 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3375 | storeLastErrno(pFile, 0); /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3376 | /* Unread parts of the buffer must be zero-filled */ |
| 3377 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 3378 | return SQLITE_IOERR_SHORT_READ; |
| 3379 | } |
| 3380 | } |
| 3381 | |
| 3382 | /* |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3383 | ** Attempt to seek the file-descriptor passed as the first argument to |
| 3384 | ** absolute offset iOff, then attempt to write nBuf bytes of data from |
| 3385 | ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise, |
| 3386 | ** return the actual number of bytes written (which may be less than |
| 3387 | ** nBuf). |
| 3388 | */ |
| 3389 | static int seekAndWriteFd( |
| 3390 | int fd, /* File descriptor to write to */ |
| 3391 | i64 iOff, /* File offset to begin writing at */ |
| 3392 | const void *pBuf, /* Copy data from this buffer to the file */ |
| 3393 | int nBuf, /* Size of buffer pBuf in bytes */ |
| 3394 | int *piErrno /* OUT: Error number if error occurs */ |
| 3395 | ){ |
| 3396 | int rc = 0; /* Value returned by system call */ |
| 3397 | |
| 3398 | assert( nBuf==(nBuf&0x1ffff) ); |
drh | 35a0379 | 2013-08-29 23:34:53 +0000 | [diff] [blame] | 3399 | assert( fd>2 ); |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3400 | assert( piErrno!=0 ); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3401 | nBuf &= 0x1ffff; |
| 3402 | TIMER_START; |
| 3403 | |
| 3404 | #if defined(USE_PREAD) |
drh | 2da47d3 | 2015-02-21 00:56:05 +0000 | [diff] [blame] | 3405 | do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR ); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3406 | #elif defined(USE_PREAD64) |
drh | 2da47d3 | 2015-02-21 00:56:05 +0000 | [diff] [blame] | 3407 | do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3408 | #else |
| 3409 | do{ |
| 3410 | i64 iSeek = lseek(fd, iOff, SEEK_SET); |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3411 | SimulateIOError( iSeek = -1 ); |
| 3412 | if( iSeek<0 ){ |
| 3413 | rc = -1; |
| 3414 | break; |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3415 | } |
| 3416 | rc = osWrite(fd, pBuf, nBuf); |
| 3417 | }while( rc<0 && errno==EINTR ); |
| 3418 | #endif |
| 3419 | |
| 3420 | TIMER_END; |
| 3421 | OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED)); |
| 3422 | |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3423 | if( rc<0 ) *piErrno = errno; |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3424 | return rc; |
| 3425 | } |
| 3426 | |
| 3427 | |
| 3428 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3429 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 3430 | ** Return the number of bytes actually read. Update the offset. |
| 3431 | ** |
| 3432 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 3433 | ** is set before returning. |
| 3434 | */ |
| 3435 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3436 | return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3437 | } |
| 3438 | |
| 3439 | |
| 3440 | /* |
| 3441 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 3442 | ** or some other error code on failure. |
| 3443 | */ |
| 3444 | static int unixWrite( |
| 3445 | sqlite3_file *id, |
| 3446 | const void *pBuf, |
| 3447 | int amt, |
| 3448 | sqlite3_int64 offset |
| 3449 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3450 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3451 | int wrote = 0; |
| 3452 | assert( id ); |
| 3453 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3454 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3455 | /* If this is a database file (not a journal, master-journal or temp |
| 3456 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3457 | #if 0 |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 3458 | assert( pFile->pPreallocatedUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3459 | || offset>=PENDING_BYTE+512 |
| 3460 | || offset+amt<=PENDING_BYTE |
| 3461 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3462 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3463 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3464 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3465 | /* If we are doing a normal write to a database file (as opposed to |
| 3466 | ** doing a hot-journal rollback or a write to some file other than a |
| 3467 | ** normal database file) then record the fact that the database |
| 3468 | ** has changed. If the transaction counter is modified, record that |
| 3469 | ** fact too. |
| 3470 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3471 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3472 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 3473 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3474 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3475 | char oldCntr[4]; |
| 3476 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3477 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3478 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3479 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3480 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 3481 | } |
| 3482 | } |
| 3483 | } |
| 3484 | #endif |
| 3485 | |
dan | fe33e39 | 2015-11-17 20:56:06 +0000 | [diff] [blame] | 3486 | #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3487 | /* Deal with as much of this write request as possible by transfering |
| 3488 | ** data from the memory mapping using memcpy(). */ |
| 3489 | if( offset<pFile->mmapSize ){ |
| 3490 | if( offset+amt <= pFile->mmapSize ){ |
| 3491 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt); |
| 3492 | return SQLITE_OK; |
| 3493 | }else{ |
| 3494 | int nCopy = pFile->mmapSize - offset; |
| 3495 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy); |
| 3496 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3497 | amt -= nCopy; |
| 3498 | offset += nCopy; |
| 3499 | } |
| 3500 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 3501 | #endif |
drh | 02bf8b4 | 2015-09-01 23:51:53 +0000 | [diff] [blame] | 3502 | |
| 3503 | while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3504 | amt -= wrote; |
| 3505 | offset += wrote; |
| 3506 | pBuf = &((char*)pBuf)[wrote]; |
| 3507 | } |
| 3508 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 3509 | SimulateDiskfullError(( wrote=0, amt=1 )); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3510 | |
drh | 02bf8b4 | 2015-09-01 23:51:53 +0000 | [diff] [blame] | 3511 | if( amt>wrote ){ |
drh | a21b83b | 2011-04-15 12:36:10 +0000 | [diff] [blame] | 3512 | if( wrote<0 && pFile->lastErrno!=ENOSPC ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3513 | /* lastErrno set by seekAndWrite */ |
| 3514 | return SQLITE_IOERR_WRITE; |
| 3515 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3516 | storeLastErrno(pFile, 0); /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3517 | return SQLITE_FULL; |
| 3518 | } |
| 3519 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3520 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3521 | return SQLITE_OK; |
| 3522 | } |
| 3523 | |
| 3524 | #ifdef SQLITE_TEST |
| 3525 | /* |
| 3526 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3527 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3528 | */ |
| 3529 | int sqlite3_sync_count = 0; |
| 3530 | int sqlite3_fullsync_count = 0; |
| 3531 | #endif |
| 3532 | |
| 3533 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3534 | ** We do not trust systems to provide a working fdatasync(). Some do. |
drh | 20f8e13 | 2011-08-31 21:01:55 +0000 | [diff] [blame] | 3535 | ** Others do no. To be safe, we will stick with the (slightly slower) |
| 3536 | ** fsync(). If you know that your system does support fdatasync() correctly, |
drh | f7a4a1b | 2015-01-10 18:02:45 +0000 | [diff] [blame] | 3537 | ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3538 | */ |
drh | f7a4a1b | 2015-01-10 18:02:45 +0000 | [diff] [blame] | 3539 | #if !defined(fdatasync) && !HAVE_FDATASYNC |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3540 | # define fdatasync fsync |
| 3541 | #endif |
| 3542 | |
| 3543 | /* |
| 3544 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 3545 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 3546 | ** only available on Mac OS X. But that could change. |
| 3547 | */ |
| 3548 | #ifdef F_FULLFSYNC |
| 3549 | # define HAVE_FULLFSYNC 1 |
| 3550 | #else |
| 3551 | # define HAVE_FULLFSYNC 0 |
| 3552 | #endif |
| 3553 | |
| 3554 | |
| 3555 | /* |
| 3556 | ** The fsync() system call does not work as advertised on many |
| 3557 | ** unix systems. The following procedure is an attempt to make |
| 3558 | ** it work better. |
| 3559 | ** |
| 3560 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 3561 | ** for testing when we want to run through the test suite quickly. |
| 3562 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 3563 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 3564 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3565 | ** |
| 3566 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 3567 | ** The idea behind dataOnly is that it should only write the file content |
| 3568 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 3569 | ** unchanged since the file size is part of the inode. However, |
| 3570 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 3571 | ** file size has changed. The only real difference between fdatasync() |
| 3572 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 3573 | ** inode if the mtime or owner or other inode attributes have changed. |
| 3574 | ** We only care about the file size, not the other file attributes, so |
| 3575 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 3576 | ** So, we always use fdatasync() if it is available, regardless of |
| 3577 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3578 | */ |
| 3579 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3580 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3581 | |
| 3582 | /* The following "ifdef/elif/else/" block has the same structure as |
| 3583 | ** the one below. It is replicated here solely to avoid cluttering |
| 3584 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 3585 | */ |
| 3586 | #ifdef SQLITE_NO_SYNC |
| 3587 | UNUSED_PARAMETER(fd); |
| 3588 | UNUSED_PARAMETER(fullSync); |
| 3589 | UNUSED_PARAMETER(dataOnly); |
| 3590 | #elif HAVE_FULLFSYNC |
| 3591 | UNUSED_PARAMETER(dataOnly); |
| 3592 | #else |
| 3593 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3594 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3595 | #endif |
| 3596 | |
| 3597 | /* Record the number of times that we do a normal fsync() and |
| 3598 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 3599 | ** gets called with the correct arguments. |
| 3600 | */ |
| 3601 | #ifdef SQLITE_TEST |
| 3602 | if( fullSync ) sqlite3_fullsync_count++; |
| 3603 | sqlite3_sync_count++; |
| 3604 | #endif |
| 3605 | |
| 3606 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
drh | 2c8fd12 | 2015-12-02 02:33:36 +0000 | [diff] [blame] | 3607 | ** no-op. But go ahead and call fstat() to validate the file |
| 3608 | ** descriptor as we need a method to provoke a failure during |
| 3609 | ** coverate testing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3610 | */ |
| 3611 | #ifdef SQLITE_NO_SYNC |
drh | 2c8fd12 | 2015-12-02 02:33:36 +0000 | [diff] [blame] | 3612 | { |
| 3613 | struct stat buf; |
| 3614 | rc = osFstat(fd, &buf); |
| 3615 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3616 | #elif HAVE_FULLFSYNC |
| 3617 | if( fullSync ){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3618 | rc = osFcntl(fd, F_FULLFSYNC, 0); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3619 | }else{ |
| 3620 | rc = 1; |
| 3621 | } |
| 3622 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3623 | ** It shouldn't be possible for fullfsync to fail on the local |
| 3624 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 3625 | ** isn't supported for this file system. So, attempt an fsync |
| 3626 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 3627 | ** It'd be better to detect fullfsync support once and avoid |
| 3628 | ** the fcntl call every time sync is called. |
| 3629 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3630 | if( rc ) rc = fsync(fd); |
| 3631 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3632 | #elif defined(__APPLE__) |
| 3633 | /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 3634 | ** so currently we default to the macro that redefines fdatasync to fsync |
| 3635 | */ |
| 3636 | rc = fsync(fd); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3637 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3638 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 3639 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3640 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3641 | rc = fsync(fd); |
| 3642 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3643 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3644 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 3645 | |
| 3646 | if( OS_VXWORKS && rc!= -1 ){ |
| 3647 | rc = 0; |
| 3648 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3649 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3652 | /* |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3653 | ** Open a file descriptor to the directory containing file zFilename. |
| 3654 | ** If successful, *pFd is set to the opened file descriptor and |
| 3655 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 3656 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 3657 | ** value. |
| 3658 | ** |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3659 | ** The directory file descriptor is used for only one thing - to |
| 3660 | ** fsync() a directory to make sure file creation and deletion events |
| 3661 | ** are flushed to disk. Such fsyncs are not needed on newer |
| 3662 | ** journaling filesystems, but are required on older filesystems. |
| 3663 | ** |
| 3664 | ** This routine can be overridden using the xSetSysCall interface. |
| 3665 | ** The ability to override this routine was added in support of the |
| 3666 | ** chromium sandbox. Opening a directory is a security risk (we are |
| 3667 | ** told) so making it overrideable allows the chromium sandbox to |
| 3668 | ** replace this routine with a harmless no-op. To make this routine |
| 3669 | ** a no-op, replace it with a stub that returns SQLITE_OK but leaves |
| 3670 | ** *pFd set to a negative number. |
| 3671 | ** |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3672 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 3673 | ** the file descriptor *pFd using close(). |
| 3674 | */ |
| 3675 | static int openDirectory(const char *zFilename, int *pFd){ |
| 3676 | int ii; |
| 3677 | int fd = -1; |
| 3678 | char zDirname[MAX_PATHNAME+1]; |
| 3679 | |
| 3680 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
drh | dc27851 | 2015-12-07 18:18:33 +0000 | [diff] [blame] | 3681 | for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--); |
| 3682 | if( ii>0 ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3683 | zDirname[ii] = '\0'; |
drh | dc27851 | 2015-12-07 18:18:33 +0000 | [diff] [blame] | 3684 | }else{ |
| 3685 | if( zDirname[0]!='/' ) zDirname[0] = '.'; |
| 3686 | zDirname[1] = 0; |
| 3687 | } |
| 3688 | fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0); |
| 3689 | if( fd>=0 ){ |
| 3690 | OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3691 | } |
| 3692 | *pFd = fd; |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 3693 | if( fd>=0 ) return SQLITE_OK; |
| 3694 | return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname); |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3698 | ** Make sure all writes to a particular file are committed to disk. |
| 3699 | ** |
| 3700 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 3701 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 3702 | ** file data is synced. |
| 3703 | ** |
| 3704 | ** Under Unix, also make sure that the directory entry for the file |
| 3705 | ** has been created by fsync-ing the directory that contains the file. |
| 3706 | ** If we do not do this and we encounter a power failure, the directory |
| 3707 | ** entry for the journal might not exist after we reboot. The next |
| 3708 | ** SQLite to access the file will not know that the journal exists (because |
| 3709 | ** the directory entry for the journal was never created) and the transaction |
| 3710 | ** will not roll back - possibly leading to database corruption. |
| 3711 | */ |
| 3712 | static int unixSync(sqlite3_file *id, int flags){ |
| 3713 | int rc; |
| 3714 | unixFile *pFile = (unixFile*)id; |
| 3715 | |
| 3716 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 3717 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 3718 | |
| 3719 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 3720 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 3721 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 3722 | ); |
| 3723 | |
| 3724 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 3725 | ** line is to test that doing so does not cause any problems. |
| 3726 | */ |
| 3727 | SimulateDiskfullError( return SQLITE_FULL ); |
| 3728 | |
| 3729 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3730 | OSTRACE(("SYNC %-3d\n", pFile->h)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3731 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 3732 | SimulateIOError( rc=1 ); |
| 3733 | if( rc ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3734 | storeLastErrno(pFile, errno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3735 | return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3736 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3737 | |
| 3738 | /* Also fsync the directory containing the file if the DIRSYNC flag |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 3739 | ** is set. This is a one-time occurrence. Many systems (examples: AIX) |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3740 | ** are unable to fsync a directory, so ignore errors on the fsync. |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3741 | */ |
| 3742 | if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){ |
| 3743 | int dirfd; |
| 3744 | OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3745 | HAVE_FULLFSYNC, isFullsync)); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3746 | rc = osOpenDirectory(pFile->zPath, &dirfd); |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 3747 | if( rc==SQLITE_OK ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3748 | full_fsync(dirfd, 0, 0); |
| 3749 | robust_close(pFile, dirfd, __LINE__); |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 3750 | }else{ |
| 3751 | assert( rc==SQLITE_CANTOPEN ); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 3752 | rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3753 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3754 | pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3755 | } |
| 3756 | return rc; |
| 3757 | } |
| 3758 | |
| 3759 | /* |
| 3760 | ** Truncate an open file to a specified size |
| 3761 | */ |
| 3762 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3763 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3764 | int rc; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3765 | assert( pFile ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3766 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3767 | |
| 3768 | /* If the user has configured a chunk-size for this file, truncate the |
| 3769 | ** file so that it consists of an integer number of chunks (i.e. the |
| 3770 | ** actual file size after the operation may be larger than the requested |
| 3771 | ** size). |
| 3772 | */ |
drh | b8af4b7 | 2012-04-05 20:04:39 +0000 | [diff] [blame] | 3773 | if( pFile->szChunk>0 ){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3774 | nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 3775 | } |
| 3776 | |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 3777 | rc = robust_ftruncate(pFile->h, nByte); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3778 | if( rc ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3779 | storeLastErrno(pFile, errno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3780 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3781 | }else{ |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3782 | #ifdef SQLITE_DEBUG |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3783 | /* If we are doing a normal write to a database file (as opposed to |
| 3784 | ** doing a hot-journal rollback or a write to some file other than a |
| 3785 | ** normal database file) and we truncate the file to zero length, |
| 3786 | ** that effectively updates the change counter. This might happen |
| 3787 | ** when restoring a database using the backup API from a zero-length |
| 3788 | ** source. |
| 3789 | */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3790 | if( pFile->inNormalWrite && nByte==0 ){ |
| 3791 | pFile->transCntrChng = 1; |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3792 | } |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3793 | #endif |
dan | c000331 | 2013-03-22 17:46:11 +0000 | [diff] [blame] | 3794 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3795 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | c000331 | 2013-03-22 17:46:11 +0000 | [diff] [blame] | 3796 | /* If the file was just truncated to a size smaller than the currently |
| 3797 | ** mapped region, reduce the effective mapping size as well. SQLite will |
| 3798 | ** use read() and write() to access data beyond this point from now on. |
| 3799 | */ |
| 3800 | if( nByte<pFile->mmapSize ){ |
| 3801 | pFile->mmapSize = nByte; |
| 3802 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3803 | #endif |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3804 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3805 | return SQLITE_OK; |
| 3806 | } |
| 3807 | } |
| 3808 | |
| 3809 | /* |
| 3810 | ** Determine the current size of a file in bytes |
| 3811 | */ |
| 3812 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 3813 | int rc; |
| 3814 | struct stat buf; |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 3815 | assert( id ); |
| 3816 | rc = osFstat(((unixFile*)id)->h, &buf); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3817 | SimulateIOError( rc=1 ); |
| 3818 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3819 | storeLastErrno((unixFile*)id, errno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3820 | return SQLITE_IOERR_FSTAT; |
| 3821 | } |
| 3822 | *pSize = buf.st_size; |
| 3823 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3824 | /* When opening a zero-size database, the findInodeInfo() procedure |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3825 | ** writes a single byte into that file in order to work around a bug |
| 3826 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 3827 | ** layers, we need to report this file size as zero even though it is |
| 3828 | ** really 1. Ticket #3260. |
| 3829 | */ |
| 3830 | if( *pSize==1 ) *pSize = 0; |
| 3831 | |
| 3832 | |
| 3833 | return SQLITE_OK; |
| 3834 | } |
| 3835 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3836 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3837 | /* |
| 3838 | ** Handler for proxy-locking file-control verbs. Defined below in the |
| 3839 | ** proxying locking division. |
| 3840 | */ |
| 3841 | static int proxyFileControl(sqlite3_file*,int,void*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3842 | #endif |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3843 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3844 | /* |
| 3845 | ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 3846 | ** file-control operation. Enlarge the database to nBytes in size |
| 3847 | ** (rounded up to the next chunk-size). If the database is already |
| 3848 | ** nBytes or larger, this routine is a no-op. |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3849 | */ |
| 3850 | static int fcntlSizeHint(unixFile *pFile, i64 nByte){ |
mistachkin | d589a54 | 2011-08-30 01:23:34 +0000 | [diff] [blame] | 3851 | if( pFile->szChunk>0 ){ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3852 | i64 nSize; /* Required file size */ |
| 3853 | struct stat buf; /* Used to hold return values of fstat() */ |
| 3854 | |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3855 | if( osFstat(pFile->h, &buf) ){ |
| 3856 | return SQLITE_IOERR_FSTAT; |
| 3857 | } |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3858 | |
| 3859 | nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; |
| 3860 | if( nSize>(i64)buf.st_size ){ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3861 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3862 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3863 | /* The code below is handling the return value of osFallocate() |
| 3864 | ** correctly. posix_fallocate() is defined to "returns zero on success, |
| 3865 | ** or an error number on failure". See the manpage for details. */ |
| 3866 | int err; |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3867 | do{ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3868 | err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size); |
| 3869 | }while( err==EINTR ); |
drh | 789df14 | 2018-06-02 14:37:39 +0000 | [diff] [blame] | 3870 | if( err && err!=EINVAL ) return SQLITE_IOERR_WRITE; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3871 | #else |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3872 | /* If the OS does not have posix_fallocate(), fake it. Write a |
| 3873 | ** single byte to the last byte in each block that falls entirely |
| 3874 | ** within the extended region. Then, if required, a single byte |
| 3875 | ** at offset (nSize-1), to set the size of the file correctly. |
| 3876 | ** This is a similar technique to that used by glibc on systems |
| 3877 | ** that do not have a real fallocate() call. |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3878 | */ |
| 3879 | int nBlk = buf.st_blksize; /* File-system block size */ |
dan | ef3d66c | 2015-01-06 21:31:47 +0000 | [diff] [blame] | 3880 | int nWrite = 0; /* Number of bytes written by seekAndWrite */ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3881 | i64 iWrite; /* Next offset to write to */ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3882 | |
drh | 053378d | 2015-12-01 22:09:42 +0000 | [diff] [blame] | 3883 | iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1; |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3884 | assert( iWrite>=buf.st_size ); |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3885 | assert( ((iWrite+1)%nBlk)==0 ); |
drh | 053378d | 2015-12-01 22:09:42 +0000 | [diff] [blame] | 3886 | for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){ |
| 3887 | if( iWrite>=nSize ) iWrite = nSize - 1; |
dan | ef3d66c | 2015-01-06 21:31:47 +0000 | [diff] [blame] | 3888 | nWrite = seekAndWrite(pFile, iWrite, "", 1); |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3889 | if( nWrite!=1 ) return SQLITE_IOERR_WRITE; |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3890 | } |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3891 | #endif |
| 3892 | } |
| 3893 | } |
| 3894 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3895 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3896 | if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3897 | int rc; |
| 3898 | if( pFile->szChunk<=0 ){ |
| 3899 | if( robust_ftruncate(pFile->h, nByte) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3900 | storeLastErrno(pFile, errno); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3901 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
| 3902 | } |
| 3903 | } |
| 3904 | |
| 3905 | rc = unixMapfile(pFile, nByte); |
| 3906 | return rc; |
| 3907 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3908 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3909 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3910 | return SQLITE_OK; |
| 3911 | } |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3912 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3913 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3914 | ** If *pArg is initially negative then this is a query. Set *pArg to |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3915 | ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set. |
| 3916 | ** |
| 3917 | ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags. |
| 3918 | */ |
| 3919 | static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){ |
| 3920 | if( *pArg<0 ){ |
| 3921 | *pArg = (pFile->ctrlFlags & mask)!=0; |
| 3922 | }else if( (*pArg)==0 ){ |
| 3923 | pFile->ctrlFlags &= ~mask; |
| 3924 | }else{ |
| 3925 | pFile->ctrlFlags |= mask; |
| 3926 | } |
| 3927 | } |
| 3928 | |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3929 | /* Forward declaration */ |
| 3930 | static int unixGetTempname(int nBuf, char *zBuf); |
| 3931 | |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3932 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3933 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3934 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3935 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3936 | unixFile *pFile = (unixFile*)id; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3937 | switch( op ){ |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 3938 | #if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3939 | case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: { |
| 3940 | int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE); |
drh | 344f763 | 2017-07-28 13:18:35 +0000 | [diff] [blame] | 3941 | return rc ? SQLITE_IOERR_BEGIN_ATOMIC : SQLITE_OK; |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3942 | } |
| 3943 | case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: { |
| 3944 | int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE); |
drh | 344f763 | 2017-07-28 13:18:35 +0000 | [diff] [blame] | 3945 | return rc ? SQLITE_IOERR_COMMIT_ATOMIC : SQLITE_OK; |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3946 | } |
| 3947 | case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: { |
| 3948 | int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE); |
drh | 344f763 | 2017-07-28 13:18:35 +0000 | [diff] [blame] | 3949 | return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC : SQLITE_OK; |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3950 | } |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 3951 | #endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3952 | |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3953 | case SQLITE_FCNTL_LOCKSTATE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3954 | *(int*)pArg = pFile->eFileLock; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3955 | return SQLITE_OK; |
| 3956 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3957 | case SQLITE_FCNTL_LAST_ERRNO: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3958 | *(int*)pArg = pFile->lastErrno; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3959 | return SQLITE_OK; |
| 3960 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3961 | case SQLITE_FCNTL_CHUNK_SIZE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3962 | pFile->szChunk = *(int *)pArg; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3963 | return SQLITE_OK; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3964 | } |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3965 | case SQLITE_FCNTL_SIZE_HINT: { |
dan | da04ea4 | 2011-08-23 05:10:39 +0000 | [diff] [blame] | 3966 | int rc; |
| 3967 | SimulateIOErrorBenign(1); |
| 3968 | rc = fcntlSizeHint(pFile, *(i64 *)pArg); |
| 3969 | SimulateIOErrorBenign(0); |
| 3970 | return rc; |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3971 | } |
| 3972 | case SQLITE_FCNTL_PERSIST_WAL: { |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3973 | unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg); |
| 3974 | return SQLITE_OK; |
| 3975 | } |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3976 | case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { |
| 3977 | unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg); |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3978 | return SQLITE_OK; |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3979 | } |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 3980 | case SQLITE_FCNTL_VFSNAME: { |
| 3981 | *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName); |
| 3982 | return SQLITE_OK; |
| 3983 | } |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3984 | case SQLITE_FCNTL_TEMPFILENAME: { |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3985 | char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname ); |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3986 | if( zTFile ){ |
| 3987 | unixGetTempname(pFile->pVfs->mxPathname, zTFile); |
| 3988 | *(char**)pArg = zTFile; |
| 3989 | } |
| 3990 | return SQLITE_OK; |
| 3991 | } |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 3992 | case SQLITE_FCNTL_HAS_MOVED: { |
| 3993 | *(int*)pArg = fileHasMoved(pFile); |
| 3994 | return SQLITE_OK; |
| 3995 | } |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 3996 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 3997 | case SQLITE_FCNTL_LOCK_TIMEOUT: { |
| 3998 | pFile->iBusyTimeout = *(int*)pArg; |
| 3999 | return SQLITE_OK; |
| 4000 | } |
| 4001 | #endif |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 4002 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4003 | case SQLITE_FCNTL_MMAP_SIZE: { |
drh | 34f7490 | 2013-04-03 13:09:18 +0000 | [diff] [blame] | 4004 | i64 newLimit = *(i64*)pArg; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 4005 | int rc = SQLITE_OK; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4006 | if( newLimit>sqlite3GlobalConfig.mxMmap ){ |
| 4007 | newLimit = sqlite3GlobalConfig.mxMmap; |
| 4008 | } |
dan | 43c1e62 | 2017-08-07 18:13:28 +0000 | [diff] [blame] | 4009 | |
| 4010 | /* The value of newLimit may be eventually cast to (size_t) and passed |
mistachkin | e35395a | 2017-08-07 19:06:54 +0000 | [diff] [blame] | 4011 | ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a |
| 4012 | ** 64-bit type. */ |
dan | 089df50 | 2017-08-07 18:54:10 +0000 | [diff] [blame] | 4013 | if( newLimit>0 && sizeof(size_t)<8 ){ |
dan | 43c1e62 | 2017-08-07 18:13:28 +0000 | [diff] [blame] | 4014 | newLimit = (newLimit & 0x7FFFFFFF); |
| 4015 | } |
| 4016 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4017 | *(i64*)pArg = pFile->mmapSizeMax; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 4018 | if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4019 | pFile->mmapSizeMax = newLimit; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 4020 | if( pFile->mmapSize>0 ){ |
| 4021 | unixUnmapfile(pFile); |
| 4022 | rc = unixMapfile(pFile, -1); |
| 4023 | } |
dan | bcb8a86 | 2013-04-08 15:30:41 +0000 | [diff] [blame] | 4024 | } |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 4025 | return rc; |
dan | b2d3de3 | 2013-03-14 18:34:37 +0000 | [diff] [blame] | 4026 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 4027 | #endif |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 4028 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 4029 | /* The pager calls this method to signal that it has done |
| 4030 | ** a rollback and that the database is therefore unchanged and |
| 4031 | ** it hence it is OK for the transaction change counter to be |
| 4032 | ** unchanged. |
| 4033 | */ |
| 4034 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 4035 | ((unixFile*)id)->dbUpdate = 0; |
| 4036 | return SQLITE_OK; |
| 4037 | } |
| 4038 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4039 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4040 | case SQLITE_FCNTL_SET_LOCKPROXYFILE: |
| 4041 | case SQLITE_FCNTL_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4042 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4043 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4044 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 4045 | } |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 4046 | return SQLITE_NOTFOUND; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 4047 | } |
| 4048 | |
| 4049 | /* |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4050 | ** If pFd->sectorSize is non-zero when this function is called, it is a |
| 4051 | ** no-op. Otherwise, the values of pFd->sectorSize and |
| 4052 | ** pFd->deviceCharacteristics are set according to the file-system |
| 4053 | ** characteristics. |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4054 | ** |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4055 | ** There are two versions of this function. One for QNX and one for all |
| 4056 | ** other systems. |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4057 | */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4058 | #ifndef __QNXNTO__ |
| 4059 | static void setDeviceCharacteristics(unixFile *pFd){ |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 4060 | assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 ); |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4061 | if( pFd->sectorSize==0 ){ |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 4062 | #if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4063 | int res; |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame] | 4064 | u32 f = 0; |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 4065 | |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4066 | /* Check for support for F2FS atomic batch writes. */ |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame] | 4067 | res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f); |
| 4068 | if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE) ){ |
dan | 77b4f52 | 2017-07-27 18:34:00 +0000 | [diff] [blame] | 4069 | pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC; |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4070 | } |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 4071 | #endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4072 | |
| 4073 | /* Set the POWERSAFE_OVERWRITE flag if requested. */ |
| 4074 | if( pFd->ctrlFlags & UNIXFILE_PSOW ){ |
| 4075 | pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE; |
| 4076 | } |
| 4077 | |
| 4078 | pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 4079 | } |
| 4080 | } |
| 4081 | #else |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 4082 | #include <sys/dcmd_blk.h> |
| 4083 | #include <sys/statvfs.h> |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4084 | static void setDeviceCharacteristics(unixFile *pFile){ |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 4085 | if( pFile->sectorSize == 0 ){ |
| 4086 | struct statvfs fsInfo; |
| 4087 | |
| 4088 | /* Set defaults for non-supported filesystems */ |
| 4089 | pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 4090 | pFile->deviceCharacteristics = 0; |
| 4091 | if( fstatvfs(pFile->h, &fsInfo) == -1 ) { |
drh | a9be508 | 2018-01-15 14:32:37 +0000 | [diff] [blame] | 4092 | return; |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 4093 | } |
| 4094 | |
| 4095 | if( !strcmp(fsInfo.f_basetype, "tmp") ) { |
| 4096 | pFile->sectorSize = fsInfo.f_bsize; |
| 4097 | pFile->deviceCharacteristics = |
| 4098 | SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */ |
| 4099 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 4100 | ** the write succeeds */ |
| 4101 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 4102 | ** so it is ordered */ |
| 4103 | 0; |
| 4104 | }else if( strstr(fsInfo.f_basetype, "etfs") ){ |
| 4105 | pFile->sectorSize = fsInfo.f_bsize; |
| 4106 | pFile->deviceCharacteristics = |
| 4107 | /* etfs cluster size writes are atomic */ |
| 4108 | (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) | |
| 4109 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 4110 | ** the write succeeds */ |
| 4111 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 4112 | ** so it is ordered */ |
| 4113 | 0; |
| 4114 | }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){ |
| 4115 | pFile->sectorSize = fsInfo.f_bsize; |
| 4116 | pFile->deviceCharacteristics = |
| 4117 | SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */ |
| 4118 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 4119 | ** the write succeeds */ |
| 4120 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 4121 | ** so it is ordered */ |
| 4122 | 0; |
| 4123 | }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){ |
| 4124 | pFile->sectorSize = fsInfo.f_bsize; |
| 4125 | pFile->deviceCharacteristics = |
| 4126 | /* full bitset of atomics from max sector size and smaller */ |
| 4127 | ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 4128 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 4129 | ** so it is ordered */ |
| 4130 | 0; |
| 4131 | }else if( strstr(fsInfo.f_basetype, "dos") ){ |
| 4132 | pFile->sectorSize = fsInfo.f_bsize; |
| 4133 | pFile->deviceCharacteristics = |
| 4134 | /* full bitset of atomics from max sector size and smaller */ |
| 4135 | ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 4136 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 4137 | ** so it is ordered */ |
| 4138 | 0; |
| 4139 | }else{ |
| 4140 | pFile->deviceCharacteristics = |
| 4141 | SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */ |
| 4142 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 4143 | ** the write succeeds */ |
| 4144 | 0; |
| 4145 | } |
| 4146 | } |
| 4147 | /* Last chance verification. If the sector size isn't a multiple of 512 |
| 4148 | ** then it isn't valid.*/ |
| 4149 | if( pFile->sectorSize % 512 != 0 ){ |
| 4150 | pFile->deviceCharacteristics = 0; |
| 4151 | pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 4152 | } |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 4153 | } |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4154 | #endif |
| 4155 | |
| 4156 | /* |
| 4157 | ** Return the sector size in bytes of the underlying block device for |
| 4158 | ** the specified file. This is almost always 512 bytes, but may be |
| 4159 | ** larger for some devices. |
| 4160 | ** |
| 4161 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 4162 | ** if two files are created in the same file-system directory (i.e. |
| 4163 | ** a database and its journal file) that the sector size will be the |
| 4164 | ** same for both. |
| 4165 | */ |
| 4166 | static int unixSectorSize(sqlite3_file *id){ |
| 4167 | unixFile *pFd = (unixFile*)id; |
| 4168 | setDeviceCharacteristics(pFd); |
| 4169 | return pFd->sectorSize; |
| 4170 | } |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4171 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4172 | /* |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 4173 | ** Return the device characteristics for the file. |
| 4174 | ** |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 4175 | ** 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] | 4176 | ** However, that choice is controversial since technically the underlying |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 4177 | ** file system does not always provide powersafe overwrites. (In other |
| 4178 | ** words, after a power-loss event, parts of the file that were never |
| 4179 | ** written might end up being altered.) However, non-PSOW behavior is very, |
| 4180 | ** very rare. And asserting PSOW makes a large reduction in the amount |
| 4181 | ** of required I/O for journaling, since a lot of padding is eliminated. |
| 4182 | ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control |
| 4183 | ** 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] | 4184 | */ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 4185 | static int unixDeviceCharacteristics(sqlite3_file *id){ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4186 | unixFile *pFd = (unixFile*)id; |
| 4187 | setDeviceCharacteristics(pFd); |
| 4188 | return pFd->deviceCharacteristics; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 4189 | } |
| 4190 | |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 4191 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4192 | |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 4193 | /* |
| 4194 | ** Return the system page size. |
| 4195 | ** |
| 4196 | ** This function should not be called directly by other code in this file. |
| 4197 | ** Instead, it should be called via macro osGetpagesize(). |
| 4198 | */ |
| 4199 | static int unixGetpagesize(void){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4200 | #if OS_VXWORKS |
| 4201 | return 1024; |
| 4202 | #elif defined(_BSD_SOURCE) |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 4203 | return getpagesize(); |
| 4204 | #else |
| 4205 | return (int)sysconf(_SC_PAGESIZE); |
| 4206 | #endif |
| 4207 | } |
| 4208 | |
| 4209 | #endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */ |
| 4210 | |
| 4211 | #ifndef SQLITE_OMIT_WAL |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4212 | |
| 4213 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4214 | ** Object used to represent an shared memory buffer. |
| 4215 | ** |
| 4216 | ** When multiple threads all reference the same wal-index, each thread |
| 4217 | ** has its own unixShm object, but they all point to a single instance |
| 4218 | ** of this unixShmNode object. In other words, each wal-index is opened |
| 4219 | ** only once per process. |
| 4220 | ** |
| 4221 | ** Each unixShmNode object is connected to a single unixInodeInfo object. |
| 4222 | ** We could coalesce this object into unixInodeInfo, but that would mean |
| 4223 | ** every open file that does not use shared memory (in other words, most |
| 4224 | ** open files) would have to carry around this extra information. So |
| 4225 | ** the unixInodeInfo object contains a pointer to this unixShmNode object |
| 4226 | ** and the unixShmNode object is created only when needed. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4227 | ** |
| 4228 | ** unixMutexHeld() must be true when creating or destroying |
| 4229 | ** this object or while reading or writing the following fields: |
| 4230 | ** |
| 4231 | ** nRef |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4232 | ** |
| 4233 | ** The following fields are read-only after the object is created: |
| 4234 | ** |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4235 | ** hShm |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4236 | ** zFilename |
| 4237 | ** |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4238 | ** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4239 | ** unixMutexHeld() is true when reading or writing any other field |
| 4240 | ** in this structure. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4241 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4242 | struct unixShmNode { |
| 4243 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4244 | sqlite3_mutex *pShmMutex; /* Mutex to access this object */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4245 | char *zFilename; /* Name of the mmapped file */ |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4246 | int hShm; /* Open file descriptor */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4247 | int szRegion; /* Size of shared-memory regions */ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4248 | u16 nRegion; /* Size of array apRegion */ |
| 4249 | u8 isReadonly; /* True if read-only */ |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4250 | u8 isUnlocked; /* True if no DMS lock held */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4251 | char **apRegion; /* Array of mapped shared-memory regions */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4252 | int nRef; /* Number of unixShm objects pointing to this */ |
| 4253 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4254 | #ifdef SQLITE_DEBUG |
| 4255 | u8 exclMask; /* Mask of exclusive locks held */ |
| 4256 | u8 sharedMask; /* Mask of shared locks held */ |
| 4257 | u8 nextShmId; /* Next available unixShm.id value */ |
| 4258 | #endif |
| 4259 | }; |
| 4260 | |
| 4261 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4262 | ** Structure used internally by this VFS to record the state of an |
| 4263 | ** open shared memory connection. |
| 4264 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4265 | ** The following fields are initialized when this object is created and |
| 4266 | ** are read-only thereafter: |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4267 | ** |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4268 | ** unixShm.pShmNode |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4269 | ** unixShm.id |
| 4270 | ** |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4271 | ** All other fields are read/write. The unixShm.pShmNode->pShmMutex must |
| 4272 | ** be held while accessing any read/write fields. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4273 | */ |
| 4274 | struct unixShm { |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4275 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 4276 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4277 | u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */ |
drh | fd53231 | 2011-08-31 18:35:34 +0000 | [diff] [blame] | 4278 | u8 id; /* Id of this connection within its unixShmNode */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4279 | u16 sharedMask; /* Mask of shared locks held */ |
| 4280 | u16 exclMask; /* Mask of exclusive locks held */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4281 | }; |
| 4282 | |
| 4283 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4284 | ** Constants used for locking |
| 4285 | */ |
drh | bd9676c | 2010-06-23 17:58:38 +0000 | [diff] [blame] | 4286 | #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
drh | 4222441 | 2010-05-31 14:28:25 +0000 | [diff] [blame] | 4287 | #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4288 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4289 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4290 | ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4291 | ** |
| 4292 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 4293 | ** otherwise. |
| 4294 | */ |
| 4295 | static int unixShmSystemLock( |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4296 | unixFile *pFile, /* Open connection to the WAL file */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4297 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4298 | int ofst, /* First byte of the locking range */ |
| 4299 | int n /* Number of bytes to lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4300 | ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4301 | unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */ |
| 4302 | struct flock f; /* The posix advisory locking structure */ |
| 4303 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4304 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4305 | /* Access to the unixShmNode object is serialized by the caller */ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4306 | pShmNode = pFile->pInode->pShmNode; |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4307 | assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) ); |
drh | 9b7e8e1 | 2018-10-02 20:16:41 +0000 | [diff] [blame] | 4308 | assert( pShmNode->nRef>0 || unixMutexHeld() ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4309 | |
dan | 9181ae9 | 2017-10-26 17:05:22 +0000 | [diff] [blame] | 4310 | /* Shared locks never span more than one byte */ |
| 4311 | assert( n==1 || lockType!=F_RDLCK ); |
| 4312 | |
| 4313 | /* Locks are within range */ |
| 4314 | assert( n>=1 && n<=SQLITE_SHM_NLOCK ); |
| 4315 | |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4316 | if( pShmNode->hShm>=0 ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4317 | /* Initialize the locking parameters */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4318 | f.l_type = lockType; |
| 4319 | f.l_whence = SEEK_SET; |
| 4320 | f.l_start = ofst; |
| 4321 | f.l_len = n; |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4322 | rc = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4323 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 4324 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4325 | |
| 4326 | /* Update the global lock state and do debug tracing */ |
| 4327 | #ifdef SQLITE_DEBUG |
dan | 9181ae9 | 2017-10-26 17:05:22 +0000 | [diff] [blame] | 4328 | { u16 mask; |
| 4329 | OSTRACE(("SHM-LOCK ")); |
| 4330 | mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst); |
| 4331 | if( rc==SQLITE_OK ){ |
| 4332 | if( lockType==F_UNLCK ){ |
| 4333 | OSTRACE(("unlock %d ok", ofst)); |
| 4334 | pShmNode->exclMask &= ~mask; |
| 4335 | pShmNode->sharedMask &= ~mask; |
| 4336 | }else if( lockType==F_RDLCK ){ |
| 4337 | OSTRACE(("read-lock %d ok", ofst)); |
| 4338 | pShmNode->exclMask &= ~mask; |
| 4339 | pShmNode->sharedMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4340 | }else{ |
dan | 9181ae9 | 2017-10-26 17:05:22 +0000 | [diff] [blame] | 4341 | assert( lockType==F_WRLCK ); |
| 4342 | OSTRACE(("write-lock %d ok", ofst)); |
| 4343 | pShmNode->exclMask |= mask; |
| 4344 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4345 | } |
dan | 9181ae9 | 2017-10-26 17:05:22 +0000 | [diff] [blame] | 4346 | }else{ |
| 4347 | if( lockType==F_UNLCK ){ |
| 4348 | OSTRACE(("unlock %d failed", ofst)); |
| 4349 | }else if( lockType==F_RDLCK ){ |
| 4350 | OSTRACE(("read-lock failed")); |
| 4351 | }else{ |
| 4352 | assert( lockType==F_WRLCK ); |
| 4353 | OSTRACE(("write-lock %d failed", ofst)); |
| 4354 | } |
| 4355 | } |
| 4356 | OSTRACE((" - afterwards %03x,%03x\n", |
| 4357 | pShmNode->sharedMask, pShmNode->exclMask)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4358 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4359 | #endif |
| 4360 | |
| 4361 | return rc; |
| 4362 | } |
| 4363 | |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4364 | /* |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4365 | ** Return the minimum number of 32KB shm regions that should be mapped at |
| 4366 | ** a time, assuming that each mapping must be an integer multiple of the |
| 4367 | ** current system page-size. |
| 4368 | ** |
| 4369 | ** Usually, this is 1. The exception seems to be systems that are configured |
| 4370 | ** to use 64KB pages - in this case each mapping must cover at least two |
| 4371 | ** shm regions. |
| 4372 | */ |
| 4373 | static int unixShmRegionPerMap(void){ |
| 4374 | int shmsz = 32*1024; /* SHM region size */ |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 4375 | int pgsz = osGetpagesize(); /* System page size */ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4376 | assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */ |
| 4377 | if( pgsz<shmsz ) return 1; |
| 4378 | return pgsz/shmsz; |
| 4379 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4380 | |
| 4381 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4382 | ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4383 | ** |
| 4384 | ** This is not a VFS shared-memory method; it is a utility function called |
| 4385 | ** by VFS shared-memory methods. |
| 4386 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4387 | static void unixShmPurge(unixFile *pFd){ |
| 4388 | unixShmNode *p = pFd->pInode->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4389 | assert( unixMutexHeld() ); |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 4390 | if( p && ALWAYS(p->nRef==0) ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4391 | int nShmPerMap = unixShmRegionPerMap(); |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4392 | int i; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4393 | assert( p->pInode==pFd->pInode ); |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4394 | sqlite3_mutex_free(p->pShmMutex); |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4395 | for(i=0; i<p->nRegion; i+=nShmPerMap){ |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4396 | if( p->hShm>=0 ){ |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 4397 | osMunmap(p->apRegion[i], p->szRegion); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4398 | }else{ |
| 4399 | sqlite3_free(p->apRegion[i]); |
| 4400 | } |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4401 | } |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4402 | sqlite3_free(p->apRegion); |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4403 | if( p->hShm>=0 ){ |
| 4404 | robust_close(pFd, p->hShm, __LINE__); |
| 4405 | p->hShm = -1; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4406 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4407 | p->pInode->pShmNode = 0; |
| 4408 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4409 | } |
| 4410 | } |
| 4411 | |
| 4412 | /* |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4413 | ** The DMS lock has not yet been taken on shm file pShmNode. Attempt to |
| 4414 | ** take it now. Return SQLITE_OK if successful, or an SQLite error |
| 4415 | ** code otherwise. |
| 4416 | ** |
| 4417 | ** If the DMS cannot be locked because this is a readonly_shm=1 |
| 4418 | ** connection and no other process already holds a lock, return |
drh | 7e45e3a | 2017-11-08 17:32:12 +0000 | [diff] [blame] | 4419 | ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1. |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4420 | */ |
| 4421 | static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){ |
| 4422 | struct flock lock; |
| 4423 | int rc = SQLITE_OK; |
| 4424 | |
| 4425 | /* Use F_GETLK to determine the locks other processes are holding |
| 4426 | ** on the DMS byte. If it indicates that another process is holding |
| 4427 | ** a SHARED lock, then this process may also take a SHARED lock |
| 4428 | ** and proceed with opening the *-shm file. |
| 4429 | ** |
| 4430 | ** Or, if no other process is holding any lock, then this process |
| 4431 | ** is the first to open it. In this case take an EXCLUSIVE lock on the |
| 4432 | ** DMS byte and truncate the *-shm file to zero bytes in size. Then |
| 4433 | ** downgrade to a SHARED lock on the DMS byte. |
| 4434 | ** |
| 4435 | ** If another process is holding an EXCLUSIVE lock on the DMS byte, |
| 4436 | ** return SQLITE_BUSY to the caller (it will try again). An earlier |
| 4437 | ** version of this code attempted the SHARED lock at this point. But |
| 4438 | ** this introduced a subtle race condition: if the process holding |
| 4439 | ** EXCLUSIVE failed just before truncating the *-shm file, then this |
| 4440 | ** process might open and use the *-shm file without truncating it. |
| 4441 | ** And if the *-shm file has been corrupted by a power failure or |
| 4442 | ** system crash, the database itself may also become corrupt. */ |
| 4443 | lock.l_whence = SEEK_SET; |
| 4444 | lock.l_start = UNIX_SHM_DMS; |
| 4445 | lock.l_len = 1; |
| 4446 | lock.l_type = F_WRLCK; |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4447 | if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) { |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4448 | rc = SQLITE_IOERR_LOCK; |
| 4449 | }else if( lock.l_type==F_UNLCK ){ |
| 4450 | if( pShmNode->isReadonly ){ |
| 4451 | pShmNode->isUnlocked = 1; |
drh | 7e45e3a | 2017-11-08 17:32:12 +0000 | [diff] [blame] | 4452 | rc = SQLITE_READONLY_CANTINIT; |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4453 | }else{ |
| 4454 | rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1); |
drh | f7f2a82 | 2018-10-11 13:51:48 +0000 | [diff] [blame] | 4455 | /* The first connection to attach must truncate the -shm file. We |
| 4456 | ** truncate to 3 bytes (an arbitrary small number, less than the |
| 4457 | ** -shm header size) rather than 0 as a system debugging aid, to |
| 4458 | ** help detect if a -shm file truncation is legitimate or is the work |
| 4459 | ** or a rogue process. */ |
| 4460 | if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){ |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4461 | rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename); |
| 4462 | } |
| 4463 | } |
| 4464 | }else if( lock.l_type==F_WRLCK ){ |
| 4465 | rc = SQLITE_BUSY; |
| 4466 | } |
| 4467 | |
| 4468 | if( rc==SQLITE_OK ){ |
| 4469 | assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK ); |
| 4470 | rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1); |
| 4471 | } |
| 4472 | return rc; |
| 4473 | } |
| 4474 | |
| 4475 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4476 | ** Open a shared-memory area associated with open database file pDbFd. |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4477 | ** This particular implementation uses mmapped files. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4478 | ** |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4479 | ** The file used to implement shared-memory is in the same directory |
| 4480 | ** as the open database file and has the same name as the open database |
| 4481 | ** file with the "-shm" suffix added. For example, if the database file |
| 4482 | ** is "/home/user1/config.db" then the file that is created and mmapped |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4483 | ** for shared memory will be called "/home/user1/config.db-shm". |
| 4484 | ** |
| 4485 | ** Another approach to is to use files in /dev/shm or /dev/tmp or an |
| 4486 | ** some other tmpfs mount. But if a file in a different directory |
| 4487 | ** from the database file is used, then differing access permissions |
| 4488 | ** or a chroot() might cause two different processes on the same |
| 4489 | ** database to end up using different files for shared memory - |
| 4490 | ** meaning that their memory would not really be shared - resulting |
| 4491 | ** in database corruption. Nevertheless, this tmpfs file usage |
| 4492 | ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" |
| 4493 | ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time |
| 4494 | ** option results in an incompatible build of SQLite; builds of SQLite |
| 4495 | ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the |
| 4496 | ** same database file at the same time, database corruption will likely |
| 4497 | ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered |
| 4498 | ** "unsupported" and may go away in a future SQLite release. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4499 | ** |
| 4500 | ** When opening a new shared-memory file, if no other instances of that |
| 4501 | ** file are currently open, in this process or in other processes, then |
| 4502 | ** the file must be truncated to zero length or have its header cleared. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4503 | ** |
| 4504 | ** If the original database file (pDbFd) is using the "unix-excl" VFS |
| 4505 | ** that means that an exclusive lock is held on the database file and |
| 4506 | ** that no other processes are able to read or write the database. In |
| 4507 | ** that case, we do not really need shared memory. No shared memory |
| 4508 | ** file is created. The shared memory will be simulated with heap memory. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4509 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4510 | static int unixOpenSharedMemory(unixFile *pDbFd){ |
| 4511 | struct unixShm *p = 0; /* The connection to be opened */ |
| 4512 | struct unixShmNode *pShmNode; /* The underlying mmapped file */ |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4513 | int rc = SQLITE_OK; /* Result code */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4514 | unixInodeInfo *pInode; /* The inode of fd */ |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4515 | char *zShm; /* Name of the file used for SHM */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4516 | int nShmFilename; /* Size of the SHM filename in bytes */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4517 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4518 | /* Allocate space for the new unixShm object. */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4519 | p = sqlite3_malloc64( sizeof(*p) ); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4520 | if( p==0 ) return SQLITE_NOMEM_BKPT; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4521 | memset(p, 0, sizeof(*p)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4522 | assert( pDbFd->pShm==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4523 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4524 | /* Check to see if a unixShmNode object already exists. Reuse an existing |
| 4525 | ** one if present. Create a new one if necessary. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4526 | */ |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 4527 | assert( unixFileMutexNotheld(pDbFd) ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4528 | unixEnterMutex(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4529 | pInode = pDbFd->pInode; |
| 4530 | pShmNode = pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4531 | if( pShmNode==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4532 | struct stat sStat; /* fstat() info for database file */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4533 | #ifndef SQLITE_SHM_DIRECTORY |
| 4534 | const char *zBasePath = pDbFd->zPath; |
| 4535 | #endif |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4536 | |
| 4537 | /* Call fstat() to figure out the permissions on the database file. If |
| 4538 | ** 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] | 4539 | ** with the same permissions. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4540 | */ |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 4541 | if( osFstat(pDbFd->h, &sStat) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4542 | rc = SQLITE_IOERR_FSTAT; |
| 4543 | goto shm_open_err; |
| 4544 | } |
| 4545 | |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4546 | #ifdef SQLITE_SHM_DIRECTORY |
drh | 52bcde0 | 2012-01-03 14:50:45 +0000 | [diff] [blame] | 4547 | nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4548 | #else |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4549 | nShmFilename = 6 + (int)strlen(zBasePath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4550 | #endif |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4551 | pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4552 | if( pShmNode==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4553 | rc = SQLITE_NOMEM_BKPT; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4554 | goto shm_open_err; |
| 4555 | } |
drh | 9cb5a0d | 2012-01-05 21:19:54 +0000 | [diff] [blame] | 4556 | memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename); |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4557 | zShm = pShmNode->zFilename = (char*)&pShmNode[1]; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4558 | #ifdef SQLITE_SHM_DIRECTORY |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4559 | sqlite3_snprintf(nShmFilename, zShm, |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4560 | SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", |
| 4561 | (u32)sStat.st_ino, (u32)sStat.st_dev); |
| 4562 | #else |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4563 | sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath); |
| 4564 | sqlite3FileSuffix3(pDbFd->zPath, zShm); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4565 | #endif |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4566 | pShmNode->hShm = -1; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4567 | pDbFd->pInode->pShmNode = pShmNode; |
| 4568 | pShmNode->pInode = pDbFd->pInode; |
drh | 97a7e5e | 2016-04-26 18:58:54 +0000 | [diff] [blame] | 4569 | if( sqlite3GlobalConfig.bCoreMutex ){ |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4570 | pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 4571 | if( pShmNode->pShmMutex==0 ){ |
drh | 97a7e5e | 2016-04-26 18:58:54 +0000 | [diff] [blame] | 4572 | rc = SQLITE_NOMEM_BKPT; |
| 4573 | goto shm_open_err; |
| 4574 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4575 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4576 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4577 | if( pInode->bProcessLock==0 ){ |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4578 | if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4579 | pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT,(sStat.st_mode&0777)); |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 4580 | } |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4581 | if( pShmNode->hShm<0 ){ |
| 4582 | pShmNode->hShm = robust_open(zShm, O_RDONLY, (sStat.st_mode&0777)); |
| 4583 | if( pShmNode->hShm<0 ){ |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4584 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm); |
| 4585 | goto shm_open_err; |
| 4586 | } |
| 4587 | pShmNode->isReadonly = 1; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4588 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4589 | |
| 4590 | /* If this process is running as root, make sure that the SHM file |
| 4591 | ** is owned by the same user that owns the original database. Otherwise, |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 4592 | ** the original owner will not be able to connect. |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4593 | */ |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4594 | robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid); |
dan | 176b2a9 | 2017-11-01 06:59:19 +0000 | [diff] [blame] | 4595 | |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4596 | rc = unixLockSharedMemory(pDbFd, pShmNode); |
drh | 7e45e3a | 2017-11-08 17:32:12 +0000 | [diff] [blame] | 4597 | if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4598 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4599 | } |
| 4600 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4601 | /* Make the new connection a child of the unixShmNode */ |
| 4602 | p->pShmNode = pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4603 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4604 | p->id = pShmNode->nextShmId++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4605 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4606 | pShmNode->nRef++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4607 | pDbFd->pShm = p; |
| 4608 | unixLeaveMutex(); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 4609 | |
| 4610 | /* The reference count on pShmNode has already been incremented under |
| 4611 | ** the cover of the unixEnterMutex() mutex and the pointer from the |
| 4612 | ** new (struct unixShm) object to the pShmNode has been set. All that is |
| 4613 | ** left to do is to link the new object into the linked list starting |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4614 | ** at pShmNode->pFirst. This must be done while holding the |
| 4615 | ** pShmNode->pShmMutex. |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 4616 | */ |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4617 | sqlite3_mutex_enter(pShmNode->pShmMutex); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 4618 | p->pNext = pShmNode->pFirst; |
| 4619 | pShmNode->pFirst = p; |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4620 | sqlite3_mutex_leave(pShmNode->pShmMutex); |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4621 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4622 | |
| 4623 | /* Jump here on any error */ |
| 4624 | shm_open_err: |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4625 | unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4626 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4627 | unixLeaveMutex(); |
| 4628 | return rc; |
| 4629 | } |
| 4630 | |
| 4631 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4632 | ** This function is called to obtain a pointer to region iRegion of the |
| 4633 | ** shared-memory associated with the database file fd. Shared-memory regions |
| 4634 | ** are numbered starting from zero. Each shared-memory region is szRegion |
| 4635 | ** bytes in size. |
| 4636 | ** |
| 4637 | ** If an error occurs, an error code is returned and *pp is set to NULL. |
| 4638 | ** |
| 4639 | ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory |
| 4640 | ** region has not been allocated (by any client, including one running in a |
| 4641 | ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 4642 | ** bExtend is non-zero and the requested shared-memory region has not yet |
| 4643 | ** been allocated, it is allocated by this function. |
| 4644 | ** |
| 4645 | ** If the shared-memory region has already been allocated or is allocated by |
| 4646 | ** this call as described above, then it is mapped into this processes |
| 4647 | ** address space (if it is not already), *pp is set to point to the mapped |
| 4648 | ** memory and SQLITE_OK returned. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4649 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4650 | static int unixShmMap( |
| 4651 | sqlite3_file *fd, /* Handle open on database file */ |
| 4652 | int iRegion, /* Region to retrieve */ |
| 4653 | int szRegion, /* Size of regions */ |
| 4654 | int bExtend, /* True to extend file if necessary */ |
| 4655 | void volatile **pp /* OUT: Mapped memory */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4656 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4657 | unixFile *pDbFd = (unixFile*)fd; |
| 4658 | unixShm *p; |
| 4659 | unixShmNode *pShmNode; |
| 4660 | int rc = SQLITE_OK; |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4661 | int nShmPerMap = unixShmRegionPerMap(); |
| 4662 | int nReqRegion; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4663 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4664 | /* If the shared-memory file has not yet been opened, open it now. */ |
| 4665 | if( pDbFd->pShm==0 ){ |
| 4666 | rc = unixOpenSharedMemory(pDbFd); |
| 4667 | if( rc!=SQLITE_OK ) return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4668 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4669 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4670 | p = pDbFd->pShm; |
| 4671 | pShmNode = p->pShmNode; |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4672 | sqlite3_mutex_enter(pShmNode->pShmMutex); |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4673 | if( pShmNode->isUnlocked ){ |
| 4674 | rc = unixLockSharedMemory(pDbFd, pShmNode); |
| 4675 | if( rc!=SQLITE_OK ) goto shmpage_out; |
| 4676 | pShmNode->isUnlocked = 0; |
| 4677 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4678 | assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4679 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4680 | assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4681 | assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 ); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4682 | |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4683 | /* Minimum number of regions required to be mapped. */ |
| 4684 | nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap; |
| 4685 | |
| 4686 | if( pShmNode->nRegion<nReqRegion ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4687 | char **apNew; /* New apRegion[] array */ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4688 | int nByte = nReqRegion*szRegion; /* Minimum required file size */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4689 | struct stat sStat; /* Used by fstat() */ |
| 4690 | |
| 4691 | pShmNode->szRegion = szRegion; |
| 4692 | |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4693 | if( pShmNode->hShm>=0 ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4694 | /* The requested region is not mapped into this processes address space. |
| 4695 | ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 4696 | ** large enough to contain the requested region). |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4697 | */ |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4698 | if( osFstat(pShmNode->hShm, &sStat) ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4699 | rc = SQLITE_IOERR_SHMSIZE; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4700 | goto shmpage_out; |
| 4701 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4702 | |
| 4703 | if( sStat.st_size<nByte ){ |
| 4704 | /* The requested memory region does not exist. If bExtend is set to |
| 4705 | ** false, exit early. *pp will be set to NULL and SQLITE_OK returned. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4706 | */ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4707 | if( !bExtend ){ |
drh | 0fbb50e | 2012-11-13 10:54:12 +0000 | [diff] [blame] | 4708 | goto shmpage_out; |
| 4709 | } |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4710 | |
| 4711 | /* Alternatively, if bExtend is true, extend the file. Do this by |
| 4712 | ** writing a single byte to the end of each (OS) page being |
| 4713 | ** allocated or extended. Technically, we need only write to the |
| 4714 | ** last page in order to extend the file. But writing to all new |
| 4715 | ** pages forces the OS to allocate them immediately, which reduces |
| 4716 | ** the chances of SIGBUS while accessing the mapped region later on. |
| 4717 | */ |
| 4718 | else{ |
| 4719 | static const int pgsz = 4096; |
| 4720 | int iPg; |
| 4721 | |
| 4722 | /* Write to the last byte of each newly allocated or extended page */ |
| 4723 | assert( (nByte % pgsz)==0 ); |
| 4724 | for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){ |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 4725 | int x = 0; |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4726 | if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4727 | const char *zFile = pShmNode->zFilename; |
| 4728 | rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile); |
| 4729 | goto shmpage_out; |
| 4730 | } |
| 4731 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4732 | } |
| 4733 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4734 | } |
| 4735 | |
| 4736 | /* Map the requested memory region into this processes address space. */ |
| 4737 | apNew = (char **)sqlite3_realloc( |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4738 | pShmNode->apRegion, nReqRegion*sizeof(char *) |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4739 | ); |
| 4740 | if( !apNew ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4741 | rc = SQLITE_IOERR_NOMEM_BKPT; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4742 | goto shmpage_out; |
| 4743 | } |
| 4744 | pShmNode->apRegion = apNew; |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4745 | while( pShmNode->nRegion<nReqRegion ){ |
| 4746 | int nMap = szRegion*nShmPerMap; |
| 4747 | int i; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4748 | void *pMem; |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4749 | if( pShmNode->hShm>=0 ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4750 | pMem = osMmap(0, nMap, |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4751 | pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4752 | MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4753 | ); |
| 4754 | if( pMem==MAP_FAILED ){ |
drh | 50990db | 2011-04-13 20:26:13 +0000 | [diff] [blame] | 4755 | rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4756 | goto shmpage_out; |
| 4757 | } |
| 4758 | }else{ |
drh | b6c4d59 | 2018-10-11 02:39:11 +0000 | [diff] [blame] | 4759 | pMem = sqlite3_malloc64(nMap); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4760 | if( pMem==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4761 | rc = SQLITE_NOMEM_BKPT; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4762 | goto shmpage_out; |
| 4763 | } |
drh | b6c4d59 | 2018-10-11 02:39:11 +0000 | [diff] [blame] | 4764 | memset(pMem, 0, nMap); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4765 | } |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4766 | |
| 4767 | for(i=0; i<nShmPerMap; i++){ |
| 4768 | pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i]; |
| 4769 | } |
| 4770 | pShmNode->nRegion += nShmPerMap; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4771 | } |
| 4772 | } |
| 4773 | |
| 4774 | shmpage_out: |
| 4775 | if( pShmNode->nRegion>iRegion ){ |
| 4776 | *pp = pShmNode->apRegion[iRegion]; |
| 4777 | }else{ |
| 4778 | *pp = 0; |
| 4779 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4780 | if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4781 | sqlite3_mutex_leave(pShmNode->pShmMutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4782 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4783 | } |
| 4784 | |
| 4785 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4786 | ** Change the lock state for a shared-memory segment. |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4787 | ** |
| 4788 | ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little |
| 4789 | ** different here than in posix. In xShmLock(), one can go from unlocked |
| 4790 | ** to shared and back or from unlocked to exclusive and back. But one may |
| 4791 | ** not go from shared to exclusive or from exclusive to shared. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4792 | */ |
| 4793 | static int unixShmLock( |
| 4794 | sqlite3_file *fd, /* Database file holding the shared memory */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4795 | int ofst, /* First lock to acquire or release */ |
| 4796 | int n, /* Number of locks to acquire or release */ |
| 4797 | int flags /* What to do with the lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4798 | ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4799 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 4800 | unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 4801 | unixShm *pX; /* For looping over all siblings */ |
| 4802 | unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 4803 | int rc = SQLITE_OK; /* Result code */ |
| 4804 | u16 mask; /* Mask of locks to take or release */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4805 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4806 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4807 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4808 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4809 | assert( n>=1 ); |
| 4810 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 4811 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 4812 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 4813 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 4814 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4815 | assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4816 | assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4817 | |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4818 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4819 | assert( n>1 || mask==(1<<ofst) ); |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4820 | sqlite3_mutex_enter(pShmNode->pShmMutex); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4821 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 4822 | u16 allMask = 0; /* Mask of locks held by siblings */ |
| 4823 | |
| 4824 | /* See if any siblings hold this same lock */ |
| 4825 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 4826 | if( pX==p ) continue; |
| 4827 | assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
| 4828 | allMask |= pX->sharedMask; |
| 4829 | } |
| 4830 | |
| 4831 | /* Unlock the system-level locks */ |
| 4832 | if( (mask & allMask)==0 ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4833 | rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4834 | }else{ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4835 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4836 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4837 | |
| 4838 | /* Undo the local locks */ |
| 4839 | if( rc==SQLITE_OK ){ |
| 4840 | p->exclMask &= ~mask; |
| 4841 | p->sharedMask &= ~mask; |
| 4842 | } |
| 4843 | }else if( flags & SQLITE_SHM_SHARED ){ |
| 4844 | u16 allShared = 0; /* Union of locks held by connections other than "p" */ |
| 4845 | |
| 4846 | /* Find out which shared locks are already held by sibling connections. |
| 4847 | ** If any sibling already holds an exclusive lock, go ahead and return |
| 4848 | ** SQLITE_BUSY. |
| 4849 | */ |
| 4850 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4851 | if( (pX->exclMask & mask)!=0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4852 | rc = SQLITE_BUSY; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4853 | break; |
| 4854 | } |
| 4855 | allShared |= pX->sharedMask; |
| 4856 | } |
| 4857 | |
| 4858 | /* Get shared locks at the system level, if necessary */ |
| 4859 | if( rc==SQLITE_OK ){ |
| 4860 | if( (allShared & mask)==0 ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4861 | rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4862 | }else{ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4863 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4864 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4865 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4866 | |
| 4867 | /* Get the local shared locks */ |
| 4868 | if( rc==SQLITE_OK ){ |
| 4869 | p->sharedMask |= mask; |
| 4870 | } |
| 4871 | }else{ |
| 4872 | /* Make sure no sibling connections hold locks that will block this |
| 4873 | ** lock. If any do, return SQLITE_BUSY right away. |
| 4874 | */ |
| 4875 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4876 | if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ |
| 4877 | rc = SQLITE_BUSY; |
| 4878 | break; |
| 4879 | } |
| 4880 | } |
| 4881 | |
| 4882 | /* Get the exclusive locks at the system level. Then if successful |
| 4883 | ** also mark the local connection as being locked. |
| 4884 | */ |
| 4885 | if( rc==SQLITE_OK ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4886 | rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4887 | if( rc==SQLITE_OK ){ |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4888 | assert( (p->sharedMask & mask)==0 ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4889 | p->exclMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4890 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4891 | } |
| 4892 | } |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4893 | sqlite3_mutex_leave(pShmNode->pShmMutex); |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 4894 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 4895 | p->id, osGetpid(0), p->sharedMask, p->exclMask)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4896 | return rc; |
| 4897 | } |
| 4898 | |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4899 | /* |
| 4900 | ** Implement a memory barrier or memory fence on shared memory. |
| 4901 | ** |
| 4902 | ** All loads and stores begun before the barrier must complete before |
| 4903 | ** any load or store begun after the barrier. |
| 4904 | */ |
| 4905 | static void unixShmBarrier( |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4906 | sqlite3_file *fd /* Database file holding the shared memory */ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4907 | ){ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 4908 | UNUSED_PARAMETER(fd); |
drh | 22c733d | 2015-09-24 12:40:43 +0000 | [diff] [blame] | 4909 | sqlite3MemoryBarrier(); /* compiler-defined memory barrier */ |
dan | a86acc2 | 2018-09-12 20:32:19 +0000 | [diff] [blame] | 4910 | assert( fd->pMethods->xLock==nolockLock |
| 4911 | || unixFileMutexNotheld((unixFile*)fd) |
| 4912 | ); |
drh | 22c733d | 2015-09-24 12:40:43 +0000 | [diff] [blame] | 4913 | unixEnterMutex(); /* Also mutex, for redundancy */ |
drh | b29ad85 | 2010-06-01 00:03:57 +0000 | [diff] [blame] | 4914 | unixLeaveMutex(); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4915 | } |
| 4916 | |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4917 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4918 | ** Close a connection to shared-memory. Delete the underlying |
| 4919 | ** storage if deleteFlag is true. |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 4920 | ** |
| 4921 | ** If there is no shared memory associated with the connection then this |
| 4922 | ** routine is a harmless no-op. |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4923 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4924 | static int unixShmUnmap( |
| 4925 | sqlite3_file *fd, /* The underlying database file */ |
| 4926 | int deleteFlag /* Delete shared-memory if true */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4927 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4928 | unixShm *p; /* The connection to be closed */ |
| 4929 | unixShmNode *pShmNode; /* The underlying shared-memory file */ |
| 4930 | unixShm **pp; /* For looping over sibling connections */ |
| 4931 | unixFile *pDbFd; /* The underlying database file */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4932 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4933 | pDbFd = (unixFile*)fd; |
| 4934 | p = pDbFd->pShm; |
| 4935 | if( p==0 ) return SQLITE_OK; |
| 4936 | pShmNode = p->pShmNode; |
| 4937 | |
| 4938 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4939 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 4940 | |
| 4941 | /* Remove connection p from the set of connections associated |
| 4942 | ** with pShmNode */ |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4943 | sqlite3_mutex_enter(pShmNode->pShmMutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4944 | for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 4945 | *pp = p->pNext; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4946 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4947 | /* Free the connection p */ |
| 4948 | sqlite3_free(p); |
| 4949 | pDbFd->pShm = 0; |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4950 | sqlite3_mutex_leave(pShmNode->pShmMutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4951 | |
| 4952 | /* If pShmNode->nRef has reached 0, then close the underlying |
| 4953 | ** shared-memory file, too */ |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 4954 | assert( unixFileMutexNotheld(pDbFd) ); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4955 | unixEnterMutex(); |
| 4956 | assert( pShmNode->nRef>0 ); |
| 4957 | pShmNode->nRef--; |
| 4958 | if( pShmNode->nRef==0 ){ |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4959 | if( deleteFlag && pShmNode->hShm>=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4960 | osUnlink(pShmNode->zFilename); |
| 4961 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4962 | unixShmPurge(pDbFd); |
| 4963 | } |
| 4964 | unixLeaveMutex(); |
| 4965 | |
| 4966 | return SQLITE_OK; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4967 | } |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4968 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4969 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4970 | #else |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 4971 | # define unixShmMap 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4972 | # define unixShmLock 0 |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4973 | # define unixShmBarrier 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4974 | # define unixShmUnmap 0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4975 | #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 4976 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 4977 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4978 | /* |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4979 | ** If it is currently memory mapped, unmap file pFd. |
dan | d306e1a | 2013-03-20 18:25:49 +0000 | [diff] [blame] | 4980 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4981 | static void unixUnmapfile(unixFile *pFd){ |
| 4982 | assert( pFd->nFetchOut==0 ); |
| 4983 | if( pFd->pMapRegion ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4984 | osMunmap(pFd->pMapRegion, pFd->mmapSizeActual); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4985 | pFd->pMapRegion = 0; |
| 4986 | pFd->mmapSize = 0; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4987 | pFd->mmapSizeActual = 0; |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 4988 | } |
| 4989 | } |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 4990 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 4991 | /* |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 4992 | ** Attempt to set the size of the memory mapping maintained by file |
| 4993 | ** descriptor pFd to nNew bytes. Any existing mapping is discarded. |
| 4994 | ** |
| 4995 | ** If successful, this function sets the following variables: |
| 4996 | ** |
| 4997 | ** unixFile.pMapRegion |
| 4998 | ** unixFile.mmapSize |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4999 | ** unixFile.mmapSizeActual |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5000 | ** |
| 5001 | ** If unsuccessful, an error message is logged via sqlite3_log() and |
| 5002 | ** the three variables above are zeroed. In this case SQLite should |
| 5003 | ** continue accessing the database using the xRead() and xWrite() |
| 5004 | ** methods. |
| 5005 | */ |
| 5006 | static void unixRemapfile( |
| 5007 | unixFile *pFd, /* File descriptor object */ |
| 5008 | i64 nNew /* Required mapping size */ |
| 5009 | ){ |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 5010 | const char *zErr = "mmap"; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5011 | int h = pFd->h; /* File descriptor open on db file */ |
| 5012 | u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5013 | i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */ |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5014 | u8 *pNew = 0; /* Location of new mapping */ |
| 5015 | int flags = PROT_READ; /* Flags to pass to mmap() */ |
| 5016 | |
| 5017 | assert( pFd->nFetchOut==0 ); |
| 5018 | assert( nNew>pFd->mmapSize ); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5019 | assert( nNew<=pFd->mmapSizeMax ); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5020 | assert( nNew>0 ); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5021 | assert( pFd->mmapSizeActual>=pFd->mmapSize ); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 5022 | assert( MAP_FAILED!=0 ); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5023 | |
dan | fe33e39 | 2015-11-17 20:56:06 +0000 | [diff] [blame] | 5024 | #ifdef SQLITE_MMAP_READWRITE |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5025 | if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE; |
dan | fe33e39 | 2015-11-17 20:56:06 +0000 | [diff] [blame] | 5026 | #endif |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5027 | |
| 5028 | if( pOrig ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 5029 | #if HAVE_MREMAP |
| 5030 | i64 nReuse = pFd->mmapSize; |
| 5031 | #else |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 5032 | const int szSyspage = osGetpagesize(); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5033 | i64 nReuse = (pFd->mmapSize & ~(szSyspage-1)); |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 5034 | #endif |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5035 | u8 *pReq = &pOrig[nReuse]; |
| 5036 | |
| 5037 | /* Unmap any pages of the existing mapping that cannot be reused. */ |
| 5038 | if( nReuse!=nOrig ){ |
| 5039 | osMunmap(pReq, nOrig-nReuse); |
| 5040 | } |
| 5041 | |
| 5042 | #if HAVE_MREMAP |
| 5043 | pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 5044 | zErr = "mremap"; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5045 | #else |
| 5046 | pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse); |
| 5047 | if( pNew!=MAP_FAILED ){ |
| 5048 | if( pNew!=pReq ){ |
| 5049 | osMunmap(pNew, nNew - nReuse); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 5050 | pNew = 0; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5051 | }else{ |
| 5052 | pNew = pOrig; |
| 5053 | } |
| 5054 | } |
| 5055 | #endif |
| 5056 | |
dan | 48ccef8 | 2013-04-02 20:55:01 +0000 | [diff] [blame] | 5057 | /* The attempt to extend the existing mapping failed. Free it. */ |
| 5058 | if( pNew==MAP_FAILED || pNew==0 ){ |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5059 | osMunmap(pOrig, nReuse); |
| 5060 | } |
| 5061 | } |
| 5062 | |
| 5063 | /* If pNew is still NULL, try to create an entirely new mapping. */ |
| 5064 | if( pNew==0 ){ |
| 5065 | pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5066 | } |
| 5067 | |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 5068 | if( pNew==MAP_FAILED ){ |
| 5069 | pNew = 0; |
| 5070 | nNew = 0; |
| 5071 | unixLogError(SQLITE_OK, zErr, pFd->zPath); |
| 5072 | |
| 5073 | /* If the mmap() above failed, assume that all subsequent mmap() calls |
| 5074 | ** will probably fail too. Fall back to using xRead/xWrite exclusively |
| 5075 | ** in this case. */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5076 | pFd->mmapSizeMax = 0; |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 5077 | } |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5078 | pFd->pMapRegion = (void *)pNew; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5079 | pFd->mmapSize = pFd->mmapSizeActual = nNew; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5080 | } |
| 5081 | |
| 5082 | /* |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5083 | ** Memory map or remap the file opened by file-descriptor pFd (if the file |
| 5084 | ** is already mapped, the existing mapping is replaced by the new). Or, if |
| 5085 | ** there already exists a mapping for this file, and there are still |
| 5086 | ** outstanding xFetch() references to it, this function is a no-op. |
| 5087 | ** |
| 5088 | ** If parameter nByte is non-negative, then it is the requested size of |
| 5089 | ** the mapping to create. Otherwise, if nByte is less than zero, then the |
| 5090 | ** requested size is the size of the file on disk. The actual size of the |
| 5091 | ** created mapping is either the requested size or the value configured |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 5092 | ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller. |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5093 | ** |
| 5094 | ** SQLITE_OK is returned if no error occurs (even if the mapping is not |
| 5095 | ** recreated as a result of outstanding references) or an SQLite error |
| 5096 | ** code otherwise. |
| 5097 | */ |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 5098 | static int unixMapfile(unixFile *pFd, i64 nMap){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5099 | assert( nMap>=0 || pFd->nFetchOut==0 ); |
drh | 333e6ca | 2015-12-02 15:44:39 +0000 | [diff] [blame] | 5100 | assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5101 | if( pFd->nFetchOut>0 ) return SQLITE_OK; |
| 5102 | |
| 5103 | if( nMap<0 ){ |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 5104 | struct stat statbuf; /* Low-level file information */ |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 5105 | if( osFstat(pFd->h, &statbuf) ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5106 | return SQLITE_IOERR_FSTAT; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 5107 | } |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 5108 | nMap = statbuf.st_size; |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5109 | } |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5110 | if( nMap>pFd->mmapSizeMax ){ |
| 5111 | nMap = pFd->mmapSizeMax; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 5112 | } |
| 5113 | |
drh | 333e6ca | 2015-12-02 15:44:39 +0000 | [diff] [blame] | 5114 | assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5115 | if( nMap!=pFd->mmapSize ){ |
drh | 333e6ca | 2015-12-02 15:44:39 +0000 | [diff] [blame] | 5116 | unixRemapfile(pFd, nMap); |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5117 | } |
| 5118 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5119 | return SQLITE_OK; |
| 5120 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 5121 | #endif /* SQLITE_MAX_MMAP_SIZE>0 */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5122 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5123 | /* |
| 5124 | ** If possible, return a pointer to a mapping of file fd starting at offset |
| 5125 | ** iOff. The mapping must be valid for at least nAmt bytes. |
| 5126 | ** |
| 5127 | ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK. |
| 5128 | ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK. |
| 5129 | ** Finally, if an error does occur, return an SQLite error code. The final |
| 5130 | ** value of *pp is undefined in this case. |
| 5131 | ** |
| 5132 | ** If this function does return a pointer, the caller must eventually |
| 5133 | ** release the reference by calling unixUnfetch(). |
| 5134 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5135 | static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5136 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5137 | unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 5138 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5139 | *pp = 0; |
| 5140 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5141 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 5142 | if( pFd->mmapSizeMax>0 ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5143 | if( pFd->pMapRegion==0 ){ |
| 5144 | int rc = unixMapfile(pFd, -1); |
| 5145 | if( rc!=SQLITE_OK ) return rc; |
| 5146 | } |
| 5147 | if( pFd->mmapSize >= iOff+nAmt ){ |
| 5148 | *pp = &((u8 *)pFd->pMapRegion)[iOff]; |
| 5149 | pFd->nFetchOut++; |
| 5150 | } |
| 5151 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 5152 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5153 | return SQLITE_OK; |
| 5154 | } |
| 5155 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5156 | /* |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 5157 | ** If the third argument is non-NULL, then this function releases a |
| 5158 | ** reference obtained by an earlier call to unixFetch(). The second |
| 5159 | ** argument passed to this function must be the same as the corresponding |
| 5160 | ** argument that was passed to the unixFetch() invocation. |
| 5161 | ** |
| 5162 | ** Or, if the third argument is NULL, then this function is being called |
| 5163 | ** to inform the VFS layer that, according to POSIX, any existing mapping |
| 5164 | ** may now be invalid and should be unmapped. |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5165 | */ |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 5166 | static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){ |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5167 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 5168 | unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
dan | 9871c59 | 2014-01-10 16:40:21 +0000 | [diff] [blame] | 5169 | UNUSED_PARAMETER(iOff); |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 5170 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5171 | /* If p==0 (unmap the entire file) then there must be no outstanding |
| 5172 | ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference), |
| 5173 | ** then there must be at least one outstanding. */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5174 | assert( (p==0)==(pFd->nFetchOut==0) ); |
| 5175 | |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 5176 | /* If p!=0, it must match the iOff value. */ |
| 5177 | assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] ); |
| 5178 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5179 | if( p ){ |
| 5180 | pFd->nFetchOut--; |
| 5181 | }else{ |
| 5182 | unixUnmapfile(pFd); |
| 5183 | } |
| 5184 | |
| 5185 | assert( pFd->nFetchOut>=0 ); |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 5186 | #else |
| 5187 | UNUSED_PARAMETER(fd); |
| 5188 | UNUSED_PARAMETER(p); |
dan | 9871c59 | 2014-01-10 16:40:21 +0000 | [diff] [blame] | 5189 | UNUSED_PARAMETER(iOff); |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5190 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5191 | return SQLITE_OK; |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5192 | } |
| 5193 | |
| 5194 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5195 | ** Here ends the implementation of all sqlite3_file methods. |
| 5196 | ** |
| 5197 | ********************** End sqlite3_file Methods ******************************* |
| 5198 | ******************************************************************************/ |
| 5199 | |
| 5200 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5201 | ** This division contains definitions of sqlite3_io_methods objects that |
| 5202 | ** implement various file locking strategies. It also contains definitions |
| 5203 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 5204 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 5205 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 5206 | ** the correct finder-function for that VFS. |
| 5207 | ** |
| 5208 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 5209 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 5210 | ** looks at the filesystem type and tries to guess the best locking |
| 5211 | ** strategy from that. |
| 5212 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5213 | ** For finder-function F, two objects are created: |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5214 | ** |
| 5215 | ** (1) The real finder-function named "FImpt()". |
| 5216 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5217 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5218 | ** |
| 5219 | ** |
| 5220 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 5221 | ** objects. We have to do this instead of letting pAppData point |
| 5222 | ** directly at the finder-function since C90 rules prevent a void* |
| 5223 | ** from be cast into a function pointer. |
| 5224 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5225 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5226 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5227 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5228 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 5229 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 5230 | ** |
| 5231 | ** * An I/O method finder function called FINDER that returns a pointer |
| 5232 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5233 | */ |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 5234 | #define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP) \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5235 | static const sqlite3_io_methods METHOD = { \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5236 | VERSION, /* iVersion */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5237 | CLOSE, /* xClose */ \ |
| 5238 | unixRead, /* xRead */ \ |
| 5239 | unixWrite, /* xWrite */ \ |
| 5240 | unixTruncate, /* xTruncate */ \ |
| 5241 | unixSync, /* xSync */ \ |
| 5242 | unixFileSize, /* xFileSize */ \ |
| 5243 | LOCK, /* xLock */ \ |
| 5244 | UNLOCK, /* xUnlock */ \ |
| 5245 | CKLOCK, /* xCheckReservedLock */ \ |
| 5246 | unixFileControl, /* xFileControl */ \ |
| 5247 | unixSectorSize, /* xSectorSize */ \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5248 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5249 | SHMMAP, /* xShmMap */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 5250 | unixShmLock, /* xShmLock */ \ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 5251 | unixShmBarrier, /* xShmBarrier */ \ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5252 | unixShmUnmap, /* xShmUnmap */ \ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5253 | unixFetch, /* xFetch */ \ |
| 5254 | unixUnfetch, /* xUnfetch */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5255 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5256 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 5257 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5258 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5259 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5260 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5261 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5262 | |
| 5263 | /* |
| 5264 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 5265 | ** locking strategies. Functions that return pointers to these methods |
| 5266 | ** are also created. |
| 5267 | */ |
| 5268 | IOMETHODS( |
| 5269 | posixIoFinder, /* Finder function name */ |
| 5270 | posixIoMethods, /* sqlite3_io_methods object name */ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5271 | 3, /* shared memory and mmap are enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5272 | unixClose, /* xClose method */ |
| 5273 | unixLock, /* xLock method */ |
| 5274 | unixUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5275 | unixCheckReservedLock, /* xCheckReservedLock method */ |
| 5276 | unixShmMap /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5277 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5278 | IOMETHODS( |
| 5279 | nolockIoFinder, /* Finder function name */ |
| 5280 | nolockIoMethods, /* sqlite3_io_methods object name */ |
drh | 3e2c842 | 2018-08-13 11:32:07 +0000 | [diff] [blame] | 5281 | 3, /* shared memory and mmap are enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5282 | nolockClose, /* xClose method */ |
| 5283 | nolockLock, /* xLock method */ |
| 5284 | nolockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5285 | nolockCheckReservedLock, /* xCheckReservedLock method */ |
| 5286 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5287 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5288 | IOMETHODS( |
| 5289 | dotlockIoFinder, /* Finder function name */ |
| 5290 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5291 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5292 | dotlockClose, /* xClose method */ |
| 5293 | dotlockLock, /* xLock method */ |
| 5294 | dotlockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5295 | dotlockCheckReservedLock, /* xCheckReservedLock method */ |
| 5296 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5297 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5298 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5299 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5300 | IOMETHODS( |
| 5301 | flockIoFinder, /* Finder function name */ |
| 5302 | flockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5303 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5304 | flockClose, /* xClose method */ |
| 5305 | flockLock, /* xLock method */ |
| 5306 | flockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5307 | flockCheckReservedLock, /* xCheckReservedLock method */ |
| 5308 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5309 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5310 | #endif |
| 5311 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5312 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5313 | IOMETHODS( |
| 5314 | semIoFinder, /* Finder function name */ |
| 5315 | semIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5316 | 1, /* shared memory is disabled */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5317 | semXClose, /* xClose method */ |
| 5318 | semXLock, /* xLock method */ |
| 5319 | semXUnlock, /* xUnlock method */ |
| 5320 | semXCheckReservedLock, /* xCheckReservedLock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5321 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5322 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5323 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5324 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5325 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5326 | IOMETHODS( |
| 5327 | afpIoFinder, /* Finder function name */ |
| 5328 | afpIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5329 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5330 | afpClose, /* xClose method */ |
| 5331 | afpLock, /* xLock method */ |
| 5332 | afpUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5333 | afpCheckReservedLock, /* xCheckReservedLock method */ |
| 5334 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5335 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5336 | #endif |
| 5337 | |
| 5338 | /* |
| 5339 | ** The proxy locking method is a "super-method" in the sense that it |
| 5340 | ** opens secondary file descriptors for the conch and lock files and |
| 5341 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 5342 | ** secondary files. For this reason, the division that implements |
| 5343 | ** proxy locking is located much further down in the file. But we need |
| 5344 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 5345 | ** for proxy locking here. So we forward declare the I/O methods. |
| 5346 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5347 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5348 | static int proxyClose(sqlite3_file*); |
| 5349 | static int proxyLock(sqlite3_file*, int); |
| 5350 | static int proxyUnlock(sqlite3_file*, int); |
| 5351 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5352 | IOMETHODS( |
| 5353 | proxyIoFinder, /* Finder function name */ |
| 5354 | proxyIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5355 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5356 | proxyClose, /* xClose method */ |
| 5357 | proxyLock, /* xLock method */ |
| 5358 | proxyUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5359 | proxyCheckReservedLock, /* xCheckReservedLock method */ |
| 5360 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5361 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5362 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5363 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5364 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 5365 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 5366 | IOMETHODS( |
| 5367 | nfsIoFinder, /* Finder function name */ |
| 5368 | nfsIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5369 | 1, /* shared memory is disabled */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5370 | unixClose, /* xClose method */ |
| 5371 | unixLock, /* xLock method */ |
| 5372 | nfsUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5373 | unixCheckReservedLock, /* xCheckReservedLock method */ |
| 5374 | 0 /* xShmMap method */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5375 | ) |
| 5376 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5377 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5378 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5379 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5380 | ** This "finder" function attempts to determine the best locking strategy |
| 5381 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5382 | ** object that implements that strategy. |
| 5383 | ** |
| 5384 | ** This is for MacOSX only. |
| 5385 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5386 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5387 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5388 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5389 | ){ |
| 5390 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5391 | const char *zFilesystem; /* Filesystem type name */ |
| 5392 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5393 | } aMap[] = { |
| 5394 | { "hfs", &posixIoMethods }, |
| 5395 | { "ufs", &posixIoMethods }, |
| 5396 | { "afpfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5397 | { "smbfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5398 | { "webdav", &nolockIoMethods }, |
| 5399 | { 0, 0 } |
| 5400 | }; |
| 5401 | int i; |
| 5402 | struct statfs fsInfo; |
| 5403 | struct flock lockInfo; |
| 5404 | |
| 5405 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5406 | /* If filePath==NULL that means we are dealing with a transient file |
| 5407 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5408 | return &nolockIoMethods; |
| 5409 | } |
| 5410 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 5411 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 5412 | return &nolockIoMethods; |
| 5413 | } |
| 5414 | for(i=0; aMap[i].zFilesystem; i++){ |
| 5415 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 5416 | return aMap[i].pMethods; |
| 5417 | } |
| 5418 | } |
| 5419 | } |
| 5420 | |
| 5421 | /* Default case. Handles, amongst others, "nfs". |
| 5422 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 5423 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5424 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5425 | lockInfo.l_len = 1; |
| 5426 | lockInfo.l_start = 0; |
| 5427 | lockInfo.l_whence = SEEK_SET; |
| 5428 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5429 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5430 | if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 5431 | return &nfsIoMethods; |
| 5432 | } else { |
| 5433 | return &posixIoMethods; |
| 5434 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5435 | }else{ |
| 5436 | return &dotlockIoMethods; |
| 5437 | } |
| 5438 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5439 | static const sqlite3_io_methods |
| 5440 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5441 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5442 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5443 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5444 | #if OS_VXWORKS |
| 5445 | /* |
| 5446 | ** This "finder" function for VxWorks checks to see if posix advisory |
| 5447 | ** locking works. If it does, then that is what is used. If it does not |
| 5448 | ** work, then fallback to named semaphore locking. |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5449 | */ |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5450 | static const sqlite3_io_methods *vxworksIoFinderImpl( |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5451 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5452 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5453 | ){ |
| 5454 | struct flock lockInfo; |
| 5455 | |
| 5456 | if( !filePath ){ |
| 5457 | /* If filePath==NULL that means we are dealing with a transient file |
| 5458 | ** that does not need to be locked. */ |
| 5459 | return &nolockIoMethods; |
| 5460 | } |
| 5461 | |
| 5462 | /* Test if fcntl() is supported and use POSIX style locks. |
| 5463 | ** Otherwise fall back to the named semaphore method. |
| 5464 | */ |
| 5465 | lockInfo.l_len = 1; |
| 5466 | lockInfo.l_start = 0; |
| 5467 | lockInfo.l_whence = SEEK_SET; |
| 5468 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5469 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5470 | return &posixIoMethods; |
| 5471 | }else{ |
| 5472 | return &semIoMethods; |
| 5473 | } |
| 5474 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5475 | static const sqlite3_io_methods |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5476 | *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5477 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5478 | #endif /* OS_VXWORKS */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5479 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5480 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5481 | ** An abstract type for a pointer to an IO method finder function: |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5482 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5483 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5484 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5485 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5486 | /**************************************************************************** |
| 5487 | **************************** sqlite3_vfs methods **************************** |
| 5488 | ** |
| 5489 | ** This division contains the implementation of methods on the |
| 5490 | ** sqlite3_vfs object. |
| 5491 | */ |
| 5492 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 5493 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5494 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5495 | */ |
| 5496 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5497 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5498 | int h, /* Open file descriptor of file being opened */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 5499 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5500 | const char *zFilename, /* Name of the file being opened */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5501 | int ctrlFlags /* Zero or more UNIXFILE_* values */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5502 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5503 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5504 | unixFile *pNew = (unixFile *)pId; |
| 5505 | int rc = SQLITE_OK; |
| 5506 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5507 | assert( pNew->pInode==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 5508 | |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5509 | /* No locking occurs in temporary files */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5510 | assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5511 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5512 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5513 | pNew->h = h; |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 5514 | pNew->pVfs = pVfs; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5515 | pNew->zPath = zFilename; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5516 | pNew->ctrlFlags = (u8)ctrlFlags; |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5517 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | ede01a9 | 2013-05-17 12:10:52 +0000 | [diff] [blame] | 5518 | pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap; |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5519 | #endif |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5520 | if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0), |
| 5521 | "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 5522 | pNew->ctrlFlags |= UNIXFILE_PSOW; |
drh | bec7c97 | 2011-12-23 00:25:02 +0000 | [diff] [blame] | 5523 | } |
drh | 503a686 | 2013-03-01 01:07:17 +0000 | [diff] [blame] | 5524 | if( strcmp(pVfs->zName,"unix-excl")==0 ){ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 5525 | pNew->ctrlFlags |= UNIXFILE_EXCL; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 5526 | } |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 5527 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5528 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 5529 | pNew->pId = vxworksFindFileId(zFilename); |
| 5530 | if( pNew->pId==0 ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5531 | ctrlFlags |= UNIXFILE_NOLOCK; |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5532 | rc = SQLITE_NOMEM_BKPT; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5533 | } |
| 5534 | #endif |
| 5535 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5536 | if( ctrlFlags & UNIXFILE_NOLOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5537 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5538 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5539 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5540 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5541 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 5542 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 5543 | ** zFilename remains valid until file is closed, to support */ |
| 5544 | pNew->lockingContext = (void*)zFilename; |
| 5545 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5546 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5547 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5548 | if( pLockingStyle == &posixIoMethods |
| 5549 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 5550 | || pLockingStyle == &nfsIoMethods |
| 5551 | #endif |
| 5552 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5553 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5554 | rc = findInodeInfo(pNew, &pNew->pInode); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5555 | if( rc!=SQLITE_OK ){ |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 5556 | /* If an error occurred in findInodeInfo(), close the file descriptor |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5557 | ** immediately, before releasing the mutex. findInodeInfo() may fail |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5558 | ** in two scenarios: |
| 5559 | ** |
| 5560 | ** (a) A call to fstat() failed. |
| 5561 | ** (b) A malloc failed. |
| 5562 | ** |
| 5563 | ** Scenario (b) may only occur if the process is holding no other |
| 5564 | ** file descriptors open on the same file. If there were other file |
| 5565 | ** descriptors on this file, then no malloc would be required by |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5566 | ** findInodeInfo(). If this is the case, it is quite safe to close |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5567 | ** handle h - as it is guaranteed that no posix locks will be released |
| 5568 | ** by doing so. |
| 5569 | ** |
| 5570 | ** If scenario (a) caused the error then things are not so safe. The |
| 5571 | ** implicit assumption here is that if fstat() fails, things are in |
| 5572 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 5573 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5574 | robust_close(pNew, h, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5575 | h = -1; |
| 5576 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5577 | unixLeaveMutex(); |
| 5578 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5579 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5580 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 5581 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5582 | /* AFP locking uses the file path so it needs to be included in |
| 5583 | ** the afpLockingContext. |
| 5584 | */ |
| 5585 | afpLockingContext *pCtx; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5586 | pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) ); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5587 | if( pCtx==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5588 | rc = SQLITE_NOMEM_BKPT; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5589 | }else{ |
| 5590 | /* NB: zFilename exists and remains valid until the file is closed |
| 5591 | ** according to requirement F11141. So we do not need to make a |
| 5592 | ** copy of the filename. */ |
| 5593 | pCtx->dbPath = zFilename; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5594 | pCtx->reserved = 0; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5595 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5596 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5597 | rc = findInodeInfo(pNew, &pNew->pInode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5598 | if( rc!=SQLITE_OK ){ |
| 5599 | sqlite3_free(pNew->lockingContext); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5600 | robust_close(pNew, h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5601 | h = -1; |
| 5602 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5603 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5604 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5605 | } |
| 5606 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5607 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5608 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 5609 | /* Dotfile locking uses the file path so it needs to be included in |
| 5610 | ** the dotlockLockingContext |
| 5611 | */ |
| 5612 | char *zLockFile; |
| 5613 | int nFilename; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5614 | assert( zFilename!=0 ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5615 | nFilename = (int)strlen(zFilename) + 6; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5616 | zLockFile = (char *)sqlite3_malloc64(nFilename); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5617 | if( zLockFile==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5618 | rc = SQLITE_NOMEM_BKPT; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5619 | }else{ |
| 5620 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5621 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5622 | pNew->lockingContext = zLockFile; |
| 5623 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5624 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5625 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5626 | else if( pLockingStyle == &semIoMethods ){ |
| 5627 | /* Named semaphore locking uses the file path so it needs to be |
| 5628 | ** included in the semLockingContext |
| 5629 | */ |
| 5630 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5631 | rc = findInodeInfo(pNew, &pNew->pInode); |
| 5632 | if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 5633 | char *zSemName = pNew->pInode->aSemName; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5634 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 5635 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5636 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 5637 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5638 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5639 | pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 5640 | if( pNew->pInode->pSem == SEM_FAILED ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5641 | rc = SQLITE_NOMEM_BKPT; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5642 | pNew->pInode->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5643 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5644 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5645 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5646 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5647 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 5648 | |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 5649 | storeLastErrno(pNew, 0); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5650 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5651 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5652 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
drh | 309e655 | 2010-02-05 18:00:26 +0000 | [diff] [blame] | 5653 | h = -1; |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5654 | osUnlink(zFilename); |
drh | c579754 | 2013-04-27 12:13:29 +0000 | [diff] [blame] | 5655 | pNew->ctrlFlags |= UNIXFILE_DELETE; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5656 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5657 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5658 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5659 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5660 | }else{ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5661 | pNew->pMethod = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5662 | OpenCounter(+1); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 5663 | verifyDbFile(pNew); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5664 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5665 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 5666 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 5667 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5668 | /* |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5669 | ** Return the name of a directory in which to put temporary files. |
| 5670 | ** If no suitable temporary file directory can be found, return NULL. |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5671 | */ |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 5672 | static const char *unixTempFileDir(void){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5673 | static const char *azDirs[] = { |
| 5674 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5675 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5676 | "/var/tmp", |
| 5677 | "/usr/tmp", |
| 5678 | "/tmp", |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5679 | "." |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5680 | }; |
drh | 2aab11f | 2016-04-29 20:30:56 +0000 | [diff] [blame] | 5681 | unsigned int i = 0; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5682 | struct stat buf; |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5683 | const char *zDir = sqlite3_temp_directory; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5684 | |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5685 | if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR"); |
| 5686 | if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); |
drh | 2aab11f | 2016-04-29 20:30:56 +0000 | [diff] [blame] | 5687 | while(1){ |
| 5688 | if( zDir!=0 |
| 5689 | && osStat(zDir, &buf)==0 |
| 5690 | && S_ISDIR(buf.st_mode) |
| 5691 | && osAccess(zDir, 03)==0 |
| 5692 | ){ |
| 5693 | return zDir; |
| 5694 | } |
| 5695 | if( i>=sizeof(azDirs)/sizeof(azDirs[0]) ) break; |
| 5696 | zDir = azDirs[i++]; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5697 | } |
drh | 7694e06 | 2016-04-21 23:37:24 +0000 | [diff] [blame] | 5698 | return 0; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5699 | } |
| 5700 | |
| 5701 | /* |
| 5702 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 5703 | ** by the calling process and must be big enough to hold at least |
| 5704 | ** pVfs->mxPathname bytes. |
| 5705 | */ |
| 5706 | static int unixGetTempname(int nBuf, char *zBuf){ |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5707 | const char *zDir; |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5708 | int iLimit = 0; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5709 | |
| 5710 | /* It's odd to simulate an io-error here, but really this is just |
| 5711 | ** using the io-error infrastructure to test that SQLite handles this |
| 5712 | ** function failing. |
| 5713 | */ |
drh | 7694e06 | 2016-04-21 23:37:24 +0000 | [diff] [blame] | 5714 | zBuf[0] = 0; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5715 | SimulateIOError( return SQLITE_IOERR ); |
| 5716 | |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 5717 | zDir = unixTempFileDir(); |
drh | 7694e06 | 2016-04-21 23:37:24 +0000 | [diff] [blame] | 5718 | if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5719 | do{ |
drh | 970942e | 2015-11-25 23:13:14 +0000 | [diff] [blame] | 5720 | u64 r; |
| 5721 | sqlite3_randomness(sizeof(r), &r); |
| 5722 | assert( nBuf>2 ); |
| 5723 | zBuf[nBuf-2] = 0; |
| 5724 | sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c", |
| 5725 | zDir, r, 0); |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5726 | if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5727 | }while( osAccess(zBuf,0)==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5728 | return SQLITE_OK; |
| 5729 | } |
| 5730 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5731 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 5732 | /* |
| 5733 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 5734 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 5735 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 5736 | */ |
| 5737 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 5738 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 5739 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5740 | /* |
| 5741 | ** Search for an unused file descriptor that was opened on the database |
| 5742 | ** file (not a journal or master-journal file) identified by pathname |
| 5743 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 5744 | ** argument to this function. |
| 5745 | ** |
| 5746 | ** Such a file descriptor may exist if a database connection was closed |
| 5747 | ** but the associated file descriptor could not be closed because some |
| 5748 | ** other file descriptor open on the same file is holding a file-lock. |
| 5749 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 5750 | ** describing "Posix Advisory Locking" at the start of this file for |
| 5751 | ** further details. Also, ticket #4018. |
| 5752 | ** |
| 5753 | ** If a suitable file descriptor is found, then it is returned. If no |
| 5754 | ** such file descriptor is located, -1 is returned. |
| 5755 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5756 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 5757 | UnixUnusedFd *pUnused = 0; |
| 5758 | |
| 5759 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 5760 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 5761 | ** but because no way to test it is currently available. It is better |
| 5762 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 5763 | ** feature. */ |
| 5764 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5765 | struct stat sStat; /* Results of stat() call */ |
| 5766 | |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 5767 | unixEnterMutex(); |
| 5768 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5769 | /* A stat() call may fail for various reasons. If this happens, it is |
| 5770 | ** almost certain that an open() call on the same path will also fail. |
| 5771 | ** For this reason, if an error occurs in the stat() call here, it is |
| 5772 | ** ignored and -1 is returned. The caller will try to open a new file |
| 5773 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 5774 | ** |
| 5775 | ** Even if a subsequent open() call does succeed, the consequences of |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5776 | ** not searching for a reusable file descriptor are not dire. */ |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 5777 | if( inodeList!=0 && 0==osStat(zPath, &sStat) ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 5778 | unixInodeInfo *pInode; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5779 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5780 | pInode = inodeList; |
| 5781 | while( pInode && (pInode->fileId.dev!=sStat.st_dev |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 5782 | || pInode->fileId.ino!=(u64)sStat.st_ino) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5783 | pInode = pInode->pNext; |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 5784 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5785 | if( pInode ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5786 | UnixUnusedFd **pp; |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 5787 | assert( sqlite3_mutex_notheld(pInode->pLockMutex) ); |
| 5788 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | 55220a6 | 2019-08-06 20:55:06 +0000 | [diff] [blame] | 5789 | flags &= (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5790 | for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5791 | pUnused = *pp; |
| 5792 | if( pUnused ){ |
| 5793 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5794 | } |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 5795 | sqlite3_mutex_leave(pInode->pLockMutex); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5796 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5797 | } |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 5798 | unixLeaveMutex(); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5799 | #endif /* if !OS_VXWORKS */ |
| 5800 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5801 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5802 | |
| 5803 | /* |
dan | 1bf4ca7 | 2016-08-11 18:05:47 +0000 | [diff] [blame] | 5804 | ** Find the mode, uid and gid of file zFile. |
| 5805 | */ |
| 5806 | static int getFileMode( |
| 5807 | const char *zFile, /* File name */ |
| 5808 | mode_t *pMode, /* OUT: Permissions of zFile */ |
| 5809 | uid_t *pUid, /* OUT: uid of zFile. */ |
| 5810 | gid_t *pGid /* OUT: gid of zFile. */ |
| 5811 | ){ |
| 5812 | struct stat sStat; /* Output of stat() on database file */ |
| 5813 | int rc = SQLITE_OK; |
| 5814 | if( 0==osStat(zFile, &sStat) ){ |
| 5815 | *pMode = sStat.st_mode & 0777; |
| 5816 | *pUid = sStat.st_uid; |
| 5817 | *pGid = sStat.st_gid; |
| 5818 | }else{ |
| 5819 | rc = SQLITE_IOERR_FSTAT; |
| 5820 | } |
| 5821 | return rc; |
| 5822 | } |
| 5823 | |
| 5824 | /* |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5825 | ** This function is called by unixOpen() to determine the unix permissions |
drh | f65bc91 | 2010-07-14 20:51:34 +0000 | [diff] [blame] | 5826 | ** 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] | 5827 | ** and a value suitable for passing as the third argument to open(2) is |
| 5828 | ** written to *pMode. If an IO error occurs, an SQLite error code is |
| 5829 | ** returned and the value of *pMode is not modified. |
| 5830 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5831 | ** In most cases, this routine sets *pMode to 0, which will become |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 5832 | ** an indication to robust_open() to create the file using |
| 5833 | ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask. |
| 5834 | ** 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] | 5835 | ** this function queries the file-system for the permissions on the |
| 5836 | ** corresponding database file and sets *pMode to this value. Whenever |
| 5837 | ** possible, WAL and journal files are created using the same permissions |
| 5838 | ** as the associated database file. |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 5839 | ** |
| 5840 | ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the |
| 5841 | ** original filename is unavailable. But 8_3_NAMES is only used for |
| 5842 | ** FAT filesystems and permissions do not matter there, so just use |
drh | 1116b17 | 2019-09-25 10:36:31 +0000 | [diff] [blame] | 5843 | ** the default permissions. In 8_3_NAMES mode, leave *pMode set to zero. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5844 | */ |
| 5845 | static int findCreateFileMode( |
| 5846 | const char *zPath, /* Path of file (possibly) being created */ |
| 5847 | int flags, /* Flags passed as 4th argument to xOpen() */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5848 | mode_t *pMode, /* OUT: Permissions to open file with */ |
| 5849 | uid_t *pUid, /* OUT: uid to set on the file */ |
| 5850 | gid_t *pGid /* OUT: gid to set on the file */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5851 | ){ |
| 5852 | int rc = SQLITE_OK; /* Return Code */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 5853 | *pMode = 0; |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5854 | *pUid = 0; |
| 5855 | *pGid = 0; |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5856 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5857 | char zDb[MAX_PATHNAME+1]; /* Database file path */ |
| 5858 | int nDb; /* Number of valid bytes in zDb */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5859 | |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5860 | /* zPath is a path to a WAL or journal file. The following block derives |
| 5861 | ** the path to the associated database file from zPath. This block handles |
| 5862 | ** the following naming conventions: |
| 5863 | ** |
| 5864 | ** "<path to db>-journal" |
| 5865 | ** "<path to db>-wal" |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 5866 | ** "<path to db>-journalNN" |
| 5867 | ** "<path to db>-walNN" |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5868 | ** |
drh | d337c5b | 2011-10-20 18:23:35 +0000 | [diff] [blame] | 5869 | ** where NN is a decimal number. The NN naming schemes are |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5870 | ** used by the test_multiplex.c module. |
| 5871 | */ |
| 5872 | nDb = sqlite3Strlen30(zPath) - 1; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5873 | while( zPath[nDb]!='-' ){ |
dan | 629ec14 | 2017-09-14 20:41:17 +0000 | [diff] [blame] | 5874 | /* In normal operation, the journal file name will always contain |
| 5875 | ** a '-' character. However in 8+3 filename mode, or if a corrupt |
| 5876 | ** rollback journal specifies a master journal with a goofy name, then |
| 5877 | ** the '-' might be missing. */ |
drh | 90e5dda | 2015-12-03 20:42:28 +0000 | [diff] [blame] | 5878 | if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5879 | nDb--; |
| 5880 | } |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5881 | memcpy(zDb, zPath, nDb); |
| 5882 | zDb[nDb] = '\0'; |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5883 | |
dan | 1bf4ca7 | 2016-08-11 18:05:47 +0000 | [diff] [blame] | 5884 | rc = getFileMode(zDb, pMode, pUid, pGid); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5885 | }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 5886 | *pMode = 0600; |
dan | 1bf4ca7 | 2016-08-11 18:05:47 +0000 | [diff] [blame] | 5887 | }else if( flags & SQLITE_OPEN_URI ){ |
| 5888 | /* If this is a main database file and the file was opened using a URI |
| 5889 | ** filename, check for the "modeof" parameter. If present, interpret |
| 5890 | ** its value as a filename and try to copy the mode, uid and gid from |
| 5891 | ** that file. */ |
| 5892 | const char *z = sqlite3_uri_parameter(zPath, "modeof"); |
| 5893 | if( z ){ |
| 5894 | rc = getFileMode(z, pMode, pUid, pGid); |
| 5895 | } |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5896 | } |
| 5897 | return rc; |
| 5898 | } |
| 5899 | |
| 5900 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5901 | ** Open the file zPath. |
| 5902 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5903 | ** Previously, the SQLite OS layer used three functions in place of this |
| 5904 | ** one: |
| 5905 | ** |
| 5906 | ** sqlite3OsOpenReadWrite(); |
| 5907 | ** sqlite3OsOpenReadOnly(); |
| 5908 | ** sqlite3OsOpenExclusive(); |
| 5909 | ** |
| 5910 | ** These calls correspond to the following combinations of flags: |
| 5911 | ** |
| 5912 | ** ReadWrite() -> (READWRITE | CREATE) |
| 5913 | ** ReadOnly() -> (READONLY) |
| 5914 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 5915 | ** |
| 5916 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 5917 | ** true, the file was configured to be automatically deleted when the |
| 5918 | ** file handle closed. To achieve the same effect using this new |
| 5919 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 5920 | ** OpenExclusive(). |
| 5921 | */ |
| 5922 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5923 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 5924 | const char *zPath, /* Pathname of file to be opened */ |
| 5925 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 5926 | int flags, /* Input flags to control the opening */ |
| 5927 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5928 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5929 | unixFile *p = (unixFile *)pFile; |
| 5930 | int fd = -1; /* File descriptor returned by open() */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5931 | int openFlags = 0; /* Flags to pass to open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5932 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5933 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5934 | int rc = SQLITE_OK; /* Function Return Code */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5935 | int ctrlFlags = 0; /* UNIXFILE_* flags */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5936 | |
| 5937 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 5938 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 5939 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 5940 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 5941 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5942 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5943 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 5944 | #endif |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 5945 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 5946 | struct statfs fsInfo; |
| 5947 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5948 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5949 | /* If creating a master or main-file journal, this function will open |
| 5950 | ** a file-descriptor on the directory too. The first time unixSync() |
| 5951 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 5952 | */ |
drh | a803a2c | 2017-12-13 20:02:29 +0000 | [diff] [blame] | 5953 | int isNewJrnl = (isCreate && ( |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5954 | eType==SQLITE_OPEN_MASTER_JOURNAL |
| 5955 | || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 5956 | || eType==SQLITE_OPEN_WAL |
| 5957 | )); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5958 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5959 | /* If argument zPath is a NULL pointer, this function is required to open |
| 5960 | ** a temporary file. Use this buffer to store the file name in. |
| 5961 | */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5962 | char zTmpname[MAX_PATHNAME+2]; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5963 | const char *zName = zPath; |
| 5964 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5965 | /* Check the following statements are true: |
| 5966 | ** |
| 5967 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 5968 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 5969 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5970 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5971 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5972 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5973 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5974 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 5975 | assert(isDelete==0 || isCreate); |
| 5976 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5977 | /* The main DB, main journal, WAL file and master journal are never |
| 5978 | ** automatically deleted. Nor are they ever temporary files. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5979 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 5980 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 5981 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5982 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5983 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5984 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 5985 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 5986 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 5987 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5988 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 5989 | ); |
| 5990 | |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 5991 | /* Detect a pid change and reset the PRNG. There is a race condition |
| 5992 | ** here such that two or more threads all trying to open databases at |
| 5993 | ** the same instant might all reset the PRNG. But multiple resets |
| 5994 | ** are harmless. |
| 5995 | */ |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 5996 | if( randomnessPid!=osGetpid(0) ){ |
| 5997 | randomnessPid = osGetpid(0); |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 5998 | sqlite3_randomness(0,0); |
| 5999 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6000 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6001 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6002 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6003 | UnixUnusedFd *pUnused; |
| 6004 | pUnused = findReusableFd(zName, flags); |
| 6005 | if( pUnused ){ |
| 6006 | fd = pUnused->fd; |
| 6007 | }else{ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 6008 | pUnused = sqlite3_malloc64(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6009 | if( !pUnused ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6010 | return SQLITE_NOMEM_BKPT; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6011 | } |
| 6012 | } |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 6013 | p->pPreallocatedUnused = pUnused; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6014 | |
| 6015 | /* Database filenames are double-zero terminated if they are not |
| 6016 | ** URIs with parameters. Hence, they can always be passed into |
| 6017 | ** sqlite3_uri_parameter(). */ |
| 6018 | assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 ); |
| 6019 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6020 | }else if( !zName ){ |
| 6021 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
drh | a803a2c | 2017-12-13 20:02:29 +0000 | [diff] [blame] | 6022 | assert(isDelete && !isNewJrnl); |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 6023 | rc = unixGetTempname(pVfs->mxPathname, zTmpname); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 6024 | if( rc!=SQLITE_OK ){ |
| 6025 | return rc; |
| 6026 | } |
| 6027 | zName = zTmpname; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6028 | |
| 6029 | /* Generated temporary filenames are always double-zero terminated |
| 6030 | ** for use by sqlite3_uri_parameter(). */ |
| 6031 | assert( zName[strlen(zName)+1]==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 6032 | } |
| 6033 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6034 | /* Determine the value of the flags parameter passed to POSIX function |
| 6035 | ** open(). These must be calculated even if open() is not called, as |
| 6036 | ** they may be stored as part of the file handle and used by the |
| 6037 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6038 | if( isReadonly ) openFlags |= O_RDONLY; |
| 6039 | if( isReadWrite ) openFlags |= O_RDWR; |
| 6040 | if( isCreate ) openFlags |= O_CREAT; |
| 6041 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 6042 | openFlags |= (O_LARGEFILE|O_BINARY); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6043 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6044 | if( fd<0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 6045 | mode_t openMode; /* Permissions to create file with */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 6046 | uid_t uid; /* Userid for the file */ |
| 6047 | gid_t gid; /* Groupid for the file */ |
| 6048 | rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 6049 | if( rc!=SQLITE_OK ){ |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 6050 | assert( !p->pPreallocatedUnused ); |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 6051 | assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 6052 | return rc; |
| 6053 | } |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 6054 | fd = robust_open(zName, openFlags, openMode); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6055 | OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); |
drh | 5a2d970 | 2015-11-26 02:21:05 +0000 | [diff] [blame] | 6056 | assert( !isExclusive || (openFlags & O_CREAT)!=0 ); |
dan | a688ca5 | 2018-01-10 11:56:03 +0000 | [diff] [blame] | 6057 | if( fd<0 ){ |
| 6058 | if( isNewJrnl && errno==EACCES && osAccess(zName, F_OK) ){ |
| 6059 | /* If unable to create a journal because the directory is not |
| 6060 | ** writable, change the error code to indicate that. */ |
| 6061 | rc = SQLITE_READONLY_DIRECTORY; |
| 6062 | }else if( errno!=EISDIR && isReadWrite ){ |
| 6063 | /* Failed to open the file for read/write access. Try read-only. */ |
| 6064 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
| 6065 | openFlags &= ~(O_RDWR|O_CREAT); |
| 6066 | flags |= SQLITE_OPEN_READONLY; |
| 6067 | openFlags |= O_RDONLY; |
| 6068 | isReadonly = 1; |
| 6069 | fd = robust_open(zName, openFlags, openMode); |
| 6070 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6071 | } |
| 6072 | if( fd<0 ){ |
dan | a688ca5 | 2018-01-10 11:56:03 +0000 | [diff] [blame] | 6073 | int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); |
| 6074 | if( rc==SQLITE_OK ) rc = rc2; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6075 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6076 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 6077 | |
drh | 1116b17 | 2019-09-25 10:36:31 +0000 | [diff] [blame] | 6078 | /* The owner of the rollback journal or WAL file should always be the |
| 6079 | ** same as the owner of the database file. Try to ensure that this is |
| 6080 | ** the case. The chown() system call will be a no-op if the current |
| 6081 | ** process lacks root privileges, be we should at least try. Without |
| 6082 | ** this step, if a root process opens a database file, it can leave |
| 6083 | ** behinds a journal/WAL that is owned by root and hence make the |
| 6084 | ** database inaccessible to unprivileged processes. |
| 6085 | ** |
drh | edf8a7b | 2019-09-25 11:49:36 +0000 | [diff] [blame] | 6086 | ** If openMode==0, then that means uid and gid are not set correctly |
drh | 1116b17 | 2019-09-25 10:36:31 +0000 | [diff] [blame] | 6087 | ** (probably because SQLite is configured to use 8+3 filename mode) and |
| 6088 | ** in that case we do not want to attempt the chown(). |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 6089 | */ |
drh | edf8a7b | 2019-09-25 11:49:36 +0000 | [diff] [blame] | 6090 | if( openMode && (flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL))!=0 ){ |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 6091 | robustFchown(fd, uid, gid); |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 6092 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6093 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6094 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6095 | if( pOutFlags ){ |
| 6096 | *pOutFlags = flags; |
| 6097 | } |
| 6098 | |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 6099 | if( p->pPreallocatedUnused ){ |
| 6100 | p->pPreallocatedUnused->fd = fd; |
drh | 55220a6 | 2019-08-06 20:55:06 +0000 | [diff] [blame] | 6101 | p->pPreallocatedUnused->flags = |
| 6102 | flags & (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6103 | } |
| 6104 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6105 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6106 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 6107 | zPath = zName; |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 6108 | #elif defined(SQLITE_UNLINK_AFTER_CLOSE) |
| 6109 | zPath = sqlite3_mprintf("%s", zName); |
| 6110 | if( zPath==0 ){ |
| 6111 | robust_close(p, fd, __LINE__); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6112 | return SQLITE_NOMEM_BKPT; |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 6113 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 6114 | #else |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 6115 | osUnlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 6116 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6117 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 6118 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 6119 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6120 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 6121 | } |
| 6122 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6123 | |
| 6124 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6125 | if( fstatfs(fd, &fsInfo) == -1 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6126 | storeLastErrno(p, errno); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6127 | robust_close(p, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6128 | return SQLITE_IOERR_ACCESS; |
| 6129 | } |
| 6130 | if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 6131 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 6132 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6133 | if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) { |
| 6134 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 6135 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6136 | #endif |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6137 | |
| 6138 | /* Set up appropriate ctrlFlags */ |
| 6139 | if( isDelete ) ctrlFlags |= UNIXFILE_DELETE; |
| 6140 | if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY; |
drh | 86151e8 | 2015-12-08 14:37:16 +0000 | [diff] [blame] | 6141 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6142 | if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK; |
drh | a803a2c | 2017-12-13 20:02:29 +0000 | [diff] [blame] | 6143 | if( isNewJrnl ) ctrlFlags |= UNIXFILE_DIRSYNC; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6144 | if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI; |
| 6145 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6146 | #if SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6147 | #if SQLITE_PREFER_PROXY_LOCKING |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6148 | isAutoProxy = 1; |
| 6149 | #endif |
| 6150 | if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6151 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 6152 | int useProxy = 0; |
| 6153 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6154 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 6155 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6156 | if( envforce!=NULL ){ |
| 6157 | useProxy = atoi(envforce)>0; |
| 6158 | }else{ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6159 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 6160 | } |
| 6161 | if( useProxy ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6162 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6163 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6164 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6165 | if( rc!=SQLITE_OK ){ |
| 6166 | /* Use unixClose to clean up the resources added in fillInUnixFile |
| 6167 | ** and clear all the structure's references. Specifically, |
| 6168 | ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 6169 | */ |
| 6170 | unixClose(pFile); |
| 6171 | return rc; |
| 6172 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6173 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6174 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6175 | } |
| 6176 | } |
| 6177 | #endif |
| 6178 | |
dan | 3ed0f1c | 2017-09-14 21:12:07 +0000 | [diff] [blame] | 6179 | assert( zPath==0 || zPath[0]=='/' |
| 6180 | || eType==SQLITE_OPEN_MASTER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 6181 | ); |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6182 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
| 6183 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6184 | open_finished: |
| 6185 | if( rc!=SQLITE_OK ){ |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 6186 | sqlite3_free(p->pPreallocatedUnused); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6187 | } |
| 6188 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6189 | } |
| 6190 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6191 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6192 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6193 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 6194 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6195 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6196 | static int unixDelete( |
| 6197 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 6198 | const char *zPath, /* Name of file to be deleted */ |
| 6199 | int dirSync /* If true, fsync() directory after deleting file */ |
| 6200 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6201 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6202 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6203 | SimulateIOError(return SQLITE_IOERR_DELETE); |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 6204 | if( osUnlink(zPath)==(-1) ){ |
drh | bd94554 | 2014-08-13 11:39:42 +0000 | [diff] [blame] | 6205 | if( errno==ENOENT |
| 6206 | #if OS_VXWORKS |
drh | 19541f3 | 2014-09-01 13:37:55 +0000 | [diff] [blame] | 6207 | || osAccess(zPath,0)!=0 |
drh | bd94554 | 2014-08-13 11:39:42 +0000 | [diff] [blame] | 6208 | #endif |
| 6209 | ){ |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 6210 | rc = SQLITE_IOERR_DELETE_NOENT; |
| 6211 | }else{ |
drh | b430816 | 2012-11-09 21:40:02 +0000 | [diff] [blame] | 6212 | rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath); |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 6213 | } |
drh | b430816 | 2012-11-09 21:40:02 +0000 | [diff] [blame] | 6214 | return rc; |
drh | 5d4feff | 2010-07-14 01:45:22 +0000 | [diff] [blame] | 6215 | } |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 6216 | #ifndef SQLITE_DISABLE_DIRSYNC |
drh | e349519 | 2012-01-05 16:07:30 +0000 | [diff] [blame] | 6217 | if( (dirSync & 1)!=0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6218 | int fd; |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 6219 | rc = osOpenDirectory(zPath, &fd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6220 | if( rc==SQLITE_OK ){ |
drh | 6d25899 | 2016-02-04 09:48:12 +0000 | [diff] [blame] | 6221 | if( full_fsync(fd,0,0) ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 6222 | rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6223 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6224 | robust_close(0, fd, __LINE__); |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 6225 | }else{ |
| 6226 | assert( rc==SQLITE_CANTOPEN ); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 6227 | rc = SQLITE_OK; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6228 | } |
| 6229 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 6230 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6231 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6232 | } |
| 6233 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6234 | /* |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 6235 | ** Test the existence of or access permissions of file zPath. The |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6236 | ** test performed depends on the value of flags: |
| 6237 | ** |
| 6238 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 6239 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 6240 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 6241 | ** |
| 6242 | ** Otherwise return 0. |
| 6243 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6244 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6245 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 6246 | const char *zPath, /* Path of the file to examine */ |
| 6247 | int flags, /* What do we want to learn about the zPath file? */ |
| 6248 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6249 | ){ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6250 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6251 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 6252 | assert( pResOut!=0 ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6253 | |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame^] | 6254 | /* The spec says there are four possible values for flags. But the |
| 6255 | ** SQLITE_ACCESS_READ flag is never used */ |
| 6256 | assert( flags==SQLITE_ACCESS_EXISTS |
| 6257 | || flags==SQLITE_ACCESS_READWRITE |
| 6258 | || flags==SQLITE_ACCESS_SYMLINK ); |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 6259 | |
| 6260 | if( flags==SQLITE_ACCESS_EXISTS ){ |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 6261 | struct stat buf; |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 6262 | *pResOut = (0==osStat(zPath, &buf) && buf.st_size>0); |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame^] | 6263 | }else if( flags==SQLITE_ACCESS_READWRITE ){ |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 6264 | *pResOut = osAccess(zPath, W_OK|R_OK)==0; |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame^] | 6265 | }else{ |
| 6266 | #if !defined(HAVE_LSTAT) |
| 6267 | *pResOut = 0; |
| 6268 | #else |
| 6269 | struct stat buf; |
| 6270 | *pResOut = (0==osLstat(zPath, &buf) && S_ISLNK(buf.st_mode)); |
| 6271 | #endif |
| 6272 | assert( flags==SQLITE_ACCESS_SYMLINK ); |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 6273 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6274 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6275 | } |
| 6276 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6277 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6278 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6279 | */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6280 | static int mkFullPathname( |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6281 | const char *zPath, /* Input path */ |
| 6282 | char *zOut, /* Output buffer */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6283 | int nOut /* Allocated size of buffer zOut */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 6284 | ){ |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6285 | int nPath = sqlite3Strlen30(zPath); |
| 6286 | int iOff = 0; |
| 6287 | if( zPath[0]!='/' ){ |
| 6288 | if( osGetcwd(zOut, nOut-2)==0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 6289 | return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6290 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6291 | iOff = sqlite3Strlen30(zOut); |
| 6292 | zOut[iOff++] = '/'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6293 | } |
dan | 2349670 | 2016-01-26 13:56:42 +0000 | [diff] [blame] | 6294 | if( (iOff+nPath+1)>nOut ){ |
| 6295 | /* SQLite assumes that xFullPathname() nul-terminates the output buffer |
| 6296 | ** even if it returns an error. */ |
| 6297 | zOut[iOff] = '\0'; |
| 6298 | return SQLITE_CANTOPEN_BKPT; |
| 6299 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6300 | sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6301 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6302 | } |
| 6303 | |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6304 | /* |
| 6305 | ** Turn a relative pathname into a full pathname. The relative path |
| 6306 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 6307 | ** zPath. |
| 6308 | ** |
| 6309 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 6310 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 6311 | ** this buffer before returning. |
| 6312 | */ |
| 6313 | static int unixFullPathname( |
| 6314 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 6315 | const char *zPath, /* Possibly relative input path */ |
| 6316 | int nOut, /* Size of output buffer in bytes */ |
| 6317 | char *zOut /* Output buffer */ |
| 6318 | ){ |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 6319 | #if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT) |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6320 | return mkFullPathname(zPath, zOut, nOut); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6321 | #else |
| 6322 | int rc = SQLITE_OK; |
| 6323 | int nByte; |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6324 | int nLink = 1; /* Number of symbolic links followed so far */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6325 | const char *zIn = zPath; /* Input path for each iteration of loop */ |
| 6326 | char *zDel = 0; |
| 6327 | |
| 6328 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
| 6329 | UNUSED_PARAMETER(pVfs); |
| 6330 | |
| 6331 | /* It's odd to simulate an io-error here, but really this is just |
| 6332 | ** using the io-error infrastructure to test that SQLite handles this |
| 6333 | ** function failing. This function could fail if, for example, the |
| 6334 | ** current working directory has been unlinked. |
| 6335 | */ |
| 6336 | SimulateIOError( return SQLITE_ERROR ); |
| 6337 | |
| 6338 | do { |
| 6339 | |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6340 | /* Call stat() on path zIn. Set bLink to true if the path is a symbolic |
| 6341 | ** link, or false otherwise. */ |
| 6342 | int bLink = 0; |
| 6343 | struct stat buf; |
| 6344 | if( osLstat(zIn, &buf)!=0 ){ |
| 6345 | if( errno!=ENOENT ){ |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 6346 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6347 | } |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6348 | }else{ |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6349 | bLink = S_ISLNK(buf.st_mode); |
| 6350 | } |
| 6351 | |
| 6352 | if( bLink ){ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6353 | if( zDel==0 ){ |
| 6354 | zDel = sqlite3_malloc(nOut); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6355 | if( zDel==0 ) rc = SQLITE_NOMEM_BKPT; |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6356 | }else if( ++nLink>SQLITE_MAX_SYMLINKS ){ |
| 6357 | rc = SQLITE_CANTOPEN_BKPT; |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6358 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6359 | |
| 6360 | if( rc==SQLITE_OK ){ |
| 6361 | nByte = osReadlink(zIn, zDel, nOut-1); |
| 6362 | if( nByte<0 ){ |
| 6363 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn); |
dan | 2349670 | 2016-01-26 13:56:42 +0000 | [diff] [blame] | 6364 | }else{ |
| 6365 | if( zDel[0]!='/' ){ |
| 6366 | int n; |
| 6367 | for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--); |
| 6368 | if( nByte+n+1>nOut ){ |
| 6369 | rc = SQLITE_CANTOPEN_BKPT; |
| 6370 | }else{ |
| 6371 | memmove(&zDel[n], zDel, nByte+1); |
| 6372 | memcpy(zDel, zIn, n); |
| 6373 | nByte += n; |
| 6374 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6375 | } |
| 6376 | zDel[nByte] = '\0'; |
| 6377 | } |
| 6378 | } |
| 6379 | |
| 6380 | zIn = zDel; |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6381 | } |
| 6382 | |
dan | 2349670 | 2016-01-26 13:56:42 +0000 | [diff] [blame] | 6383 | assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' ); |
| 6384 | if( rc==SQLITE_OK && zIn!=zOut ){ |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6385 | rc = mkFullPathname(zIn, zOut, nOut); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6386 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6387 | if( bLink==0 ) break; |
| 6388 | zIn = zOut; |
| 6389 | }while( rc==SQLITE_OK ); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6390 | |
| 6391 | sqlite3_free(zDel); |
| 6392 | return rc; |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 6393 | #endif /* HAVE_READLINK && HAVE_LSTAT */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6394 | } |
| 6395 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 6396 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6397 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 6398 | /* |
| 6399 | ** Interfaces for opening a shared library, finding entry points |
| 6400 | ** within the shared library, and closing the shared library. |
| 6401 | */ |
| 6402 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6403 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 6404 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6405 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 6406 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 6407 | |
| 6408 | /* |
| 6409 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 6410 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 6411 | ** message is available, it is written to zBufOut. If no error message |
| 6412 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 6413 | ** error message. |
| 6414 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6415 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
dan | 3239053 | 2010-11-29 18:36:22 +0000 | [diff] [blame] | 6416 | const char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6417 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6418 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6419 | zErr = dlerror(); |
| 6420 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6421 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6422 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6423 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6424 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6425 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 6426 | /* |
| 6427 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 6428 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 6429 | ** returns a void* which is really a pointer to a function. So how do we |
| 6430 | ** use dlsym() with -pedantic-errors? |
| 6431 | ** |
| 6432 | ** Variable x below is defined to be a pointer to a function taking |
| 6433 | ** parameters void* and const char* and returning a pointer to a function. |
| 6434 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 6435 | ** (That assignment requires a cast.) Then we call the function that |
| 6436 | ** x points to. |
| 6437 | ** |
| 6438 | ** This work-around is unlikely to work correctly on any system where |
| 6439 | ** you really cannot cast a function pointer into void*. But then, on the |
| 6440 | ** other hand, dlsym() will not work on such a system either, so we have |
| 6441 | ** not really lost anything. |
| 6442 | */ |
| 6443 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6444 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6445 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 6446 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6447 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6448 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 6449 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6450 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6451 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6452 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 6453 | #define unixDlOpen 0 |
| 6454 | #define unixDlError 0 |
| 6455 | #define unixDlSym 0 |
| 6456 | #define unixDlClose 0 |
| 6457 | #endif |
| 6458 | |
| 6459 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6460 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6461 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6462 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 6463 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 6464 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6465 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6466 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 6467 | ** errors. The reports issued by valgrind are incorrect - we would |
| 6468 | ** prefer that the randomness be increased by making use of the |
| 6469 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 6470 | ** some users. Rather than argue, it seems easier just to initialize |
| 6471 | ** the whole array and silence valgrind, even if that means less randomness |
| 6472 | ** in the random seed. |
| 6473 | ** |
| 6474 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 6475 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6476 | ** tests repeatable. |
| 6477 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6478 | memset(zBuf, 0, nBuf); |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6479 | randomnessPid = osGetpid(0); |
drh | 6a412b8 | 2015-04-30 12:31:49 +0000 | [diff] [blame] | 6480 | #if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6481 | { |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 6482 | int fd, got; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 6483 | fd = robust_open("/dev/urandom", O_RDONLY, 0); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6484 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 6485 | time_t t; |
| 6486 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6487 | memcpy(zBuf, &t, sizeof(t)); |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 6488 | memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid)); |
| 6489 | assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf ); |
| 6490 | nBuf = sizeof(t) + sizeof(randomnessPid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6491 | }else{ |
drh | c18b404 | 2012-02-10 03:10:27 +0000 | [diff] [blame] | 6492 | do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6493 | robust_close(0, fd, __LINE__); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6494 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6495 | } |
| 6496 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 6497 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6498 | } |
| 6499 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6500 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6501 | /* |
| 6502 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6503 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 6504 | ** The return value is the number of microseconds of sleep actually |
| 6505 | ** requested from the underlying operating system, a number which |
| 6506 | ** might be greater than or equal to the argument, but not less |
| 6507 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6508 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6509 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6510 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 6511 | struct timespec sp; |
| 6512 | |
| 6513 | sp.tv_sec = microseconds / 1000000; |
| 6514 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 6515 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6516 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6517 | return microseconds; |
| 6518 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6519 | usleep(microseconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6520 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6521 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6522 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6523 | int seconds = (microseconds+999999)/1000000; |
| 6524 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6525 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 6526 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 6527 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 6528 | } |
| 6529 | |
| 6530 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6531 | ** The following variable, if set to a non-zero value, is interpreted as |
| 6532 | ** the number of seconds since 1970 and is used to set the result of |
| 6533 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6534 | */ |
| 6535 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6536 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6537 | #endif |
| 6538 | |
| 6539 | /* |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6540 | ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 6541 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 6542 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 6543 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 6544 | ** proleptic Gregorian calendar. |
| 6545 | ** |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6546 | ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
| 6547 | ** cannot be found. |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6548 | */ |
| 6549 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 6550 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6551 | int rc = SQLITE_OK; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6552 | #if defined(NO_GETTOD) |
| 6553 | time_t t; |
| 6554 | time(&t); |
dan | 15eac4e | 2010-11-22 17:26:07 +0000 | [diff] [blame] | 6555 | *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6556 | #elif OS_VXWORKS |
| 6557 | struct timespec sNow; |
| 6558 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 6559 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 6560 | #else |
| 6561 | struct timeval sNow; |
drh | 970942e | 2015-11-25 23:13:14 +0000 | [diff] [blame] | 6562 | (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */ |
| 6563 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6564 | #endif |
| 6565 | |
| 6566 | #ifdef SQLITE_TEST |
| 6567 | if( sqlite3_current_time ){ |
| 6568 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 6569 | } |
| 6570 | #endif |
| 6571 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6572 | return rc; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6573 | } |
| 6574 | |
drh | c3dfa5e | 2016-01-22 19:44:03 +0000 | [diff] [blame] | 6575 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6576 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6577 | ** Find the current time (in Universal Coordinated Time). Write the |
| 6578 | ** current time and date as a Julian Day number into *prNow and |
| 6579 | ** return 0. Return 1 if the time and date cannot be found. |
| 6580 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6581 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | b87a666 | 2011-10-13 01:01:14 +0000 | [diff] [blame] | 6582 | sqlite3_int64 i = 0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6583 | int rc; |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 6584 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6585 | rc = unixCurrentTimeInt64(0, &i); |
drh | 0dcb0a7 | 2010-05-03 18:22:52 +0000 | [diff] [blame] | 6586 | *prNow = i/86400000.0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6587 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6588 | } |
drh | 5337dac | 2015-11-25 15:15:03 +0000 | [diff] [blame] | 6589 | #else |
| 6590 | # define unixCurrentTime 0 |
| 6591 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6592 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6593 | /* |
drh | 1b9f214 | 2016-03-17 16:01:23 +0000 | [diff] [blame] | 6594 | ** The xGetLastError() method is designed to return a better |
| 6595 | ** low-level error message when operating-system problems come up |
| 6596 | ** during SQLite operation. Only the integer return code is currently |
| 6597 | ** used. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6598 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6599 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 6600 | UNUSED_PARAMETER(NotUsed); |
| 6601 | UNUSED_PARAMETER(NotUsed2); |
| 6602 | UNUSED_PARAMETER(NotUsed3); |
drh | 1b9f214 | 2016-03-17 16:01:23 +0000 | [diff] [blame] | 6603 | return errno; |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 6604 | } |
| 6605 | |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6606 | |
| 6607 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6608 | ************************ End of sqlite3_vfs methods *************************** |
| 6609 | ******************************************************************************/ |
| 6610 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6611 | /****************************************************************************** |
| 6612 | ************************** Begin Proxy Locking ******************************** |
| 6613 | ** |
| 6614 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 6615 | ** other locking methods on secondary lock files. Proxy locking is a |
| 6616 | ** meta-layer over top of the primitive locking implemented above. For |
| 6617 | ** this reason, the division that implements of proxy locking is deferred |
| 6618 | ** until late in the file (here) after all of the other I/O methods have |
| 6619 | ** been defined - so that the primitive locking methods are available |
| 6620 | ** as services to help with the implementation of proxy locking. |
| 6621 | ** |
| 6622 | **** |
| 6623 | ** |
| 6624 | ** The default locking schemes in SQLite use byte-range locks on the |
| 6625 | ** database file to coordinate safe, concurrent access by multiple readers |
| 6626 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 6627 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 6628 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 6629 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 6630 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 6631 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 6632 | ** address in the shared range is taken for a SHARED lock, the entire |
| 6633 | ** shared range is taken for an EXCLUSIVE lock): |
| 6634 | ** |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 6635 | ** PENDING_BYTE 0x40000000 |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6636 | ** RESERVED_BYTE 0x40000001 |
| 6637 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 6638 | ** |
| 6639 | ** This works well on the local file system, but shows a nearly 100x |
| 6640 | ** slowdown in read performance on AFP because the AFP client disables |
| 6641 | ** the read cache when byte-range locks are present. Enabling the read |
| 6642 | ** cache exposes a cache coherency problem that is present on all OS X |
| 6643 | ** supported network file systems. NFS and AFP both observe the |
| 6644 | ** close-to-open semantics for ensuring cache coherency |
| 6645 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 6646 | ** address the requirements for concurrent database access by multiple |
| 6647 | ** readers and writers |
| 6648 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 6649 | ** |
| 6650 | ** To address the performance and cache coherency issues, proxy file locking |
| 6651 | ** changes the way database access is controlled by limiting access to a |
| 6652 | ** single host at a time and moving file locks off of the database file |
| 6653 | ** and onto a proxy file on the local file system. |
| 6654 | ** |
| 6655 | ** |
| 6656 | ** Using proxy locks |
| 6657 | ** ----------------- |
| 6658 | ** |
| 6659 | ** C APIs |
| 6660 | ** |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6661 | ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6662 | ** <proxy_path> | ":auto:"); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6663 | ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE, |
| 6664 | ** &<proxy_path>); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6665 | ** |
| 6666 | ** |
| 6667 | ** SQL pragmas |
| 6668 | ** |
| 6669 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 6670 | ** PRAGMA [database.]lock_proxy_file |
| 6671 | ** |
| 6672 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 6673 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 6674 | ** a proxy path based on the user's temp dir |
| 6675 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 6676 | ** actual proxy file name is generated from the name and path of the |
| 6677 | ** database file. For example: |
| 6678 | ** |
| 6679 | ** For database path "/Users/me/foo.db" |
| 6680 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 6681 | ** |
| 6682 | ** Once a lock proxy is configured for a database connection, it can not |
| 6683 | ** be removed, however it may be switched to a different proxy path via |
| 6684 | ** the above APIs (assuming the conch file is not being held by another |
| 6685 | ** connection or process). |
| 6686 | ** |
| 6687 | ** |
| 6688 | ** How proxy locking works |
| 6689 | ** ----------------------- |
| 6690 | ** |
| 6691 | ** Proxy file locking relies primarily on two new supporting files: |
| 6692 | ** |
| 6693 | ** * conch file to limit access to the database file to a single host |
| 6694 | ** at a time |
| 6695 | ** |
| 6696 | ** * proxy file to act as a proxy for the advisory locks normally |
| 6697 | ** taken on the database |
| 6698 | ** |
| 6699 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 6700 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 6701 | ** contents and comparing the host's unique host ID (see below) and lock |
| 6702 | ** proxy path against the values stored in the conch. The conch file is |
| 6703 | ** stored in the same directory as the database file and the file name |
| 6704 | ** is patterned after the database file name as ".<databasename>-conch". |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 6705 | ** 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] | 6706 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 6707 | ** lock and the conch file contents is updated with the host ID and proxy |
| 6708 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 6709 | ** is held by another process (with a shared lock), the exclusive lock |
| 6710 | ** will fail and SQLITE_BUSY is returned. |
| 6711 | ** |
| 6712 | ** The proxy file - a single-byte file used for all advisory file locks |
| 6713 | ** normally taken on the database file. This allows for safe sharing |
| 6714 | ** of the database file for multiple readers and writers on the same |
| 6715 | ** host (the conch ensures that they all use the same local lock file). |
| 6716 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6717 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 6718 | ** only taken when the first request to lock database file is made. |
| 6719 | ** This matches the semantics of the traditional locking behavior, where |
| 6720 | ** opening a connection to a database file does not take a lock on it. |
| 6721 | ** The shared lock and an open file descriptor are maintained until |
| 6722 | ** the connection to the database is closed. |
| 6723 | ** |
| 6724 | ** The proxy file and the lock file are never deleted so they only need |
| 6725 | ** to be created the first time they are used. |
| 6726 | ** |
| 6727 | ** Configuration options |
| 6728 | ** --------------------- |
| 6729 | ** |
| 6730 | ** SQLITE_PREFER_PROXY_LOCKING |
| 6731 | ** |
| 6732 | ** Database files accessed on non-local file systems are |
| 6733 | ** automatically configured for proxy locking, lock files are |
| 6734 | ** named automatically using the same logic as |
| 6735 | ** PRAGMA lock_proxy_file=":auto:" |
| 6736 | ** |
| 6737 | ** SQLITE_PROXY_DEBUG |
| 6738 | ** |
| 6739 | ** Enables the logging of error messages during host id file |
| 6740 | ** retrieval and creation |
| 6741 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6742 | ** LOCKPROXYDIR |
| 6743 | ** |
| 6744 | ** Overrides the default directory used for lock proxy files that |
| 6745 | ** are named automatically via the ":auto:" setting |
| 6746 | ** |
| 6747 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 6748 | ** |
| 6749 | ** Permissions to use when creating a directory for storing the |
| 6750 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 6751 | ** |
| 6752 | ** |
| 6753 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 6754 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 6755 | ** force proxy locking to be used for every database file opened, and 0 |
| 6756 | ** will force automatic proxy locking to be disabled for all database |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6757 | ** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6758 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 6759 | */ |
| 6760 | |
| 6761 | /* |
| 6762 | ** Proxy locking is only available on MacOSX |
| 6763 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6764 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6765 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6766 | /* |
| 6767 | ** The proxyLockingContext has the path and file structures for the remote |
| 6768 | ** and local proxy files in it |
| 6769 | */ |
| 6770 | typedef struct proxyLockingContext proxyLockingContext; |
| 6771 | struct proxyLockingContext { |
| 6772 | unixFile *conchFile; /* Open conch file */ |
| 6773 | char *conchFilePath; /* Name of the conch file */ |
| 6774 | unixFile *lockProxy; /* Open proxy lock file */ |
| 6775 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 6776 | char *dbPath; /* Name of the open file */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6777 | int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6778 | int nFails; /* Number of conch taking failures */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6779 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 6780 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 6781 | }; |
| 6782 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6783 | /* |
| 6784 | ** The proxy lock file path for the database at dbPath is written into lPath, |
| 6785 | ** which must point to valid, writable memory large enough for a maxLen length |
| 6786 | ** file path. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6787 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6788 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 6789 | int len; |
| 6790 | int dbLen; |
| 6791 | int i; |
| 6792 | |
| 6793 | #ifdef LOCKPROXYDIR |
| 6794 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 6795 | #else |
| 6796 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 6797 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6798 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6799 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6800 | lPath, errno, osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6801 | return SQLITE_IOERR_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6802 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6803 | len = strlcat(lPath, "sqliteplocks", maxLen); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6804 | } |
| 6805 | # else |
| 6806 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 6807 | # endif |
| 6808 | #endif |
| 6809 | |
| 6810 | if( lPath[len-1]!='/' ){ |
| 6811 | len = strlcat(lPath, "/", maxLen); |
| 6812 | } |
| 6813 | |
| 6814 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6815 | dbLen = (int)strlen(dbPath); |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6816 | for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6817 | char c = dbPath[i]; |
| 6818 | lPath[i+len] = (c=='/')?'_':c; |
| 6819 | } |
| 6820 | lPath[i+len]='\0'; |
| 6821 | strlcat(lPath, ":auto:", maxLen); |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6822 | OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6823 | return SQLITE_OK; |
| 6824 | } |
| 6825 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6826 | /* |
| 6827 | ** Creates the lock file and any missing directories in lockPath |
| 6828 | */ |
| 6829 | static int proxyCreateLockPath(const char *lockPath){ |
| 6830 | int i, len; |
| 6831 | char buf[MAXPATHLEN]; |
| 6832 | int start = 0; |
| 6833 | |
| 6834 | assert(lockPath!=NULL); |
| 6835 | /* try to create all the intermediate directories */ |
| 6836 | len = (int)strlen(lockPath); |
| 6837 | buf[0] = lockPath[0]; |
| 6838 | for( i=1; i<len; i++ ){ |
| 6839 | if( lockPath[i] == '/' && (i - start > 0) ){ |
| 6840 | /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 6841 | if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 6842 | || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 6843 | buf[i]='\0'; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 6844 | if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6845 | int err=errno; |
| 6846 | if( err!=EEXIST ) { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6847 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6848 | "'%s' proxy lock path=%s pid=%d\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6849 | buf, strerror(err), lockPath, osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6850 | return err; |
| 6851 | } |
| 6852 | } |
| 6853 | } |
| 6854 | start=i+1; |
| 6855 | } |
| 6856 | buf[i] = lockPath[i]; |
| 6857 | } |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 6858 | OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6859 | return 0; |
| 6860 | } |
| 6861 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6862 | /* |
| 6863 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 6864 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 6865 | ** |
| 6866 | ** The caller is responsible not only for closing the file descriptor |
| 6867 | ** but also for freeing the memory associated with the file descriptor. |
| 6868 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6869 | static int proxyCreateUnixFile( |
| 6870 | const char *path, /* path for the new unixFile */ |
| 6871 | unixFile **ppFile, /* unixFile created and returned by ref */ |
| 6872 | int islockfile /* if non zero missing dirs will be created */ |
| 6873 | ) { |
| 6874 | int fd = -1; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6875 | unixFile *pNew; |
| 6876 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6877 | int openFlags = O_RDWR | O_CREAT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6878 | sqlite3_vfs dummyVfs; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6879 | int terrno = 0; |
| 6880 | UnixUnusedFd *pUnused = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6881 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6882 | /* 1. first try to open/create the file |
| 6883 | ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 6884 | ** the parent directories and then try again. |
| 6885 | ** 3. if that fails, try to open the file read-only |
| 6886 | ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 6887 | */ |
| 6888 | pUnused = findReusableFd(path, openFlags); |
| 6889 | if( pUnused ){ |
| 6890 | fd = pUnused->fd; |
| 6891 | }else{ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 6892 | pUnused = sqlite3_malloc64(sizeof(*pUnused)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6893 | if( !pUnused ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6894 | return SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6895 | } |
| 6896 | } |
| 6897 | if( fd<0 ){ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6898 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6899 | terrno = errno; |
| 6900 | if( fd<0 && errno==ENOENT && islockfile ){ |
| 6901 | if( proxyCreateLockPath(path) == SQLITE_OK ){ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6902 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6903 | } |
| 6904 | } |
| 6905 | } |
| 6906 | if( fd<0 ){ |
| 6907 | openFlags = O_RDONLY; |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 6908 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6909 | terrno = errno; |
| 6910 | } |
| 6911 | if( fd<0 ){ |
| 6912 | if( islockfile ){ |
| 6913 | return SQLITE_BUSY; |
| 6914 | } |
| 6915 | switch (terrno) { |
| 6916 | case EACCES: |
| 6917 | return SQLITE_PERM; |
| 6918 | case EIO: |
| 6919 | return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 6920 | default: |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 6921 | return SQLITE_CANTOPEN_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6922 | } |
| 6923 | } |
| 6924 | |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 6925 | pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6926 | if( pNew==NULL ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6927 | rc = SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6928 | goto end_create_proxy; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6929 | } |
| 6930 | memset(pNew, 0, sizeof(unixFile)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6931 | pNew->openFlags = openFlags; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 6932 | memset(&dummyVfs, 0, sizeof(dummyVfs)); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6933 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 6934 | dummyVfs.zName = "dummy"; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6935 | pUnused->fd = fd; |
| 6936 | pUnused->flags = openFlags; |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 6937 | pNew->pPreallocatedUnused = pUnused; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6938 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6939 | rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6940 | if( rc==SQLITE_OK ){ |
| 6941 | *ppFile = pNew; |
| 6942 | return SQLITE_OK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6943 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6944 | end_create_proxy: |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6945 | robust_close(pNew, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6946 | sqlite3_free(pNew); |
| 6947 | sqlite3_free(pUnused); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6948 | return rc; |
| 6949 | } |
| 6950 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6951 | #ifdef SQLITE_TEST |
| 6952 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 6953 | int sqlite3_hostid_num = 0; |
| 6954 | #endif |
| 6955 | |
| 6956 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 6957 | |
drh | e4079e1 | 2019-09-27 16:33:27 +0000 | [diff] [blame] | 6958 | #if HAVE_GETHOSTUUID |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6959 | /* Not always defined in the headers as it ought to be */ |
| 6960 | extern int gethostuuid(uuid_t id, const struct timespec *wait); |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 6961 | #endif |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6962 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6963 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 6964 | ** bytes of writable memory. |
| 6965 | */ |
| 6966 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6967 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 6968 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
drh | e4079e1 | 2019-09-27 16:33:27 +0000 | [diff] [blame] | 6969 | #if HAVE_GETHOSTUUID |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 6970 | { |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6971 | struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 6972 | if( gethostuuid(pHostID, &timeout) ){ |
| 6973 | int err = errno; |
| 6974 | if( pError ){ |
| 6975 | *pError = err; |
| 6976 | } |
| 6977 | return SQLITE_IOERR; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6978 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6979 | } |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6980 | #else |
| 6981 | UNUSED_PARAMETER(pError); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 6982 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6983 | #ifdef SQLITE_TEST |
| 6984 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 6985 | if( sqlite3_hostid_num != 0){ |
| 6986 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 6987 | } |
| 6988 | #endif |
| 6989 | |
| 6990 | return SQLITE_OK; |
| 6991 | } |
| 6992 | |
| 6993 | /* The conch file contains the header, host id and lock file path |
| 6994 | */ |
| 6995 | #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 6996 | #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 6997 | #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 6998 | #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 6999 | |
| 7000 | /* |
| 7001 | ** Takes an open conch file, copies the contents to a new path and then moves |
| 7002 | ** it back. The newly created file's file descriptor is assigned to the |
| 7003 | ** conch file structure and finally the original conch file descriptor is |
| 7004 | ** closed. Returns zero if successful. |
| 7005 | */ |
| 7006 | static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 7007 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7008 | unixFile *conchFile = pCtx->conchFile; |
| 7009 | char tPath[MAXPATHLEN]; |
| 7010 | char buf[PROXY_MAXCONCHLEN]; |
| 7011 | char *cPath = pCtx->conchFilePath; |
| 7012 | size_t readLen = 0; |
| 7013 | size_t pathLen = 0; |
| 7014 | char errmsg[64] = ""; |
| 7015 | int fd = -1; |
| 7016 | int rc = -1; |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 7017 | UNUSED_PARAMETER(myHostID); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7018 | |
| 7019 | /* create a new path by replace the trailing '-conch' with '-break' */ |
| 7020 | pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 7021 | if( pathLen>MAXPATHLEN || pathLen<6 || |
| 7022 | (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 7023 | sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7024 | goto end_breaklock; |
| 7025 | } |
| 7026 | /* read the conch content */ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7027 | readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7028 | if( readLen<PROXY_PATHINDEX ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 7029 | sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7030 | goto end_breaklock; |
| 7031 | } |
| 7032 | /* write it out to the temporary break file */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 7033 | fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7034 | if( fd<0 ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 7035 | sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7036 | goto end_breaklock; |
| 7037 | } |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7038 | if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 7039 | sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7040 | goto end_breaklock; |
| 7041 | } |
| 7042 | if( rename(tPath, cPath) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 7043 | sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7044 | goto end_breaklock; |
| 7045 | } |
| 7046 | rc = 0; |
| 7047 | fprintf(stderr, "broke stale lock on %s\n", cPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 7048 | robust_close(pFile, conchFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7049 | conchFile->h = fd; |
| 7050 | conchFile->openFlags = O_RDWR | O_CREAT; |
| 7051 | |
| 7052 | end_breaklock: |
| 7053 | if( rc ){ |
| 7054 | if( fd>=0 ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 7055 | osUnlink(tPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 7056 | robust_close(pFile, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7057 | } |
| 7058 | fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 7059 | } |
| 7060 | return rc; |
| 7061 | } |
| 7062 | |
| 7063 | /* Take the requested lock on the conch file and break a stale lock if the |
| 7064 | ** host id matches. |
| 7065 | */ |
| 7066 | static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 7067 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7068 | unixFile *conchFile = pCtx->conchFile; |
| 7069 | int rc = SQLITE_OK; |
| 7070 | int nTries = 0; |
| 7071 | struct timespec conchModTime; |
| 7072 | |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 7073 | memset(&conchModTime, 0, sizeof(conchModTime)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7074 | do { |
| 7075 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 7076 | nTries ++; |
| 7077 | if( rc==SQLITE_BUSY ){ |
| 7078 | /* If the lock failed (busy): |
| 7079 | * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 7080 | * 2nd try: fail if the mod time changed or host id is different, wait |
| 7081 | * 10 sec and try again |
| 7082 | * 3rd try: break the lock unless the mod time has changed. |
| 7083 | */ |
| 7084 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7085 | if( osFstat(conchFile->h, &buf) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7086 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7087 | return SQLITE_IOERR_LOCK; |
| 7088 | } |
| 7089 | |
| 7090 | if( nTries==1 ){ |
| 7091 | conchModTime = buf.st_mtimespec; |
| 7092 | usleep(500000); /* wait 0.5 sec and try the lock again*/ |
| 7093 | continue; |
| 7094 | } |
| 7095 | |
| 7096 | assert( nTries>1 ); |
| 7097 | if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 7098 | conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 7099 | return SQLITE_BUSY; |
| 7100 | } |
| 7101 | |
| 7102 | if( nTries==2 ){ |
| 7103 | char tBuf[PROXY_MAXCONCHLEN]; |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7104 | int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7105 | if( len<0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7106 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7107 | return SQLITE_IOERR_LOCK; |
| 7108 | } |
| 7109 | if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 7110 | /* don't break the lock if the host id doesn't match */ |
| 7111 | if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 7112 | return SQLITE_BUSY; |
| 7113 | } |
| 7114 | }else{ |
| 7115 | /* don't break the lock on short read or a version mismatch */ |
| 7116 | return SQLITE_BUSY; |
| 7117 | } |
| 7118 | usleep(10000000); /* wait 10 sec and try the lock again */ |
| 7119 | continue; |
| 7120 | } |
| 7121 | |
| 7122 | assert( nTries==3 ); |
| 7123 | if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 7124 | rc = SQLITE_OK; |
| 7125 | if( lockType==EXCLUSIVE_LOCK ){ |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 7126 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7127 | } |
| 7128 | if( !rc ){ |
| 7129 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 7130 | } |
| 7131 | } |
| 7132 | } |
| 7133 | } while( rc==SQLITE_BUSY && nTries<3 ); |
| 7134 | |
| 7135 | return rc; |
| 7136 | } |
| 7137 | |
| 7138 | /* 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] | 7139 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 7140 | ** lockPath means that the lockPath in the conch file will be used if the |
| 7141 | ** host IDs match, or a new lock path will be generated automatically |
| 7142 | ** and written to the conch file. |
| 7143 | */ |
| 7144 | static int proxyTakeConch(unixFile *pFile){ |
| 7145 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7146 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7147 | if( pCtx->conchHeld!=0 ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7148 | return SQLITE_OK; |
| 7149 | }else{ |
| 7150 | unixFile *conchFile = pCtx->conchFile; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7151 | uuid_t myHostID; |
| 7152 | int pError = 0; |
| 7153 | char readBuf[PROXY_MAXCONCHLEN]; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7154 | char lockPath[MAXPATHLEN]; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7155 | char *tempLockPath = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7156 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7157 | int createConch = 0; |
| 7158 | int hostIdMatch = 0; |
| 7159 | int readLen = 0; |
| 7160 | int tryOldLockPath = 0; |
| 7161 | int forceNewLockPath = 0; |
| 7162 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7163 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 7164 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 7165 | osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7166 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7167 | rc = proxyGetHostID(myHostID, &pError); |
| 7168 | if( (rc&0xff)==SQLITE_IOERR ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7169 | storeLastErrno(pFile, pError); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7170 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7171 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7172 | rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7173 | if( rc!=SQLITE_OK ){ |
| 7174 | goto end_takeconch; |
| 7175 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7176 | /* read the existing conch file */ |
| 7177 | readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 7178 | if( readLen<0 ){ |
| 7179 | /* I/O error: lastErrno set by seekAndRead */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7180 | storeLastErrno(pFile, conchFile->lastErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7181 | rc = SQLITE_IOERR_READ; |
| 7182 | goto end_takeconch; |
| 7183 | }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 7184 | readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 7185 | /* a short read or version format mismatch means we need to create a new |
| 7186 | ** conch file. |
| 7187 | */ |
| 7188 | createConch = 1; |
| 7189 | } |
| 7190 | /* if the host id matches and the lock path already exists in the conch |
| 7191 | ** we'll try to use the path there, if we can't open that path, we'll |
| 7192 | ** retry with a new auto-generated path |
| 7193 | */ |
| 7194 | do { /* in case we need to try again for an :auto: named lock file */ |
| 7195 | |
| 7196 | if( !createConch && !forceNewLockPath ){ |
| 7197 | hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 7198 | PROXY_HOSTIDLEN); |
| 7199 | /* if the conch has data compare the contents */ |
| 7200 | if( !pCtx->lockProxyPath ){ |
| 7201 | /* for auto-named local lock file, just check the host ID and we'll |
| 7202 | ** use the local lock file path that's already in there |
| 7203 | */ |
| 7204 | if( hostIdMatch ){ |
| 7205 | size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 7206 | |
| 7207 | if( pathLen>=MAXPATHLEN ){ |
| 7208 | pathLen=MAXPATHLEN-1; |
| 7209 | } |
| 7210 | memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 7211 | lockPath[pathLen] = 0; |
| 7212 | tempLockPath = lockPath; |
| 7213 | tryOldLockPath = 1; |
| 7214 | /* create a copy of the lock path if the conch is taken */ |
| 7215 | goto end_takeconch; |
| 7216 | } |
| 7217 | }else if( hostIdMatch |
| 7218 | && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 7219 | readLen-PROXY_PATHINDEX) |
| 7220 | ){ |
| 7221 | /* conch host and lock path match */ |
| 7222 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7223 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7224 | } |
| 7225 | |
| 7226 | /* if the conch isn't writable and doesn't match, we can't take it */ |
| 7227 | if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 7228 | rc = SQLITE_BUSY; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7229 | goto end_takeconch; |
| 7230 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7231 | |
| 7232 | /* 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] | 7233 | if( !pCtx->lockProxyPath ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7234 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 7235 | tempLockPath = lockPath; |
| 7236 | /* create a copy of the lock path _only_ if the conch is taken */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7237 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7238 | |
| 7239 | /* update conch with host and path (this will fail if other process |
| 7240 | ** has a shared lock already), if the host id matches, use the big |
| 7241 | ** stick. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7242 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7243 | futimes(conchFile->h, NULL); |
| 7244 | if( hostIdMatch && !createConch ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 7245 | if( conchFile->pInode && conchFile->pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7246 | /* We are trying for an exclusive lock but another thread in this |
| 7247 | ** same process is still holding a shared lock. */ |
| 7248 | rc = SQLITE_BUSY; |
| 7249 | } else { |
| 7250 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7251 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7252 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7253 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7254 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7255 | if( rc==SQLITE_OK ){ |
| 7256 | char writeBuffer[PROXY_MAXCONCHLEN]; |
| 7257 | int writeSize = 0; |
| 7258 | |
| 7259 | writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 7260 | memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 7261 | if( pCtx->lockProxyPath!=NULL ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7262 | strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, |
| 7263 | MAXPATHLEN); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7264 | }else{ |
| 7265 | strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 7266 | } |
| 7267 | writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 7268 | robust_ftruncate(conchFile->h, writeSize); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7269 | rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
drh | 6d25899 | 2016-02-04 09:48:12 +0000 | [diff] [blame] | 7270 | full_fsync(conchFile->h,0,0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7271 | /* If we created a new conch file (not just updated the contents of a |
| 7272 | ** valid conch file), try to match the permissions of the database |
| 7273 | */ |
| 7274 | if( rc==SQLITE_OK && createConch ){ |
| 7275 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7276 | int err = osFstat(pFile->h, &buf); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7277 | if( err==0 ){ |
| 7278 | mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 7279 | S_IROTH|S_IWOTH); |
| 7280 | /* try to match the database file R/W permissions, ignore failure */ |
| 7281 | #ifndef SQLITE_PROXY_DEBUG |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7282 | osFchmod(conchFile->h, cmode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7283 | #else |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 7284 | do{ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7285 | rc = osFchmod(conchFile->h, cmode); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 7286 | }while( rc==(-1) && errno==EINTR ); |
| 7287 | if( rc!=0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7288 | int code = errno; |
| 7289 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 7290 | cmode, code, strerror(code)); |
| 7291 | } else { |
| 7292 | fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 7293 | } |
| 7294 | }else{ |
| 7295 | int code = errno; |
| 7296 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 7297 | err, code, strerror(code)); |
| 7298 | #endif |
| 7299 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7300 | } |
| 7301 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7302 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 7303 | |
| 7304 | end_takeconch: |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7305 | OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7306 | if( rc==SQLITE_OK && pFile->openFlags ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 7307 | int fd; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7308 | if( pFile->h>=0 ){ |
drh | e84009f | 2011-03-02 17:54:32 +0000 | [diff] [blame] | 7309 | robust_close(pFile, pFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7310 | } |
| 7311 | pFile->h = -1; |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 7312 | fd = robust_open(pCtx->dbPath, pFile->openFlags, 0); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7313 | OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7314 | if( fd>=0 ){ |
| 7315 | pFile->h = fd; |
| 7316 | }else{ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 7317 | rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7318 | during locking */ |
| 7319 | } |
| 7320 | } |
| 7321 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 7322 | char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 7323 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 7324 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 7325 | /* we couldn't create the proxy lock file with the old lock file path |
| 7326 | ** so try again via auto-naming |
| 7327 | */ |
| 7328 | forceNewLockPath = 1; |
| 7329 | tryOldLockPath = 0; |
dan | 2b0ef47 | 2010-02-16 12:18:47 +0000 | [diff] [blame] | 7330 | continue; /* go back to the do {} while start point, try again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7331 | } |
| 7332 | } |
| 7333 | if( rc==SQLITE_OK ){ |
| 7334 | /* Need to make a copy of path if we extracted the value |
| 7335 | ** from the conch file or the path was allocated on the stack |
| 7336 | */ |
| 7337 | if( tempLockPath ){ |
| 7338 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 7339 | if( !pCtx->lockProxyPath ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7340 | rc = SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7341 | } |
| 7342 | } |
| 7343 | } |
| 7344 | if( rc==SQLITE_OK ){ |
| 7345 | pCtx->conchHeld = 1; |
| 7346 | |
| 7347 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 7348 | afpLockingContext *afpCtx; |
| 7349 | afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 7350 | afpCtx->dbPath = pCtx->lockProxyPath; |
| 7351 | } |
| 7352 | } else { |
| 7353 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 7354 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7355 | OSTRACE(("TAKECONCH %d %s\n", conchFile->h, |
| 7356 | rc==SQLITE_OK?"ok":"failed")); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7357 | return rc; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7358 | } while (1); /* in case we need to retry the :auto: lock file - |
| 7359 | ** we should never get here except via the 'continue' call. */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7360 | } |
| 7361 | } |
| 7362 | |
| 7363 | /* |
| 7364 | ** If pFile holds a lock on a conch file, then release that lock. |
| 7365 | */ |
| 7366 | static int proxyReleaseConch(unixFile *pFile){ |
drh | 1c5bb4d | 2010-05-10 17:29:28 +0000 | [diff] [blame] | 7367 | int rc = SQLITE_OK; /* Subroutine return code */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7368 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 7369 | unixFile *conchFile; /* Name of the conch file */ |
| 7370 | |
| 7371 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7372 | conchFile = pCtx->conchFile; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7373 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7374 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 7375 | osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7376 | if( pCtx->conchHeld>0 ){ |
| 7377 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 7378 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7379 | pCtx->conchHeld = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7380 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| 7381 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7382 | return rc; |
| 7383 | } |
| 7384 | |
| 7385 | /* |
| 7386 | ** 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] | 7387 | ** Store the conch filename in memory obtained from sqlite3_malloc64(). |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7388 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 7389 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 7390 | ** |
| 7391 | ** The caller is responsible for ensuring that the allocated memory |
| 7392 | ** space is eventually freed. |
| 7393 | ** |
| 7394 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 7395 | */ |
| 7396 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 7397 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7398 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7399 | char *conchPath; /* buffer in which to construct conch name */ |
| 7400 | |
| 7401 | /* Allocate space for the conch filename and initialize the name to |
| 7402 | ** the name of the original database file. */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 7403 | *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7404 | if( conchPath==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7405 | return SQLITE_NOMEM_BKPT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7406 | } |
| 7407 | memcpy(conchPath, dbPath, len+1); |
| 7408 | |
| 7409 | /* now insert a "." before the last / character */ |
| 7410 | for( i=(len-1); i>=0; i-- ){ |
| 7411 | if( conchPath[i]=='/' ){ |
| 7412 | i++; |
| 7413 | break; |
| 7414 | } |
| 7415 | } |
| 7416 | conchPath[i]='.'; |
| 7417 | while ( i<len ){ |
| 7418 | conchPath[i+1]=dbPath[i]; |
| 7419 | i++; |
| 7420 | } |
| 7421 | |
| 7422 | /* append the "-conch" suffix to the file */ |
| 7423 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7424 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7425 | |
| 7426 | return SQLITE_OK; |
| 7427 | } |
| 7428 | |
| 7429 | |
| 7430 | /* Takes a fully configured proxy locking-style unix file and switches |
| 7431 | ** the local lock file path |
| 7432 | */ |
| 7433 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 7434 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 7435 | char *oldPath = pCtx->lockProxyPath; |
| 7436 | int rc = SQLITE_OK; |
| 7437 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7438 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7439 | return SQLITE_BUSY; |
| 7440 | } |
| 7441 | |
| 7442 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 7443 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 7444 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 7445 | return SQLITE_OK; |
| 7446 | }else{ |
| 7447 | unixFile *lockProxy = pCtx->lockProxy; |
| 7448 | pCtx->lockProxy=NULL; |
| 7449 | pCtx->conchHeld = 0; |
| 7450 | if( lockProxy!=NULL ){ |
| 7451 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 7452 | if( rc ) return rc; |
| 7453 | sqlite3_free(lockProxy); |
| 7454 | } |
| 7455 | sqlite3_free(oldPath); |
| 7456 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 7457 | } |
| 7458 | |
| 7459 | return rc; |
| 7460 | } |
| 7461 | |
| 7462 | /* |
| 7463 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 7464 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 7465 | ** |
| 7466 | ** This routine find the filename associated with pFile and writes it |
| 7467 | ** int dbPath. |
| 7468 | */ |
| 7469 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7470 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7471 | if( pFile->pMethod == &afpIoMethods ){ |
| 7472 | /* afp style keeps a reference to the db path in the filePath field |
| 7473 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7474 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7475 | strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, |
| 7476 | MAXPATHLEN); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7477 | } else |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7478 | #endif |
| 7479 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 7480 | /* dot lock style uses the locking context to store the dot lock |
| 7481 | ** file path */ |
| 7482 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 7483 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 7484 | }else{ |
| 7485 | /* all other styles use the locking context to store the db file path */ |
| 7486 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7487 | strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7488 | } |
| 7489 | return SQLITE_OK; |
| 7490 | } |
| 7491 | |
| 7492 | /* |
| 7493 | ** Takes an already filled in unix file and alters it so all file locking |
| 7494 | ** will be performed on the local proxy lock file. The following fields |
| 7495 | ** are preserved in the locking context so that they can be restored and |
| 7496 | ** the unix structure properly cleaned up at close time: |
| 7497 | ** ->lockingContext |
| 7498 | ** ->pMethod |
| 7499 | */ |
| 7500 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 7501 | proxyLockingContext *pCtx; |
| 7502 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 7503 | char *lockPath=NULL; |
| 7504 | int rc = SQLITE_OK; |
| 7505 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7506 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7507 | return SQLITE_BUSY; |
| 7508 | } |
| 7509 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 7510 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 7511 | lockPath=NULL; |
| 7512 | }else{ |
| 7513 | lockPath=(char *)path; |
| 7514 | } |
| 7515 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7516 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 7517 | (lockPath ? lockPath : ":auto:"), osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7518 | |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 7519 | pCtx = sqlite3_malloc64( sizeof(*pCtx) ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7520 | if( pCtx==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7521 | return SQLITE_NOMEM_BKPT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7522 | } |
| 7523 | memset(pCtx, 0, sizeof(*pCtx)); |
| 7524 | |
| 7525 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 7526 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7527 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 7528 | if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 7529 | /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 7530 | ** (c) the file system is read-only, then enable no-locking access. |
| 7531 | ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 7532 | ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 7533 | */ |
| 7534 | struct statfs fsInfo; |
| 7535 | struct stat conchInfo; |
| 7536 | int goLockless = 0; |
| 7537 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7538 | if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7539 | int err = errno; |
| 7540 | if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 7541 | goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 7542 | } |
| 7543 | } |
| 7544 | if( goLockless ){ |
| 7545 | pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 7546 | rc = SQLITE_OK; |
| 7547 | } |
| 7548 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7549 | } |
| 7550 | if( rc==SQLITE_OK && lockPath ){ |
| 7551 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 7552 | } |
| 7553 | |
| 7554 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7555 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 7556 | if( pCtx->dbPath==NULL ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7557 | rc = SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7558 | } |
| 7559 | } |
| 7560 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7561 | /* all memory is allocated, proxys are created and assigned, |
| 7562 | ** switch the locking context and pMethod then return. |
| 7563 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7564 | pCtx->oldLockingContext = pFile->lockingContext; |
| 7565 | pFile->lockingContext = pCtx; |
| 7566 | pCtx->pOldMethod = pFile->pMethod; |
| 7567 | pFile->pMethod = &proxyIoMethods; |
| 7568 | }else{ |
| 7569 | if( pCtx->conchFile ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7570 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7571 | sqlite3_free(pCtx->conchFile); |
| 7572 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7573 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7574 | sqlite3_free(pCtx->conchFilePath); |
| 7575 | sqlite3_free(pCtx); |
| 7576 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7577 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 7578 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7579 | return rc; |
| 7580 | } |
| 7581 | |
| 7582 | |
| 7583 | /* |
| 7584 | ** This routine handles sqlite3_file_control() calls that are specific |
| 7585 | ** to proxy locking. |
| 7586 | */ |
| 7587 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 7588 | switch( op ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7589 | case SQLITE_FCNTL_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7590 | unixFile *pFile = (unixFile*)id; |
| 7591 | if( pFile->pMethod == &proxyIoMethods ){ |
| 7592 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 7593 | proxyTakeConch(pFile); |
| 7594 | if( pCtx->lockProxyPath ){ |
| 7595 | *(const char **)pArg = pCtx->lockProxyPath; |
| 7596 | }else{ |
| 7597 | *(const char **)pArg = ":auto: (not held)"; |
| 7598 | } |
| 7599 | } else { |
| 7600 | *(const char **)pArg = NULL; |
| 7601 | } |
| 7602 | return SQLITE_OK; |
| 7603 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7604 | case SQLITE_FCNTL_SET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7605 | unixFile *pFile = (unixFile*)id; |
| 7606 | int rc = SQLITE_OK; |
| 7607 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 7608 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 7609 | if( isProxyStyle ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7610 | /* turn off proxy locking - not supported. If support is added for |
| 7611 | ** switching proxy locking mode off then it will need to fail if |
| 7612 | ** the journal mode is WAL mode. |
| 7613 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7614 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 7615 | }else{ |
| 7616 | /* turn off proxy locking - already off - NOOP */ |
| 7617 | rc = SQLITE_OK; |
| 7618 | } |
| 7619 | }else{ |
| 7620 | const char *proxyPath = (const char *)pArg; |
| 7621 | if( isProxyStyle ){ |
| 7622 | proxyLockingContext *pCtx = |
| 7623 | (proxyLockingContext*)pFile->lockingContext; |
| 7624 | if( !strcmp(pArg, ":auto:") |
| 7625 | || (pCtx->lockProxyPath && |
| 7626 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 7627 | ){ |
| 7628 | rc = SQLITE_OK; |
| 7629 | }else{ |
| 7630 | rc = switchLockProxyPath(pFile, proxyPath); |
| 7631 | } |
| 7632 | }else{ |
| 7633 | /* turn on proxy file locking */ |
| 7634 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 7635 | } |
| 7636 | } |
| 7637 | return rc; |
| 7638 | } |
| 7639 | default: { |
| 7640 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 7641 | } |
| 7642 | } |
drh | 8616cff | 2019-07-13 16:15:23 +0000 | [diff] [blame] | 7643 | /*NOTREACHED*/ assert(0); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7644 | return SQLITE_ERROR; |
| 7645 | } |
| 7646 | |
| 7647 | /* |
| 7648 | ** Within this division (the proxying locking implementation) the procedures |
| 7649 | ** above this point are all utilities. The lock-related methods of the |
| 7650 | ** proxy-locking sqlite3_io_method object follow. |
| 7651 | */ |
| 7652 | |
| 7653 | |
| 7654 | /* |
| 7655 | ** This routine checks if there is a RESERVED lock held on the specified |
| 7656 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 7657 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 7658 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 7659 | */ |
| 7660 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 7661 | unixFile *pFile = (unixFile*)id; |
| 7662 | int rc = proxyTakeConch(pFile); |
| 7663 | if( rc==SQLITE_OK ){ |
| 7664 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7665 | if( pCtx->conchHeld>0 ){ |
| 7666 | unixFile *proxy = pCtx->lockProxy; |
| 7667 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 7668 | }else{ /* conchHeld < 0 is lockless */ |
| 7669 | pResOut=0; |
| 7670 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7671 | } |
| 7672 | return rc; |
| 7673 | } |
| 7674 | |
| 7675 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7676 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7677 | ** of the following: |
| 7678 | ** |
| 7679 | ** (1) SHARED_LOCK |
| 7680 | ** (2) RESERVED_LOCK |
| 7681 | ** (3) PENDING_LOCK |
| 7682 | ** (4) EXCLUSIVE_LOCK |
| 7683 | ** |
| 7684 | ** Sometimes when requesting one lock state, additional lock states |
| 7685 | ** are inserted in between. The locking might fail on one of the later |
| 7686 | ** transitions leaving the lock state different from what it started but |
| 7687 | ** still short of its goal. The following chart shows the allowed |
| 7688 | ** transitions and the inserted intermediate states: |
| 7689 | ** |
| 7690 | ** UNLOCKED -> SHARED |
| 7691 | ** SHARED -> RESERVED |
| 7692 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 7693 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 7694 | ** PENDING -> EXCLUSIVE |
| 7695 | ** |
| 7696 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 7697 | ** routine to lower a locking level. |
| 7698 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7699 | static int proxyLock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7700 | unixFile *pFile = (unixFile*)id; |
| 7701 | int rc = proxyTakeConch(pFile); |
| 7702 | if( rc==SQLITE_OK ){ |
| 7703 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7704 | if( pCtx->conchHeld>0 ){ |
| 7705 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7706 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); |
| 7707 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7708 | }else{ |
| 7709 | /* conchHeld < 0 is lockless */ |
| 7710 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7711 | } |
| 7712 | return rc; |
| 7713 | } |
| 7714 | |
| 7715 | |
| 7716 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7717 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7718 | ** must be either NO_LOCK or SHARED_LOCK. |
| 7719 | ** |
| 7720 | ** If the locking level of the file descriptor is already at or below |
| 7721 | ** the requested locking level, this routine is a no-op. |
| 7722 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7723 | static int proxyUnlock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7724 | unixFile *pFile = (unixFile*)id; |
| 7725 | int rc = proxyTakeConch(pFile); |
| 7726 | if( rc==SQLITE_OK ){ |
| 7727 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7728 | if( pCtx->conchHeld>0 ){ |
| 7729 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7730 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); |
| 7731 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7732 | }else{ |
| 7733 | /* conchHeld < 0 is lockless */ |
| 7734 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7735 | } |
| 7736 | return rc; |
| 7737 | } |
| 7738 | |
| 7739 | /* |
| 7740 | ** Close a file that uses proxy locks. |
| 7741 | */ |
| 7742 | static int proxyClose(sqlite3_file *id) { |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 7743 | if( ALWAYS(id) ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7744 | unixFile *pFile = (unixFile*)id; |
| 7745 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7746 | unixFile *lockProxy = pCtx->lockProxy; |
| 7747 | unixFile *conchFile = pCtx->conchFile; |
| 7748 | int rc = SQLITE_OK; |
| 7749 | |
| 7750 | if( lockProxy ){ |
| 7751 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 7752 | if( rc ) return rc; |
| 7753 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 7754 | if( rc ) return rc; |
| 7755 | sqlite3_free(lockProxy); |
| 7756 | pCtx->lockProxy = 0; |
| 7757 | } |
| 7758 | if( conchFile ){ |
| 7759 | if( pCtx->conchHeld ){ |
| 7760 | rc = proxyReleaseConch(pFile); |
| 7761 | if( rc ) return rc; |
| 7762 | } |
| 7763 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 7764 | if( rc ) return rc; |
| 7765 | sqlite3_free(conchFile); |
| 7766 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7767 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7768 | sqlite3_free(pCtx->conchFilePath); |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7769 | sqlite3DbFree(0, pCtx->dbPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7770 | /* restore the original locking context and pMethod then close it */ |
| 7771 | pFile->lockingContext = pCtx->oldLockingContext; |
| 7772 | pFile->pMethod = pCtx->pOldMethod; |
| 7773 | sqlite3_free(pCtx); |
| 7774 | return pFile->pMethod->xClose(id); |
| 7775 | } |
| 7776 | return SQLITE_OK; |
| 7777 | } |
| 7778 | |
| 7779 | |
| 7780 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7781 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7782 | /* |
| 7783 | ** The proxy locking style is intended for use with AFP filesystems. |
| 7784 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 7785 | ** restricted to MacOSX. |
| 7786 | ** |
| 7787 | ** |
| 7788 | ******************* End of the proxy lock implementation ********************** |
| 7789 | ******************************************************************************/ |
| 7790 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7791 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7792 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7793 | ** |
| 7794 | ** This routine registers all VFS implementations for unix-like operating |
| 7795 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 7796 | ** should be the only routines in this file that are visible from other |
| 7797 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7798 | ** |
| 7799 | ** This routine is called once during SQLite initialization and by a |
| 7800 | ** single thread. The memory allocation and mutex subsystems have not |
| 7801 | ** necessarily been initialized when this routine is called, and so they |
| 7802 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7803 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7804 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7805 | /* |
| 7806 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7807 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 7808 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 7809 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 7810 | ** and so we have to go through the intermediate pointer to avoid problems |
| 7811 | ** when compiling with -pedantic-errors on GCC.) |
| 7812 | ** |
| 7813 | ** 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] | 7814 | ** finder-function. The finder-function returns a pointer to the |
| 7815 | ** sqlite_io_methods object that implements the desired locking |
| 7816 | ** behaviors. See the division above that contains the IOMETHODS |
| 7817 | ** macro for addition information on finder-functions. |
| 7818 | ** |
| 7819 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 7820 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 7821 | ** more than that; it looks at the filesystem type that hosts the |
| 7822 | ** database file and tries to choose an locking method appropriate for |
| 7823 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7824 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7825 | #define UNIXVFS(VFSNAME, FINDER) { \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7826 | 3, /* iVersion */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7827 | sizeof(unixFile), /* szOsFile */ \ |
| 7828 | MAX_PATHNAME, /* mxPathname */ \ |
| 7829 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7830 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7831 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7832 | unixOpen, /* xOpen */ \ |
| 7833 | unixDelete, /* xDelete */ \ |
| 7834 | unixAccess, /* xAccess */ \ |
| 7835 | unixFullPathname, /* xFullPathname */ \ |
| 7836 | unixDlOpen, /* xDlOpen */ \ |
| 7837 | unixDlError, /* xDlError */ \ |
| 7838 | unixDlSym, /* xDlSym */ \ |
| 7839 | unixDlClose, /* xDlClose */ \ |
| 7840 | unixRandomness, /* xRandomness */ \ |
| 7841 | unixSleep, /* xSleep */ \ |
| 7842 | unixCurrentTime, /* xCurrentTime */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 7843 | unixGetLastError, /* xGetLastError */ \ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 7844 | unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7845 | unixSetSystemCall, /* xSetSystemCall */ \ |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 7846 | unixGetSystemCall, /* xGetSystemCall */ \ |
| 7847 | unixNextSystemCall, /* xNextSystemCall */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7848 | } |
| 7849 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7850 | /* |
| 7851 | ** All default VFSes for unix are contained in the following array. |
| 7852 | ** |
| 7853 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 7854 | ** by the SQLite core when the VFS is registered. So the following |
| 7855 | ** array cannot be const. |
| 7856 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7857 | static sqlite3_vfs aVfs[] = { |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7858 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7859 | UNIXVFS("unix", autolockIoFinder ), |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7860 | #elif OS_VXWORKS |
| 7861 | UNIXVFS("unix", vxworksIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7862 | #else |
| 7863 | UNIXVFS("unix", posixIoFinder ), |
| 7864 | #endif |
| 7865 | UNIXVFS("unix-none", nolockIoFinder ), |
| 7866 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7867 | UNIXVFS("unix-excl", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7868 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7869 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7870 | #endif |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7871 | #if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7872 | UNIXVFS("unix-posix", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7873 | #endif |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7874 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 7875 | UNIXVFS("unix-flock", flockIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 7876 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7877 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7878 | UNIXVFS("unix-afp", afpIoFinder ), |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7879 | UNIXVFS("unix-nfs", nfsIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7880 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7881 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7882 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7883 | unsigned int i; /* Loop counter */ |
| 7884 | |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 7885 | /* Double-check that the aSyscall[] array has been constructed |
| 7886 | ** correctly. See ticket [bb3a86e890c8e96ab] */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 7887 | assert( ArraySize(aSyscall)==29 ); |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 7888 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7889 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7890 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7891 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7892 | } |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame] | 7893 | unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1); |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7894 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7895 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7896 | |
| 7897 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7898 | ** Shutdown the operating system interface. |
| 7899 | ** |
| 7900 | ** Some operating systems might need to do some cleanup in this routine, |
| 7901 | ** to release dynamically allocated objects. But not on unix. |
| 7902 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7903 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7904 | int sqlite3_os_end(void){ |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame] | 7905 | unixBigLock = 0; |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7906 | return SQLITE_OK; |
| 7907 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 7908 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 7909 | #endif /* SQLITE_OS_UNIX */ |