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)) \ |
drh | 14f38b3 | 2020-08-19 23:51:54 +0000 | [diff] [blame] | 125 | && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0))\ |
| 126 | && (!defined(TARGET_OS_MACCATALYST) || (TARGET_OS_MACCATALYST==0)) |
drh | e4079e1 | 2019-09-27 16:33:27 +0000 | [diff] [blame] | 127 | # undef HAVE_GETHOSTUUID |
| 128 | # define HAVE_GETHOSTUUID 1 |
| 129 | # else |
| 130 | # warning "gethostuuid() is disabled." |
| 131 | # endif |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 132 | # endif |
| 133 | #endif |
| 134 | |
| 135 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 136 | #if OS_VXWORKS |
| 137 | # include <sys/ioctl.h> |
| 138 | # include <semaphore.h> |
| 139 | # include <limits.h> |
| 140 | #endif /* OS_VXWORKS */ |
| 141 | |
| 142 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 84a2bf6 | 2010-03-05 13:41:06 +0000 | [diff] [blame] | 143 | # include <sys/mount.h> |
| 144 | #endif |
| 145 | |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 146 | #ifdef HAVE_UTIME |
| 147 | # include <utime.h> |
| 148 | #endif |
| 149 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 150 | /* |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 151 | ** Allowed values of unixFile.fsFlags |
| 152 | */ |
| 153 | #define SQLITE_FSFLAGS_IS_MSDOS 0x1 |
| 154 | |
| 155 | /* |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 156 | ** If we are to be thread-safe, include the pthreads header. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 157 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 158 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 159 | # include <pthread.h> |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 160 | #endif |
| 161 | |
| 162 | /* |
| 163 | ** Default permissions when creating a new file |
| 164 | */ |
| 165 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 166 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 167 | #endif |
| 168 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 169 | /* |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 170 | ** Default permissions when creating auto proxy dir |
| 171 | */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 172 | #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 173 | # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 |
| 174 | #endif |
| 175 | |
| 176 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 177 | ** Maximum supported path-length. |
| 178 | */ |
| 179 | #define MAX_PATHNAME 512 |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 180 | |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 181 | /* |
| 182 | ** Maximum supported symbolic links |
| 183 | */ |
| 184 | #define SQLITE_MAX_SYMLINKS 100 |
| 185 | |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 186 | /* Always cast the getpid() return type for compatibility with |
| 187 | ** kernel modules in VxWorks. */ |
| 188 | #define osGetpid(X) (pid_t)getpid() |
| 189 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 190 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 191 | ** Only set the lastErrno if the error code is a real error and not |
| 192 | ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 193 | */ |
| 194 | #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |
| 195 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 196 | /* Forward references */ |
| 197 | typedef struct unixShm unixShm; /* Connection shared memory */ |
| 198 | typedef struct unixShmNode unixShmNode; /* Shared memory instance */ |
| 199 | typedef struct unixInodeInfo unixInodeInfo; /* An i-node */ |
| 200 | typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 201 | |
| 202 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 203 | ** Sometimes, after a file handle is closed by SQLite, the file descriptor |
| 204 | ** cannot be closed immediately. In these cases, instances of the following |
| 205 | ** structure are used to store the file descriptor while waiting for an |
| 206 | ** opportunity to either close or reuse it. |
| 207 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 208 | struct UnixUnusedFd { |
| 209 | int fd; /* File descriptor to close */ |
| 210 | int flags; /* Flags this file descriptor was opened with */ |
| 211 | UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ |
| 212 | }; |
| 213 | |
| 214 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 215 | ** The unixFile structure is subclass of sqlite3_file specific to the unix |
| 216 | ** VFS implementations. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 217 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 218 | typedef struct unixFile unixFile; |
| 219 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 220 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 221 | sqlite3_vfs *pVfs; /* The VFS that created this unixFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 222 | unixInodeInfo *pInode; /* Info about locks on this inode */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 223 | int h; /* The file descriptor */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 224 | unsigned char eFileLock; /* The type of lock held on this fd */ |
drh | 3ee3484 | 2012-02-11 21:21:17 +0000 | [diff] [blame] | 225 | unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 226 | int lastErrno; /* The unix errno from last I/O error */ |
| 227 | void *lockingContext; /* Locking style specific state */ |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 228 | UnixUnusedFd *pPreallocatedUnused; /* Pre-allocated UnixUnusedFd */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 229 | const char *zPath; /* Name of the file */ |
| 230 | unixShm *pShm; /* Shared memory segment information */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 231 | int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 232 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 233 | int nFetchOut; /* Number of outstanding xFetch refs */ |
| 234 | sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 235 | sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */ |
| 236 | sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 237 | void *pMapRegion; /* Memory mapped region */ |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 238 | #endif |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 239 | int sectorSize; /* Device sector size */ |
| 240 | int deviceCharacteristics; /* Precomputed device characteristics */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 241 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 242 | int openFlags; /* The flags specified at open() */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 243 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 244 | #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 245 | unsigned fsFlags; /* cached details from statfs() */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 246 | #endif |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 247 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 248 | unsigned iBusyTimeout; /* Wait this many millisec on locks */ |
| 249 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 250 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 251 | struct vxworksFileId *pId; /* Unique file ID */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 252 | #endif |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 253 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 254 | /* The next group of variables are used to track whether or not the |
| 255 | ** transaction counter in bytes 24-27 of database files are updated |
| 256 | ** whenever any part of the database changes. An assertion fault will |
| 257 | ** occur if a file is updated without also updating the transaction |
| 258 | ** counter. This test is made to avoid new problems similar to the |
| 259 | ** one described by ticket #3584. |
| 260 | */ |
| 261 | unsigned char transCntrChng; /* True if the transaction counter changed */ |
| 262 | unsigned char dbUpdate; /* True if any part of database file changed */ |
| 263 | unsigned char inNormalWrite; /* True if in a normal write operation */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 264 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 265 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 266 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 267 | #ifdef SQLITE_TEST |
| 268 | /* In test mode, increase the size of this structure a bit so that |
| 269 | ** it is larger than the struct CrashFile defined in test6.c. |
| 270 | */ |
| 271 | char aPadding[32]; |
| 272 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 275 | /* This variable holds the process id (pid) from when the xRandomness() |
| 276 | ** method was called. If xOpen() is called from a different process id, |
| 277 | ** indicating that a fork() has occurred, the PRNG will be reset. |
| 278 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 279 | static pid_t randomnessPid = 0; |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 280 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 281 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 282 | ** Allowed values for the unixFile.ctrlFlags bitmask: |
| 283 | */ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 284 | #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ |
| 285 | #define UNIXFILE_RDONLY 0x02 /* Connection is read only */ |
| 286 | #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ |
dan | ee140c4 | 2011-08-25 13:46:32 +0000 | [diff] [blame] | 287 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 288 | # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ |
| 289 | #else |
| 290 | # define UNIXFILE_DIRSYNC 0x00 |
| 291 | #endif |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 292 | #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 293 | #define UNIXFILE_DELETE 0x20 /* Delete on close */ |
| 294 | #define UNIXFILE_URI 0x40 /* Filename might have query parameters */ |
| 295 | #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 296 | |
| 297 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 298 | ** Include code that is common to all os_*.c files |
| 299 | */ |
| 300 | #include "os_common.h" |
| 301 | |
| 302 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 303 | ** Define various macros that are missing from some systems. |
| 304 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 305 | #ifndef O_LARGEFILE |
| 306 | # define O_LARGEFILE 0 |
| 307 | #endif |
| 308 | #ifdef SQLITE_DISABLE_LFS |
| 309 | # undef O_LARGEFILE |
| 310 | # define O_LARGEFILE 0 |
| 311 | #endif |
| 312 | #ifndef O_NOFOLLOW |
| 313 | # define O_NOFOLLOW 0 |
| 314 | #endif |
| 315 | #ifndef O_BINARY |
| 316 | # define O_BINARY 0 |
| 317 | #endif |
| 318 | |
| 319 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 320 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 321 | ** testing and debugging only. |
| 322 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 323 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 324 | #define threadid pthread_self() |
| 325 | #else |
| 326 | #define threadid 0 |
| 327 | #endif |
| 328 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 329 | /* |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 330 | ** HAVE_MREMAP defaults to true on Linux and false everywhere else. |
| 331 | */ |
| 332 | #if !defined(HAVE_MREMAP) |
| 333 | # if defined(__linux__) && defined(_GNU_SOURCE) |
| 334 | # define HAVE_MREMAP 1 |
| 335 | # else |
| 336 | # define HAVE_MREMAP 0 |
| 337 | # endif |
| 338 | #endif |
| 339 | |
| 340 | /* |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 341 | ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek() |
| 342 | ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined. |
| 343 | */ |
| 344 | #ifdef __ANDROID__ |
| 345 | # define lseek lseek64 |
| 346 | #endif |
| 347 | |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 348 | #ifdef __linux__ |
| 349 | /* |
| 350 | ** Linux-specific IOCTL magic numbers used for controlling F2FS |
| 351 | */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 352 | #define F2FS_IOCTL_MAGIC 0xf5 |
| 353 | #define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1) |
| 354 | #define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2) |
| 355 | #define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3) |
| 356 | #define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5) |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame] | 357 | #define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, u32) |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame] | 358 | #define F2FS_FEATURE_ATOMIC_WRITE 0x0004 |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 359 | #endif /* __linux__ */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 360 | |
| 361 | |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 362 | /* |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 363 | ** Different Unix systems declare open() in different ways. Same use |
| 364 | ** open(const char*,int,mode_t). Others use open(const char*,int,...). |
| 365 | ** The difference is important when using a pointer to the function. |
| 366 | ** |
| 367 | ** The safest way to deal with the problem is to always use this wrapper |
| 368 | ** which always has the same well-defined interface. |
| 369 | */ |
| 370 | static int posixOpen(const char *zFile, int flags, int mode){ |
| 371 | return open(zFile, flags, mode); |
| 372 | } |
| 373 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 374 | /* Forward reference */ |
| 375 | static int openDirectory(const char*, int*); |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 376 | static int unixGetpagesize(void); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 377 | |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 378 | /* |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 379 | ** Many system calls are accessed through pointer-to-functions so that |
| 380 | ** they may be overridden at runtime to facilitate fault injection during |
| 381 | ** testing and sandboxing. The following array holds the names and pointers |
| 382 | ** to all overrideable system calls. |
| 383 | */ |
| 384 | static struct unix_syscall { |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 385 | const char *zName; /* Name of the system call */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 386 | sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ |
| 387 | sqlite3_syscall_ptr pDefault; /* Default value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 388 | } aSyscall[] = { |
drh | 9a3baf1 | 2011-04-25 18:01:27 +0000 | [diff] [blame] | 389 | { "open", (sqlite3_syscall_ptr)posixOpen, 0 }, |
| 390 | #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent) |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 391 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 392 | { "close", (sqlite3_syscall_ptr)close, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 393 | #define osClose ((int(*)(int))aSyscall[1].pCurrent) |
| 394 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 395 | { "access", (sqlite3_syscall_ptr)access, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 396 | #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent) |
| 397 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 398 | { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 399 | #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent) |
| 400 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 401 | { "stat", (sqlite3_syscall_ptr)stat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 402 | #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) |
| 403 | |
| 404 | /* |
| 405 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 406 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 407 | ** that always succeeds. This means that locking does not occur under |
| 408 | ** DJGPP. But it is DOS - what did you expect? |
| 409 | */ |
| 410 | #ifdef __DJGPP__ |
| 411 | { "fstat", 0, 0 }, |
| 412 | #define osFstat(a,b,c) 0 |
| 413 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 414 | { "fstat", (sqlite3_syscall_ptr)fstat, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 415 | #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent) |
| 416 | #endif |
| 417 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 418 | { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 419 | #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent) |
| 420 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 421 | { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 }, |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 422 | #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 423 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 424 | { "read", (sqlite3_syscall_ptr)read, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 425 | #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) |
| 426 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 427 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 428 | { "pread", (sqlite3_syscall_ptr)pread, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 429 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 430 | { "pread", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 431 | #endif |
| 432 | #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) |
| 433 | |
| 434 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 435 | { "pread64", (sqlite3_syscall_ptr)pread64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 436 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 437 | { "pread64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 438 | #endif |
drh | f9986d9 | 2016-04-18 13:09:55 +0000 | [diff] [blame] | 439 | #define osPread64 ((ssize_t(*)(int,void*,size_t,off64_t))aSyscall[10].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 440 | |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 441 | { "write", (sqlite3_syscall_ptr)write, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 442 | #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) |
| 443 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 444 | #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 445 | { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 446 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 447 | { "pwrite", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 448 | #endif |
| 449 | #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 450 | aSyscall[12].pCurrent) |
| 451 | |
| 452 | #if defined(USE_PREAD64) |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 453 | { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 454 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 455 | { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 456 | #endif |
drh | f9986d9 | 2016-04-18 13:09:55 +0000 | [diff] [blame] | 457 | #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off64_t))\ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 458 | aSyscall[13].pCurrent) |
| 459 | |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 460 | { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 461 | #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 462 | |
| 463 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 464 | { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 465 | #else |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 466 | { "fallocate", (sqlite3_syscall_ptr)0, 0 }, |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 467 | #endif |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 468 | #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 469 | |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 470 | { "unlink", (sqlite3_syscall_ptr)unlink, 0 }, |
| 471 | #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent) |
| 472 | |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 473 | { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 }, |
| 474 | #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent) |
| 475 | |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 476 | { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 }, |
| 477 | #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent) |
| 478 | |
| 479 | { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 }, |
| 480 | #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent) |
| 481 | |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 482 | #if defined(HAVE_FCHOWN) |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 483 | { "fchown", (sqlite3_syscall_ptr)fchown, 0 }, |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 484 | #else |
| 485 | { "fchown", (sqlite3_syscall_ptr)0, 0 }, |
| 486 | #endif |
dan | d3eaebd | 2012-02-13 08:50:23 +0000 | [diff] [blame] | 487 | #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent) |
drh | 23c4b97 | 2012-02-11 23:55:15 +0000 | [diff] [blame] | 488 | |
drh | 26f625f | 2018-02-19 16:34:31 +0000 | [diff] [blame] | 489 | #if defined(HAVE_FCHOWN) |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 490 | { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 }, |
drh | 26f625f | 2018-02-19 16:34:31 +0000 | [diff] [blame] | 491 | #else |
| 492 | { "geteuid", (sqlite3_syscall_ptr)0, 0 }, |
| 493 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 494 | #define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent) |
| 495 | |
dan | 4dd5144 | 2013-08-26 14:30:25 +0000 | [diff] [blame] | 496 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 497 | { "mmap", (sqlite3_syscall_ptr)mmap, 0 }, |
| 498 | #else |
| 499 | { "mmap", (sqlite3_syscall_ptr)0, 0 }, |
| 500 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 501 | #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] | 502 | |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 503 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 504 | { "munmap", (sqlite3_syscall_ptr)munmap, 0 }, |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 505 | #else |
drh | a829992 | 2016-01-08 22:31:00 +0000 | [diff] [blame] | 506 | { "munmap", (sqlite3_syscall_ptr)0, 0 }, |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 507 | #endif |
drh | 62be1fa | 2017-12-09 01:02:33 +0000 | [diff] [blame] | 508 | #define osMunmap ((int(*)(void*,size_t))aSyscall[23].pCurrent) |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 509 | |
drh | e4a08f9 | 2016-01-08 19:17:30 +0000 | [diff] [blame] | 510 | #if HAVE_MREMAP && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 511 | { "mremap", (sqlite3_syscall_ptr)mremap, 0 }, |
| 512 | #else |
| 513 | { "mremap", (sqlite3_syscall_ptr)0, 0 }, |
| 514 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 515 | #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent) |
| 516 | |
drh | 24dbeae | 2016-01-08 22:18:00 +0000 | [diff] [blame] | 517 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 518 | { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 }, |
drh | 24dbeae | 2016-01-08 22:18:00 +0000 | [diff] [blame] | 519 | #else |
| 520 | { "getpagesize", (sqlite3_syscall_ptr)0, 0 }, |
| 521 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 522 | #define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent) |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 523 | |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 524 | #if defined(HAVE_READLINK) |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 525 | { "readlink", (sqlite3_syscall_ptr)readlink, 0 }, |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 526 | #else |
| 527 | { "readlink", (sqlite3_syscall_ptr)0, 0 }, |
| 528 | #endif |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 529 | #define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent) |
dan | 245fdc6 | 2015-10-31 17:58:33 +0000 | [diff] [blame] | 530 | |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 531 | #if defined(HAVE_LSTAT) |
| 532 | { "lstat", (sqlite3_syscall_ptr)lstat, 0 }, |
| 533 | #else |
| 534 | { "lstat", (sqlite3_syscall_ptr)0, 0 }, |
| 535 | #endif |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 536 | #define osLstat ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent) |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 537 | |
drh | b5d013e | 2017-10-25 16:14:12 +0000 | [diff] [blame] | 538 | #if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) |
dan | 16f39b6 | 2018-09-18 19:40:18 +0000 | [diff] [blame] | 539 | # ifdef __ANDROID__ |
| 540 | { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 }, |
dan | ec9b2a1 | 2019-07-15 07:58:28 +0000 | [diff] [blame] | 541 | #define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent) |
dan | 16f39b6 | 2018-09-18 19:40:18 +0000 | [diff] [blame] | 542 | # else |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 543 | { "ioctl", (sqlite3_syscall_ptr)ioctl, 0 }, |
dan | ec9b2a1 | 2019-07-15 07:58:28 +0000 | [diff] [blame] | 544 | #define osIoctl ((int(*)(int,unsigned long,...))aSyscall[28].pCurrent) |
dan | 16f39b6 | 2018-09-18 19:40:18 +0000 | [diff] [blame] | 545 | # endif |
drh | b5d013e | 2017-10-25 16:14:12 +0000 | [diff] [blame] | 546 | #else |
| 547 | { "ioctl", (sqlite3_syscall_ptr)0, 0 }, |
| 548 | #endif |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 549 | |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 550 | }; /* End of the overrideable system calls */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 551 | |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 552 | |
| 553 | /* |
| 554 | ** On some systems, calls to fchown() will trigger a message in a security |
| 555 | ** log if they come from non-root processes. So avoid calling fchown() if |
| 556 | ** we are not running as root. |
| 557 | */ |
| 558 | static int robustFchown(int fd, uid_t uid, gid_t gid){ |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 559 | #if defined(HAVE_FCHOWN) |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 560 | return osGeteuid() ? 0 : osFchown(fd,uid,gid); |
drh | e2258a2 | 2016-01-12 00:37:55 +0000 | [diff] [blame] | 561 | #else |
| 562 | return 0; |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 563 | #endif |
| 564 | } |
| 565 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 566 | /* |
| 567 | ** This is the xSetSystemCall() method of sqlite3_vfs for all of the |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 568 | ** "unix" VFSes. Return SQLITE_OK opon successfully updating the |
| 569 | ** system call pointer, or SQLITE_NOTFOUND if there is no configurable |
| 570 | ** system call named zName. |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 571 | */ |
| 572 | static int unixSetSystemCall( |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 573 | sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ |
| 574 | const char *zName, /* Name of system call to override */ |
| 575 | sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 576 | ){ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 577 | unsigned int i; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 578 | int rc = SQLITE_NOTFOUND; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 579 | |
| 580 | UNUSED_PARAMETER(pNotUsed); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 581 | if( zName==0 ){ |
| 582 | /* If no zName is given, restore all system calls to their default |
| 583 | ** settings and return NULL |
| 584 | */ |
dan | 51438a7 | 2011-04-02 17:00:47 +0000 | [diff] [blame] | 585 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 586 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 587 | if( aSyscall[i].pDefault ){ |
| 588 | aSyscall[i].pCurrent = aSyscall[i].pDefault; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | }else{ |
| 592 | /* If zName is specified, operate on only the one system call |
| 593 | ** specified. |
| 594 | */ |
| 595 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 596 | if( strcmp(zName, aSyscall[i].zName)==0 ){ |
| 597 | if( aSyscall[i].pDefault==0 ){ |
| 598 | aSyscall[i].pDefault = aSyscall[i].pCurrent; |
| 599 | } |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 600 | rc = SQLITE_OK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 601 | if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault; |
| 602 | aSyscall[i].pCurrent = pNewFunc; |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | return rc; |
| 608 | } |
| 609 | |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 610 | /* |
| 611 | ** Return the value of a system call. Return NULL if zName is not a |
| 612 | ** recognized system call name. NULL is also returned if the system call |
| 613 | ** is currently undefined. |
| 614 | */ |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 615 | static sqlite3_syscall_ptr unixGetSystemCall( |
| 616 | sqlite3_vfs *pNotUsed, |
| 617 | const char *zName |
| 618 | ){ |
| 619 | unsigned int i; |
| 620 | |
| 621 | UNUSED_PARAMETER(pNotUsed); |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 622 | for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 623 | if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent; |
| 624 | } |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | /* |
| 629 | ** Return the name of the first system call after zName. If zName==NULL |
| 630 | ** then return the name of the first system call. Return NULL if zName |
| 631 | ** is the last system call or if zName is not the name of a valid |
| 632 | ** system call. |
| 633 | */ |
| 634 | static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){ |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 635 | int i = -1; |
drh | 58ad580 | 2011-03-23 22:02:23 +0000 | [diff] [blame] | 636 | |
| 637 | UNUSED_PARAMETER(p); |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 638 | if( zName ){ |
| 639 | for(i=0; i<ArraySize(aSyscall)-1; i++){ |
| 640 | if( strcmp(zName, aSyscall[i].zName)==0 ) break; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 641 | } |
| 642 | } |
dan | 0fd7d86 | 2011-03-29 10:04:23 +0000 | [diff] [blame] | 643 | for(i++; i<ArraySize(aSyscall); i++){ |
| 644 | if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName; |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 645 | } |
| 646 | return 0; |
| 647 | } |
| 648 | |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 649 | /* |
drh | 77a3fdc | 2013-08-30 14:24:12 +0000 | [diff] [blame] | 650 | ** Do not accept any file descriptor less than this value, in order to avoid |
| 651 | ** opening database file using file descriptors that are commonly used for |
| 652 | ** standard input, output, and error. |
| 653 | */ |
| 654 | #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR |
| 655 | # define SQLITE_MINIMUM_FILE_DESCRIPTOR 3 |
| 656 | #endif |
| 657 | |
| 658 | /* |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 659 | ** Invoke open(). Do so multiple times, until it either succeeds or |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 660 | ** fails for some reason other than EINTR. |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 661 | ** |
| 662 | ** If the file creation mode "m" is 0 then set it to the default for |
| 663 | ** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally |
| 664 | ** 0644) as modified by the system umask. If m is not 0, then |
| 665 | ** make the file creation mode be exactly m ignoring the umask. |
| 666 | ** |
| 667 | ** The m parameter will be non-zero only when creating -wal, -journal, |
| 668 | ** and -shm files. We want those files to have *exactly* the same |
| 669 | ** permissions as their original database, unadulterated by the umask. |
| 670 | ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a |
| 671 | ** transaction crashes and leaves behind hot journals, then any |
| 672 | ** process that is able to write to the database will also be able to |
| 673 | ** recover the hot journals. |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 674 | */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 675 | static int robust_open(const char *z, int f, mode_t m){ |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 676 | int fd; |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 677 | mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS; |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 678 | while(1){ |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 679 | #if defined(O_CLOEXEC) |
| 680 | fd = osOpen(z,f|O_CLOEXEC,m2); |
| 681 | #else |
| 682 | fd = osOpen(z,f,m2); |
| 683 | #endif |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 684 | if( fd<0 ){ |
| 685 | if( errno==EINTR ) continue; |
| 686 | break; |
| 687 | } |
drh | 77a3fdc | 2013-08-30 14:24:12 +0000 | [diff] [blame] | 688 | if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break; |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 689 | osClose(fd); |
| 690 | sqlite3_log(SQLITE_WARNING, |
| 691 | "attempt to open \"%s\" as file descriptor %d", z, fd); |
| 692 | fd = -1; |
drh | 0ba3621 | 2020-02-13 13:45:04 +0000 | [diff] [blame] | 693 | if( osOpen("/dev/null", O_RDONLY, m)<0 ) break; |
drh | 5128d00 | 2013-08-30 06:20:23 +0000 | [diff] [blame] | 694 | } |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 695 | if( fd>=0 ){ |
| 696 | if( m!=0 ){ |
| 697 | struct stat statbuf; |
dan | b83c21e | 2013-03-05 15:27:34 +0000 | [diff] [blame] | 698 | if( osFstat(fd, &statbuf)==0 |
| 699 | && statbuf.st_size==0 |
drh | cfc1769 | 2013-03-06 01:41:53 +0000 | [diff] [blame] | 700 | && (statbuf.st_mode&0777)!=m |
dan | b83c21e | 2013-03-05 15:27:34 +0000 | [diff] [blame] | 701 | ){ |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 702 | osFchmod(fd, m); |
| 703 | } |
| 704 | } |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 705 | #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0) |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 706 | osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 707 | #endif |
drh | e1186ab | 2013-01-04 20:45:13 +0000 | [diff] [blame] | 708 | } |
drh | 5adc60b | 2012-04-14 13:25:11 +0000 | [diff] [blame] | 709 | return fd; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 710 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 711 | |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 712 | /* |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 713 | ** Helper functions to obtain and relinquish the global mutex. The |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 714 | ** global mutex is used to protect the unixInodeInfo and |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 715 | ** vxworksFileId objects used by this file, all of which may be |
| 716 | ** shared by multiple threads. |
| 717 | ** |
| 718 | ** Function unixMutexHeld() is used to assert() that the global mutex |
| 719 | ** is held when required. This function is only used as part of assert() |
| 720 | ** statements. e.g. |
| 721 | ** |
| 722 | ** unixEnterMutex() |
| 723 | ** assert( unixMutexHeld() ); |
| 724 | ** unixEnterLeave() |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 725 | ** |
| 726 | ** To prevent deadlock, the global unixBigLock must must be acquired |
| 727 | ** before the unixInodeInfo.pLockMutex mutex, if both are held. It is |
| 728 | ** OK to get the pLockMutex without holding unixBigLock first, but if |
| 729 | ** that happens, the unixBigLock mutex must not be acquired until after |
| 730 | ** pLockMutex is released. |
| 731 | ** |
| 732 | ** OK: enter(unixBigLock), enter(pLockInfo) |
| 733 | ** OK: enter(unixBigLock) |
| 734 | ** OK: enter(pLockInfo) |
| 735 | ** ERROR: enter(pLockInfo), enter(unixBigLock) |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 736 | */ |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame] | 737 | static sqlite3_mutex *unixBigLock = 0; |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 738 | static void unixEnterMutex(void){ |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 739 | assert( sqlite3_mutex_notheld(unixBigLock) ); /* Not a recursive mutex */ |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame] | 740 | sqlite3_mutex_enter(unixBigLock); |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 741 | } |
| 742 | static void unixLeaveMutex(void){ |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 743 | assert( sqlite3_mutex_held(unixBigLock) ); |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame] | 744 | sqlite3_mutex_leave(unixBigLock); |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 745 | } |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 746 | #ifdef SQLITE_DEBUG |
| 747 | static int unixMutexHeld(void) { |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame] | 748 | return sqlite3_mutex_held(unixBigLock); |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 749 | } |
| 750 | #endif |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 751 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 752 | |
mistachkin | fb383e9 | 2015-04-16 03:24:38 +0000 | [diff] [blame] | 753 | #ifdef SQLITE_HAVE_OS_TRACE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 754 | /* |
| 755 | ** Helper function for printing out trace information from debugging |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 756 | ** binaries. This returns the string representation of the supplied |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 757 | ** integer lock-type. |
| 758 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 759 | static const char *azFileLock(int eFileLock){ |
| 760 | switch( eFileLock ){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 761 | case NO_LOCK: return "NONE"; |
| 762 | case SHARED_LOCK: return "SHARED"; |
| 763 | case RESERVED_LOCK: return "RESERVED"; |
| 764 | case PENDING_LOCK: return "PENDING"; |
| 765 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 766 | } |
| 767 | return "ERROR"; |
| 768 | } |
| 769 | #endif |
| 770 | |
| 771 | #ifdef SQLITE_LOCK_TRACE |
| 772 | /* |
| 773 | ** Print out information about all locking operations. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 774 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 775 | ** This routine is used for troubleshooting locks on multithreaded |
| 776 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 777 | ** command-line option on the compiler. This code is normally |
| 778 | ** turned off. |
| 779 | */ |
| 780 | static int lockTrace(int fd, int op, struct flock *p){ |
| 781 | char *zOpName, *zType; |
| 782 | int s; |
| 783 | int savedErrno; |
| 784 | if( op==F_GETLK ){ |
| 785 | zOpName = "GETLK"; |
| 786 | }else if( op==F_SETLK ){ |
| 787 | zOpName = "SETLK"; |
| 788 | }else{ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 789 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 790 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 791 | return s; |
| 792 | } |
| 793 | if( p->l_type==F_RDLCK ){ |
| 794 | zType = "RDLCK"; |
| 795 | }else if( p->l_type==F_WRLCK ){ |
| 796 | zType = "WRLCK"; |
| 797 | }else if( p->l_type==F_UNLCK ){ |
| 798 | zType = "UNLCK"; |
| 799 | }else{ |
| 800 | assert( 0 ); |
| 801 | } |
| 802 | assert( p->l_whence==SEEK_SET ); |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 803 | s = osFcntl(fd, op, p); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 804 | savedErrno = errno; |
| 805 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 806 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 807 | (int)p->l_pid, s); |
| 808 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 809 | struct flock l2; |
| 810 | l2 = *p; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 811 | osFcntl(fd, F_GETLK, &l2); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 812 | if( l2.l_type==F_RDLCK ){ |
| 813 | zType = "RDLCK"; |
| 814 | }else if( l2.l_type==F_WRLCK ){ |
| 815 | zType = "WRLCK"; |
| 816 | }else if( l2.l_type==F_UNLCK ){ |
| 817 | zType = "UNLCK"; |
| 818 | }else{ |
| 819 | assert( 0 ); |
| 820 | } |
| 821 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 822 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 823 | } |
| 824 | errno = savedErrno; |
| 825 | return s; |
| 826 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 827 | #undef osFcntl |
| 828 | #define osFcntl lockTrace |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 829 | #endif /* SQLITE_LOCK_TRACE */ |
| 830 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 831 | /* |
| 832 | ** Retry ftruncate() calls that fail due to EINTR |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 833 | ** |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 834 | ** All calls to ftruncate() within this file should be made through |
| 835 | ** this wrapper. On the Android platform, bypassing the logic below |
| 836 | ** could lead to a corrupt database. |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 837 | */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 838 | static int robust_ftruncate(int h, sqlite3_int64 sz){ |
| 839 | int rc; |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 840 | #ifdef __ANDROID__ |
| 841 | /* On Android, ftruncate() always uses 32-bit offsets, even if |
| 842 | ** _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] | 843 | ** truncate a file to any size larger than 2GiB. Silently ignore any |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 844 | ** such attempts. */ |
| 845 | if( sz>(sqlite3_int64)0x7FFFFFFF ){ |
| 846 | rc = SQLITE_OK; |
| 847 | }else |
| 848 | #endif |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 849 | do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR ); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 850 | return rc; |
| 851 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 852 | |
| 853 | /* |
| 854 | ** This routine translates a standard POSIX errno code into something |
| 855 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 856 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 857 | ** and a variety of "please close the file descriptor NOW" errors into |
| 858 | ** SQLITE_IOERR |
| 859 | ** |
| 860 | ** Errors during initialization of locks, or file system support for locks, |
| 861 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 862 | */ |
| 863 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
drh | 91c4def | 2015-11-25 14:00:07 +0000 | [diff] [blame] | 864 | assert( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
| 865 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 866 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 867 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 868 | switch (posixError) { |
drh | 91c4def | 2015-11-25 14:00:07 +0000 | [diff] [blame] | 869 | case EACCES: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 870 | case EAGAIN: |
| 871 | case ETIMEDOUT: |
| 872 | case EBUSY: |
| 873 | case EINTR: |
| 874 | case ENOLCK: |
| 875 | /* random NFS retry error, unless during file system support |
| 876 | * introspection, in which it actually means what it says */ |
| 877 | return SQLITE_BUSY; |
| 878 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 879 | case EPERM: |
| 880 | return SQLITE_PERM; |
| 881 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 882 | default: |
| 883 | return sqliteIOErr; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 888 | /****************************************************************************** |
| 889 | ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 890 | ** |
| 891 | ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 892 | ** the device number and the inode number. But this does not work on VxWorks. |
| 893 | ** On VxWorks, a unique file id must be based on the canonical filename. |
| 894 | ** |
| 895 | ** A pointer to an instance of the following structure can be used as a |
| 896 | ** unique file ID in VxWorks. Each instance of this structure contains |
| 897 | ** a copy of the canonical filename. There is also a reference count. |
| 898 | ** The structure is reclaimed when the number of pointers to it drops to |
| 899 | ** zero. |
| 900 | ** |
| 901 | ** There are never very many files open at one time and lookups are not |
| 902 | ** a performance-critical path, so it is sufficient to put these |
| 903 | ** structures on a linked list. |
| 904 | */ |
| 905 | struct vxworksFileId { |
| 906 | struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 907 | int nRef; /* Number of references to this one */ |
| 908 | int nName; /* Length of the zCanonicalName[] string */ |
| 909 | char *zCanonicalName; /* Canonical filename */ |
| 910 | }; |
| 911 | |
| 912 | #if OS_VXWORKS |
| 913 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 914 | ** All unique filenames are held on a linked list headed by this |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 915 | ** variable: |
| 916 | */ |
| 917 | static struct vxworksFileId *vxworksFileList = 0; |
| 918 | |
| 919 | /* |
| 920 | ** Simplify a filename into its canonical form |
| 921 | ** by making the following changes: |
| 922 | ** |
| 923 | ** * removing any trailing and duplicate / |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 924 | ** * convert /./ into just / |
| 925 | ** * convert /A/../ where A is any simple name into just / |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 926 | ** |
| 927 | ** Changes are made in-place. Return the new name length. |
| 928 | ** |
| 929 | ** The original filename is in z[0..n-1]. Return the number of |
| 930 | ** characters in the simplified name. |
| 931 | */ |
| 932 | static int vxworksSimplifyName(char *z, int n){ |
| 933 | int i, j; |
| 934 | while( n>1 && z[n-1]=='/' ){ n--; } |
| 935 | for(i=j=0; i<n; i++){ |
| 936 | if( z[i]=='/' ){ |
| 937 | if( z[i+1]=='/' ) continue; |
| 938 | if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 939 | i += 1; |
| 940 | continue; |
| 941 | } |
| 942 | if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 943 | while( j>0 && z[j-1]!='/' ){ j--; } |
| 944 | if( j>0 ){ j--; } |
| 945 | i += 2; |
| 946 | continue; |
| 947 | } |
| 948 | } |
| 949 | z[j++] = z[i]; |
| 950 | } |
| 951 | z[j] = 0; |
| 952 | return j; |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | ** Find a unique file ID for the given absolute pathname. Return |
| 957 | ** a pointer to the vxworksFileId object. This pointer is the unique |
| 958 | ** file ID. |
| 959 | ** |
| 960 | ** The nRef field of the vxworksFileId object is incremented before |
| 961 | ** the object is returned. A new vxworksFileId object is created |
| 962 | ** and added to the global list if necessary. |
| 963 | ** |
| 964 | ** If a memory allocation error occurs, return NULL. |
| 965 | */ |
| 966 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 967 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 968 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 969 | int n; /* Length of zAbsoluteName string */ |
| 970 | |
| 971 | assert( zAbsoluteName[0]=='/' ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 972 | n = (int)strlen(zAbsoluteName); |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 973 | pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 974 | if( pNew==0 ) return 0; |
| 975 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 976 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 977 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 978 | |
| 979 | /* Search for an existing entry that matching the canonical name. |
| 980 | ** If found, increment the reference count and return a pointer to |
| 981 | ** the existing file ID. |
| 982 | */ |
| 983 | unixEnterMutex(); |
| 984 | for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 985 | if( pCandidate->nName==n |
| 986 | && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 987 | ){ |
| 988 | sqlite3_free(pNew); |
| 989 | pCandidate->nRef++; |
| 990 | unixLeaveMutex(); |
| 991 | return pCandidate; |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | /* No match was found. We will make a new file ID */ |
| 996 | pNew->nRef = 1; |
| 997 | pNew->nName = n; |
| 998 | pNew->pNext = vxworksFileList; |
| 999 | vxworksFileList = pNew; |
| 1000 | unixLeaveMutex(); |
| 1001 | return pNew; |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | ** Decrement the reference count on a vxworksFileId object. Free |
| 1006 | ** the object when the reference count reaches zero. |
| 1007 | */ |
| 1008 | static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 1009 | unixEnterMutex(); |
| 1010 | assert( pId->nRef>0 ); |
| 1011 | pId->nRef--; |
| 1012 | if( pId->nRef==0 ){ |
| 1013 | struct vxworksFileId **pp; |
| 1014 | for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 1015 | assert( *pp==pId ); |
| 1016 | *pp = pId->pNext; |
| 1017 | sqlite3_free(pId); |
| 1018 | } |
| 1019 | unixLeaveMutex(); |
| 1020 | } |
| 1021 | #endif /* OS_VXWORKS */ |
| 1022 | /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 1023 | ******************************************************************************/ |
| 1024 | |
| 1025 | |
| 1026 | /****************************************************************************** |
| 1027 | *************************** Posix Advisory Locking **************************** |
| 1028 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1029 | ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1030 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 1031 | ** sets or clears a lock, that operation overrides any prior locks set |
| 1032 | ** by the same process. It does not explicitly say so, but this implies |
| 1033 | ** that it overrides locks set by the same process using a different |
| 1034 | ** file descriptor. Consider this test case: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1035 | ** |
| 1036 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1037 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 1038 | ** |
| 1039 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 1040 | ** one is a hard or symbolic link to the other) then if you set |
| 1041 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 1042 | ** on fd2, it works. I would have expected the second lock to |
| 1043 | ** fail since there was already a lock on the file due to fd1. |
| 1044 | ** But not so. Since both locks came from the same process, the |
| 1045 | ** second overrides the first, even though they were on different |
| 1046 | ** file descriptors opened on different file names. |
| 1047 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1048 | ** This means that we cannot use POSIX locks to synchronize file access |
| 1049 | ** among competing threads of the same process. POSIX locks will work fine |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1050 | ** to synchronize access for threads in separate processes, but not |
| 1051 | ** threads within the same process. |
| 1052 | ** |
| 1053 | ** To work around the problem, SQLite has to manage file locks internally |
| 1054 | ** on its own. Whenever a new database is opened, we have to find the |
| 1055 | ** specific inode of the database file (the inode is determined by the |
| 1056 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 1057 | ** and check for locks already existing on that inode. When locks are |
| 1058 | ** created or removed, we have to look at our own internal record of the |
| 1059 | ** locks to see if another thread has previously set a lock on that same |
| 1060 | ** inode. |
| 1061 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1062 | ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 1063 | ** For VxWorks, we have to use the alternative unique ID system based on |
| 1064 | ** canonical filename and implemented in the previous division.) |
| 1065 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1066 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1067 | ** descriptor. It is now a structure that holds the integer file |
| 1068 | ** descriptor and a pointer to a structure that describes the internal |
| 1069 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1070 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1071 | ** point to the same locking structure. The locking structure keeps |
| 1072 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 1073 | ** field that tells us its internal lock status. cnt==0 means the |
| 1074 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 1075 | ** cnt>0 means there are cnt shared locks on the file. |
| 1076 | ** |
| 1077 | ** Any attempt to lock or unlock a file first checks the locking |
| 1078 | ** structure. The fcntl() system call is only invoked to set a |
| 1079 | ** POSIX lock if the internal lock structure transitions between |
| 1080 | ** a locked and an unlocked state. |
| 1081 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1082 | ** But wait: there are yet more problems with POSIX advisory locks. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1083 | ** |
| 1084 | ** If you close a file descriptor that points to a file that has locks, |
| 1085 | ** all locks on that file that are owned by the current process are |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1086 | ** released. To work around this problem, each unixInodeInfo object |
| 1087 | ** maintains a count of the number of pending locks on tha inode. |
| 1088 | ** When an attempt is made to close an unixFile, if there are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1089 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1090 | ** to close() the file descriptor is deferred until all of the locks clear. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1091 | ** The unixInodeInfo structure keeps a list of file descriptors that need to |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1092 | ** be closed and that list is walked (and cleared) when the last lock |
| 1093 | ** clears. |
| 1094 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1095 | ** Yet another problem: LinuxThreads do not play well with posix locks. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1096 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1097 | ** Many older versions of linux use the LinuxThreads library which is |
| 1098 | ** not posix compliant. Under LinuxThreads, a lock created by thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1099 | ** A cannot be modified or overridden by a different thread B. |
| 1100 | ** Only thread A can modify the lock. Locking behavior is correct |
| 1101 | ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 1102 | ** on linux - with NPTL a lock created by thread A can override locks |
| 1103 | ** in thread B. But there is no way to know at compile-time which |
| 1104 | ** threading library is being used. So there is no way to know at |
| 1105 | ** compile-time whether or not thread A can override locks on thread B. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1106 | ** 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] | 1107 | ** current process. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1108 | ** |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1109 | ** SQLite used to support LinuxThreads. But support for LinuxThreads |
| 1110 | ** was dropped beginning with version 3.7.0. SQLite will still work with |
| 1111 | ** LinuxThreads provided that (1) there is no more than one connection |
| 1112 | ** per database file in the same process and (2) database connections |
| 1113 | ** do not move across threads. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1114 | */ |
| 1115 | |
| 1116 | /* |
| 1117 | ** An instance of the following structure serves as the key used |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1118 | ** to locate a particular unixInodeInfo object. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1119 | */ |
| 1120 | struct unixFileId { |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1121 | dev_t dev; /* Device number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1122 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1123 | struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1124 | #else |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 1125 | /* We are told that some versions of Android contain a bug that |
| 1126 | ** sizes ino_t at only 32-bits instead of 64-bits. (See |
| 1127 | ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c) |
| 1128 | ** To work around this, always allocate 64-bits for the inode number. |
| 1129 | ** On small machines that only have 32-bit inodes, this wastes 4 bytes, |
| 1130 | ** but that should not be a big deal. */ |
| 1131 | /* WAS: ino_t ino; */ |
| 1132 | u64 ino; /* Inode number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1133 | #endif |
| 1134 | }; |
| 1135 | |
| 1136 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1137 | ** An instance of the following structure is allocated for each open |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1138 | ** inode. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1139 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1140 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1141 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1142 | ** object keeps a count of the number of unixFile pointing to it. |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1143 | ** |
| 1144 | ** Mutex rules: |
| 1145 | ** |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 1146 | ** (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] | 1147 | ** any of the locking fields: |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1148 | ** nShared, nLock, eFileLock, bProcessLock, pUnused |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1149 | ** |
| 1150 | ** (2) When nRef>0, then the following fields are unchanging and can |
| 1151 | ** be read (but not written) without holding any mutex: |
| 1152 | ** fileId, pLockMutex |
| 1153 | ** |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1154 | ** (3) With the exceptions above, all the fields may only be read |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1155 | ** or written while holding the global unixBigLock mutex. |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 1156 | ** |
| 1157 | ** Deadlock prevention: The global unixBigLock mutex may not |
| 1158 | ** be acquired while holding the pLockMutex mutex. If both unixBigLock |
| 1159 | ** and pLockMutex are needed, then unixBigLock must be acquired first. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1160 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1161 | struct unixInodeInfo { |
| 1162 | struct unixFileId fileId; /* The lookup key */ |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1163 | sqlite3_mutex *pLockMutex; /* Hold this mutex for... */ |
| 1164 | int nShared; /* Number of SHARED locks held */ |
| 1165 | int nLock; /* Number of outstanding file locks */ |
| 1166 | unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
| 1167 | unsigned char bProcessLock; /* An exclusive process lock is held */ |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1168 | UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1169 | int nRef; /* Number of pointers to this structure */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1170 | unixShmNode *pShmNode; /* Shared memory associated with this inode */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1171 | unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ |
| 1172 | unixInodeInfo *pPrev; /* .... doubly linked */ |
drh | d4a8031 | 2011-04-15 14:33:20 +0000 | [diff] [blame] | 1173 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1174 | unsigned long long sharedByte; /* for AFP simulated shared lock */ |
| 1175 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1176 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1177 | sem_t *pSem; /* Named POSIX semaphore */ |
| 1178 | char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1179 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1180 | }; |
| 1181 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1182 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1183 | ** A lists of all unixInodeInfo objects. |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1184 | ** |
| 1185 | ** Must hold unixBigLock in order to read or write this variable. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1186 | */ |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 1187 | static unixInodeInfo *inodeList = 0; /* All unixInodeInfo objects */ |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 1188 | |
| 1189 | #ifdef SQLITE_DEBUG |
| 1190 | /* |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1191 | ** True if the inode mutex (on the unixFile.pFileMutex field) is held, or not. |
| 1192 | ** This routine is used only within assert() to help verify correct mutex |
| 1193 | ** usage. |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 1194 | */ |
| 1195 | int unixFileMutexHeld(unixFile *pFile){ |
| 1196 | assert( pFile->pInode ); |
| 1197 | return sqlite3_mutex_held(pFile->pInode->pLockMutex); |
| 1198 | } |
| 1199 | int unixFileMutexNotheld(unixFile *pFile){ |
| 1200 | assert( pFile->pInode ); |
| 1201 | return sqlite3_mutex_notheld(pFile->pInode->pLockMutex); |
| 1202 | } |
| 1203 | #endif |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1204 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 1205 | /* |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1206 | ** |
drh | aaeaa18 | 2015-11-24 15:12:47 +0000 | [diff] [blame] | 1207 | ** This function - unixLogErrorAtLine(), is only ever called via the macro |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1208 | ** unixLogError(). |
| 1209 | ** |
| 1210 | ** It is invoked after an error occurs in an OS function and errno has been |
| 1211 | ** set. It logs a message using sqlite3_log() containing the current value of |
| 1212 | ** errno and, if possible, the human-readable equivalent from strerror() or |
| 1213 | ** strerror_r(). |
| 1214 | ** |
| 1215 | ** The first argument passed to the macro should be the error code that |
| 1216 | ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). |
| 1217 | ** The two subsequent arguments should be the name of the OS function that |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 1218 | ** failed (e.g. "unlink", "open") and the associated file-system path, |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1219 | ** if any. |
| 1220 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1221 | #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__) |
| 1222 | static int unixLogErrorAtLine( |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1223 | int errcode, /* SQLite error code */ |
| 1224 | const char *zFunc, /* Name of OS function that failed */ |
| 1225 | const char *zPath, /* File path associated with error */ |
| 1226 | int iLine /* Source line number where error occurred */ |
| 1227 | ){ |
| 1228 | char *zErr; /* Message from strerror() or equivalent */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1229 | int iErrno = errno; /* Saved syscall error number */ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1230 | |
| 1231 | /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use |
| 1232 | ** the strerror() function to obtain the human-readable error message |
| 1233 | ** equivalent to errno. Otherwise, use strerror_r(). |
| 1234 | */ |
| 1235 | #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) |
| 1236 | char aErr[80]; |
| 1237 | memset(aErr, 0, sizeof(aErr)); |
| 1238 | zErr = aErr; |
| 1239 | |
| 1240 | /* 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] | 1241 | ** assume that the system provides the GNU version of strerror_r() that |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1242 | ** returns a pointer to a buffer containing the error message. That pointer |
| 1243 | ** may point to aErr[], or it may point to some static storage somewhere. |
| 1244 | ** Otherwise, assume that the system provides the POSIX version of |
| 1245 | ** strerror_r(), which always writes an error message into aErr[]. |
| 1246 | ** |
| 1247 | ** If the code incorrectly assumes that it is the POSIX version that is |
| 1248 | ** available, the error message will often be an empty string. Not a |
| 1249 | ** huge problem. Incorrectly concluding that the GNU version is available |
| 1250 | ** could lead to a segfault though. |
| 1251 | */ |
| 1252 | #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) |
| 1253 | zErr = |
| 1254 | # endif |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1255 | strerror_r(iErrno, aErr, sizeof(aErr)-1); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1256 | |
| 1257 | #elif SQLITE_THREADSAFE |
| 1258 | /* This is a threadsafe build, but strerror_r() is not available. */ |
| 1259 | zErr = ""; |
| 1260 | #else |
| 1261 | /* Non-threadsafe build, use strerror(). */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1262 | zErr = strerror(iErrno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1263 | #endif |
| 1264 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1265 | if( zPath==0 ) zPath = ""; |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1266 | sqlite3_log(errcode, |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1267 | "os_unix.c:%d: (%d) %s(%s) - %s", |
| 1268 | iLine, iErrno, zFunc, zPath, zErr |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1269 | ); |
| 1270 | |
| 1271 | return errcode; |
| 1272 | } |
| 1273 | |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1274 | /* |
| 1275 | ** Close a file descriptor. |
| 1276 | ** |
| 1277 | ** We assume that close() almost always works, since it is only in a |
| 1278 | ** very sick application or on a very sick platform that it might fail. |
| 1279 | ** If it does fail, simply leak the file descriptor, but do log the |
| 1280 | ** error. |
| 1281 | ** |
| 1282 | ** Note that it is not safe to retry close() after EINTR since the |
| 1283 | ** file descriptor might have already been reused by another thread. |
| 1284 | ** So we don't even try to recover from an EINTR. Just log the error |
| 1285 | ** and move on. |
| 1286 | */ |
| 1287 | static void robust_close(unixFile *pFile, int h, int lineno){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1288 | if( osClose(h) ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1289 | unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", |
| 1290 | pFile ? pFile->zPath : 0, lineno); |
| 1291 | } |
| 1292 | } |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 1293 | |
| 1294 | /* |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 1295 | ** Set the pFile->lastErrno. Do this in a subroutine as that provides |
| 1296 | ** a convenient place to set a breakpoint. |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1297 | */ |
| 1298 | static void storeLastErrno(unixFile *pFile, int error){ |
| 1299 | pFile->lastErrno = error; |
| 1300 | } |
| 1301 | |
| 1302 | /* |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1303 | ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1304 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1305 | static void closePendingFds(unixFile *pFile){ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1306 | unixInodeInfo *pInode = pFile->pInode; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1307 | UnixUnusedFd *p; |
| 1308 | UnixUnusedFd *pNext; |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1309 | assert( unixFileMutexHeld(pFile) ); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1310 | for(p=pInode->pUnused; p; p=pNext){ |
| 1311 | pNext = p->pNext; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1312 | robust_close(pFile, p->fd, __LINE__); |
| 1313 | sqlite3_free(p); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1314 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 1315 | pInode->pUnused = 0; |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1319 | ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1320 | ** |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1321 | ** The global mutex must be held when this routine is called, but the mutex |
| 1322 | ** on the inode being deleted must NOT be held. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1323 | */ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1324 | static void releaseInodeInfo(unixFile *pFile){ |
| 1325 | unixInodeInfo *pInode = pFile->pInode; |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1326 | assert( unixMutexHeld() ); |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 1327 | assert( unixFileMutexNotheld(pFile) ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1328 | if( ALWAYS(pInode) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1329 | pInode->nRef--; |
| 1330 | if( pInode->nRef==0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1331 | assert( pInode->pShmNode==0 ); |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1332 | sqlite3_mutex_enter(pInode->pLockMutex); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1333 | closePendingFds(pFile); |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1334 | sqlite3_mutex_leave(pInode->pLockMutex); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1335 | if( pInode->pPrev ){ |
| 1336 | assert( pInode->pPrev->pNext==pInode ); |
| 1337 | pInode->pPrev->pNext = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1338 | }else{ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1339 | assert( inodeList==pInode ); |
| 1340 | inodeList = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1341 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1342 | if( pInode->pNext ){ |
| 1343 | assert( pInode->pNext->pPrev==pInode ); |
| 1344 | pInode->pNext->pPrev = pInode->pPrev; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 1345 | } |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1346 | sqlite3_mutex_free(pInode->pLockMutex); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1347 | sqlite3_free(pInode); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1348 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1353 | ** Given a file descriptor, locate the unixInodeInfo object that |
| 1354 | ** describes that file descriptor. Create a new one if necessary. The |
| 1355 | ** return value might be uninitialized if an error occurs. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1356 | ** |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1357 | ** The global mutex must held when calling this routine. |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1358 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1359 | ** Return an appropriate error code. |
| 1360 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1361 | static int findInodeInfo( |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1362 | unixFile *pFile, /* Unix file with file desc used in the key */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1363 | unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1364 | ){ |
| 1365 | int rc; /* System call return code */ |
| 1366 | int fd; /* The file descriptor for pFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1367 | struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ |
| 1368 | struct stat statbuf; /* Low-level file information */ |
| 1369 | unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1370 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 1371 | assert( unixMutexHeld() ); |
| 1372 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1373 | /* Get low-level information about the file that we can used to |
| 1374 | ** create a unique name for the file. |
| 1375 | */ |
| 1376 | fd = pFile->h; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1377 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1378 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1379 | storeLastErrno(pFile, errno); |
drh | 40fe8d3 | 2015-11-30 20:36:26 +0000 | [diff] [blame] | 1380 | #if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS) |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1381 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 1382 | #endif |
| 1383 | return SQLITE_IOERR; |
| 1384 | } |
| 1385 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1386 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1387 | /* On OS X on an msdos filesystem, the inode number is reported |
| 1388 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 1389 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 1390 | ** we always increase the file size to 1 by writing a single byte |
| 1391 | ** prior to accessing the inode number. The one byte written is |
| 1392 | ** an ASCII 'S' character which also happens to be the first byte |
| 1393 | ** in the header of every SQLite database. In this way, if there |
| 1394 | ** is a race condition such that another thread has already populated |
| 1395 | ** the first page of the database, no damage is done. |
| 1396 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1397 | if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 1398 | do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR ); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1399 | if( rc!=1 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1400 | storeLastErrno(pFile, errno); |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1401 | return SQLITE_IOERR; |
| 1402 | } |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 1403 | rc = osFstat(fd, &statbuf); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1404 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1405 | storeLastErrno(pFile, errno); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1406 | return SQLITE_IOERR; |
| 1407 | } |
| 1408 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 1409 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1410 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1411 | memset(&fileId, 0, sizeof(fileId)); |
| 1412 | fileId.dev = statbuf.st_dev; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1413 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1414 | fileId.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1415 | #else |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 1416 | fileId.ino = (u64)statbuf.st_ino; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1417 | #endif |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1418 | assert( unixMutexHeld() ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1419 | pInode = inodeList; |
| 1420 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 1421 | pInode = pInode->pNext; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1422 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1423 | if( pInode==0 ){ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 1424 | pInode = sqlite3_malloc64( sizeof(*pInode) ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1425 | if( pInode==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 1426 | return SQLITE_NOMEM_BKPT; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1427 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1428 | memset(pInode, 0, sizeof(*pInode)); |
| 1429 | memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
drh | 6886d6d | 2018-07-23 22:55:10 +0000 | [diff] [blame] | 1430 | if( sqlite3GlobalConfig.bCoreMutex ){ |
| 1431 | pInode->pLockMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 1432 | if( pInode->pLockMutex==0 ){ |
| 1433 | sqlite3_free(pInode); |
| 1434 | return SQLITE_NOMEM_BKPT; |
| 1435 | } |
| 1436 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1437 | pInode->nRef = 1; |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 1438 | assert( unixMutexHeld() ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1439 | pInode->pNext = inodeList; |
| 1440 | pInode->pPrev = 0; |
| 1441 | if( inodeList ) inodeList->pPrev = pInode; |
| 1442 | inodeList = pInode; |
| 1443 | }else{ |
| 1444 | pInode->nRef++; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1445 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1446 | *ppInode = pInode; |
| 1447 | return SQLITE_OK; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1448 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1449 | |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1450 | /* |
| 1451 | ** Return TRUE if pFile has been renamed or unlinked since it was first opened. |
| 1452 | */ |
| 1453 | static int fileHasMoved(unixFile *pFile){ |
drh | 61ffea5 | 2014-08-12 12:19:25 +0000 | [diff] [blame] | 1454 | #if OS_VXWORKS |
| 1455 | return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId; |
| 1456 | #else |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1457 | struct stat buf; |
| 1458 | return pFile->pInode!=0 && |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 1459 | (osStat(pFile->zPath, &buf)!=0 |
| 1460 | || (u64)buf.st_ino!=pFile->pInode->fileId.ino); |
drh | 91be7dc | 2014-08-11 13:53:30 +0000 | [diff] [blame] | 1461 | #endif |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1464 | |
| 1465 | /* |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1466 | ** Check a unixFile that is a database. Verify the following: |
| 1467 | ** |
| 1468 | ** (1) There is exactly one hard link on the file |
| 1469 | ** (2) The file is not a symbolic link |
| 1470 | ** (3) The file has not been renamed or unlinked |
| 1471 | ** |
| 1472 | ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right. |
| 1473 | */ |
| 1474 | static void verifyDbFile(unixFile *pFile){ |
| 1475 | struct stat buf; |
| 1476 | int rc; |
drh | 86151e8 | 2015-12-08 14:37:16 +0000 | [diff] [blame] | 1477 | |
| 1478 | /* These verifications occurs for the main database only */ |
| 1479 | if( pFile->ctrlFlags & UNIXFILE_NOLOCK ) return; |
| 1480 | |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1481 | rc = osFstat(pFile->h, &buf); |
| 1482 | if( rc!=0 ){ |
| 1483 | sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1484 | return; |
| 1485 | } |
drh | 6369bc3 | 2016-03-21 16:06:42 +0000 | [diff] [blame] | 1486 | if( buf.st_nlink==0 ){ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1487 | sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1488 | return; |
| 1489 | } |
| 1490 | if( buf.st_nlink>1 ){ |
| 1491 | sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1492 | return; |
| 1493 | } |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 1494 | if( fileHasMoved(pFile) ){ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1495 | sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 1496 | return; |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | |
| 1501 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1502 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1503 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1504 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1505 | ** 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] | 1506 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1507 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1508 | int rc = SQLITE_OK; |
| 1509 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1510 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1511 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 1512 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1513 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1514 | assert( pFile ); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 1515 | assert( pFile->eFileLock<=SHARED_LOCK ); |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1516 | sqlite3_mutex_enter(pFile->pInode->pLockMutex); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1517 | |
| 1518 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1519 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1520 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1523 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1524 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1525 | #ifndef __DJGPP__ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1526 | if( !reserved && !pFile->pInode->bProcessLock ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1527 | struct flock lock; |
| 1528 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1529 | lock.l_start = RESERVED_BYTE; |
| 1530 | lock.l_len = 1; |
| 1531 | lock.l_type = F_WRLCK; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1532 | if( osFcntl(pFile->h, F_GETLK, &lock) ){ |
| 1533 | rc = SQLITE_IOERR_CHECKRESERVEDLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1534 | storeLastErrno(pFile, errno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1535 | } else if( lock.l_type!=F_UNLCK ){ |
| 1536 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1537 | } |
| 1538 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 1539 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1540 | |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1541 | sqlite3_mutex_leave(pFile->pInode->pLockMutex); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1542 | OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1543 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1544 | *pResOut = reserved; |
| 1545 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
drh | ddcfe92 | 2020-09-15 12:29:35 +0000 | [diff] [blame] | 1548 | /* Forward declaration*/ |
| 1549 | static int unixSleep(sqlite3_vfs*,int); |
| 1550 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1551 | /* |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 1552 | ** Set a posix-advisory-lock. |
| 1553 | ** |
| 1554 | ** There are two versions of this routine. If compiled with |
| 1555 | ** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter |
| 1556 | ** which is a pointer to a unixFile. If the unixFile->iBusyTimeout |
| 1557 | ** value is set, then it is the number of milliseconds to wait before |
| 1558 | ** failing the lock. The iBusyTimeout value is always reset back to |
| 1559 | ** zero on each call. |
| 1560 | ** |
| 1561 | ** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking |
| 1562 | ** attempt to set the lock. |
| 1563 | */ |
| 1564 | #ifndef SQLITE_ENABLE_SETLK_TIMEOUT |
| 1565 | # define osSetPosixAdvisoryLock(h,x,t) osFcntl(h,F_SETLK,x) |
| 1566 | #else |
| 1567 | static int osSetPosixAdvisoryLock( |
| 1568 | int h, /* The file descriptor on which to take the lock */ |
| 1569 | struct flock *pLock, /* The description of the lock */ |
| 1570 | unixFile *pFile /* Structure holding timeout value */ |
| 1571 | ){ |
dan | 7bb8b8a | 2020-05-06 20:27:18 +0000 | [diff] [blame] | 1572 | int tm = pFile->iBusyTimeout; |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 1573 | int rc = osFcntl(h,F_SETLK,pLock); |
dan | 7bb8b8a | 2020-05-06 20:27:18 +0000 | [diff] [blame] | 1574 | while( rc<0 && tm>0 ){ |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 1575 | /* On systems that support some kind of blocking file lock with a timeout, |
| 1576 | ** make appropriate changes here to invoke that blocking file lock. On |
| 1577 | ** generic posix, however, there is no such API. So we simply try the |
| 1578 | ** lock once every millisecond until either the timeout expires, or until |
| 1579 | ** the lock is obtained. */ |
drh | ddcfe92 | 2020-09-15 12:29:35 +0000 | [diff] [blame] | 1580 | unixSleep(0,1000); |
drh | fd72563 | 2018-03-26 20:43:05 +0000 | [diff] [blame] | 1581 | rc = osFcntl(h,F_SETLK,pLock); |
dan | 7bb8b8a | 2020-05-06 20:27:18 +0000 | [diff] [blame] | 1582 | tm--; |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 1583 | } |
| 1584 | return rc; |
| 1585 | } |
| 1586 | #endif /* SQLITE_ENABLE_SETLK_TIMEOUT */ |
| 1587 | |
| 1588 | |
| 1589 | /* |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1590 | ** Attempt to set a system-lock on the file pFile. The lock is |
| 1591 | ** described by pLock. |
| 1592 | ** |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1593 | ** If the pFile was opened read/write from unix-excl, then the only lock |
| 1594 | ** ever obtained is an exclusive lock, and it is obtained exactly once |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1595 | ** the first time any lock is attempted. All subsequent system locking |
| 1596 | ** operations become no-ops. Locking operations still happen internally, |
| 1597 | ** in order to coordinate access between separate database connections |
| 1598 | ** within this process, but all of that is handled in memory and the |
| 1599 | ** operating system does not participate. |
drh | 7719711 | 2011-03-15 19:08:48 +0000 | [diff] [blame] | 1600 | ** |
| 1601 | ** This function is a pass-through to fcntl(F_SETLK) if pFile is using |
| 1602 | ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl" |
| 1603 | ** and is read-only. |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1604 | ** |
| 1605 | ** Zero is returned if the call completes successfully, or -1 if a call |
| 1606 | ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()). |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1607 | */ |
| 1608 | static int unixFileLock(unixFile *pFile, struct flock *pLock){ |
| 1609 | int rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1610 | unixInodeInfo *pInode = pFile->pInode; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1611 | assert( pInode!=0 ); |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1612 | assert( sqlite3_mutex_held(pInode->pLockMutex) ); |
drh | 50358ad | 2015-12-02 01:04:33 +0000 | [diff] [blame] | 1613 | if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1614 | if( pInode->bProcessLock==0 ){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1615 | struct flock lock; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1616 | assert( pInode->nLock==0 ); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1617 | lock.l_whence = SEEK_SET; |
| 1618 | lock.l_start = SHARED_FIRST; |
| 1619 | lock.l_len = SHARED_SIZE; |
| 1620 | lock.l_type = F_WRLCK; |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 1621 | rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1622 | if( rc<0 ) return rc; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 1623 | pInode->bProcessLock = 1; |
| 1624 | pInode->nLock++; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1625 | }else{ |
| 1626 | rc = 0; |
| 1627 | } |
| 1628 | }else{ |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 1629 | rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile); |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1630 | } |
| 1631 | return rc; |
| 1632 | } |
| 1633 | |
| 1634 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1635 | ** Lock the file with the lock specified by parameter eFileLock - one |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1636 | ** of the following: |
| 1637 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1638 | ** (1) SHARED_LOCK |
| 1639 | ** (2) RESERVED_LOCK |
| 1640 | ** (3) PENDING_LOCK |
| 1641 | ** (4) EXCLUSIVE_LOCK |
| 1642 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1643 | ** Sometimes when requesting one lock state, additional lock states |
| 1644 | ** are inserted in between. The locking might fail on one of the later |
| 1645 | ** transitions leaving the lock state different from what it started but |
| 1646 | ** still short of its goal. The following chart shows the allowed |
| 1647 | ** transitions and the inserted intermediate states: |
| 1648 | ** |
| 1649 | ** UNLOCKED -> SHARED |
| 1650 | ** SHARED -> RESERVED |
| 1651 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1652 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1653 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1654 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1655 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1656 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1657 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1658 | static int unixLock(sqlite3_file *id, int eFileLock){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1659 | /* The following describes the implementation of the various locks and |
| 1660 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 1661 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 1662 | ** confusion with SQLite lock names). The algorithms are complicated |
drh | f878e6e | 2016-04-07 13:45:20 +0000 | [diff] [blame] | 1663 | ** slightly in order to be compatible with Windows95 systems simultaneously |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1664 | ** accessing the same database file, in case that is ever required. |
| 1665 | ** |
| 1666 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 1667 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 1668 | ** range', a range of 510 bytes at a well known offset. |
| 1669 | ** |
| 1670 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
drh | f878e6e | 2016-04-07 13:45:20 +0000 | [diff] [blame] | 1671 | ** byte'. If this is successful, 'shared byte range' is read-locked |
| 1672 | ** and the lock on the 'pending byte' released. (Legacy note: When |
| 1673 | ** SQLite was first developed, Windows95 systems were still very common, |
| 1674 | ** and Widnows95 lacks a shared-lock capability. So on Windows95, a |
| 1675 | ** single randomly selected by from the 'shared byte range' is locked. |
| 1676 | ** Windows95 is now pretty much extinct, but this work-around for the |
| 1677 | ** lack of shared-locks on Windows95 lives on, for backwards |
| 1678 | ** compatibility.) |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1679 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1680 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 1681 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 1682 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1683 | ** |
| 1684 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1685 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 1686 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 1687 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 1688 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 1689 | ** This property is used by the algorithm for rolling back a journal file |
| 1690 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1691 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 1692 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 1693 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 1694 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 1695 | ** within this range, this ensures that no other locks are held on the |
| 1696 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 1697 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1698 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1699 | unixFile *pFile = (unixFile*)id; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 1700 | unixInodeInfo *pInode; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1701 | struct flock lock; |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 1702 | int tErrno = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1703 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1704 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1705 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 1706 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 1707 | azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 1708 | osGetpid(0))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1709 | |
| 1710 | /* 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] | 1711 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1712 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1713 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1714 | if( pFile->eFileLock>=eFileLock ){ |
| 1715 | OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
| 1716 | azFileLock(eFileLock))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1717 | return SQLITE_OK; |
| 1718 | } |
| 1719 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1720 | /* Make sure the locking sequence is correct. |
| 1721 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 1722 | ** (2) SQLite never explicitly requests a pendig lock. |
| 1723 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1724 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1725 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 1726 | assert( eFileLock!=PENDING_LOCK ); |
| 1727 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1728 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1729 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1730 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1731 | pInode = pFile->pInode; |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1732 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1733 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1734 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1735 | ** handle that precludes the requested lock, return BUSY. |
| 1736 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1737 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 1738 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1739 | ){ |
| 1740 | rc = SQLITE_BUSY; |
| 1741 | goto end_lock; |
| 1742 | } |
| 1743 | |
| 1744 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1745 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1746 | ** return SQLITE_OK. |
| 1747 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1748 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1749 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1750 | assert( eFileLock==SHARED_LOCK ); |
| 1751 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1752 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1753 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1754 | pInode->nShared++; |
| 1755 | pInode->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1756 | goto end_lock; |
| 1757 | } |
| 1758 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1759 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1760 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1761 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1762 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1763 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1764 | lock.l_len = 1L; |
| 1765 | lock.l_whence = SEEK_SET; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1766 | if( eFileLock==SHARED_LOCK |
| 1767 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1768 | ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1769 | lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1770 | lock.l_start = PENDING_BYTE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1771 | if( unixFileLock(pFile, &lock) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1772 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1773 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1774 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1775 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1776 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1777 | goto end_lock; |
| 1778 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | |
| 1782 | /* If control gets to this point, then actually go ahead and make |
| 1783 | ** operating system calls for the specified lock. |
| 1784 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1785 | if( eFileLock==SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1786 | assert( pInode->nShared==0 ); |
| 1787 | assert( pInode->eFileLock==0 ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1788 | assert( rc==SQLITE_OK ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1789 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1790 | /* Now get the read-lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1791 | lock.l_start = SHARED_FIRST; |
| 1792 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1793 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1794 | tErrno = errno; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1795 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1796 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1797 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1798 | /* Drop the temporary PENDING lock */ |
| 1799 | lock.l_start = PENDING_BYTE; |
| 1800 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1801 | lock.l_type = F_UNLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1802 | if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){ |
| 1803 | /* This could happen with a network mount */ |
| 1804 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1805 | rc = SQLITE_IOERR_UNLOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1806 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1807 | |
| 1808 | if( rc ){ |
| 1809 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1810 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1811 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1812 | goto end_lock; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1813 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1814 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1815 | pInode->nLock++; |
| 1816 | pInode->nShared = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1817 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1818 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1819 | /* We are trying for an exclusive lock but another thread in this |
| 1820 | ** same process is still holding a shared lock. */ |
| 1821 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1822 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1823 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1824 | ** assumed that there is a SHARED or greater lock on the file |
| 1825 | ** already. |
| 1826 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1827 | assert( 0!=pFile->eFileLock ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1828 | lock.l_type = F_WRLCK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1829 | |
| 1830 | assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK ); |
| 1831 | if( eFileLock==RESERVED_LOCK ){ |
| 1832 | lock.l_start = RESERVED_BYTE; |
| 1833 | lock.l_len = 1L; |
| 1834 | }else{ |
| 1835 | lock.l_start = SHARED_FIRST; |
| 1836 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1837 | } |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1838 | |
| 1839 | if( unixFileLock(pFile, &lock) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1840 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1841 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1842 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1843 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1844 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1845 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1846 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1847 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1848 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 1849 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1850 | /* Set up the transaction-counter change checking flags when |
| 1851 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1852 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1853 | ** write operation (not a hot journal rollback). |
| 1854 | */ |
| 1855 | if( rc==SQLITE_OK |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1856 | && pFile->eFileLock<=SHARED_LOCK |
| 1857 | && eFileLock==RESERVED_LOCK |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1858 | ){ |
| 1859 | pFile->transCntrChng = 0; |
| 1860 | pFile->dbUpdate = 0; |
| 1861 | pFile->inNormalWrite = 1; |
| 1862 | } |
| 1863 | #endif |
| 1864 | |
| 1865 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1866 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1867 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1868 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1869 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 1870 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1871 | pInode->eFileLock = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1872 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1873 | |
| 1874 | end_lock: |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1875 | sqlite3_mutex_leave(pInode->pLockMutex); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1876 | OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), |
| 1877 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1878 | return rc; |
| 1879 | } |
| 1880 | |
| 1881 | /* |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1882 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1883 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1884 | */ |
| 1885 | static void setPendingFd(unixFile *pFile){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1886 | unixInodeInfo *pInode = pFile->pInode; |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 1887 | UnixUnusedFd *p = pFile->pPreallocatedUnused; |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 1888 | assert( unixFileMutexHeld(pFile) ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1889 | p->pNext = pInode->pUnused; |
| 1890 | pInode->pUnused = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1891 | pFile->h = -1; |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 1892 | pFile->pPreallocatedUnused = 0; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1896 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1897 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1898 | ** |
| 1899 | ** If the locking level of the file descriptor is already at or below |
| 1900 | ** the requested locking level, this routine is a no-op. |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1901 | ** |
| 1902 | ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 1903 | ** the byte range is divided into 2 parts and the first part is unlocked then |
| 1904 | ** set to a read lock, then the other part is simply unlocked. This works |
| 1905 | ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 1906 | ** remove the write lock on a region when a read lock is set. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1907 | */ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1908 | static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1909 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1910 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1911 | struct flock lock; |
| 1912 | int rc = SQLITE_OK; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1913 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1914 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1915 | 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] | 1916 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 1917 | osGetpid(0))); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1918 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1919 | assert( eFileLock<=SHARED_LOCK ); |
| 1920 | if( pFile->eFileLock<=eFileLock ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1921 | return SQLITE_OK; |
| 1922 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1923 | pInode = pFile->pInode; |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 1924 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1925 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1926 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1927 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1928 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 1929 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1930 | /* When reducing a lock such that other processes can start |
| 1931 | ** reading the database file again, make sure that the |
| 1932 | ** transaction counter was updated if any part of the database |
| 1933 | ** file changed. If the transaction counter is not updated, |
| 1934 | ** other connections to the same file might not realize that |
| 1935 | ** the file has changed and hence might not know to flush their |
| 1936 | ** cache. The use of a stale cache can lead to database corruption. |
| 1937 | */ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1938 | pFile->inNormalWrite = 0; |
| 1939 | #endif |
| 1940 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1941 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 1942 | ** before establishing the readlock - to avoid a race condition we downgrade |
| 1943 | ** the lock in 2 blocks, so that part of the range will be covered by a |
| 1944 | ** write lock until the rest is covered by a read lock: |
| 1945 | ** 1: [WWWWW] |
| 1946 | ** 2: [....W] |
| 1947 | ** 3: [RRRRW] |
| 1948 | ** 4: [RRRR.] |
| 1949 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1950 | if( eFileLock==SHARED_LOCK ){ |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1951 | #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE |
drh | 87e79ae | 2011-03-08 13:06:41 +0000 | [diff] [blame] | 1952 | (void)handleNFSUnlock; |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1953 | assert( handleNFSUnlock==0 ); |
| 1954 | #endif |
| 1955 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1956 | if( handleNFSUnlock ){ |
drh | a712b4b | 2015-02-19 16:12:04 +0000 | [diff] [blame] | 1957 | int tErrno; /* Error code from system call errors */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1958 | off_t divSize = SHARED_SIZE - 1; |
| 1959 | |
| 1960 | lock.l_type = F_UNLCK; |
| 1961 | lock.l_whence = SEEK_SET; |
| 1962 | lock.l_start = SHARED_FIRST; |
| 1963 | lock.l_len = divSize; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 1964 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1965 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1966 | rc = SQLITE_IOERR_UNLOCK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 1967 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1968 | goto end_unlock; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1969 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1970 | lock.l_type = F_RDLCK; |
| 1971 | lock.l_whence = SEEK_SET; |
| 1972 | lock.l_start = SHARED_FIRST; |
| 1973 | lock.l_len = divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1974 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1975 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1976 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1977 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 1978 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1979 | } |
| 1980 | goto end_unlock; |
| 1981 | } |
| 1982 | lock.l_type = F_UNLCK; |
| 1983 | lock.l_whence = SEEK_SET; |
| 1984 | lock.l_start = SHARED_FIRST+divSize; |
| 1985 | lock.l_len = SHARED_SIZE-divSize; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 1986 | if( unixFileLock(pFile, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1987 | tErrno = errno; |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 1988 | rc = SQLITE_IOERR_UNLOCK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 1989 | storeLastErrno(pFile, tErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1990 | goto end_unlock; |
| 1991 | } |
drh | 30f776f | 2011-02-25 03:25:07 +0000 | [diff] [blame] | 1992 | }else |
| 1993 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 1994 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1995 | lock.l_type = F_RDLCK; |
| 1996 | lock.l_whence = SEEK_SET; |
| 1997 | lock.l_start = SHARED_FIRST; |
| 1998 | lock.l_len = SHARED_SIZE; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 1999 | if( unixFileLock(pFile, &lock) ){ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2000 | /* In theory, the call to unixFileLock() cannot fail because another |
| 2001 | ** process is holding an incompatible lock. If it does, this |
| 2002 | ** indicates that the other process is not following the locking |
| 2003 | ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning |
| 2004 | ** SQLITE_BUSY would confuse the upper layer (in practice it causes |
| 2005 | ** an assert to fail). */ |
| 2006 | rc = SQLITE_IOERR_RDLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2007 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2008 | goto end_unlock; |
| 2009 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 2010 | } |
| 2011 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2012 | lock.l_type = F_UNLCK; |
| 2013 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2014 | lock.l_start = PENDING_BYTE; |
| 2015 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2016 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2017 | pInode->eFileLock = SHARED_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 2018 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2019 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2020 | storeLastErrno(pFile, errno); |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 2021 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 2022 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2023 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2024 | if( eFileLock==NO_LOCK ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2025 | /* Decrement the shared lock counter. Release the lock using an |
| 2026 | ** OS call only when all threads in this same process have released |
| 2027 | ** the lock. |
| 2028 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2029 | pInode->nShared--; |
| 2030 | if( pInode->nShared==0 ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2031 | lock.l_type = F_UNLCK; |
| 2032 | lock.l_whence = SEEK_SET; |
| 2033 | lock.l_start = lock.l_len = 0L; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2034 | if( unixFileLock(pFile, &lock)==0 ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2035 | pInode->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 2036 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2037 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2038 | storeLastErrno(pFile, errno); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2039 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2040 | pFile->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 2041 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2044 | /* Decrement the count of locks against this same file. When the |
| 2045 | ** count reaches zero, close any other file descriptors whose close |
| 2046 | ** was deferred because of outstanding locks. |
| 2047 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2048 | pInode->nLock--; |
| 2049 | assert( pInode->nLock>=0 ); |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 2050 | if( pInode->nLock==0 ) closePendingFds(pFile); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2051 | } |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 2052 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2053 | end_unlock: |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 2054 | sqlite3_mutex_leave(pInode->pLockMutex); |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 2055 | if( rc==SQLITE_OK ){ |
| 2056 | pFile->eFileLock = eFileLock; |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 2057 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 2058 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2062 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2063 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2064 | ** |
| 2065 | ** If the locking level of the file descriptor is already at or below |
| 2066 | ** the requested locking level, this routine is a no-op. |
| 2067 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2068 | static int unixUnlock(sqlite3_file *id, int eFileLock){ |
dan | f52a469 | 2013-10-31 18:49:58 +0000 | [diff] [blame] | 2069 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | a1afc74 | 2013-03-25 13:50:49 +0000 | [diff] [blame] | 2070 | assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 ); |
dan | f52a469 | 2013-10-31 18:49:58 +0000 | [diff] [blame] | 2071 | #endif |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 2072 | return posixUnlock(id, eFileLock, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 2075 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 2076 | static int unixMapfile(unixFile *pFd, i64 nByte); |
| 2077 | static void unixUnmapfile(unixFile *pFd); |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 2078 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 2079 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2080 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2081 | ** This function performs the parts of the "close file" operation |
| 2082 | ** common to all locking schemes. It closes the directory and file |
| 2083 | ** handles, if they are valid, and sets all fields of the unixFile |
| 2084 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2085 | ** |
| 2086 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 2087 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 2088 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2089 | */ |
| 2090 | static int closeUnixFile(sqlite3_file *id){ |
| 2091 | unixFile *pFile = (unixFile*)id; |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 2092 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 2093 | unixUnmapfile(pFile); |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 2094 | #endif |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2095 | if( pFile->h>=0 ){ |
| 2096 | robust_close(pFile, pFile->h, __LINE__); |
| 2097 | pFile->h = -1; |
| 2098 | } |
| 2099 | #if OS_VXWORKS |
| 2100 | if( pFile->pId ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 2101 | if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 2102 | osUnlink(pFile->pId->zCanonicalName); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2103 | } |
| 2104 | vxworksReleaseFileId(pFile->pId); |
| 2105 | pFile->pId = 0; |
| 2106 | } |
| 2107 | #endif |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 2108 | #ifdef SQLITE_UNLINK_AFTER_CLOSE |
| 2109 | if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
| 2110 | osUnlink(pFile->zPath); |
| 2111 | sqlite3_free(*(char**)&pFile->zPath); |
| 2112 | pFile->zPath = 0; |
| 2113 | } |
| 2114 | #endif |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2115 | OSTRACE(("CLOSE %-3d\n", pFile->h)); |
| 2116 | OpenCounter(-1); |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 2117 | sqlite3_free(pFile->pPreallocatedUnused); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2118 | memset(pFile, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2119 | return SQLITE_OK; |
| 2120 | } |
| 2121 | |
| 2122 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 2123 | ** Close a file. |
| 2124 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 2125 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2126 | int rc = SQLITE_OK; |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2127 | unixFile *pFile = (unixFile *)id; |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 2128 | unixInodeInfo *pInode = pFile->pInode; |
| 2129 | |
| 2130 | assert( pInode!=0 ); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 2131 | verifyDbFile(pFile); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2132 | unixUnlock(id, NO_LOCK); |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 2133 | assert( unixFileMutexNotheld(pFile) ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2134 | unixEnterMutex(); |
| 2135 | |
| 2136 | /* unixFile.pInode is always valid here. Otherwise, a different close |
| 2137 | ** routine (e.g. nolockClose()) would be called instead. |
| 2138 | */ |
| 2139 | assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 2140 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | 3fcef1a | 2018-08-16 16:24:24 +0000 | [diff] [blame] | 2141 | if( pInode->nLock ){ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2142 | /* If there are outstanding locks, do not actually close the file just |
| 2143 | ** yet because that would clear those locks. Instead, add the file |
| 2144 | ** descriptor to pInode->pUnused list. It will be automatically closed |
| 2145 | ** when the last lock is cleared. |
| 2146 | */ |
| 2147 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 2148 | } |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 2149 | sqlite3_mutex_leave(pInode->pLockMutex); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2150 | releaseInodeInfo(pFile); |
dan | 2b06b07 | 2020-09-04 17:30:59 +0000 | [diff] [blame] | 2151 | assert( pFile->pShm==0 ); |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 2152 | rc = closeUnixFile(id); |
| 2153 | unixLeaveMutex(); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2154 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2157 | /************** End of the posix advisory lock implementation ***************** |
| 2158 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2159 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2160 | /****************************************************************************** |
| 2161 | ****************************** No-op Locking ********************************** |
| 2162 | ** |
| 2163 | ** Of the various locking implementations available, this is by far the |
| 2164 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 2165 | ** file for reading or writing. |
| 2166 | ** |
| 2167 | ** This locking mode is appropriate for use on read-only databases |
| 2168 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 2169 | ** also be used if the application employs some external mechanism to |
| 2170 | ** prevent simultaneous access of the same database by two or more |
| 2171 | ** database connections. But there is a serious risk of database |
| 2172 | ** corruption if this locking mode is used in situations where multiple |
| 2173 | ** database connections are accessing the same database file at the same |
| 2174 | ** time and one or more of those connections are writing. |
| 2175 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2176 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2177 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 2178 | UNUSED_PARAMETER(NotUsed); |
| 2179 | *pResOut = 0; |
| 2180 | return SQLITE_OK; |
| 2181 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2182 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 2183 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 2184 | return SQLITE_OK; |
| 2185 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2186 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 2187 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 2188 | return SQLITE_OK; |
| 2189 | } |
| 2190 | |
| 2191 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2192 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2193 | */ |
| 2194 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2195 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | /******************* End of the no-op lock implementation ********************* |
| 2199 | ******************************************************************************/ |
| 2200 | |
| 2201 | /****************************************************************************** |
| 2202 | ************************* Begin dot-file Locking ****************************** |
| 2203 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 2204 | ** The dotfile locking implementation uses the existence of separate lock |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2205 | ** files (really a directory) to control access to the database. This works |
| 2206 | ** on just about every filesystem imaginable. But there are serious downsides: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2207 | ** |
| 2208 | ** (1) There is zero concurrency. A single reader blocks all other |
| 2209 | ** connections from reading or writing the database. |
| 2210 | ** |
| 2211 | ** (2) An application crash or power loss can leave stale lock files |
| 2212 | ** sitting around that need to be cleared manually. |
| 2213 | ** |
| 2214 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 2215 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2216 | ** |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2217 | ** Dotfile locking works by creating a subdirectory in the same directory as |
| 2218 | ** the database and with the same name but with a ".lock" extension added. |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 2219 | ** The existence of a lock directory implies an EXCLUSIVE lock. All other |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2220 | ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2221 | */ |
| 2222 | |
| 2223 | /* |
| 2224 | ** 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] | 2225 | ** lock directory. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2226 | */ |
| 2227 | #define DOTLOCK_SUFFIX ".lock" |
| 2228 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2229 | /* |
| 2230 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2231 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2232 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2233 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2234 | ** |
| 2235 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 2236 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 2237 | ** is held on the file and false if the file is unlocked. |
| 2238 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2239 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 2240 | int rc = SQLITE_OK; |
| 2241 | int reserved = 0; |
| 2242 | unixFile *pFile = (unixFile*)id; |
| 2243 | |
| 2244 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2245 | |
| 2246 | assert( pFile ); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2247 | reserved = osAccess((const char*)pFile->lockingContext, 0)==0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2248 | OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2249 | *pResOut = reserved; |
| 2250 | return rc; |
| 2251 | } |
| 2252 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2253 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2254 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2255 | ** of the following: |
| 2256 | ** |
| 2257 | ** (1) SHARED_LOCK |
| 2258 | ** (2) RESERVED_LOCK |
| 2259 | ** (3) PENDING_LOCK |
| 2260 | ** (4) EXCLUSIVE_LOCK |
| 2261 | ** |
| 2262 | ** Sometimes when requesting one lock state, additional lock states |
| 2263 | ** are inserted in between. The locking might fail on one of the later |
| 2264 | ** transitions leaving the lock state different from what it started but |
| 2265 | ** still short of its goal. The following chart shows the allowed |
| 2266 | ** transitions and the inserted intermediate states: |
| 2267 | ** |
| 2268 | ** UNLOCKED -> SHARED |
| 2269 | ** SHARED -> RESERVED |
| 2270 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2271 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2272 | ** PENDING -> EXCLUSIVE |
| 2273 | ** |
| 2274 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2275 | ** routine to lower a locking level. |
| 2276 | ** |
| 2277 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 2278 | ** But we track the other locking levels internally. |
| 2279 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2280 | static int dotlockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2281 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2282 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2283 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2284 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2285 | |
| 2286 | /* If we have any lock, then the lock file already exists. All we have |
| 2287 | ** to do is adjust our internal record of the lock level. |
| 2288 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2289 | if( pFile->eFileLock > NO_LOCK ){ |
| 2290 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2291 | /* Always update the timestamp on the old file */ |
drh | dbe4b88 | 2011-06-20 18:00:17 +0000 | [diff] [blame] | 2292 | #ifdef HAVE_UTIME |
| 2293 | utime(zLockFile, NULL); |
| 2294 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2295 | utimes(zLockFile, NULL); |
| 2296 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2297 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | /* grab an exclusive lock */ |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2301 | rc = osMkdir(zLockFile, 0777); |
| 2302 | if( rc<0 ){ |
| 2303 | /* failed to open/create the lock directory */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2304 | int tErrno = errno; |
| 2305 | if( EEXIST == tErrno ){ |
| 2306 | rc = SQLITE_BUSY; |
| 2307 | } else { |
| 2308 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2309 | if( rc!=SQLITE_BUSY ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2310 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2311 | } |
| 2312 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2313 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2314 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2315 | |
| 2316 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2317 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2318 | return rc; |
| 2319 | } |
| 2320 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2321 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2322 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2323 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2324 | ** |
| 2325 | ** If the locking level of the file descriptor is already at or below |
| 2326 | ** the requested locking level, this routine is a no-op. |
| 2327 | ** |
| 2328 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 2329 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2330 | static int dotlockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2331 | unixFile *pFile = (unixFile*)id; |
| 2332 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2333 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2334 | |
| 2335 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2336 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2337 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2338 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2339 | |
| 2340 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2341 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2342 | return SQLITE_OK; |
| 2343 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2344 | |
| 2345 | /* To downgrade to shared, simply update our internal notion of the |
| 2346 | ** lock state. No need to mess with the file on disk. |
| 2347 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2348 | if( eFileLock==SHARED_LOCK ){ |
| 2349 | pFile->eFileLock = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2350 | return SQLITE_OK; |
| 2351 | } |
| 2352 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 2353 | /* To fully unlock the database, delete the lock file */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2354 | assert( eFileLock==NO_LOCK ); |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2355 | rc = osRmdir(zLockFile); |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 2356 | if( rc<0 ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 2357 | int tErrno = errno; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2358 | if( tErrno==ENOENT ){ |
| 2359 | rc = SQLITE_OK; |
| 2360 | }else{ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2361 | rc = SQLITE_IOERR_UNLOCK; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2362 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2363 | } |
| 2364 | return rc; |
| 2365 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2366 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2367 | return SQLITE_OK; |
| 2368 | } |
| 2369 | |
| 2370 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 2371 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2372 | */ |
| 2373 | static int dotlockClose(sqlite3_file *id) { |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2374 | unixFile *pFile = (unixFile*)id; |
| 2375 | assert( id!=0 ); |
| 2376 | dotlockUnlock(id, NO_LOCK); |
| 2377 | sqlite3_free(pFile->lockingContext); |
| 2378 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2379 | } |
| 2380 | /****************** End of the dot-file lock implementation ******************* |
| 2381 | ******************************************************************************/ |
| 2382 | |
| 2383 | /****************************************************************************** |
| 2384 | ************************** Begin flock Locking ******************************** |
| 2385 | ** |
| 2386 | ** Use the flock() system call to do file locking. |
| 2387 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2388 | ** flock() locking is like dot-file locking in that the various |
| 2389 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 2390 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 2391 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 2392 | ** still works when you do this, but concurrency is reduced since |
| 2393 | ** only a single process can be reading the database at a time. |
| 2394 | ** |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 2395 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2396 | */ |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 2397 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2398 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2399 | /* |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2400 | ** Retry flock() calls that fail with EINTR |
| 2401 | */ |
| 2402 | #ifdef EINTR |
| 2403 | static int robust_flock(int fd, int op){ |
| 2404 | int rc; |
| 2405 | do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR ); |
| 2406 | return rc; |
| 2407 | } |
| 2408 | #else |
drh | 5c81927 | 2011-02-23 14:00:12 +0000 | [diff] [blame] | 2409 | # define robust_flock(a,b) flock(a,b) |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2410 | #endif |
| 2411 | |
| 2412 | |
| 2413 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2414 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2415 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2416 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2417 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2418 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2419 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 2420 | int rc = SQLITE_OK; |
| 2421 | int reserved = 0; |
| 2422 | unixFile *pFile = (unixFile*)id; |
| 2423 | |
| 2424 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2425 | |
| 2426 | assert( pFile ); |
| 2427 | |
| 2428 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2429 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2430 | reserved = 1; |
| 2431 | } |
| 2432 | |
| 2433 | /* Otherwise see if some other process holds it. */ |
| 2434 | if( !reserved ){ |
| 2435 | /* attempt to get the lock */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2436 | int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2437 | if( !lrc ){ |
| 2438 | /* got the lock, unlock it */ |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2439 | lrc = robust_flock(pFile->h, LOCK_UN); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2440 | if ( lrc ) { |
| 2441 | int tErrno = errno; |
| 2442 | /* unlock failed with an error */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2443 | lrc = SQLITE_IOERR_UNLOCK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2444 | storeLastErrno(pFile, tErrno); |
| 2445 | rc = lrc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2446 | } |
| 2447 | } else { |
| 2448 | int tErrno = errno; |
| 2449 | reserved = 1; |
| 2450 | /* someone else might have it reserved */ |
| 2451 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2452 | if( IS_LOCK_ERROR(lrc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2453 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2454 | rc = lrc; |
| 2455 | } |
| 2456 | } |
| 2457 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2458 | OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2459 | |
| 2460 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
drh | 2e23381 | 2017-08-22 15:21:54 +0000 | [diff] [blame] | 2461 | if( (rc & 0xff) == SQLITE_IOERR ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2462 | rc = SQLITE_OK; |
| 2463 | reserved=1; |
| 2464 | } |
| 2465 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2466 | *pResOut = reserved; |
| 2467 | return rc; |
| 2468 | } |
| 2469 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2470 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2471 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2472 | ** of the following: |
| 2473 | ** |
| 2474 | ** (1) SHARED_LOCK |
| 2475 | ** (2) RESERVED_LOCK |
| 2476 | ** (3) PENDING_LOCK |
| 2477 | ** (4) EXCLUSIVE_LOCK |
| 2478 | ** |
| 2479 | ** Sometimes when requesting one lock state, additional lock states |
| 2480 | ** are inserted in between. The locking might fail on one of the later |
| 2481 | ** transitions leaving the lock state different from what it started but |
| 2482 | ** still short of its goal. The following chart shows the allowed |
| 2483 | ** transitions and the inserted intermediate states: |
| 2484 | ** |
| 2485 | ** UNLOCKED -> SHARED |
| 2486 | ** SHARED -> RESERVED |
| 2487 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2488 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2489 | ** PENDING -> EXCLUSIVE |
| 2490 | ** |
| 2491 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 2492 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2493 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2494 | ** access the file. |
| 2495 | ** |
| 2496 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2497 | ** routine to lower a locking level. |
| 2498 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2499 | static int flockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2500 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2501 | unixFile *pFile = (unixFile*)id; |
| 2502 | |
| 2503 | assert( pFile ); |
| 2504 | |
| 2505 | /* if we already have a lock, it is exclusive. |
| 2506 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2507 | if (pFile->eFileLock > NO_LOCK) { |
| 2508 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2509 | return SQLITE_OK; |
| 2510 | } |
| 2511 | |
| 2512 | /* grab an exclusive lock */ |
| 2513 | |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 2514 | if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2515 | int tErrno = errno; |
| 2516 | /* didn't get, must be busy */ |
| 2517 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 2518 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2519 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2520 | } |
| 2521 | } else { |
| 2522 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2523 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2524 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2525 | OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), |
| 2526 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2527 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
drh | 2e23381 | 2017-08-22 15:21:54 +0000 | [diff] [blame] | 2528 | if( (rc & 0xff) == SQLITE_IOERR ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2529 | rc = SQLITE_BUSY; |
| 2530 | } |
| 2531 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 2532 | return rc; |
| 2533 | } |
| 2534 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2535 | |
| 2536 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2537 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2538 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2539 | ** |
| 2540 | ** If the locking level of the file descriptor is already at or below |
| 2541 | ** the requested locking level, this routine is a no-op. |
| 2542 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2543 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2544 | unixFile *pFile = (unixFile*)id; |
| 2545 | |
| 2546 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2547 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2548 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2549 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2550 | |
| 2551 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2552 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2553 | return SQLITE_OK; |
| 2554 | } |
| 2555 | |
| 2556 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2557 | if (eFileLock==SHARED_LOCK) { |
| 2558 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2559 | return SQLITE_OK; |
| 2560 | } |
| 2561 | |
| 2562 | /* no, really, unlock. */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2563 | if( robust_flock(pFile->h, LOCK_UN) ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2564 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2565 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2566 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
dan | ea83bc6 | 2011-04-01 11:56:32 +0000 | [diff] [blame] | 2567 | return SQLITE_IOERR_UNLOCK; |
| 2568 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2569 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2570 | return SQLITE_OK; |
| 2571 | } |
| 2572 | } |
| 2573 | |
| 2574 | /* |
| 2575 | ** Close a file. |
| 2576 | */ |
| 2577 | static int flockClose(sqlite3_file *id) { |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 2578 | assert( id!=0 ); |
| 2579 | flockUnlock(id, NO_LOCK); |
| 2580 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
| 2583 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 2584 | |
| 2585 | /******************* End of the flock lock implementation ********************* |
| 2586 | ******************************************************************************/ |
| 2587 | |
| 2588 | /****************************************************************************** |
| 2589 | ************************ Begin Named Semaphore Locking ************************ |
| 2590 | ** |
| 2591 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2592 | ** |
| 2593 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 2594 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 2595 | ** the database file at a time. This reduces potential concurrency, but |
| 2596 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2597 | */ |
| 2598 | #if OS_VXWORKS |
| 2599 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2600 | /* |
| 2601 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2602 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2603 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2604 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2605 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2606 | static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2607 | int rc = SQLITE_OK; |
| 2608 | int reserved = 0; |
| 2609 | unixFile *pFile = (unixFile*)id; |
| 2610 | |
| 2611 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2612 | |
| 2613 | assert( pFile ); |
| 2614 | |
| 2615 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2616 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2617 | reserved = 1; |
| 2618 | } |
| 2619 | |
| 2620 | /* Otherwise see if some other process holds it. */ |
| 2621 | if( !reserved ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2622 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2623 | |
| 2624 | if( sem_trywait(pSem)==-1 ){ |
| 2625 | int tErrno = errno; |
| 2626 | if( EAGAIN != tErrno ){ |
| 2627 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2628 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2629 | } else { |
| 2630 | /* someone else has the lock when we are in NO_LOCK */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2631 | reserved = (pFile->eFileLock < SHARED_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2632 | } |
| 2633 | }else{ |
| 2634 | /* we could have it if we want it */ |
| 2635 | sem_post(pSem); |
| 2636 | } |
| 2637 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2638 | OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2639 | |
| 2640 | *pResOut = reserved; |
| 2641 | return rc; |
| 2642 | } |
| 2643 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2644 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2645 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2646 | ** of the following: |
| 2647 | ** |
| 2648 | ** (1) SHARED_LOCK |
| 2649 | ** (2) RESERVED_LOCK |
| 2650 | ** (3) PENDING_LOCK |
| 2651 | ** (4) EXCLUSIVE_LOCK |
| 2652 | ** |
| 2653 | ** Sometimes when requesting one lock state, additional lock states |
| 2654 | ** are inserted in between. The locking might fail on one of the later |
| 2655 | ** transitions leaving the lock state different from what it started but |
| 2656 | ** still short of its goal. The following chart shows the allowed |
| 2657 | ** transitions and the inserted intermediate states: |
| 2658 | ** |
| 2659 | ** UNLOCKED -> SHARED |
| 2660 | ** SHARED -> RESERVED |
| 2661 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2662 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2663 | ** PENDING -> EXCLUSIVE |
| 2664 | ** |
| 2665 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 2666 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 2667 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 2668 | ** access the file. |
| 2669 | ** |
| 2670 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2671 | ** routine to lower a locking level. |
| 2672 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2673 | static int semXLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2674 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2675 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2676 | int rc = SQLITE_OK; |
| 2677 | |
| 2678 | /* if we already have a lock, it is exclusive. |
| 2679 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2680 | if (pFile->eFileLock > NO_LOCK) { |
| 2681 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2682 | rc = SQLITE_OK; |
| 2683 | goto sem_end_lock; |
| 2684 | } |
| 2685 | |
| 2686 | /* lock semaphore now but bail out when already locked. */ |
| 2687 | if( sem_trywait(pSem)==-1 ){ |
| 2688 | rc = SQLITE_BUSY; |
| 2689 | goto sem_end_lock; |
| 2690 | } |
| 2691 | |
| 2692 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2693 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2694 | |
| 2695 | sem_end_lock: |
| 2696 | return rc; |
| 2697 | } |
| 2698 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2699 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2700 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2701 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2702 | ** |
| 2703 | ** If the locking level of the file descriptor is already at or below |
| 2704 | ** the requested locking level, this routine is a no-op. |
| 2705 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2706 | static int semXUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2707 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2708 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2709 | |
| 2710 | assert( pFile ); |
| 2711 | assert( pSem ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2712 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2713 | pFile->eFileLock, osGetpid(0))); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2714 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2715 | |
| 2716 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2717 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2718 | return SQLITE_OK; |
| 2719 | } |
| 2720 | |
| 2721 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2722 | if (eFileLock==SHARED_LOCK) { |
| 2723 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2724 | return SQLITE_OK; |
| 2725 | } |
| 2726 | |
| 2727 | /* no, really unlock. */ |
| 2728 | if ( sem_post(pSem)==-1 ) { |
| 2729 | int rc, tErrno = errno; |
| 2730 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2731 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2732 | storeLastErrno(pFile, tErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2733 | } |
| 2734 | return rc; |
| 2735 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2736 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2737 | return SQLITE_OK; |
| 2738 | } |
| 2739 | |
| 2740 | /* |
| 2741 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2742 | */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2743 | static int semXClose(sqlite3_file *id) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2744 | if( id ){ |
| 2745 | unixFile *pFile = (unixFile*)id; |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 2746 | semXUnlock(id, NO_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2747 | assert( pFile ); |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 2748 | assert( unixFileMutexNotheld(pFile) ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2749 | unixEnterMutex(); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2750 | releaseInodeInfo(pFile); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2751 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2752 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2753 | } |
| 2754 | return SQLITE_OK; |
| 2755 | } |
| 2756 | |
| 2757 | #endif /* OS_VXWORKS */ |
| 2758 | /* |
| 2759 | ** Named semaphore locking is only available on VxWorks. |
| 2760 | ** |
| 2761 | *************** End of the named semaphore lock implementation **************** |
| 2762 | ******************************************************************************/ |
| 2763 | |
| 2764 | |
| 2765 | /****************************************************************************** |
| 2766 | *************************** Begin AFP Locking ********************************* |
| 2767 | ** |
| 2768 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2769 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2770 | ** |
| 2771 | ** Third-party implementations of AFP are available. But this code here |
| 2772 | ** only works on OSX. |
| 2773 | */ |
| 2774 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2775 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2776 | /* |
| 2777 | ** The afpLockingContext structure contains all afp lock specific state |
| 2778 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2779 | typedef struct afpLockingContext afpLockingContext; |
| 2780 | struct afpLockingContext { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2781 | int reserved; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2782 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2783 | }; |
| 2784 | |
| 2785 | struct ByteRangeLockPB2 |
| 2786 | { |
| 2787 | unsigned long long offset; /* offset to first byte to lock */ |
| 2788 | unsigned long long length; /* nbr of bytes to lock */ |
| 2789 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2790 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2791 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2792 | int fd; /* file desc to assoc this lock with */ |
| 2793 | }; |
| 2794 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2795 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2796 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2797 | /* |
| 2798 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2799 | ** AFP filesystem. |
| 2800 | ** |
| 2801 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2802 | */ |
| 2803 | static int afpSetLock( |
| 2804 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2805 | unixFile *pFile, /* Open file descriptor on path */ |
| 2806 | unsigned long long offset, /* First byte to be locked */ |
| 2807 | unsigned long long length, /* Number of bytes to lock */ |
| 2808 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2809 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2810 | struct ByteRangeLockPB2 pb; |
| 2811 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2812 | |
| 2813 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2814 | pb.startEndFlag = 0; |
| 2815 | pb.offset = offset; |
| 2816 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2817 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2818 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2819 | OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2820 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2821 | offset, length)); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2822 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2823 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2824 | int rc; |
| 2825 | int tErrno = errno; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2826 | OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2827 | path, tErrno, strerror(tErrno))); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2828 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2829 | rc = SQLITE_BUSY; |
| 2830 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2831 | rc = sqliteErrorFromPosixError(tErrno, |
| 2832 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2833 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2834 | if( IS_LOCK_ERROR(rc) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 2835 | storeLastErrno(pFile, tErrno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2836 | } |
| 2837 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2838 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2839 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2840 | } |
| 2841 | } |
| 2842 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2843 | /* |
| 2844 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2845 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2846 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2847 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2848 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2849 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2850 | int rc = SQLITE_OK; |
| 2851 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2852 | unixFile *pFile = (unixFile*)id; |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2853 | afpLockingContext *context; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2854 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2855 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2856 | |
| 2857 | assert( pFile ); |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2858 | context = (afpLockingContext *) pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2859 | if( context->reserved ){ |
| 2860 | *pResOut = 1; |
| 2861 | return SQLITE_OK; |
| 2862 | } |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 2863 | sqlite3_mutex_enter(pFile->pInode->pLockMutex); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2864 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2865 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2866 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
| 2869 | /* Otherwise see if some other process holds it. |
| 2870 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2871 | if( !reserved ){ |
| 2872 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2873 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2874 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2875 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2876 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2877 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2878 | } else { |
| 2879 | /* if we failed to get the lock then someone else must have it */ |
| 2880 | reserved = 1; |
| 2881 | } |
| 2882 | if( IS_LOCK_ERROR(lrc) ){ |
| 2883 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2884 | } |
| 2885 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2886 | |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 2887 | sqlite3_mutex_leave(pFile->pInode->pLockMutex); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2888 | OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2889 | |
| 2890 | *pResOut = reserved; |
| 2891 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2894 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2895 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2896 | ** of the following: |
| 2897 | ** |
| 2898 | ** (1) SHARED_LOCK |
| 2899 | ** (2) RESERVED_LOCK |
| 2900 | ** (3) PENDING_LOCK |
| 2901 | ** (4) EXCLUSIVE_LOCK |
| 2902 | ** |
| 2903 | ** Sometimes when requesting one lock state, additional lock states |
| 2904 | ** are inserted in between. The locking might fail on one of the later |
| 2905 | ** transitions leaving the lock state different from what it started but |
| 2906 | ** still short of its goal. The following chart shows the allowed |
| 2907 | ** transitions and the inserted intermediate states: |
| 2908 | ** |
| 2909 | ** UNLOCKED -> SHARED |
| 2910 | ** SHARED -> RESERVED |
| 2911 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2912 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2913 | ** PENDING -> EXCLUSIVE |
| 2914 | ** |
| 2915 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2916 | ** routine to lower a locking level. |
| 2917 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2918 | static int afpLock(sqlite3_file *id, int eFileLock){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2919 | int rc = SQLITE_OK; |
| 2920 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2921 | unixInodeInfo *pInode = pFile->pInode; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2922 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2923 | |
| 2924 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2925 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 2926 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 2927 | azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0))); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2928 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2929 | /* 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] | 2930 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2931 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2932 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2933 | if( pFile->eFileLock>=eFileLock ){ |
| 2934 | OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
| 2935 | azFileLock(eFileLock))); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2936 | return SQLITE_OK; |
| 2937 | } |
| 2938 | |
| 2939 | /* Make sure the locking sequence is correct |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2940 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 2941 | ** (2) SQLite never explicitly requests a pendig lock. |
| 2942 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2943 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2944 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 2945 | assert( eFileLock!=PENDING_LOCK ); |
| 2946 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2947 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2948 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2949 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2950 | pInode = pFile->pInode; |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 2951 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2952 | |
| 2953 | /* If some thread using this PID has a lock via a different unixFile* |
| 2954 | ** handle that precludes the requested lock, return BUSY. |
| 2955 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2956 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 2957 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2958 | ){ |
| 2959 | rc = SQLITE_BUSY; |
| 2960 | goto afp_end_lock; |
| 2961 | } |
| 2962 | |
| 2963 | /* If a SHARED lock is requested, and some thread using this PID already |
| 2964 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 2965 | ** return SQLITE_OK. |
| 2966 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2967 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2968 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2969 | assert( eFileLock==SHARED_LOCK ); |
| 2970 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2971 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2972 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2973 | pInode->nShared++; |
| 2974 | pInode->nLock++; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2975 | goto afp_end_lock; |
| 2976 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2977 | |
| 2978 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2979 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2980 | ** be released. |
| 2981 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2982 | if( eFileLock==SHARED_LOCK |
| 2983 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2984 | ){ |
| 2985 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2986 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2987 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2988 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2989 | goto afp_end_lock; |
| 2990 | } |
| 2991 | } |
| 2992 | |
| 2993 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2994 | ** operating system calls for the specified lock. |
| 2995 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2996 | if( eFileLock==SHARED_LOCK ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 2997 | int lrc1, lrc2, lrc1Errno = 0; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2998 | long lk, mask; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2999 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3000 | assert( pInode->nShared==0 ); |
| 3001 | assert( pInode->eFileLock==0 ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3002 | |
| 3003 | mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3004 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3005 | /* note that the quality of the randomness doesn't matter that much */ |
| 3006 | lk = random(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3007 | pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3008 | lrc1 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3009 | SHARED_FIRST+pInode->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3010 | if( IS_LOCK_ERROR(lrc1) ){ |
| 3011 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3012 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3013 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3014 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3015 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3016 | if( IS_LOCK_ERROR(lrc1) ) { |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3017 | storeLastErrno(pFile, lrc1Errno); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3018 | rc = lrc1; |
| 3019 | goto afp_end_lock; |
| 3020 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 3021 | rc = lrc2; |
| 3022 | goto afp_end_lock; |
| 3023 | } else if( lrc1 != SQLITE_OK ) { |
| 3024 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3025 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3026 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3027 | pInode->nLock++; |
| 3028 | pInode->nShared = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3029 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3030 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3031 | /* We are trying for an exclusive lock but another thread in this |
| 3032 | ** same process is still holding a shared lock. */ |
| 3033 | rc = SQLITE_BUSY; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3034 | }else{ |
| 3035 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 3036 | ** assumed that there is a SHARED or greater lock on the file |
| 3037 | ** already. |
| 3038 | */ |
| 3039 | int failed = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3040 | assert( 0!=pFile->eFileLock ); |
| 3041 | if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3042 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3043 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3044 | if( !failed ){ |
| 3045 | context->reserved = 1; |
| 3046 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3047 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3048 | if (!failed && eFileLock == EXCLUSIVE_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3049 | /* Acquire an EXCLUSIVE lock */ |
| 3050 | |
| 3051 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3052 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3053 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3054 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3055 | pInode->sharedByte, 1, 0)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3056 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3057 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3058 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3059 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3060 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3061 | SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3062 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 3063 | ** a critical I/O error |
| 3064 | */ |
drh | 2e23381 | 2017-08-22 15:21:54 +0000 | [diff] [blame] | 3065 | rc = ((failed & 0xff) == SQLITE_IOERR) ? failed2 : |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3066 | SQLITE_IOERR_LOCK; |
| 3067 | goto afp_end_lock; |
| 3068 | } |
| 3069 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3070 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3071 | } |
| 3072 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3073 | if( failed ){ |
| 3074 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3075 | } |
| 3076 | } |
| 3077 | |
| 3078 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3079 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3080 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3081 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 3082 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3083 | pInode->eFileLock = PENDING_LOCK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3084 | } |
| 3085 | |
| 3086 | afp_end_lock: |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 3087 | sqlite3_mutex_leave(pInode->pLockMutex); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3088 | OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), |
| 3089 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3090 | return rc; |
| 3091 | } |
| 3092 | |
| 3093 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3094 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3095 | ** must be either NO_LOCK or SHARED_LOCK. |
| 3096 | ** |
| 3097 | ** If the locking level of the file descriptor is already at or below |
| 3098 | ** the requested locking level, this routine is a no-op. |
| 3099 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3100 | static int afpUnlock(sqlite3_file *id, int eFileLock) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3101 | int rc = SQLITE_OK; |
| 3102 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3103 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3104 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 3105 | int skipShared = 0; |
| 3106 | #ifdef SQLITE_TEST |
| 3107 | int h = pFile->h; |
| 3108 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3109 | |
| 3110 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3111 | 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] | 3112 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 3113 | osGetpid(0))); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 3114 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3115 | assert( eFileLock<=SHARED_LOCK ); |
| 3116 | if( pFile->eFileLock<=eFileLock ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3117 | return SQLITE_OK; |
| 3118 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3119 | pInode = pFile->pInode; |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 3120 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3121 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3122 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3123 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3124 | SimulateIOErrorBenign(1); |
| 3125 | SimulateIOError( h=(-1) ) |
| 3126 | SimulateIOErrorBenign(0); |
| 3127 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3128 | #ifdef SQLITE_DEBUG |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3129 | /* When reducing a lock such that other processes can start |
| 3130 | ** reading the database file again, make sure that the |
| 3131 | ** transaction counter was updated if any part of the database |
| 3132 | ** file changed. If the transaction counter is not updated, |
| 3133 | ** other connections to the same file might not realize that |
| 3134 | ** the file has changed and hence might not know to flush their |
| 3135 | ** cache. The use of a stale cache can lead to database corruption. |
| 3136 | */ |
| 3137 | assert( pFile->inNormalWrite==0 |
| 3138 | || pFile->dbUpdate==0 |
| 3139 | || pFile->transCntrChng==1 ); |
| 3140 | pFile->inNormalWrite = 0; |
| 3141 | #endif |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3142 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3143 | if( pFile->eFileLock==EXCLUSIVE_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3144 | rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3145 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3146 | /* only re-establish the shared lock if necessary */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3147 | int sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3148 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 3149 | } else { |
| 3150 | skipShared = 1; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3151 | } |
| 3152 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3153 | if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3154 | rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3155 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3156 | if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3157 | rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 3158 | if( !rc ){ |
| 3159 | context->reserved = 0; |
| 3160 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3161 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3162 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ |
| 3163 | pInode->eFileLock = SHARED_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3164 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3165 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3166 | if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3167 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3168 | /* Decrement the shared lock counter. Release the lock using an |
| 3169 | ** OS call only when all threads in this same process have released |
| 3170 | ** the lock. |
| 3171 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3172 | unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 3173 | pInode->nShared--; |
| 3174 | if( pInode->nShared==0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3175 | SimulateIOErrorBenign(1); |
| 3176 | SimulateIOError( h=(-1) ) |
| 3177 | SimulateIOErrorBenign(0); |
| 3178 | if( !skipShared ){ |
| 3179 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 3180 | } |
| 3181 | if( !rc ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3182 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3183 | pFile->eFileLock = NO_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3184 | } |
| 3185 | } |
| 3186 | if( rc==SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3187 | pInode->nLock--; |
| 3188 | assert( pInode->nLock>=0 ); |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 3189 | if( pInode->nLock==0 ) closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3190 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3191 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3192 | |
drh | da6dc24 | 2018-07-23 21:10:37 +0000 | [diff] [blame] | 3193 | sqlite3_mutex_leave(pInode->pLockMutex); |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 3194 | if( rc==SQLITE_OK ){ |
| 3195 | pFile->eFileLock = eFileLock; |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 3196 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3197 | return rc; |
| 3198 | } |
| 3199 | |
| 3200 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3201 | ** Close a file & cleanup AFP specific locking context |
| 3202 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3203 | static int afpClose(sqlite3_file *id) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3204 | int rc = SQLITE_OK; |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 3205 | unixFile *pFile = (unixFile*)id; |
| 3206 | assert( id!=0 ); |
| 3207 | afpUnlock(id, NO_LOCK); |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 3208 | assert( unixFileMutexNotheld(pFile) ); |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 3209 | unixEnterMutex(); |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 3210 | if( pFile->pInode ){ |
| 3211 | unixInodeInfo *pInode = pFile->pInode; |
| 3212 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | cb4e4b0 | 2018-09-06 19:36:29 +0000 | [diff] [blame] | 3213 | if( pInode->nLock ){ |
drh | ef52b36 | 2018-08-13 22:50:34 +0000 | [diff] [blame] | 3214 | /* If there are outstanding locks, do not actually close the file just |
| 3215 | ** yet because that would clear those locks. Instead, add the file |
| 3216 | ** descriptor to pInode->aPending. It will be automatically closed when |
| 3217 | ** the last lock is cleared. |
| 3218 | */ |
| 3219 | setPendingFd(pFile); |
| 3220 | } |
| 3221 | sqlite3_mutex_leave(pInode->pLockMutex); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 3222 | } |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 3223 | releaseInodeInfo(pFile); |
| 3224 | sqlite3_free(pFile->lockingContext); |
| 3225 | rc = closeUnixFile(id); |
| 3226 | unixLeaveMutex(); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3227 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3228 | } |
| 3229 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3230 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3231 | /* |
| 3232 | ** The code above is the AFP lock implementation. The code is specific |
| 3233 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 3234 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 3235 | ** VFS is not available. |
| 3236 | ** |
| 3237 | ********************* End of the AFP lock implementation ********************** |
| 3238 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3239 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3240 | /****************************************************************************** |
| 3241 | *************************** Begin NFS Locking ********************************/ |
| 3242 | |
| 3243 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 3244 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3245 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3246 | ** must be either NO_LOCK or SHARED_LOCK. |
| 3247 | ** |
| 3248 | ** If the locking level of the file descriptor is already at or below |
| 3249 | ** the requested locking level, this routine is a no-op. |
| 3250 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3251 | static int nfsUnlock(sqlite3_file *id, int eFileLock){ |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 3252 | return posixUnlock(id, eFileLock, 1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 3256 | /* |
| 3257 | ** The code above is the NFS lock implementation. The code is specific |
| 3258 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 3259 | ** is available. |
| 3260 | ** |
| 3261 | ********************* End of the NFS lock implementation ********************** |
| 3262 | ******************************************************************************/ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3263 | |
| 3264 | /****************************************************************************** |
| 3265 | **************** Non-locking sqlite3_file methods ***************************** |
| 3266 | ** |
| 3267 | ** The next division contains implementations for all methods of the |
| 3268 | ** sqlite3_file object other than the locking methods. The locking |
| 3269 | ** methods were defined in divisions above (one locking method per |
| 3270 | ** division). Those methods that are common to all locking modes |
| 3271 | ** are gather together into this division. |
| 3272 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3273 | |
| 3274 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3275 | ** Seek to the offset passed as the second argument, then read cnt |
| 3276 | ** bytes into pBuf. Return the number of bytes actually read. |
| 3277 | ** |
| 3278 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 3279 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 3280 | ** one system to another. Since SQLite does not define USE_PREAD |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3281 | ** in any form by default, we will not attempt to define _XOPEN_SOURCE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3282 | ** See tickets #2741 and #2681. |
| 3283 | ** |
| 3284 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 3285 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3286 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3287 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 3288 | int got; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3289 | int prior = 0; |
drh | a46cadc | 2016-03-04 03:02:06 +0000 | [diff] [blame] | 3290 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
| 3291 | i64 newOffset; |
| 3292 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3293 | TIMER_START; |
drh | c1fd2cf | 2012-10-01 12:16:26 +0000 | [diff] [blame] | 3294 | assert( cnt==(cnt&0x1ffff) ); |
drh | 35a0379 | 2013-08-29 23:34:53 +0000 | [diff] [blame] | 3295 | assert( id->h>2 ); |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3296 | do{ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3297 | #if defined(USE_PREAD) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3298 | got = osPread(id->h, pBuf, cnt, offset); |
| 3299 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3300 | #elif defined(USE_PREAD64) |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3301 | got = osPread64(id->h, pBuf, cnt, offset); |
| 3302 | SimulateIOError( got = -1 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3303 | #else |
drh | a46cadc | 2016-03-04 03:02:06 +0000 | [diff] [blame] | 3304 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 3305 | SimulateIOError( newOffset = -1 ); |
| 3306 | if( newOffset<0 ){ |
| 3307 | storeLastErrno((unixFile*)id, errno); |
| 3308 | return -1; |
| 3309 | } |
| 3310 | got = osRead(id->h, pBuf, cnt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3311 | #endif |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3312 | if( got==cnt ) break; |
| 3313 | if( got<0 ){ |
| 3314 | if( errno==EINTR ){ got = 1; continue; } |
| 3315 | prior = 0; |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3316 | storeLastErrno((unixFile*)id, errno); |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3317 | break; |
| 3318 | }else if( got>0 ){ |
| 3319 | cnt -= got; |
| 3320 | offset += got; |
| 3321 | prior += got; |
| 3322 | pBuf = (void*)(got + (char*)pBuf); |
| 3323 | } |
| 3324 | }while( got>0 ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3325 | TIMER_END; |
drh | 5802464 | 2011-11-07 18:16:00 +0000 | [diff] [blame] | 3326 | OSTRACE(("READ %-3d %5d %7lld %llu\n", |
| 3327 | id->h, got+prior, offset-prior, TIMER_ELAPSED)); |
| 3328 | return got+prior; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3329 | } |
| 3330 | |
| 3331 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3332 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 3333 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 3334 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 3335 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3336 | static int unixRead( |
| 3337 | sqlite3_file *id, |
| 3338 | void *pBuf, |
| 3339 | int amt, |
| 3340 | sqlite3_int64 offset |
| 3341 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3342 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3343 | int got; |
| 3344 | assert( id ); |
drh | 6cf9d8d | 2013-05-09 18:12:40 +0000 | [diff] [blame] | 3345 | assert( offset>=0 ); |
| 3346 | assert( amt>0 ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3347 | |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 3348 | /* If this is a database file (not a journal, super-journal or temp |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3349 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3350 | #if 0 |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 3351 | assert( pFile->pPreallocatedUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3352 | || offset>=PENDING_BYTE+512 |
| 3353 | || offset+amt<=PENDING_BYTE |
| 3354 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3355 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3356 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3357 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 6c56963 | 2013-03-26 18:48:11 +0000 | [diff] [blame] | 3358 | /* Deal with as much of this read request as possible by transfering |
| 3359 | ** data from the memory mapping using memcpy(). */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3360 | if( offset<pFile->mmapSize ){ |
| 3361 | if( offset+amt <= pFile->mmapSize ){ |
| 3362 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt); |
| 3363 | return SQLITE_OK; |
| 3364 | }else{ |
| 3365 | int nCopy = pFile->mmapSize - offset; |
| 3366 | memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy); |
| 3367 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3368 | amt -= nCopy; |
| 3369 | offset += nCopy; |
| 3370 | } |
| 3371 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 3372 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3373 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3374 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3375 | if( got==amt ){ |
| 3376 | return SQLITE_OK; |
| 3377 | }else if( got<0 ){ |
| 3378 | /* lastErrno set by seekAndRead */ |
| 3379 | return SQLITE_IOERR_READ; |
| 3380 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3381 | storeLastErrno(pFile, 0); /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3382 | /* Unread parts of the buffer must be zero-filled */ |
| 3383 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 3384 | return SQLITE_IOERR_SHORT_READ; |
| 3385 | } |
| 3386 | } |
| 3387 | |
| 3388 | /* |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3389 | ** Attempt to seek the file-descriptor passed as the first argument to |
| 3390 | ** absolute offset iOff, then attempt to write nBuf bytes of data from |
| 3391 | ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise, |
| 3392 | ** return the actual number of bytes written (which may be less than |
| 3393 | ** nBuf). |
| 3394 | */ |
| 3395 | static int seekAndWriteFd( |
| 3396 | int fd, /* File descriptor to write to */ |
| 3397 | i64 iOff, /* File offset to begin writing at */ |
| 3398 | const void *pBuf, /* Copy data from this buffer to the file */ |
| 3399 | int nBuf, /* Size of buffer pBuf in bytes */ |
| 3400 | int *piErrno /* OUT: Error number if error occurs */ |
| 3401 | ){ |
| 3402 | int rc = 0; /* Value returned by system call */ |
| 3403 | |
| 3404 | assert( nBuf==(nBuf&0x1ffff) ); |
drh | 35a0379 | 2013-08-29 23:34:53 +0000 | [diff] [blame] | 3405 | assert( fd>2 ); |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3406 | assert( piErrno!=0 ); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3407 | nBuf &= 0x1ffff; |
| 3408 | TIMER_START; |
| 3409 | |
| 3410 | #if defined(USE_PREAD) |
drh | 2da47d3 | 2015-02-21 00:56:05 +0000 | [diff] [blame] | 3411 | do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR ); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3412 | #elif defined(USE_PREAD64) |
drh | 2da47d3 | 2015-02-21 00:56:05 +0000 | [diff] [blame] | 3413 | do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR); |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3414 | #else |
| 3415 | do{ |
| 3416 | i64 iSeek = lseek(fd, iOff, SEEK_SET); |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3417 | SimulateIOError( iSeek = -1 ); |
| 3418 | if( iSeek<0 ){ |
| 3419 | rc = -1; |
| 3420 | break; |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3421 | } |
| 3422 | rc = osWrite(fd, pBuf, nBuf); |
| 3423 | }while( rc<0 && errno==EINTR ); |
| 3424 | #endif |
| 3425 | |
| 3426 | TIMER_END; |
| 3427 | OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED)); |
| 3428 | |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 3429 | if( rc<0 ) *piErrno = errno; |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3430 | return rc; |
| 3431 | } |
| 3432 | |
| 3433 | |
| 3434 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3435 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 3436 | ** Return the number of bytes actually read. Update the offset. |
| 3437 | ** |
| 3438 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 3439 | ** is set before returning. |
| 3440 | */ |
| 3441 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 3442 | return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | |
| 3446 | /* |
| 3447 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 3448 | ** or some other error code on failure. |
| 3449 | */ |
| 3450 | static int unixWrite( |
| 3451 | sqlite3_file *id, |
| 3452 | const void *pBuf, |
| 3453 | int amt, |
| 3454 | sqlite3_int64 offset |
| 3455 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3456 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3457 | int wrote = 0; |
| 3458 | assert( id ); |
| 3459 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3460 | |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 3461 | /* If this is a database file (not a journal, super-journal or temp |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3462 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3463 | #if 0 |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 3464 | assert( pFile->pPreallocatedUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3465 | || offset>=PENDING_BYTE+512 |
| 3466 | || offset+amt<=PENDING_BYTE |
| 3467 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 3468 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 3469 | |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3470 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3471 | /* If we are doing a normal write to a database file (as opposed to |
| 3472 | ** doing a hot-journal rollback or a write to some file other than a |
| 3473 | ** normal database file) then record the fact that the database |
| 3474 | ** has changed. If the transaction counter is modified, record that |
| 3475 | ** fact too. |
| 3476 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 3477 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3478 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 3479 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3480 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3481 | char oldCntr[4]; |
| 3482 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3483 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3484 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 3485 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3486 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 3487 | } |
| 3488 | } |
| 3489 | } |
| 3490 | #endif |
| 3491 | |
dan | fe33e39 | 2015-11-17 20:56:06 +0000 | [diff] [blame] | 3492 | #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3493 | /* Deal with as much of this write request as possible by transfering |
| 3494 | ** data from the memory mapping using memcpy(). */ |
| 3495 | if( offset<pFile->mmapSize ){ |
| 3496 | if( offset+amt <= pFile->mmapSize ){ |
| 3497 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt); |
| 3498 | return SQLITE_OK; |
| 3499 | }else{ |
| 3500 | int nCopy = pFile->mmapSize - offset; |
| 3501 | memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy); |
| 3502 | pBuf = &((u8 *)pBuf)[nCopy]; |
| 3503 | amt -= nCopy; |
| 3504 | offset += nCopy; |
| 3505 | } |
| 3506 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 3507 | #endif |
drh | 02bf8b4 | 2015-09-01 23:51:53 +0000 | [diff] [blame] | 3508 | |
| 3509 | while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3510 | amt -= wrote; |
| 3511 | offset += wrote; |
| 3512 | pBuf = &((char*)pBuf)[wrote]; |
| 3513 | } |
| 3514 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 3515 | SimulateDiskfullError(( wrote=0, amt=1 )); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3516 | |
drh | 02bf8b4 | 2015-09-01 23:51:53 +0000 | [diff] [blame] | 3517 | if( amt>wrote ){ |
drh | a21b83b | 2011-04-15 12:36:10 +0000 | [diff] [blame] | 3518 | if( wrote<0 && pFile->lastErrno!=ENOSPC ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3519 | /* lastErrno set by seekAndWrite */ |
| 3520 | return SQLITE_IOERR_WRITE; |
| 3521 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3522 | storeLastErrno(pFile, 0); /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3523 | return SQLITE_FULL; |
| 3524 | } |
| 3525 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3526 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3527 | return SQLITE_OK; |
| 3528 | } |
| 3529 | |
| 3530 | #ifdef SQLITE_TEST |
| 3531 | /* |
| 3532 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3533 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3534 | */ |
| 3535 | int sqlite3_sync_count = 0; |
| 3536 | int sqlite3_fullsync_count = 0; |
| 3537 | #endif |
| 3538 | |
| 3539 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 3540 | ** We do not trust systems to provide a working fdatasync(). Some do. |
drh | 20f8e13 | 2011-08-31 21:01:55 +0000 | [diff] [blame] | 3541 | ** Others do no. To be safe, we will stick with the (slightly slower) |
| 3542 | ** fsync(). If you know that your system does support fdatasync() correctly, |
drh | f7a4a1b | 2015-01-10 18:02:45 +0000 | [diff] [blame] | 3543 | ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3544 | */ |
drh | f7a4a1b | 2015-01-10 18:02:45 +0000 | [diff] [blame] | 3545 | #if !defined(fdatasync) && !HAVE_FDATASYNC |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3546 | # define fdatasync fsync |
| 3547 | #endif |
| 3548 | |
| 3549 | /* |
| 3550 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 3551 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 3552 | ** only available on Mac OS X. But that could change. |
| 3553 | */ |
| 3554 | #ifdef F_FULLFSYNC |
| 3555 | # define HAVE_FULLFSYNC 1 |
| 3556 | #else |
| 3557 | # define HAVE_FULLFSYNC 0 |
| 3558 | #endif |
| 3559 | |
| 3560 | |
| 3561 | /* |
| 3562 | ** The fsync() system call does not work as advertised on many |
| 3563 | ** unix systems. The following procedure is an attempt to make |
| 3564 | ** it work better. |
| 3565 | ** |
| 3566 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 3567 | ** for testing when we want to run through the test suite quickly. |
| 3568 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 3569 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 3570 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3571 | ** |
| 3572 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 3573 | ** The idea behind dataOnly is that it should only write the file content |
| 3574 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 3575 | ** unchanged since the file size is part of the inode. However, |
| 3576 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 3577 | ** file size has changed. The only real difference between fdatasync() |
| 3578 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 3579 | ** inode if the mtime or owner or other inode attributes have changed. |
| 3580 | ** We only care about the file size, not the other file attributes, so |
| 3581 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 3582 | ** So, we always use fdatasync() if it is available, regardless of |
| 3583 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3584 | */ |
| 3585 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3586 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3587 | |
| 3588 | /* The following "ifdef/elif/else/" block has the same structure as |
| 3589 | ** the one below. It is replicated here solely to avoid cluttering |
| 3590 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 3591 | */ |
| 3592 | #ifdef SQLITE_NO_SYNC |
| 3593 | UNUSED_PARAMETER(fd); |
| 3594 | UNUSED_PARAMETER(fullSync); |
| 3595 | UNUSED_PARAMETER(dataOnly); |
| 3596 | #elif HAVE_FULLFSYNC |
| 3597 | UNUSED_PARAMETER(dataOnly); |
| 3598 | #else |
| 3599 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3600 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3601 | #endif |
| 3602 | |
| 3603 | /* Record the number of times that we do a normal fsync() and |
| 3604 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 3605 | ** gets called with the correct arguments. |
| 3606 | */ |
| 3607 | #ifdef SQLITE_TEST |
| 3608 | if( fullSync ) sqlite3_fullsync_count++; |
| 3609 | sqlite3_sync_count++; |
| 3610 | #endif |
| 3611 | |
| 3612 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
drh | 2c8fd12 | 2015-12-02 02:33:36 +0000 | [diff] [blame] | 3613 | ** no-op. But go ahead and call fstat() to validate the file |
| 3614 | ** descriptor as we need a method to provoke a failure during |
| 3615 | ** coverate testing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3616 | */ |
| 3617 | #ifdef SQLITE_NO_SYNC |
drh | 2c8fd12 | 2015-12-02 02:33:36 +0000 | [diff] [blame] | 3618 | { |
| 3619 | struct stat buf; |
| 3620 | rc = osFstat(fd, &buf); |
| 3621 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3622 | #elif HAVE_FULLFSYNC |
| 3623 | if( fullSync ){ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 3624 | rc = osFcntl(fd, F_FULLFSYNC, 0); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3625 | }else{ |
| 3626 | rc = 1; |
| 3627 | } |
| 3628 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3629 | ** It shouldn't be possible for fullfsync to fail on the local |
| 3630 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 3631 | ** isn't supported for this file system. So, attempt an fsync |
| 3632 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 3633 | ** It'd be better to detect fullfsync support once and avoid |
| 3634 | ** the fcntl call every time sync is called. |
| 3635 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3636 | if( rc ) rc = fsync(fd); |
| 3637 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3638 | #elif defined(__APPLE__) |
| 3639 | /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 3640 | ** so currently we default to the macro that redefines fdatasync to fsync |
| 3641 | */ |
| 3642 | rc = fsync(fd); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3643 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3644 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 3645 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3646 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3647 | rc = fsync(fd); |
| 3648 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 3649 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3650 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 3651 | |
| 3652 | if( OS_VXWORKS && rc!= -1 ){ |
| 3653 | rc = 0; |
| 3654 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3655 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 3656 | } |
| 3657 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3658 | /* |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3659 | ** Open a file descriptor to the directory containing file zFilename. |
| 3660 | ** If successful, *pFd is set to the opened file descriptor and |
| 3661 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 3662 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 3663 | ** value. |
| 3664 | ** |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3665 | ** The directory file descriptor is used for only one thing - to |
| 3666 | ** fsync() a directory to make sure file creation and deletion events |
| 3667 | ** are flushed to disk. Such fsyncs are not needed on newer |
| 3668 | ** journaling filesystems, but are required on older filesystems. |
| 3669 | ** |
| 3670 | ** This routine can be overridden using the xSetSysCall interface. |
| 3671 | ** The ability to override this routine was added in support of the |
| 3672 | ** chromium sandbox. Opening a directory is a security risk (we are |
| 3673 | ** told) so making it overrideable allows the chromium sandbox to |
| 3674 | ** replace this routine with a harmless no-op. To make this routine |
| 3675 | ** a no-op, replace it with a stub that returns SQLITE_OK but leaves |
| 3676 | ** *pFd set to a negative number. |
| 3677 | ** |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3678 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 3679 | ** the file descriptor *pFd using close(). |
| 3680 | */ |
| 3681 | static int openDirectory(const char *zFilename, int *pFd){ |
| 3682 | int ii; |
| 3683 | int fd = -1; |
| 3684 | char zDirname[MAX_PATHNAME+1]; |
| 3685 | |
| 3686 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
drh | dc27851 | 2015-12-07 18:18:33 +0000 | [diff] [blame] | 3687 | for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--); |
| 3688 | if( ii>0 ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3689 | zDirname[ii] = '\0'; |
drh | dc27851 | 2015-12-07 18:18:33 +0000 | [diff] [blame] | 3690 | }else{ |
| 3691 | if( zDirname[0]!='/' ) zDirname[0] = '.'; |
| 3692 | zDirname[1] = 0; |
| 3693 | } |
drh | 3b9f154 | 2020-04-20 17:35:32 +0000 | [diff] [blame] | 3694 | fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0); |
drh | dc27851 | 2015-12-07 18:18:33 +0000 | [diff] [blame] | 3695 | if( fd>=0 ){ |
| 3696 | OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3697 | } |
| 3698 | *pFd = fd; |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 3699 | if( fd>=0 ) return SQLITE_OK; |
| 3700 | return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname); |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3701 | } |
| 3702 | |
| 3703 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3704 | ** Make sure all writes to a particular file are committed to disk. |
| 3705 | ** |
| 3706 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 3707 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 3708 | ** file data is synced. |
| 3709 | ** |
| 3710 | ** Under Unix, also make sure that the directory entry for the file |
| 3711 | ** has been created by fsync-ing the directory that contains the file. |
| 3712 | ** If we do not do this and we encounter a power failure, the directory |
| 3713 | ** entry for the journal might not exist after we reboot. The next |
| 3714 | ** SQLite to access the file will not know that the journal exists (because |
| 3715 | ** the directory entry for the journal was never created) and the transaction |
| 3716 | ** will not roll back - possibly leading to database corruption. |
| 3717 | */ |
| 3718 | static int unixSync(sqlite3_file *id, int flags){ |
| 3719 | int rc; |
| 3720 | unixFile *pFile = (unixFile*)id; |
| 3721 | |
| 3722 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 3723 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 3724 | |
| 3725 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 3726 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 3727 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 3728 | ); |
| 3729 | |
| 3730 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 3731 | ** line is to test that doing so does not cause any problems. |
| 3732 | */ |
| 3733 | SimulateDiskfullError( return SQLITE_FULL ); |
| 3734 | |
| 3735 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3736 | OSTRACE(("SYNC %-3d\n", pFile->h)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3737 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 3738 | SimulateIOError( rc=1 ); |
| 3739 | if( rc ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3740 | storeLastErrno(pFile, errno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3741 | return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3742 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3743 | |
| 3744 | /* Also fsync the directory containing the file if the DIRSYNC flag |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 3745 | ** is set. This is a one-time occurrence. Many systems (examples: AIX) |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3746 | ** are unable to fsync a directory, so ignore errors on the fsync. |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3747 | */ |
| 3748 | if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){ |
| 3749 | int dirfd; |
| 3750 | OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3751 | HAVE_FULLFSYNC, isFullsync)); |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 3752 | rc = osOpenDirectory(pFile->zPath, &dirfd); |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 3753 | if( rc==SQLITE_OK ){ |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3754 | full_fsync(dirfd, 0, 0); |
| 3755 | robust_close(pFile, dirfd, __LINE__); |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 3756 | }else{ |
| 3757 | assert( rc==SQLITE_CANTOPEN ); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 3758 | rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3759 | } |
drh | 0059eae | 2011-08-08 23:48:40 +0000 | [diff] [blame] | 3760 | pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3761 | } |
| 3762 | return rc; |
| 3763 | } |
| 3764 | |
| 3765 | /* |
| 3766 | ** Truncate an open file to a specified size |
| 3767 | */ |
| 3768 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3769 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3770 | int rc; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3771 | assert( pFile ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3772 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3773 | |
| 3774 | /* If the user has configured a chunk-size for this file, truncate the |
| 3775 | ** file so that it consists of an integer number of chunks (i.e. the |
| 3776 | ** actual file size after the operation may be larger than the requested |
| 3777 | ** size). |
| 3778 | */ |
drh | b8af4b7 | 2012-04-05 20:04:39 +0000 | [diff] [blame] | 3779 | if( pFile->szChunk>0 ){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3780 | nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 3781 | } |
| 3782 | |
dan | 2ee5341 | 2014-09-06 16:49:40 +0000 | [diff] [blame] | 3783 | rc = robust_ftruncate(pFile->h, nByte); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3784 | if( rc ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3785 | storeLastErrno(pFile, errno); |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 3786 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3787 | }else{ |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 3788 | #ifdef SQLITE_DEBUG |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3789 | /* If we are doing a normal write to a database file (as opposed to |
| 3790 | ** doing a hot-journal rollback or a write to some file other than a |
| 3791 | ** normal database file) and we truncate the file to zero length, |
| 3792 | ** that effectively updates the change counter. This might happen |
| 3793 | ** when restoring a database using the backup API from a zero-length |
| 3794 | ** source. |
| 3795 | */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3796 | if( pFile->inNormalWrite && nByte==0 ){ |
| 3797 | pFile->transCntrChng = 1; |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3798 | } |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3799 | #endif |
dan | c000331 | 2013-03-22 17:46:11 +0000 | [diff] [blame] | 3800 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3801 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | c000331 | 2013-03-22 17:46:11 +0000 | [diff] [blame] | 3802 | /* If the file was just truncated to a size smaller than the currently |
| 3803 | ** mapped region, reduce the effective mapping size as well. SQLite will |
| 3804 | ** use read() and write() to access data beyond this point from now on. |
| 3805 | */ |
| 3806 | if( nByte<pFile->mmapSize ){ |
| 3807 | pFile->mmapSize = nByte; |
| 3808 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3809 | #endif |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3810 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3811 | return SQLITE_OK; |
| 3812 | } |
| 3813 | } |
| 3814 | |
| 3815 | /* |
| 3816 | ** Determine the current size of a file in bytes |
| 3817 | */ |
| 3818 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 3819 | int rc; |
| 3820 | struct stat buf; |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 3821 | assert( id ); |
| 3822 | rc = osFstat(((unixFile*)id)->h, &buf); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3823 | SimulateIOError( rc=1 ); |
| 3824 | if( rc!=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3825 | storeLastErrno((unixFile*)id, errno); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3826 | return SQLITE_IOERR_FSTAT; |
| 3827 | } |
| 3828 | *pSize = buf.st_size; |
| 3829 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3830 | /* When opening a zero-size database, the findInodeInfo() procedure |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3831 | ** writes a single byte into that file in order to work around a bug |
| 3832 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 3833 | ** layers, we need to report this file size as zero even though it is |
| 3834 | ** really 1. Ticket #3260. |
| 3835 | */ |
| 3836 | if( *pSize==1 ) *pSize = 0; |
| 3837 | |
| 3838 | |
| 3839 | return SQLITE_OK; |
| 3840 | } |
| 3841 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3842 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3843 | /* |
| 3844 | ** Handler for proxy-locking file-control verbs. Defined below in the |
| 3845 | ** proxying locking division. |
| 3846 | */ |
| 3847 | static int proxyFileControl(sqlite3_file*,int,void*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3848 | #endif |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3849 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3850 | /* |
| 3851 | ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 3852 | ** file-control operation. Enlarge the database to nBytes in size |
| 3853 | ** (rounded up to the next chunk-size). If the database is already |
| 3854 | ** nBytes or larger, this routine is a no-op. |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3855 | */ |
| 3856 | static int fcntlSizeHint(unixFile *pFile, i64 nByte){ |
mistachkin | d589a54 | 2011-08-30 01:23:34 +0000 | [diff] [blame] | 3857 | if( pFile->szChunk>0 ){ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3858 | i64 nSize; /* Required file size */ |
| 3859 | struct stat buf; /* Used to hold return values of fstat() */ |
| 3860 | |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3861 | if( osFstat(pFile->h, &buf) ){ |
| 3862 | return SQLITE_IOERR_FSTAT; |
| 3863 | } |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3864 | |
| 3865 | nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; |
| 3866 | if( nSize>(i64)buf.st_size ){ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3867 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3868 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3869 | /* The code below is handling the return value of osFallocate() |
| 3870 | ** correctly. posix_fallocate() is defined to "returns zero on success, |
| 3871 | ** or an error number on failure". See the manpage for details. */ |
| 3872 | int err; |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 3873 | do{ |
dan | 661d71a | 2011-03-30 19:08:03 +0000 | [diff] [blame] | 3874 | err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size); |
| 3875 | }while( err==EINTR ); |
drh | 789df14 | 2018-06-02 14:37:39 +0000 | [diff] [blame] | 3876 | if( err && err!=EINVAL ) return SQLITE_IOERR_WRITE; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3877 | #else |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3878 | /* If the OS does not have posix_fallocate(), fake it. Write a |
| 3879 | ** single byte to the last byte in each block that falls entirely |
| 3880 | ** within the extended region. Then, if required, a single byte |
| 3881 | ** at offset (nSize-1), to set the size of the file correctly. |
| 3882 | ** This is a similar technique to that used by glibc on systems |
| 3883 | ** that do not have a real fallocate() call. |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3884 | */ |
| 3885 | int nBlk = buf.st_blksize; /* File-system block size */ |
dan | ef3d66c | 2015-01-06 21:31:47 +0000 | [diff] [blame] | 3886 | int nWrite = 0; /* Number of bytes written by seekAndWrite */ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3887 | i64 iWrite; /* Next offset to write to */ |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3888 | |
drh | 053378d | 2015-12-01 22:09:42 +0000 | [diff] [blame] | 3889 | iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1; |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3890 | assert( iWrite>=buf.st_size ); |
dan | 592bf7f | 2014-12-30 19:58:31 +0000 | [diff] [blame] | 3891 | assert( ((iWrite+1)%nBlk)==0 ); |
drh | 053378d | 2015-12-01 22:09:42 +0000 | [diff] [blame] | 3892 | for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){ |
| 3893 | if( iWrite>=nSize ) iWrite = nSize - 1; |
dan | ef3d66c | 2015-01-06 21:31:47 +0000 | [diff] [blame] | 3894 | nWrite = seekAndWrite(pFile, iWrite, "", 1); |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3895 | if( nWrite!=1 ) return SQLITE_IOERR_WRITE; |
dan | dc5df0f | 2011-04-06 19:15:45 +0000 | [diff] [blame] | 3896 | } |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3897 | #endif |
| 3898 | } |
| 3899 | } |
| 3900 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3901 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 3902 | if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3903 | int rc; |
| 3904 | if( pFile->szChunk<=0 ){ |
| 3905 | if( robust_ftruncate(pFile->h, nByte) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3906 | storeLastErrno(pFile, errno); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3907 | return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
| 3908 | } |
| 3909 | } |
| 3910 | |
| 3911 | rc = unixMapfile(pFile, nByte); |
| 3912 | return rc; |
| 3913 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 3914 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 3915 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3916 | return SQLITE_OK; |
| 3917 | } |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3918 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3919 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3920 | ** If *pArg is initially negative then this is a query. Set *pArg to |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3921 | ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set. |
| 3922 | ** |
| 3923 | ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags. |
| 3924 | */ |
| 3925 | static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){ |
| 3926 | if( *pArg<0 ){ |
| 3927 | *pArg = (pFile->ctrlFlags & mask)!=0; |
| 3928 | }else if( (*pArg)==0 ){ |
| 3929 | pFile->ctrlFlags &= ~mask; |
| 3930 | }else{ |
| 3931 | pFile->ctrlFlags |= mask; |
| 3932 | } |
| 3933 | } |
| 3934 | |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3935 | /* Forward declaration */ |
| 3936 | static int unixGetTempname(int nBuf, char *zBuf); |
| 3937 | |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3938 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3939 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3940 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3941 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3942 | unixFile *pFile = (unixFile*)id; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3943 | switch( op ){ |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 3944 | #if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3945 | case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: { |
| 3946 | int rc = osIoctl(pFile->h, F2FS_IOC_START_ATOMIC_WRITE); |
drh | 344f763 | 2017-07-28 13:18:35 +0000 | [diff] [blame] | 3947 | return rc ? SQLITE_IOERR_BEGIN_ATOMIC : SQLITE_OK; |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3948 | } |
| 3949 | case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: { |
| 3950 | int rc = osIoctl(pFile->h, F2FS_IOC_COMMIT_ATOMIC_WRITE); |
drh | 344f763 | 2017-07-28 13:18:35 +0000 | [diff] [blame] | 3951 | return rc ? SQLITE_IOERR_COMMIT_ATOMIC : SQLITE_OK; |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3952 | } |
| 3953 | case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: { |
| 3954 | int rc = osIoctl(pFile->h, F2FS_IOC_ABORT_VOLATILE_WRITE); |
drh | 344f763 | 2017-07-28 13:18:35 +0000 | [diff] [blame] | 3955 | return rc ? SQLITE_IOERR_ROLLBACK_ATOMIC : SQLITE_OK; |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3956 | } |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 3957 | #endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 3958 | |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3959 | case SQLITE_FCNTL_LOCKSTATE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3960 | *(int*)pArg = pFile->eFileLock; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3961 | return SQLITE_OK; |
| 3962 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 3963 | case SQLITE_FCNTL_LAST_ERRNO: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3964 | *(int*)pArg = pFile->lastErrno; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3965 | return SQLITE_OK; |
| 3966 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3967 | case SQLITE_FCNTL_CHUNK_SIZE: { |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3968 | pFile->szChunk = *(int *)pArg; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3969 | return SQLITE_OK; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3970 | } |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3971 | case SQLITE_FCNTL_SIZE_HINT: { |
dan | da04ea4 | 2011-08-23 05:10:39 +0000 | [diff] [blame] | 3972 | int rc; |
| 3973 | SimulateIOErrorBenign(1); |
| 3974 | rc = fcntlSizeHint(pFile, *(i64 *)pArg); |
| 3975 | SimulateIOErrorBenign(0); |
| 3976 | return rc; |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3977 | } |
| 3978 | case SQLITE_FCNTL_PERSIST_WAL: { |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 3979 | unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg); |
| 3980 | return SQLITE_OK; |
| 3981 | } |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 3982 | case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { |
| 3983 | unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg); |
drh | f0b190d | 2011-07-26 16:03:07 +0000 | [diff] [blame] | 3984 | return SQLITE_OK; |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3985 | } |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 3986 | case SQLITE_FCNTL_VFSNAME: { |
| 3987 | *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName); |
| 3988 | return SQLITE_OK; |
| 3989 | } |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3990 | case SQLITE_FCNTL_TEMPFILENAME: { |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 3991 | char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname ); |
drh | 696b33e | 2012-12-06 19:01:42 +0000 | [diff] [blame] | 3992 | if( zTFile ){ |
| 3993 | unixGetTempname(pFile->pVfs->mxPathname, zTFile); |
| 3994 | *(char**)pArg = zTFile; |
| 3995 | } |
| 3996 | return SQLITE_OK; |
| 3997 | } |
drh | b959a01 | 2013-12-07 12:29:22 +0000 | [diff] [blame] | 3998 | case SQLITE_FCNTL_HAS_MOVED: { |
| 3999 | *(int*)pArg = fileHasMoved(pFile); |
| 4000 | return SQLITE_OK; |
| 4001 | } |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 4002 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
| 4003 | case SQLITE_FCNTL_LOCK_TIMEOUT: { |
dan | 97ccc1b | 2020-03-27 17:23:17 +0000 | [diff] [blame] | 4004 | int iOld = pFile->iBusyTimeout; |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 4005 | pFile->iBusyTimeout = *(int*)pArg; |
dan | 97ccc1b | 2020-03-27 17:23:17 +0000 | [diff] [blame] | 4006 | *(int*)pArg = iOld; |
drh | f0119b2 | 2018-03-26 17:40:53 +0000 | [diff] [blame] | 4007 | return SQLITE_OK; |
| 4008 | } |
| 4009 | #endif |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 4010 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4011 | case SQLITE_FCNTL_MMAP_SIZE: { |
drh | 34f7490 | 2013-04-03 13:09:18 +0000 | [diff] [blame] | 4012 | i64 newLimit = *(i64*)pArg; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 4013 | int rc = SQLITE_OK; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4014 | if( newLimit>sqlite3GlobalConfig.mxMmap ){ |
| 4015 | newLimit = sqlite3GlobalConfig.mxMmap; |
| 4016 | } |
dan | 43c1e62 | 2017-08-07 18:13:28 +0000 | [diff] [blame] | 4017 | |
| 4018 | /* The value of newLimit may be eventually cast to (size_t) and passed |
mistachkin | e35395a | 2017-08-07 19:06:54 +0000 | [diff] [blame] | 4019 | ** to mmap(). Restrict its value to 2GB if (size_t) is not at least a |
| 4020 | ** 64-bit type. */ |
dan | 089df50 | 2017-08-07 18:54:10 +0000 | [diff] [blame] | 4021 | if( newLimit>0 && sizeof(size_t)<8 ){ |
dan | 43c1e62 | 2017-08-07 18:13:28 +0000 | [diff] [blame] | 4022 | newLimit = (newLimit & 0x7FFFFFFF); |
| 4023 | } |
| 4024 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4025 | *(i64*)pArg = pFile->mmapSizeMax; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 4026 | if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 4027 | pFile->mmapSizeMax = newLimit; |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 4028 | if( pFile->mmapSize>0 ){ |
| 4029 | unixUnmapfile(pFile); |
| 4030 | rc = unixMapfile(pFile, -1); |
| 4031 | } |
dan | bcb8a86 | 2013-04-08 15:30:41 +0000 | [diff] [blame] | 4032 | } |
drh | 34e258c | 2013-05-23 01:40:53 +0000 | [diff] [blame] | 4033 | return rc; |
dan | b2d3de3 | 2013-03-14 18:34:37 +0000 | [diff] [blame] | 4034 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 4035 | #endif |
drh | d3d8c04 | 2012-05-29 17:02:40 +0000 | [diff] [blame] | 4036 | #ifdef SQLITE_DEBUG |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 4037 | /* The pager calls this method to signal that it has done |
| 4038 | ** a rollback and that the database is therefore unchanged and |
| 4039 | ** it hence it is OK for the transaction change counter to be |
| 4040 | ** unchanged. |
| 4041 | */ |
| 4042 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 4043 | ((unixFile*)id)->dbUpdate = 0; |
| 4044 | return SQLITE_OK; |
| 4045 | } |
| 4046 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4047 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4048 | case SQLITE_FCNTL_SET_LOCKPROXYFILE: |
| 4049 | case SQLITE_FCNTL_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4050 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4051 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4052 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 4053 | } |
drh | 0b52b7d | 2011-01-26 19:46:22 +0000 | [diff] [blame] | 4054 | return SQLITE_NOTFOUND; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 4055 | } |
| 4056 | |
| 4057 | /* |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4058 | ** If pFd->sectorSize is non-zero when this function is called, it is a |
| 4059 | ** no-op. Otherwise, the values of pFd->sectorSize and |
| 4060 | ** pFd->deviceCharacteristics are set according to the file-system |
| 4061 | ** characteristics. |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4062 | ** |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4063 | ** There are two versions of this function. One for QNX and one for all |
| 4064 | ** other systems. |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4065 | */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4066 | #ifndef __QNXNTO__ |
| 4067 | static void setDeviceCharacteristics(unixFile *pFd){ |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 4068 | assert( pFd->deviceCharacteristics==0 || pFd->sectorSize!=0 ); |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4069 | if( pFd->sectorSize==0 ){ |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 4070 | #if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE) |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4071 | int res; |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame] | 4072 | u32 f = 0; |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 4073 | |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4074 | /* Check for support for F2FS atomic batch writes. */ |
dan | 9d70954 | 2017-07-21 21:06:24 +0000 | [diff] [blame] | 4075 | res = osIoctl(pFd->h, F2FS_IOC_GET_FEATURES, &f); |
| 4076 | if( res==0 && (f & F2FS_FEATURE_ATOMIC_WRITE) ){ |
dan | 77b4f52 | 2017-07-27 18:34:00 +0000 | [diff] [blame] | 4077 | pFd->deviceCharacteristics = SQLITE_IOCAP_BATCH_ATOMIC; |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4078 | } |
drh | d76dba7 | 2017-07-22 16:00:34 +0000 | [diff] [blame] | 4079 | #endif /* __linux__ && SQLITE_ENABLE_BATCH_ATOMIC_WRITE */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4080 | |
| 4081 | /* Set the POWERSAFE_OVERWRITE flag if requested. */ |
| 4082 | if( pFd->ctrlFlags & UNIXFILE_PSOW ){ |
| 4083 | pFd->deviceCharacteristics |= SQLITE_IOCAP_POWERSAFE_OVERWRITE; |
| 4084 | } |
| 4085 | |
| 4086 | pFd->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 4087 | } |
| 4088 | } |
| 4089 | #else |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 4090 | #include <sys/dcmd_blk.h> |
| 4091 | #include <sys/statvfs.h> |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4092 | static void setDeviceCharacteristics(unixFile *pFile){ |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 4093 | if( pFile->sectorSize == 0 ){ |
| 4094 | struct statvfs fsInfo; |
| 4095 | |
| 4096 | /* Set defaults for non-supported filesystems */ |
| 4097 | pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 4098 | pFile->deviceCharacteristics = 0; |
| 4099 | if( fstatvfs(pFile->h, &fsInfo) == -1 ) { |
drh | a9be508 | 2018-01-15 14:32:37 +0000 | [diff] [blame] | 4100 | return; |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 4101 | } |
| 4102 | |
| 4103 | if( !strcmp(fsInfo.f_basetype, "tmp") ) { |
| 4104 | pFile->sectorSize = fsInfo.f_bsize; |
| 4105 | pFile->deviceCharacteristics = |
| 4106 | SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */ |
| 4107 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 4108 | ** the write succeeds */ |
| 4109 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 4110 | ** so it is ordered */ |
| 4111 | 0; |
| 4112 | }else if( strstr(fsInfo.f_basetype, "etfs") ){ |
| 4113 | pFile->sectorSize = fsInfo.f_bsize; |
| 4114 | pFile->deviceCharacteristics = |
| 4115 | /* etfs cluster size writes are atomic */ |
| 4116 | (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) | |
| 4117 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 4118 | ** the write succeeds */ |
| 4119 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 4120 | ** so it is ordered */ |
| 4121 | 0; |
| 4122 | }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){ |
| 4123 | pFile->sectorSize = fsInfo.f_bsize; |
| 4124 | pFile->deviceCharacteristics = |
| 4125 | SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */ |
| 4126 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 4127 | ** the write succeeds */ |
| 4128 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 4129 | ** so it is ordered */ |
| 4130 | 0; |
| 4131 | }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){ |
| 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 if( strstr(fsInfo.f_basetype, "dos") ){ |
| 4140 | pFile->sectorSize = fsInfo.f_bsize; |
| 4141 | pFile->deviceCharacteristics = |
| 4142 | /* full bitset of atomics from max sector size and smaller */ |
| 4143 | ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 4144 | SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 4145 | ** so it is ordered */ |
| 4146 | 0; |
| 4147 | }else{ |
| 4148 | pFile->deviceCharacteristics = |
| 4149 | SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */ |
| 4150 | SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 4151 | ** the write succeeds */ |
| 4152 | 0; |
| 4153 | } |
| 4154 | } |
| 4155 | /* Last chance verification. If the sector size isn't a multiple of 512 |
| 4156 | ** then it isn't valid.*/ |
| 4157 | if( pFile->sectorSize % 512 != 0 ){ |
| 4158 | pFile->deviceCharacteristics = 0; |
| 4159 | pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 4160 | } |
drh | 537dddf | 2012-10-26 13:46:24 +0000 | [diff] [blame] | 4161 | } |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4162 | #endif |
| 4163 | |
| 4164 | /* |
| 4165 | ** Return the sector size in bytes of the underlying block device for |
| 4166 | ** the specified file. This is almost always 512 bytes, but may be |
| 4167 | ** larger for some devices. |
| 4168 | ** |
| 4169 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 4170 | ** if two files are created in the same file-system directory (i.e. |
| 4171 | ** a database and its journal file) that the sector size will be the |
| 4172 | ** same for both. |
| 4173 | */ |
| 4174 | static int unixSectorSize(sqlite3_file *id){ |
| 4175 | unixFile *pFd = (unixFile*)id; |
| 4176 | setDeviceCharacteristics(pFd); |
| 4177 | return pFd->sectorSize; |
| 4178 | } |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4179 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4180 | /* |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 4181 | ** Return the device characteristics for the file. |
| 4182 | ** |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 4183 | ** 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] | 4184 | ** However, that choice is controversial since technically the underlying |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 4185 | ** file system does not always provide powersafe overwrites. (In other |
| 4186 | ** words, after a power-loss event, parts of the file that were never |
| 4187 | ** written might end up being altered.) However, non-PSOW behavior is very, |
| 4188 | ** very rare. And asserting PSOW makes a large reduction in the amount |
| 4189 | ** of required I/O for journaling, since a lot of padding is eliminated. |
| 4190 | ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control |
| 4191 | ** 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] | 4192 | */ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 4193 | static int unixDeviceCharacteristics(sqlite3_file *id){ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 4194 | unixFile *pFd = (unixFile*)id; |
| 4195 | setDeviceCharacteristics(pFd); |
| 4196 | return pFd->deviceCharacteristics; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 4197 | } |
| 4198 | |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 4199 | #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4200 | |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 4201 | /* |
| 4202 | ** Return the system page size. |
| 4203 | ** |
| 4204 | ** This function should not be called directly by other code in this file. |
| 4205 | ** Instead, it should be called via macro osGetpagesize(). |
| 4206 | */ |
| 4207 | static int unixGetpagesize(void){ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 4208 | #if OS_VXWORKS |
| 4209 | return 1024; |
| 4210 | #elif defined(_BSD_SOURCE) |
dan | 702eec1 | 2014-06-23 10:04:58 +0000 | [diff] [blame] | 4211 | return getpagesize(); |
| 4212 | #else |
| 4213 | return (int)sysconf(_SC_PAGESIZE); |
| 4214 | #endif |
| 4215 | } |
| 4216 | |
| 4217 | #endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */ |
| 4218 | |
| 4219 | #ifndef SQLITE_OMIT_WAL |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4220 | |
| 4221 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4222 | ** Object used to represent an shared memory buffer. |
| 4223 | ** |
| 4224 | ** When multiple threads all reference the same wal-index, each thread |
| 4225 | ** has its own unixShm object, but they all point to a single instance |
| 4226 | ** of this unixShmNode object. In other words, each wal-index is opened |
| 4227 | ** only once per process. |
| 4228 | ** |
| 4229 | ** Each unixShmNode object is connected to a single unixInodeInfo object. |
| 4230 | ** We could coalesce this object into unixInodeInfo, but that would mean |
| 4231 | ** every open file that does not use shared memory (in other words, most |
| 4232 | ** open files) would have to carry around this extra information. So |
| 4233 | ** the unixInodeInfo object contains a pointer to this unixShmNode object |
| 4234 | ** and the unixShmNode object is created only when needed. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4235 | ** |
| 4236 | ** unixMutexHeld() must be true when creating or destroying |
| 4237 | ** this object or while reading or writing the following fields: |
| 4238 | ** |
| 4239 | ** nRef |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4240 | ** |
| 4241 | ** The following fields are read-only after the object is created: |
| 4242 | ** |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4243 | ** hShm |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4244 | ** zFilename |
| 4245 | ** |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4246 | ** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4247 | ** unixMutexHeld() is true when reading or writing any other field |
| 4248 | ** in this structure. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4249 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4250 | struct unixShmNode { |
| 4251 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4252 | sqlite3_mutex *pShmMutex; /* Mutex to access this object */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4253 | char *zFilename; /* Name of the mmapped file */ |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4254 | int hShm; /* Open file descriptor */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4255 | int szRegion; /* Size of shared-memory regions */ |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4256 | u16 nRegion; /* Size of array apRegion */ |
| 4257 | u8 isReadonly; /* True if read-only */ |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4258 | u8 isUnlocked; /* True if no DMS lock held */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4259 | char **apRegion; /* Array of mapped shared-memory regions */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4260 | int nRef; /* Number of unixShm objects pointing to this */ |
| 4261 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
dan | 8337da6 | 2020-08-28 19:27:15 +0000 | [diff] [blame] | 4262 | int aLock[SQLITE_SHM_NLOCK]; /* # shared locks on slot, -1==excl lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4263 | #ifdef SQLITE_DEBUG |
| 4264 | u8 exclMask; /* Mask of exclusive locks held */ |
| 4265 | u8 sharedMask; /* Mask of shared locks held */ |
| 4266 | u8 nextShmId; /* Next available unixShm.id value */ |
| 4267 | #endif |
| 4268 | }; |
| 4269 | |
| 4270 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4271 | ** Structure used internally by this VFS to record the state of an |
| 4272 | ** open shared memory connection. |
| 4273 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4274 | ** The following fields are initialized when this object is created and |
| 4275 | ** are read-only thereafter: |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4276 | ** |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4277 | ** unixShm.pShmNode |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4278 | ** unixShm.id |
| 4279 | ** |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4280 | ** All other fields are read/write. The unixShm.pShmNode->pShmMutex must |
| 4281 | ** be held while accessing any read/write fields. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4282 | */ |
| 4283 | struct unixShm { |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4284 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 4285 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4286 | u8 hasMutex; /* True if holding the unixShmNode->pShmMutex */ |
drh | fd53231 | 2011-08-31 18:35:34 +0000 | [diff] [blame] | 4287 | u8 id; /* Id of this connection within its unixShmNode */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4288 | u16 sharedMask; /* Mask of shared locks held */ |
| 4289 | u16 exclMask; /* Mask of exclusive locks held */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4290 | }; |
| 4291 | |
| 4292 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4293 | ** Constants used for locking |
| 4294 | */ |
drh | bd9676c | 2010-06-23 17:58:38 +0000 | [diff] [blame] | 4295 | #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
drh | 4222441 | 2010-05-31 14:28:25 +0000 | [diff] [blame] | 4296 | #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4297 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4298 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4299 | ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4300 | ** |
| 4301 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 4302 | ** otherwise. |
| 4303 | */ |
| 4304 | static int unixShmSystemLock( |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4305 | unixFile *pFile, /* Open connection to the WAL file */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4306 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4307 | int ofst, /* First byte of the locking range */ |
| 4308 | int n /* Number of bytes to lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4309 | ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4310 | unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */ |
| 4311 | struct flock f; /* The posix advisory locking structure */ |
| 4312 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4313 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4314 | /* Access to the unixShmNode object is serialized by the caller */ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4315 | pShmNode = pFile->pInode->pShmNode; |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4316 | assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) ); |
drh | 9b7e8e1 | 2018-10-02 20:16:41 +0000 | [diff] [blame] | 4317 | assert( pShmNode->nRef>0 || unixMutexHeld() ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4318 | |
dan | 9181ae9 | 2017-10-26 17:05:22 +0000 | [diff] [blame] | 4319 | /* Shared locks never span more than one byte */ |
| 4320 | assert( n==1 || lockType!=F_RDLCK ); |
| 4321 | |
| 4322 | /* Locks are within range */ |
| 4323 | assert( n>=1 && n<=SQLITE_SHM_NLOCK ); |
| 4324 | |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4325 | if( pShmNode->hShm>=0 ){ |
dan | 7bb8b8a | 2020-05-06 20:27:18 +0000 | [diff] [blame] | 4326 | int res; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4327 | /* Initialize the locking parameters */ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4328 | f.l_type = lockType; |
| 4329 | f.l_whence = SEEK_SET; |
| 4330 | f.l_start = ofst; |
| 4331 | f.l_len = n; |
dan | 7bb8b8a | 2020-05-06 20:27:18 +0000 | [diff] [blame] | 4332 | res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile); |
| 4333 | if( res==-1 ){ |
dan | 7a623e1 | 2020-05-06 20:45:11 +0000 | [diff] [blame] | 4334 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
dan | 7bb8b8a | 2020-05-06 20:27:18 +0000 | [diff] [blame] | 4335 | rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY); |
dan | 7a623e1 | 2020-05-06 20:45:11 +0000 | [diff] [blame] | 4336 | #else |
| 4337 | rc = SQLITE_BUSY; |
| 4338 | #endif |
dan | 7bb8b8a | 2020-05-06 20:27:18 +0000 | [diff] [blame] | 4339 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4340 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4341 | |
| 4342 | /* Update the global lock state and do debug tracing */ |
| 4343 | #ifdef SQLITE_DEBUG |
dan | 9181ae9 | 2017-10-26 17:05:22 +0000 | [diff] [blame] | 4344 | { u16 mask; |
| 4345 | OSTRACE(("SHM-LOCK ")); |
| 4346 | mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst); |
| 4347 | if( rc==SQLITE_OK ){ |
| 4348 | if( lockType==F_UNLCK ){ |
| 4349 | OSTRACE(("unlock %d ok", ofst)); |
| 4350 | pShmNode->exclMask &= ~mask; |
| 4351 | pShmNode->sharedMask &= ~mask; |
| 4352 | }else if( lockType==F_RDLCK ){ |
| 4353 | OSTRACE(("read-lock %d ok", ofst)); |
| 4354 | pShmNode->exclMask &= ~mask; |
| 4355 | pShmNode->sharedMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4356 | }else{ |
dan | 9181ae9 | 2017-10-26 17:05:22 +0000 | [diff] [blame] | 4357 | assert( lockType==F_WRLCK ); |
| 4358 | OSTRACE(("write-lock %d ok", ofst)); |
| 4359 | pShmNode->exclMask |= mask; |
| 4360 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4361 | } |
dan | 9181ae9 | 2017-10-26 17:05:22 +0000 | [diff] [blame] | 4362 | }else{ |
| 4363 | if( lockType==F_UNLCK ){ |
| 4364 | OSTRACE(("unlock %d failed", ofst)); |
| 4365 | }else if( lockType==F_RDLCK ){ |
| 4366 | OSTRACE(("read-lock failed")); |
| 4367 | }else{ |
| 4368 | assert( lockType==F_WRLCK ); |
| 4369 | OSTRACE(("write-lock %d failed", ofst)); |
| 4370 | } |
| 4371 | } |
| 4372 | OSTRACE((" - afterwards %03x,%03x\n", |
| 4373 | pShmNode->sharedMask, pShmNode->exclMask)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4374 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4375 | #endif |
| 4376 | |
| 4377 | return rc; |
| 4378 | } |
| 4379 | |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4380 | /* |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4381 | ** Return the minimum number of 32KB shm regions that should be mapped at |
| 4382 | ** a time, assuming that each mapping must be an integer multiple of the |
| 4383 | ** current system page-size. |
| 4384 | ** |
| 4385 | ** Usually, this is 1. The exception seems to be systems that are configured |
| 4386 | ** to use 64KB pages - in this case each mapping must cover at least two |
| 4387 | ** shm regions. |
| 4388 | */ |
| 4389 | static int unixShmRegionPerMap(void){ |
| 4390 | int shmsz = 32*1024; /* SHM region size */ |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 4391 | int pgsz = osGetpagesize(); /* System page size */ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4392 | assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */ |
| 4393 | if( pgsz<shmsz ) return 1; |
| 4394 | return pgsz/shmsz; |
| 4395 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4396 | |
| 4397 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4398 | ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4399 | ** |
| 4400 | ** This is not a VFS shared-memory method; it is a utility function called |
| 4401 | ** by VFS shared-memory methods. |
| 4402 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4403 | static void unixShmPurge(unixFile *pFd){ |
| 4404 | unixShmNode *p = pFd->pInode->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4405 | assert( unixMutexHeld() ); |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 4406 | if( p && ALWAYS(p->nRef==0) ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4407 | int nShmPerMap = unixShmRegionPerMap(); |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4408 | int i; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4409 | assert( p->pInode==pFd->pInode ); |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4410 | sqlite3_mutex_free(p->pShmMutex); |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4411 | for(i=0; i<p->nRegion; i+=nShmPerMap){ |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4412 | if( p->hShm>=0 ){ |
drh | d1ab806 | 2013-03-25 20:50:25 +0000 | [diff] [blame] | 4413 | osMunmap(p->apRegion[i], p->szRegion); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4414 | }else{ |
| 4415 | sqlite3_free(p->apRegion[i]); |
| 4416 | } |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4417 | } |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4418 | sqlite3_free(p->apRegion); |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4419 | if( p->hShm>=0 ){ |
| 4420 | robust_close(pFd, p->hShm, __LINE__); |
| 4421 | p->hShm = -1; |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 4422 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4423 | p->pInode->pShmNode = 0; |
| 4424 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4425 | } |
| 4426 | } |
| 4427 | |
| 4428 | /* |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4429 | ** The DMS lock has not yet been taken on shm file pShmNode. Attempt to |
| 4430 | ** take it now. Return SQLITE_OK if successful, or an SQLite error |
| 4431 | ** code otherwise. |
| 4432 | ** |
| 4433 | ** If the DMS cannot be locked because this is a readonly_shm=1 |
| 4434 | ** connection and no other process already holds a lock, return |
drh | 7e45e3a | 2017-11-08 17:32:12 +0000 | [diff] [blame] | 4435 | ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1. |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4436 | */ |
| 4437 | static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){ |
| 4438 | struct flock lock; |
| 4439 | int rc = SQLITE_OK; |
| 4440 | |
| 4441 | /* Use F_GETLK to determine the locks other processes are holding |
| 4442 | ** on the DMS byte. If it indicates that another process is holding |
| 4443 | ** a SHARED lock, then this process may also take a SHARED lock |
| 4444 | ** and proceed with opening the *-shm file. |
| 4445 | ** |
| 4446 | ** Or, if no other process is holding any lock, then this process |
| 4447 | ** is the first to open it. In this case take an EXCLUSIVE lock on the |
| 4448 | ** DMS byte and truncate the *-shm file to zero bytes in size. Then |
| 4449 | ** downgrade to a SHARED lock on the DMS byte. |
| 4450 | ** |
| 4451 | ** If another process is holding an EXCLUSIVE lock on the DMS byte, |
| 4452 | ** return SQLITE_BUSY to the caller (it will try again). An earlier |
| 4453 | ** version of this code attempted the SHARED lock at this point. But |
| 4454 | ** this introduced a subtle race condition: if the process holding |
| 4455 | ** EXCLUSIVE failed just before truncating the *-shm file, then this |
| 4456 | ** process might open and use the *-shm file without truncating it. |
| 4457 | ** And if the *-shm file has been corrupted by a power failure or |
| 4458 | ** system crash, the database itself may also become corrupt. */ |
| 4459 | lock.l_whence = SEEK_SET; |
| 4460 | lock.l_start = UNIX_SHM_DMS; |
| 4461 | lock.l_len = 1; |
| 4462 | lock.l_type = F_WRLCK; |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4463 | if( osFcntl(pShmNode->hShm, F_GETLK, &lock)!=0 ) { |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4464 | rc = SQLITE_IOERR_LOCK; |
| 4465 | }else if( lock.l_type==F_UNLCK ){ |
| 4466 | if( pShmNode->isReadonly ){ |
| 4467 | pShmNode->isUnlocked = 1; |
drh | 7e45e3a | 2017-11-08 17:32:12 +0000 | [diff] [blame] | 4468 | rc = SQLITE_READONLY_CANTINIT; |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4469 | }else{ |
| 4470 | rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1); |
drh | f7f2a82 | 2018-10-11 13:51:48 +0000 | [diff] [blame] | 4471 | /* The first connection to attach must truncate the -shm file. We |
| 4472 | ** truncate to 3 bytes (an arbitrary small number, less than the |
| 4473 | ** -shm header size) rather than 0 as a system debugging aid, to |
| 4474 | ** help detect if a -shm file truncation is legitimate or is the work |
| 4475 | ** or a rogue process. */ |
| 4476 | if( rc==SQLITE_OK && robust_ftruncate(pShmNode->hShm, 3) ){ |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4477 | rc = unixLogError(SQLITE_IOERR_SHMOPEN,"ftruncate",pShmNode->zFilename); |
| 4478 | } |
| 4479 | } |
| 4480 | }else if( lock.l_type==F_WRLCK ){ |
| 4481 | rc = SQLITE_BUSY; |
| 4482 | } |
| 4483 | |
| 4484 | if( rc==SQLITE_OK ){ |
| 4485 | assert( lock.l_type==F_UNLCK || lock.l_type==F_RDLCK ); |
| 4486 | rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1); |
| 4487 | } |
| 4488 | return rc; |
| 4489 | } |
| 4490 | |
| 4491 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4492 | ** Open a shared-memory area associated with open database file pDbFd. |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4493 | ** This particular implementation uses mmapped files. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4494 | ** |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4495 | ** The file used to implement shared-memory is in the same directory |
| 4496 | ** as the open database file and has the same name as the open database |
| 4497 | ** file with the "-shm" suffix added. For example, if the database file |
| 4498 | ** is "/home/user1/config.db" then the file that is created and mmapped |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4499 | ** for shared memory will be called "/home/user1/config.db-shm". |
| 4500 | ** |
| 4501 | ** Another approach to is to use files in /dev/shm or /dev/tmp or an |
| 4502 | ** some other tmpfs mount. But if a file in a different directory |
| 4503 | ** from the database file is used, then differing access permissions |
| 4504 | ** or a chroot() might cause two different processes on the same |
| 4505 | ** database to end up using different files for shared memory - |
| 4506 | ** meaning that their memory would not really be shared - resulting |
| 4507 | ** in database corruption. Nevertheless, this tmpfs file usage |
| 4508 | ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" |
| 4509 | ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time |
| 4510 | ** option results in an incompatible build of SQLite; builds of SQLite |
| 4511 | ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the |
| 4512 | ** same database file at the same time, database corruption will likely |
| 4513 | ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered |
| 4514 | ** "unsupported" and may go away in a future SQLite release. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4515 | ** |
| 4516 | ** When opening a new shared-memory file, if no other instances of that |
| 4517 | ** file are currently open, in this process or in other processes, then |
| 4518 | ** the file must be truncated to zero length or have its header cleared. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4519 | ** |
| 4520 | ** If the original database file (pDbFd) is using the "unix-excl" VFS |
| 4521 | ** that means that an exclusive lock is held on the database file and |
| 4522 | ** that no other processes are able to read or write the database. In |
| 4523 | ** that case, we do not really need shared memory. No shared memory |
| 4524 | ** file is created. The shared memory will be simulated with heap memory. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4525 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4526 | static int unixOpenSharedMemory(unixFile *pDbFd){ |
| 4527 | struct unixShm *p = 0; /* The connection to be opened */ |
| 4528 | struct unixShmNode *pShmNode; /* The underlying mmapped file */ |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4529 | int rc = SQLITE_OK; /* Result code */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4530 | unixInodeInfo *pInode; /* The inode of fd */ |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4531 | char *zShm; /* Name of the file used for SHM */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4532 | int nShmFilename; /* Size of the SHM filename in bytes */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4533 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4534 | /* Allocate space for the new unixShm object. */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4535 | p = sqlite3_malloc64( sizeof(*p) ); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4536 | if( p==0 ) return SQLITE_NOMEM_BKPT; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4537 | memset(p, 0, sizeof(*p)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4538 | assert( pDbFd->pShm==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4539 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4540 | /* Check to see if a unixShmNode object already exists. Reuse an existing |
| 4541 | ** one if present. Create a new one if necessary. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4542 | */ |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 4543 | assert( unixFileMutexNotheld(pDbFd) ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4544 | unixEnterMutex(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4545 | pInode = pDbFd->pInode; |
| 4546 | pShmNode = pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4547 | if( pShmNode==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4548 | struct stat sStat; /* fstat() info for database file */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4549 | #ifndef SQLITE_SHM_DIRECTORY |
| 4550 | const char *zBasePath = pDbFd->zPath; |
| 4551 | #endif |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4552 | |
| 4553 | /* Call fstat() to figure out the permissions on the database file. If |
| 4554 | ** 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] | 4555 | ** with the same permissions. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4556 | */ |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 4557 | if( osFstat(pDbFd->h, &sStat) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4558 | rc = SQLITE_IOERR_FSTAT; |
| 4559 | goto shm_open_err; |
| 4560 | } |
| 4561 | |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4562 | #ifdef SQLITE_SHM_DIRECTORY |
drh | 52bcde0 | 2012-01-03 14:50:45 +0000 | [diff] [blame] | 4563 | nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4564 | #else |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 4565 | nShmFilename = 6 + (int)strlen(zBasePath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4566 | #endif |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 4567 | pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4568 | if( pShmNode==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4569 | rc = SQLITE_NOMEM_BKPT; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4570 | goto shm_open_err; |
| 4571 | } |
drh | 9cb5a0d | 2012-01-05 21:19:54 +0000 | [diff] [blame] | 4572 | memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename); |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4573 | zShm = pShmNode->zFilename = (char*)&pShmNode[1]; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4574 | #ifdef SQLITE_SHM_DIRECTORY |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4575 | sqlite3_snprintf(nShmFilename, zShm, |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4576 | SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", |
| 4577 | (u32)sStat.st_ino, (u32)sStat.st_dev); |
| 4578 | #else |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4579 | sqlite3_snprintf(nShmFilename, zShm, "%s-shm", zBasePath); |
| 4580 | sqlite3FileSuffix3(pDbFd->zPath, zShm); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 4581 | #endif |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4582 | pShmNode->hShm = -1; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4583 | pDbFd->pInode->pShmNode = pShmNode; |
| 4584 | pShmNode->pInode = pDbFd->pInode; |
drh | 97a7e5e | 2016-04-26 18:58:54 +0000 | [diff] [blame] | 4585 | if( sqlite3GlobalConfig.bCoreMutex ){ |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4586 | pShmNode->pShmMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 4587 | if( pShmNode->pShmMutex==0 ){ |
drh | 97a7e5e | 2016-04-26 18:58:54 +0000 | [diff] [blame] | 4588 | rc = SQLITE_NOMEM_BKPT; |
| 4589 | goto shm_open_err; |
| 4590 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4591 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4592 | |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4593 | if( pInode->bProcessLock==0 ){ |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4594 | if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ |
drh | c398c65 | 2019-11-22 00:42:01 +0000 | [diff] [blame] | 4595 | pShmNode->hShm = robust_open(zShm, O_RDWR|O_CREAT|O_NOFOLLOW, |
| 4596 | (sStat.st_mode&0777)); |
drh | 3ec4a0c | 2011-10-11 18:18:54 +0000 | [diff] [blame] | 4597 | } |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4598 | if( pShmNode->hShm<0 ){ |
drh | c398c65 | 2019-11-22 00:42:01 +0000 | [diff] [blame] | 4599 | pShmNode->hShm = robust_open(zShm, O_RDONLY|O_NOFOLLOW, |
| 4600 | (sStat.st_mode&0777)); |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4601 | if( pShmNode->hShm<0 ){ |
dan | f12ba66 | 2017-11-07 15:43:52 +0000 | [diff] [blame] | 4602 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShm); |
| 4603 | goto shm_open_err; |
| 4604 | } |
| 4605 | pShmNode->isReadonly = 1; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4606 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4607 | |
| 4608 | /* If this process is running as root, make sure that the SHM file |
| 4609 | ** is owned by the same user that owns the original database. Otherwise, |
drh | ed46682 | 2012-05-31 13:10:49 +0000 | [diff] [blame] | 4610 | ** the original owner will not be able to connect. |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 4611 | */ |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4612 | robustFchown(pShmNode->hShm, sStat.st_uid, sStat.st_gid); |
dan | 176b2a9 | 2017-11-01 06:59:19 +0000 | [diff] [blame] | 4613 | |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4614 | rc = unixLockSharedMemory(pDbFd, pShmNode); |
drh | 7e45e3a | 2017-11-08 17:32:12 +0000 | [diff] [blame] | 4615 | if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4616 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4617 | } |
| 4618 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4619 | /* Make the new connection a child of the unixShmNode */ |
| 4620 | p->pShmNode = pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4621 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4622 | p->id = pShmNode->nextShmId++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4623 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4624 | pShmNode->nRef++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4625 | pDbFd->pShm = p; |
| 4626 | unixLeaveMutex(); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 4627 | |
| 4628 | /* The reference count on pShmNode has already been incremented under |
| 4629 | ** the cover of the unixEnterMutex() mutex and the pointer from the |
| 4630 | ** new (struct unixShm) object to the pShmNode has been set. All that is |
| 4631 | ** 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] | 4632 | ** at pShmNode->pFirst. This must be done while holding the |
| 4633 | ** pShmNode->pShmMutex. |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 4634 | */ |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4635 | sqlite3_mutex_enter(pShmNode->pShmMutex); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 4636 | p->pNext = pShmNode->pFirst; |
| 4637 | pShmNode->pFirst = p; |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4638 | sqlite3_mutex_leave(pShmNode->pShmMutex); |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4639 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4640 | |
| 4641 | /* Jump here on any error */ |
| 4642 | shm_open_err: |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4643 | unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4644 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4645 | unixLeaveMutex(); |
| 4646 | return rc; |
| 4647 | } |
| 4648 | |
| 4649 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4650 | ** This function is called to obtain a pointer to region iRegion of the |
| 4651 | ** shared-memory associated with the database file fd. Shared-memory regions |
| 4652 | ** are numbered starting from zero. Each shared-memory region is szRegion |
| 4653 | ** bytes in size. |
| 4654 | ** |
| 4655 | ** If an error occurs, an error code is returned and *pp is set to NULL. |
| 4656 | ** |
| 4657 | ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory |
| 4658 | ** region has not been allocated (by any client, including one running in a |
| 4659 | ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 4660 | ** bExtend is non-zero and the requested shared-memory region has not yet |
| 4661 | ** been allocated, it is allocated by this function. |
| 4662 | ** |
| 4663 | ** If the shared-memory region has already been allocated or is allocated by |
| 4664 | ** this call as described above, then it is mapped into this processes |
| 4665 | ** address space (if it is not already), *pp is set to point to the mapped |
| 4666 | ** memory and SQLITE_OK returned. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4667 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4668 | static int unixShmMap( |
| 4669 | sqlite3_file *fd, /* Handle open on database file */ |
| 4670 | int iRegion, /* Region to retrieve */ |
| 4671 | int szRegion, /* Size of regions */ |
| 4672 | int bExtend, /* True to extend file if necessary */ |
| 4673 | void volatile **pp /* OUT: Mapped memory */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4674 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4675 | unixFile *pDbFd = (unixFile*)fd; |
| 4676 | unixShm *p; |
| 4677 | unixShmNode *pShmNode; |
| 4678 | int rc = SQLITE_OK; |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4679 | int nShmPerMap = unixShmRegionPerMap(); |
| 4680 | int nReqRegion; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4681 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4682 | /* If the shared-memory file has not yet been opened, open it now. */ |
| 4683 | if( pDbFd->pShm==0 ){ |
| 4684 | rc = unixOpenSharedMemory(pDbFd); |
| 4685 | if( rc!=SQLITE_OK ) return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4686 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4687 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4688 | p = pDbFd->pShm; |
| 4689 | pShmNode = p->pShmNode; |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4690 | sqlite3_mutex_enter(pShmNode->pShmMutex); |
dan | 92c02da | 2017-11-01 20:59:28 +0000 | [diff] [blame] | 4691 | if( pShmNode->isUnlocked ){ |
| 4692 | rc = unixLockSharedMemory(pDbFd, pShmNode); |
| 4693 | if( rc!=SQLITE_OK ) goto shmpage_out; |
| 4694 | pShmNode->isUnlocked = 0; |
| 4695 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4696 | assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4697 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4698 | assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4699 | assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 ); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4700 | |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4701 | /* Minimum number of regions required to be mapped. */ |
| 4702 | nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap; |
| 4703 | |
| 4704 | if( pShmNode->nRegion<nReqRegion ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4705 | char **apNew; /* New apRegion[] array */ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4706 | int nByte = nReqRegion*szRegion; /* Minimum required file size */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4707 | struct stat sStat; /* Used by fstat() */ |
| 4708 | |
| 4709 | pShmNode->szRegion = szRegion; |
| 4710 | |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4711 | if( pShmNode->hShm>=0 ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4712 | /* The requested region is not mapped into this processes address space. |
| 4713 | ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 4714 | ** large enough to contain the requested region). |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4715 | */ |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4716 | if( osFstat(pShmNode->hShm, &sStat) ){ |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4717 | rc = SQLITE_IOERR_SHMSIZE; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4718 | goto shmpage_out; |
| 4719 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4720 | |
| 4721 | if( sStat.st_size<nByte ){ |
| 4722 | /* The requested memory region does not exist. If bExtend is set to |
| 4723 | ** false, exit early. *pp will be set to NULL and SQLITE_OK returned. |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4724 | */ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4725 | if( !bExtend ){ |
drh | 0fbb50e | 2012-11-13 10:54:12 +0000 | [diff] [blame] | 4726 | goto shmpage_out; |
| 4727 | } |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4728 | |
| 4729 | /* Alternatively, if bExtend is true, extend the file. Do this by |
| 4730 | ** writing a single byte to the end of each (OS) page being |
| 4731 | ** allocated or extended. Technically, we need only write to the |
| 4732 | ** last page in order to extend the file. But writing to all new |
| 4733 | ** pages forces the OS to allocate them immediately, which reduces |
| 4734 | ** the chances of SIGBUS while accessing the mapped region later on. |
| 4735 | */ |
| 4736 | else{ |
| 4737 | static const int pgsz = 4096; |
| 4738 | int iPg; |
| 4739 | |
| 4740 | /* Write to the last byte of each newly allocated or extended page */ |
| 4741 | assert( (nByte % pgsz)==0 ); |
| 4742 | for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){ |
drh | e1818ec | 2015-12-01 16:21:35 +0000 | [diff] [blame] | 4743 | int x = 0; |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4744 | if( seekAndWriteFd(pShmNode->hShm, iPg*pgsz + pgsz-1,"",1,&x)!=1 ){ |
dan | 47a2b4a | 2013-04-26 16:09:29 +0000 | [diff] [blame] | 4745 | const char *zFile = pShmNode->zFilename; |
| 4746 | rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile); |
| 4747 | goto shmpage_out; |
| 4748 | } |
| 4749 | } |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4750 | } |
| 4751 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | /* Map the requested memory region into this processes address space. */ |
| 4755 | apNew = (char **)sqlite3_realloc( |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4756 | pShmNode->apRegion, nReqRegion*sizeof(char *) |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4757 | ); |
| 4758 | if( !apNew ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4759 | rc = SQLITE_IOERR_NOMEM_BKPT; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4760 | goto shmpage_out; |
| 4761 | } |
| 4762 | pShmNode->apRegion = apNew; |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4763 | while( pShmNode->nRegion<nReqRegion ){ |
| 4764 | int nMap = szRegion*nShmPerMap; |
| 4765 | int i; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4766 | void *pMem; |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4767 | if( pShmNode->hShm>=0 ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4768 | pMem = osMmap(0, nMap, |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4769 | pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4770 | MAP_SHARED, pShmNode->hShm, szRegion*(i64)pShmNode->nRegion |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4771 | ); |
| 4772 | if( pMem==MAP_FAILED ){ |
drh | 50990db | 2011-04-13 20:26:13 +0000 | [diff] [blame] | 4773 | rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4774 | goto shmpage_out; |
| 4775 | } |
| 4776 | }else{ |
drh | b6c4d59 | 2018-10-11 02:39:11 +0000 | [diff] [blame] | 4777 | pMem = sqlite3_malloc64(nMap); |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4778 | if( pMem==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 4779 | rc = SQLITE_NOMEM_BKPT; |
drh | 3cb9339 | 2011-03-12 18:10:44 +0000 | [diff] [blame] | 4780 | goto shmpage_out; |
| 4781 | } |
drh | b6c4d59 | 2018-10-11 02:39:11 +0000 | [diff] [blame] | 4782 | memset(pMem, 0, nMap); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4783 | } |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 4784 | |
| 4785 | for(i=0; i<nShmPerMap; i++){ |
| 4786 | pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i]; |
| 4787 | } |
| 4788 | pShmNode->nRegion += nShmPerMap; |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4789 | } |
| 4790 | } |
| 4791 | |
| 4792 | shmpage_out: |
| 4793 | if( pShmNode->nRegion>iRegion ){ |
| 4794 | *pp = pShmNode->apRegion[iRegion]; |
| 4795 | }else{ |
| 4796 | *pp = 0; |
| 4797 | } |
drh | 66dfec8b | 2011-06-01 20:01:49 +0000 | [diff] [blame] | 4798 | if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4799 | sqlite3_mutex_leave(pShmNode->pShmMutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4800 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
| 4803 | /* |
dan | 8337da6 | 2020-08-28 19:27:15 +0000 | [diff] [blame] | 4804 | ** Check that the pShmNode->aLock[] array comports with the locking bitmasks |
| 4805 | ** held by each client. Return true if it does, or false otherwise. This |
| 4806 | ** is to be used in an assert(). e.g. |
| 4807 | ** |
| 4808 | ** assert( assertLockingArrayOk(pShmNode) ); |
| 4809 | */ |
| 4810 | #ifdef SQLITE_DEBUG |
| 4811 | static int assertLockingArrayOk(unixShmNode *pShmNode){ |
| 4812 | unixShm *pX; |
| 4813 | int aLock[SQLITE_SHM_NLOCK]; |
| 4814 | assert( sqlite3_mutex_held(pShmNode->pShmMutex) ); |
| 4815 | |
| 4816 | memset(aLock, 0, sizeof(aLock)); |
| 4817 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 4818 | int i; |
| 4819 | for(i=0; i<SQLITE_SHM_NLOCK; i++){ |
| 4820 | if( pX->exclMask & (1<<i) ){ |
| 4821 | assert( aLock[i]==0 ); |
| 4822 | aLock[i] = -1; |
| 4823 | }else if( pX->sharedMask & (1<<i) ){ |
| 4824 | assert( aLock[i]>=0 ); |
| 4825 | aLock[i]++; |
| 4826 | } |
| 4827 | } |
| 4828 | } |
| 4829 | |
| 4830 | assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) ); |
| 4831 | return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0); |
| 4832 | } |
| 4833 | #endif |
| 4834 | |
| 4835 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4836 | ** Change the lock state for a shared-memory segment. |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4837 | ** |
| 4838 | ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little |
| 4839 | ** different here than in posix. In xShmLock(), one can go from unlocked |
| 4840 | ** to shared and back or from unlocked to exclusive and back. But one may |
| 4841 | ** not go from shared to exclusive or from exclusive to shared. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4842 | */ |
| 4843 | static int unixShmLock( |
| 4844 | sqlite3_file *fd, /* Database file holding the shared memory */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4845 | int ofst, /* First lock to acquire or release */ |
| 4846 | int n, /* Number of locks to acquire or release */ |
| 4847 | int flags /* What to do with the lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4848 | ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4849 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 4850 | unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4851 | unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 4852 | int rc = SQLITE_OK; /* Result code */ |
| 4853 | u16 mask; /* Mask of locks to take or release */ |
dan | 8337da6 | 2020-08-28 19:27:15 +0000 | [diff] [blame] | 4854 | int *aLock = pShmNode->aLock; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4855 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4856 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 4857 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4858 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4859 | assert( n>=1 ); |
| 4860 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 4861 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 4862 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 4863 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 4864 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 4865 | assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 4866 | assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4867 | |
dan | 58021b2 | 2020-05-05 20:30:07 +0000 | [diff] [blame] | 4868 | /* Check that, if this to be a blocking lock, no locks that occur later |
| 4869 | ** in the following list than the lock being obtained are already held: |
dan | 97ccc1b | 2020-03-27 17:23:17 +0000 | [diff] [blame] | 4870 | ** |
| 4871 | ** 1. Checkpointer lock (ofst==1). |
dan | 58021b2 | 2020-05-05 20:30:07 +0000 | [diff] [blame] | 4872 | ** 2. Write lock (ofst==0). |
dan | 97ccc1b | 2020-03-27 17:23:17 +0000 | [diff] [blame] | 4873 | ** 3. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK). |
dan | 97ccc1b | 2020-03-27 17:23:17 +0000 | [diff] [blame] | 4874 | ** |
| 4875 | ** In other words, if this is a blocking lock, none of the locks that |
| 4876 | ** occur later in the above list than the lock being obtained may be |
dan | d31fcd4 | 2020-05-29 11:07:20 +0000 | [diff] [blame] | 4877 | ** held. |
| 4878 | ** |
| 4879 | ** It is not permitted to block on the RECOVER lock. |
| 4880 | */ |
dan | 97ccc1b | 2020-03-27 17:23:17 +0000 | [diff] [blame] | 4881 | #ifdef SQLITE_ENABLE_SETLK_TIMEOUT |
dan | 58021b2 | 2020-05-05 20:30:07 +0000 | [diff] [blame] | 4882 | assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || ( |
| 4883 | (ofst!=2) /* not RECOVER */ |
dan | 58021b2 | 2020-05-05 20:30:07 +0000 | [diff] [blame] | 4884 | && (ofst!=1 || (p->exclMask|p->sharedMask)==0) |
| 4885 | && (ofst!=0 || (p->exclMask|p->sharedMask)<3) |
| 4886 | && (ofst<3 || (p->exclMask|p->sharedMask)<(1<<ofst)) |
| 4887 | )); |
dan | 97ccc1b | 2020-03-27 17:23:17 +0000 | [diff] [blame] | 4888 | #endif |
| 4889 | |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 4890 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4891 | assert( n>1 || mask==(1<<ofst) ); |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4892 | sqlite3_mutex_enter(pShmNode->pShmMutex); |
dan | 8337da6 | 2020-08-28 19:27:15 +0000 | [diff] [blame] | 4893 | assert( assertLockingArrayOk(pShmNode) ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4894 | if( flags & SQLITE_SHM_UNLOCK ){ |
dan | 6acdee6 | 2020-08-28 20:01:06 +0000 | [diff] [blame] | 4895 | if( (p->exclMask|p->sharedMask) & mask ){ |
| 4896 | int ii; |
| 4897 | int bUnlock = 1; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4898 | |
dan | 6acdee6 | 2020-08-28 20:01:06 +0000 | [diff] [blame] | 4899 | for(ii=ofst; ii<ofst+n; ii++){ |
| 4900 | if( aLock[ii]>((p->sharedMask & (1<<ii)) ? 1 : 0) ){ |
| 4901 | bUnlock = 0; |
| 4902 | } |
dan | 8337da6 | 2020-08-28 19:27:15 +0000 | [diff] [blame] | 4903 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4904 | |
dan | 6acdee6 | 2020-08-28 20:01:06 +0000 | [diff] [blame] | 4905 | if( bUnlock ){ |
| 4906 | rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
| 4907 | if( rc==SQLITE_OK ){ |
| 4908 | memset(&aLock[ofst], 0, sizeof(int)*n); |
| 4909 | } |
drh | 78043e8 | 2020-11-06 16:48:55 +0000 | [diff] [blame] | 4910 | }else if( ALWAYS(p->sharedMask & (1<<ofst)) ){ |
dan | 6acdee6 | 2020-08-28 20:01:06 +0000 | [diff] [blame] | 4911 | assert( n==1 && aLock[ofst]>1 ); |
| 4912 | aLock[ofst]--; |
| 4913 | } |
| 4914 | |
| 4915 | /* Undo the local locks */ |
dan | 8337da6 | 2020-08-28 19:27:15 +0000 | [diff] [blame] | 4916 | if( rc==SQLITE_OK ){ |
dan | 6acdee6 | 2020-08-28 20:01:06 +0000 | [diff] [blame] | 4917 | p->exclMask &= ~mask; |
| 4918 | p->sharedMask &= ~mask; |
| 4919 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4920 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4921 | }else if( flags & SQLITE_SHM_SHARED ){ |
dan | 8337da6 | 2020-08-28 19:27:15 +0000 | [diff] [blame] | 4922 | assert( n==1 ); |
| 4923 | assert( (p->exclMask & (1<<ofst))==0 ); |
| 4924 | if( (p->sharedMask & mask)==0 ){ |
| 4925 | if( aLock[ofst]<0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4926 | rc = SQLITE_BUSY; |
dan | 8337da6 | 2020-08-28 19:27:15 +0000 | [diff] [blame] | 4927 | }else if( aLock[ofst]==0 ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4928 | rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4929 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4930 | |
dan | 8337da6 | 2020-08-28 19:27:15 +0000 | [diff] [blame] | 4931 | /* Get the local shared locks */ |
| 4932 | if( rc==SQLITE_OK ){ |
| 4933 | p->sharedMask |= mask; |
| 4934 | aLock[ofst]++; |
| 4935 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4936 | } |
| 4937 | }else{ |
| 4938 | /* Make sure no sibling connections hold locks that will block this |
dan | 8337da6 | 2020-08-28 19:27:15 +0000 | [diff] [blame] | 4939 | ** lock. If any do, return SQLITE_BUSY right away. */ |
| 4940 | int ii; |
| 4941 | for(ii=ofst; ii<ofst+n; ii++){ |
| 4942 | assert( (p->sharedMask & mask)==0 ); |
drh | 78043e8 | 2020-11-06 16:48:55 +0000 | [diff] [blame] | 4943 | if( ALWAYS((p->exclMask & (1<<ii))==0) && aLock[ii] ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4944 | rc = SQLITE_BUSY; |
| 4945 | break; |
| 4946 | } |
| 4947 | } |
dan | 8337da6 | 2020-08-28 19:27:15 +0000 | [diff] [blame] | 4948 | |
| 4949 | /* Get the exclusive locks at the system level. Then if successful |
| 4950 | ** also update the in-memory values. */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4951 | if( rc==SQLITE_OK ){ |
drh | bbf76ee | 2015-03-10 20:22:35 +0000 | [diff] [blame] | 4952 | rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4953 | if( rc==SQLITE_OK ){ |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 4954 | assert( (p->sharedMask & mask)==0 ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 4955 | p->exclMask |= mask; |
dan | 8337da6 | 2020-08-28 19:27:15 +0000 | [diff] [blame] | 4956 | for(ii=ofst; ii<ofst+n; ii++){ |
| 4957 | aLock[ii] = -1; |
| 4958 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4959 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4960 | } |
| 4961 | } |
dan | 8337da6 | 2020-08-28 19:27:15 +0000 | [diff] [blame] | 4962 | assert( assertLockingArrayOk(pShmNode) ); |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 4963 | sqlite3_mutex_leave(pShmNode->pShmMutex); |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 4964 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 4965 | p->id, osGetpid(0), p->sharedMask, p->exclMask)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4966 | return rc; |
| 4967 | } |
| 4968 | |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4969 | /* |
| 4970 | ** Implement a memory barrier or memory fence on shared memory. |
| 4971 | ** |
| 4972 | ** All loads and stores begun before the barrier must complete before |
| 4973 | ** any load or store begun after the barrier. |
| 4974 | */ |
| 4975 | static void unixShmBarrier( |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4976 | sqlite3_file *fd /* Database file holding the shared memory */ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4977 | ){ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 4978 | UNUSED_PARAMETER(fd); |
drh | 22c733d | 2015-09-24 12:40:43 +0000 | [diff] [blame] | 4979 | sqlite3MemoryBarrier(); /* compiler-defined memory barrier */ |
dan | a86acc2 | 2018-09-12 20:32:19 +0000 | [diff] [blame] | 4980 | assert( fd->pMethods->xLock==nolockLock |
| 4981 | || unixFileMutexNotheld((unixFile*)fd) |
| 4982 | ); |
drh | 22c733d | 2015-09-24 12:40:43 +0000 | [diff] [blame] | 4983 | unixEnterMutex(); /* Also mutex, for redundancy */ |
drh | b29ad85 | 2010-06-01 00:03:57 +0000 | [diff] [blame] | 4984 | unixLeaveMutex(); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 4985 | } |
| 4986 | |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4987 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4988 | ** Close a connection to shared-memory. Delete the underlying |
| 4989 | ** storage if deleteFlag is true. |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 4990 | ** |
| 4991 | ** If there is no shared memory associated with the connection then this |
| 4992 | ** routine is a harmless no-op. |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 4993 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4994 | static int unixShmUnmap( |
| 4995 | sqlite3_file *fd, /* The underlying database file */ |
| 4996 | int deleteFlag /* Delete shared-memory if true */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 4997 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 4998 | unixShm *p; /* The connection to be closed */ |
| 4999 | unixShmNode *pShmNode; /* The underlying shared-memory file */ |
| 5000 | unixShm **pp; /* For looping over sibling connections */ |
| 5001 | unixFile *pDbFd; /* The underlying database file */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 5002 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 5003 | pDbFd = (unixFile*)fd; |
| 5004 | p = pDbFd->pShm; |
| 5005 | if( p==0 ) return SQLITE_OK; |
| 5006 | pShmNode = p->pShmNode; |
| 5007 | |
| 5008 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 5009 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 5010 | |
| 5011 | /* Remove connection p from the set of connections associated |
| 5012 | ** with pShmNode */ |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 5013 | sqlite3_mutex_enter(pShmNode->pShmMutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 5014 | for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 5015 | *pp = p->pNext; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 5016 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 5017 | /* Free the connection p */ |
| 5018 | sqlite3_free(p); |
| 5019 | pDbFd->pShm = 0; |
drh | 24efa54 | 2018-10-02 19:36:40 +0000 | [diff] [blame] | 5020 | sqlite3_mutex_leave(pShmNode->pShmMutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 5021 | |
| 5022 | /* If pShmNode->nRef has reached 0, then close the underlying |
| 5023 | ** shared-memory file, too */ |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 5024 | assert( unixFileMutexNotheld(pDbFd) ); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 5025 | unixEnterMutex(); |
| 5026 | assert( pShmNode->nRef>0 ); |
| 5027 | pShmNode->nRef--; |
| 5028 | if( pShmNode->nRef==0 ){ |
drh | 8820c8d | 2018-10-02 19:58:08 +0000 | [diff] [blame] | 5029 | if( deleteFlag && pShmNode->hShm>=0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 5030 | osUnlink(pShmNode->zFilename); |
| 5031 | } |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 5032 | unixShmPurge(pDbFd); |
| 5033 | } |
| 5034 | unixLeaveMutex(); |
| 5035 | |
| 5036 | return SQLITE_OK; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 5037 | } |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 5038 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 5039 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5040 | #else |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 5041 | # define unixShmMap 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 5042 | # define unixShmLock 0 |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 5043 | # define unixShmBarrier 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 5044 | # define unixShmUnmap 0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5045 | #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 5046 | |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 5047 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5048 | /* |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5049 | ** If it is currently memory mapped, unmap file pFd. |
dan | d306e1a | 2013-03-20 18:25:49 +0000 | [diff] [blame] | 5050 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5051 | static void unixUnmapfile(unixFile *pFd){ |
| 5052 | assert( pFd->nFetchOut==0 ); |
| 5053 | if( pFd->pMapRegion ){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5054 | osMunmap(pFd->pMapRegion, pFd->mmapSizeActual); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5055 | pFd->pMapRegion = 0; |
| 5056 | pFd->mmapSize = 0; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5057 | pFd->mmapSizeActual = 0; |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5058 | } |
| 5059 | } |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5060 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5061 | /* |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5062 | ** Attempt to set the size of the memory mapping maintained by file |
| 5063 | ** descriptor pFd to nNew bytes. Any existing mapping is discarded. |
| 5064 | ** |
| 5065 | ** If successful, this function sets the following variables: |
| 5066 | ** |
| 5067 | ** unixFile.pMapRegion |
| 5068 | ** unixFile.mmapSize |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5069 | ** unixFile.mmapSizeActual |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5070 | ** |
| 5071 | ** If unsuccessful, an error message is logged via sqlite3_log() and |
| 5072 | ** the three variables above are zeroed. In this case SQLite should |
| 5073 | ** continue accessing the database using the xRead() and xWrite() |
| 5074 | ** methods. |
| 5075 | */ |
| 5076 | static void unixRemapfile( |
| 5077 | unixFile *pFd, /* File descriptor object */ |
| 5078 | i64 nNew /* Required mapping size */ |
| 5079 | ){ |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 5080 | const char *zErr = "mmap"; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5081 | int h = pFd->h; /* File descriptor open on db file */ |
| 5082 | u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5083 | i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */ |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5084 | u8 *pNew = 0; /* Location of new mapping */ |
| 5085 | int flags = PROT_READ; /* Flags to pass to mmap() */ |
| 5086 | |
| 5087 | assert( pFd->nFetchOut==0 ); |
| 5088 | assert( nNew>pFd->mmapSize ); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5089 | assert( nNew<=pFd->mmapSizeMax ); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5090 | assert( nNew>0 ); |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5091 | assert( pFd->mmapSizeActual>=pFd->mmapSize ); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 5092 | assert( MAP_FAILED!=0 ); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5093 | |
dan | fe33e39 | 2015-11-17 20:56:06 +0000 | [diff] [blame] | 5094 | #ifdef SQLITE_MMAP_READWRITE |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5095 | if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE; |
dan | fe33e39 | 2015-11-17 20:56:06 +0000 | [diff] [blame] | 5096 | #endif |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5097 | |
| 5098 | if( pOrig ){ |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 5099 | #if HAVE_MREMAP |
| 5100 | i64 nReuse = pFd->mmapSize; |
| 5101 | #else |
dan | bc76063 | 2014-03-20 09:42:09 +0000 | [diff] [blame] | 5102 | const int szSyspage = osGetpagesize(); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5103 | i64 nReuse = (pFd->mmapSize & ~(szSyspage-1)); |
dan | 781e34c | 2014-03-20 08:59:47 +0000 | [diff] [blame] | 5104 | #endif |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5105 | u8 *pReq = &pOrig[nReuse]; |
| 5106 | |
| 5107 | /* Unmap any pages of the existing mapping that cannot be reused. */ |
| 5108 | if( nReuse!=nOrig ){ |
| 5109 | osMunmap(pReq, nOrig-nReuse); |
| 5110 | } |
| 5111 | |
| 5112 | #if HAVE_MREMAP |
| 5113 | pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 5114 | zErr = "mremap"; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5115 | #else |
| 5116 | pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse); |
| 5117 | if( pNew!=MAP_FAILED ){ |
| 5118 | if( pNew!=pReq ){ |
| 5119 | osMunmap(pNew, nNew - nReuse); |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 5120 | pNew = 0; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5121 | }else{ |
| 5122 | pNew = pOrig; |
| 5123 | } |
| 5124 | } |
| 5125 | #endif |
| 5126 | |
dan | 48ccef8 | 2013-04-02 20:55:01 +0000 | [diff] [blame] | 5127 | /* The attempt to extend the existing mapping failed. Free it. */ |
| 5128 | if( pNew==MAP_FAILED || pNew==0 ){ |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5129 | osMunmap(pOrig, nReuse); |
| 5130 | } |
| 5131 | } |
| 5132 | |
| 5133 | /* If pNew is still NULL, try to create an entirely new mapping. */ |
| 5134 | if( pNew==0 ){ |
| 5135 | pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0); |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5136 | } |
| 5137 | |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 5138 | if( pNew==MAP_FAILED ){ |
| 5139 | pNew = 0; |
| 5140 | nNew = 0; |
| 5141 | unixLogError(SQLITE_OK, zErr, pFd->zPath); |
| 5142 | |
| 5143 | /* If the mmap() above failed, assume that all subsequent mmap() calls |
| 5144 | ** will probably fail too. Fall back to using xRead/xWrite exclusively |
| 5145 | ** in this case. */ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5146 | pFd->mmapSizeMax = 0; |
dan | 4ff7bc4 | 2013-04-02 12:04:09 +0000 | [diff] [blame] | 5147 | } |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5148 | pFd->pMapRegion = (void *)pNew; |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5149 | pFd->mmapSize = pFd->mmapSizeActual = nNew; |
dan | e6ecd66 | 2013-04-01 17:56:59 +0000 | [diff] [blame] | 5150 | } |
| 5151 | |
| 5152 | /* |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5153 | ** Memory map or remap the file opened by file-descriptor pFd (if the file |
| 5154 | ** is already mapped, the existing mapping is replaced by the new). Or, if |
| 5155 | ** there already exists a mapping for this file, and there are still |
| 5156 | ** outstanding xFetch() references to it, this function is a no-op. |
| 5157 | ** |
| 5158 | ** If parameter nByte is non-negative, then it is the requested size of |
| 5159 | ** the mapping to create. Otherwise, if nByte is less than zero, then the |
| 5160 | ** requested size is the size of the file on disk. The actual size of the |
| 5161 | ** created mapping is either the requested size or the value configured |
drh | 0d0614b | 2013-03-25 23:09:28 +0000 | [diff] [blame] | 5162 | ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller. |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5163 | ** |
| 5164 | ** SQLITE_OK is returned if no error occurs (even if the mapping is not |
| 5165 | ** recreated as a result of outstanding references) or an SQLite error |
| 5166 | ** code otherwise. |
| 5167 | */ |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 5168 | static int unixMapfile(unixFile *pFd, i64 nMap){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5169 | assert( nMap>=0 || pFd->nFetchOut==0 ); |
drh | 333e6ca | 2015-12-02 15:44:39 +0000 | [diff] [blame] | 5170 | assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5171 | if( pFd->nFetchOut>0 ) return SQLITE_OK; |
| 5172 | |
| 5173 | if( nMap<0 ){ |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 5174 | struct stat statbuf; /* Low-level file information */ |
drh | f3b1ed0 | 2015-12-02 13:11:03 +0000 | [diff] [blame] | 5175 | if( osFstat(pFd->h, &statbuf) ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5176 | return SQLITE_IOERR_FSTAT; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 5177 | } |
drh | 3044b51 | 2014-06-16 16:41:52 +0000 | [diff] [blame] | 5178 | nMap = statbuf.st_size; |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5179 | } |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5180 | if( nMap>pFd->mmapSizeMax ){ |
| 5181 | nMap = pFd->mmapSizeMax; |
dan | eb97b29 | 2013-03-20 14:26:59 +0000 | [diff] [blame] | 5182 | } |
| 5183 | |
drh | 333e6ca | 2015-12-02 15:44:39 +0000 | [diff] [blame] | 5184 | assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5185 | if( nMap!=pFd->mmapSize ){ |
drh | 333e6ca | 2015-12-02 15:44:39 +0000 | [diff] [blame] | 5186 | unixRemapfile(pFd, nMap); |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5187 | } |
| 5188 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5189 | return SQLITE_OK; |
| 5190 | } |
mistachkin | e98844f | 2013-08-24 00:59:24 +0000 | [diff] [blame] | 5191 | #endif /* SQLITE_MAX_MMAP_SIZE>0 */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5192 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5193 | /* |
| 5194 | ** If possible, return a pointer to a mapping of file fd starting at offset |
| 5195 | ** iOff. The mapping must be valid for at least nAmt bytes. |
| 5196 | ** |
| 5197 | ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK. |
| 5198 | ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK. |
| 5199 | ** Finally, if an error does occur, return an SQLite error code. The final |
| 5200 | ** value of *pp is undefined in this case. |
| 5201 | ** |
| 5202 | ** If this function does return a pointer, the caller must eventually |
| 5203 | ** release the reference by calling unixUnfetch(). |
| 5204 | */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5205 | static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5206 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5207 | unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 5208 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5209 | *pp = 0; |
| 5210 | |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 5211 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 5212 | if( pFd->mmapSizeMax>0 ){ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5213 | if( pFd->pMapRegion==0 ){ |
| 5214 | int rc = unixMapfile(pFd, -1); |
| 5215 | if( rc!=SQLITE_OK ) return rc; |
| 5216 | } |
| 5217 | if( pFd->mmapSize >= iOff+nAmt ){ |
| 5218 | *pp = &((u8 *)pFd->pMapRegion)[iOff]; |
| 5219 | pFd->nFetchOut++; |
| 5220 | } |
| 5221 | } |
drh | 6e0b6d5 | 2013-04-09 16:19:20 +0000 | [diff] [blame] | 5222 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5223 | return SQLITE_OK; |
| 5224 | } |
| 5225 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5226 | /* |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 5227 | ** If the third argument is non-NULL, then this function releases a |
| 5228 | ** reference obtained by an earlier call to unixFetch(). The second |
| 5229 | ** argument passed to this function must be the same as the corresponding |
| 5230 | ** argument that was passed to the unixFetch() invocation. |
| 5231 | ** |
| 5232 | ** Or, if the third argument is NULL, then this function is being called |
| 5233 | ** to inform the VFS layer that, according to POSIX, any existing mapping |
| 5234 | ** may now be invalid and should be unmapped. |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5235 | */ |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 5236 | static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){ |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5237 | #if SQLITE_MAX_MMAP_SIZE>0 |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 5238 | unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
dan | 9871c59 | 2014-01-10 16:40:21 +0000 | [diff] [blame] | 5239 | UNUSED_PARAMETER(iOff); |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 5240 | |
dan | aef49d7 | 2013-03-25 16:28:54 +0000 | [diff] [blame] | 5241 | /* If p==0 (unmap the entire file) then there must be no outstanding |
| 5242 | ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference), |
| 5243 | ** then there must be at least one outstanding. */ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5244 | assert( (p==0)==(pFd->nFetchOut==0) ); |
| 5245 | |
dan | df737fe | 2013-03-25 17:00:24 +0000 | [diff] [blame] | 5246 | /* If p!=0, it must match the iOff value. */ |
| 5247 | assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] ); |
| 5248 | |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5249 | if( p ){ |
| 5250 | pFd->nFetchOut--; |
| 5251 | }else{ |
| 5252 | unixUnmapfile(pFd); |
| 5253 | } |
| 5254 | |
| 5255 | assert( pFd->nFetchOut>=0 ); |
drh | 1bcbc62 | 2014-01-09 13:39:07 +0000 | [diff] [blame] | 5256 | #else |
| 5257 | UNUSED_PARAMETER(fd); |
| 5258 | UNUSED_PARAMETER(p); |
dan | 9871c59 | 2014-01-10 16:40:21 +0000 | [diff] [blame] | 5259 | UNUSED_PARAMETER(iOff); |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5260 | #endif |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5261 | return SQLITE_OK; |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5262 | } |
| 5263 | |
| 5264 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5265 | ** Here ends the implementation of all sqlite3_file methods. |
| 5266 | ** |
| 5267 | ********************** End sqlite3_file Methods ******************************* |
| 5268 | ******************************************************************************/ |
| 5269 | |
| 5270 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5271 | ** This division contains definitions of sqlite3_io_methods objects that |
| 5272 | ** implement various file locking strategies. It also contains definitions |
| 5273 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 5274 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 5275 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 5276 | ** the correct finder-function for that VFS. |
| 5277 | ** |
| 5278 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 5279 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 5280 | ** looks at the filesystem type and tries to guess the best locking |
| 5281 | ** strategy from that. |
| 5282 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5283 | ** For finder-function F, two objects are created: |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5284 | ** |
| 5285 | ** (1) The real finder-function named "FImpt()". |
| 5286 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5287 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5288 | ** |
| 5289 | ** |
| 5290 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 5291 | ** objects. We have to do this instead of letting pAppData point |
| 5292 | ** directly at the finder-function since C90 rules prevent a void* |
| 5293 | ** from be cast into a function pointer. |
| 5294 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5295 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5296 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5297 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5298 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 5299 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 5300 | ** |
| 5301 | ** * An I/O method finder function called FINDER that returns a pointer |
| 5302 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5303 | */ |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 5304 | #define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP) \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5305 | static const sqlite3_io_methods METHOD = { \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5306 | VERSION, /* iVersion */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5307 | CLOSE, /* xClose */ \ |
| 5308 | unixRead, /* xRead */ \ |
| 5309 | unixWrite, /* xWrite */ \ |
| 5310 | unixTruncate, /* xTruncate */ \ |
| 5311 | unixSync, /* xSync */ \ |
| 5312 | unixFileSize, /* xFileSize */ \ |
| 5313 | LOCK, /* xLock */ \ |
| 5314 | UNLOCK, /* xUnlock */ \ |
| 5315 | CKLOCK, /* xCheckReservedLock */ \ |
| 5316 | unixFileControl, /* xFileControl */ \ |
| 5317 | unixSectorSize, /* xSectorSize */ \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5318 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5319 | SHMMAP, /* xShmMap */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 5320 | unixShmLock, /* xShmLock */ \ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 5321 | unixShmBarrier, /* xShmBarrier */ \ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5322 | unixShmUnmap, /* xShmUnmap */ \ |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame] | 5323 | unixFetch, /* xFetch */ \ |
| 5324 | unixUnfetch, /* xUnfetch */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5325 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5326 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 5327 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5328 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5329 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5330 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5331 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5332 | |
| 5333 | /* |
| 5334 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 5335 | ** locking strategies. Functions that return pointers to these methods |
| 5336 | ** are also created. |
| 5337 | */ |
| 5338 | IOMETHODS( |
| 5339 | posixIoFinder, /* Finder function name */ |
| 5340 | posixIoMethods, /* sqlite3_io_methods object name */ |
dan | 5d8a137 | 2013-03-19 19:28:06 +0000 | [diff] [blame] | 5341 | 3, /* shared memory and mmap are enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5342 | unixClose, /* xClose method */ |
| 5343 | unixLock, /* xLock method */ |
| 5344 | unixUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5345 | unixCheckReservedLock, /* xCheckReservedLock method */ |
| 5346 | unixShmMap /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5347 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5348 | IOMETHODS( |
| 5349 | nolockIoFinder, /* Finder function name */ |
| 5350 | nolockIoMethods, /* sqlite3_io_methods object name */ |
drh | 3e2c842 | 2018-08-13 11:32:07 +0000 | [diff] [blame] | 5351 | 3, /* shared memory and mmap are enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5352 | nolockClose, /* xClose method */ |
| 5353 | nolockLock, /* xLock method */ |
| 5354 | nolockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5355 | nolockCheckReservedLock, /* xCheckReservedLock method */ |
| 5356 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5357 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5358 | IOMETHODS( |
| 5359 | dotlockIoFinder, /* Finder function name */ |
| 5360 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5361 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5362 | dotlockClose, /* xClose method */ |
| 5363 | dotlockLock, /* xLock method */ |
| 5364 | dotlockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5365 | dotlockCheckReservedLock, /* xCheckReservedLock method */ |
| 5366 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5367 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5368 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5369 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5370 | IOMETHODS( |
| 5371 | flockIoFinder, /* Finder function name */ |
| 5372 | flockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5373 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5374 | flockClose, /* xClose method */ |
| 5375 | flockLock, /* xLock method */ |
| 5376 | flockUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5377 | flockCheckReservedLock, /* xCheckReservedLock method */ |
| 5378 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5379 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5380 | #endif |
| 5381 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5382 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5383 | IOMETHODS( |
| 5384 | semIoFinder, /* Finder function name */ |
| 5385 | semIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5386 | 1, /* shared memory is disabled */ |
drh | 8cd5b25 | 2015-03-02 22:06:43 +0000 | [diff] [blame] | 5387 | semXClose, /* xClose method */ |
| 5388 | semXLock, /* xLock method */ |
| 5389 | semXUnlock, /* xUnlock method */ |
| 5390 | semXCheckReservedLock, /* xCheckReservedLock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5391 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5392 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5393 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5394 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5395 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5396 | IOMETHODS( |
| 5397 | afpIoFinder, /* Finder function name */ |
| 5398 | afpIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5399 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5400 | afpClose, /* xClose method */ |
| 5401 | afpLock, /* xLock method */ |
| 5402 | afpUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5403 | afpCheckReservedLock, /* xCheckReservedLock method */ |
| 5404 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5405 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5406 | #endif |
| 5407 | |
| 5408 | /* |
| 5409 | ** The proxy locking method is a "super-method" in the sense that it |
| 5410 | ** opens secondary file descriptors for the conch and lock files and |
| 5411 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 5412 | ** secondary files. For this reason, the division that implements |
| 5413 | ** proxy locking is located much further down in the file. But we need |
| 5414 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 5415 | ** for proxy locking here. So we forward declare the I/O methods. |
| 5416 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5417 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5418 | static int proxyClose(sqlite3_file*); |
| 5419 | static int proxyLock(sqlite3_file*, int); |
| 5420 | static int proxyUnlock(sqlite3_file*, int); |
| 5421 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5422 | IOMETHODS( |
| 5423 | proxyIoFinder, /* Finder function name */ |
| 5424 | proxyIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5425 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5426 | proxyClose, /* xClose method */ |
| 5427 | proxyLock, /* xLock method */ |
| 5428 | proxyUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5429 | proxyCheckReservedLock, /* xCheckReservedLock method */ |
| 5430 | 0 /* xShmMap method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5431 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5432 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5433 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5434 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 5435 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 5436 | IOMETHODS( |
| 5437 | nfsIoFinder, /* Finder function name */ |
| 5438 | nfsIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 5439 | 1, /* shared memory is disabled */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5440 | unixClose, /* xClose method */ |
| 5441 | unixLock, /* xLock method */ |
| 5442 | nfsUnlock, /* xUnlock method */ |
drh | d9f9441 | 2014-09-22 03:22:27 +0000 | [diff] [blame] | 5443 | unixCheckReservedLock, /* xCheckReservedLock method */ |
| 5444 | 0 /* xShmMap method */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5445 | ) |
| 5446 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5447 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5448 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5449 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5450 | ** This "finder" function attempts to determine the best locking strategy |
| 5451 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5452 | ** object that implements that strategy. |
| 5453 | ** |
| 5454 | ** This is for MacOSX only. |
| 5455 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5456 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5457 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5458 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5459 | ){ |
| 5460 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5461 | const char *zFilesystem; /* Filesystem type name */ |
| 5462 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5463 | } aMap[] = { |
| 5464 | { "hfs", &posixIoMethods }, |
| 5465 | { "ufs", &posixIoMethods }, |
| 5466 | { "afpfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5467 | { "smbfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5468 | { "webdav", &nolockIoMethods }, |
| 5469 | { 0, 0 } |
| 5470 | }; |
| 5471 | int i; |
| 5472 | struct statfs fsInfo; |
| 5473 | struct flock lockInfo; |
| 5474 | |
| 5475 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5476 | /* If filePath==NULL that means we are dealing with a transient file |
| 5477 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5478 | return &nolockIoMethods; |
| 5479 | } |
| 5480 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 5481 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 5482 | return &nolockIoMethods; |
| 5483 | } |
| 5484 | for(i=0; aMap[i].zFilesystem; i++){ |
| 5485 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 5486 | return aMap[i].pMethods; |
| 5487 | } |
| 5488 | } |
| 5489 | } |
| 5490 | |
| 5491 | /* Default case. Handles, amongst others, "nfs". |
| 5492 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 5493 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5494 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5495 | lockInfo.l_len = 1; |
| 5496 | lockInfo.l_start = 0; |
| 5497 | lockInfo.l_whence = SEEK_SET; |
| 5498 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5499 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5500 | if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 5501 | return &nfsIoMethods; |
| 5502 | } else { |
| 5503 | return &posixIoMethods; |
| 5504 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5505 | }else{ |
| 5506 | return &dotlockIoMethods; |
| 5507 | } |
| 5508 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5509 | static const sqlite3_io_methods |
| 5510 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5511 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5512 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5513 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5514 | #if OS_VXWORKS |
| 5515 | /* |
| 5516 | ** This "finder" function for VxWorks checks to see if posix advisory |
| 5517 | ** locking works. If it does, then that is what is used. If it does not |
| 5518 | ** work, then fallback to named semaphore locking. |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5519 | */ |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5520 | static const sqlite3_io_methods *vxworksIoFinderImpl( |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5521 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5522 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5523 | ){ |
| 5524 | struct flock lockInfo; |
| 5525 | |
| 5526 | if( !filePath ){ |
| 5527 | /* If filePath==NULL that means we are dealing with a transient file |
| 5528 | ** that does not need to be locked. */ |
| 5529 | return &nolockIoMethods; |
| 5530 | } |
| 5531 | |
| 5532 | /* Test if fcntl() is supported and use POSIX style locks. |
| 5533 | ** Otherwise fall back to the named semaphore method. |
| 5534 | */ |
| 5535 | lockInfo.l_len = 1; |
| 5536 | lockInfo.l_start = 0; |
| 5537 | lockInfo.l_whence = SEEK_SET; |
| 5538 | lockInfo.l_type = F_RDLCK; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5539 | if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5540 | return &posixIoMethods; |
| 5541 | }else{ |
| 5542 | return &semIoMethods; |
| 5543 | } |
| 5544 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5545 | static const sqlite3_io_methods |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5546 | *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5547 | |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 5548 | #endif /* OS_VXWORKS */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 5549 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5550 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5551 | ** An abstract type for a pointer to an IO method finder function: |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5552 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5553 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5554 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5555 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5556 | /**************************************************************************** |
| 5557 | **************************** sqlite3_vfs methods **************************** |
| 5558 | ** |
| 5559 | ** This division contains the implementation of methods on the |
| 5560 | ** sqlite3_vfs object. |
| 5561 | */ |
| 5562 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 5563 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5564 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5565 | */ |
| 5566 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5567 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5568 | int h, /* Open file descriptor of file being opened */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 5569 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5570 | const char *zFilename, /* Name of the file being opened */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5571 | int ctrlFlags /* Zero or more UNIXFILE_* values */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5572 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5573 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5574 | unixFile *pNew = (unixFile *)pId; |
| 5575 | int rc = SQLITE_OK; |
| 5576 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5577 | assert( pNew->pInode==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 5578 | |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5579 | /* No locking occurs in temporary files */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5580 | assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 ); |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5581 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5582 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5583 | pNew->h = h; |
drh | de60fc2 | 2011-12-14 17:53:36 +0000 | [diff] [blame] | 5584 | pNew->pVfs = pVfs; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 5585 | pNew->zPath = zFilename; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5586 | pNew->ctrlFlags = (u8)ctrlFlags; |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5587 | #if SQLITE_MAX_MMAP_SIZE>0 |
dan | ede01a9 | 2013-05-17 12:10:52 +0000 | [diff] [blame] | 5588 | pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap; |
mistachkin | b5ca3cb | 2013-08-24 01:12:03 +0000 | [diff] [blame] | 5589 | #endif |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5590 | if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0), |
| 5591 | "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 5592 | pNew->ctrlFlags |= UNIXFILE_PSOW; |
drh | bec7c97 | 2011-12-23 00:25:02 +0000 | [diff] [blame] | 5593 | } |
drh | 503a686 | 2013-03-01 01:07:17 +0000 | [diff] [blame] | 5594 | if( strcmp(pVfs->zName,"unix-excl")==0 ){ |
drh | f12b3f6 | 2011-12-21 14:42:29 +0000 | [diff] [blame] | 5595 | pNew->ctrlFlags |= UNIXFILE_EXCL; |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 5596 | } |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 5597 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5598 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 5599 | pNew->pId = vxworksFindFileId(zFilename); |
| 5600 | if( pNew->pId==0 ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5601 | ctrlFlags |= UNIXFILE_NOLOCK; |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5602 | rc = SQLITE_NOMEM_BKPT; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5603 | } |
| 5604 | #endif |
| 5605 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 5606 | if( ctrlFlags & UNIXFILE_NOLOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5607 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5608 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 5609 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5610 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 5611 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 5612 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 5613 | ** zFilename remains valid until file is closed, to support */ |
| 5614 | pNew->lockingContext = (void*)zFilename; |
| 5615 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 5616 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5617 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5618 | if( pLockingStyle == &posixIoMethods |
| 5619 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 5620 | || pLockingStyle == &nfsIoMethods |
| 5621 | #endif |
| 5622 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5623 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5624 | rc = findInodeInfo(pNew, &pNew->pInode); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5625 | if( rc!=SQLITE_OK ){ |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 5626 | /* If an error occurred in findInodeInfo(), close the file descriptor |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5627 | ** immediately, before releasing the mutex. findInodeInfo() may fail |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5628 | ** in two scenarios: |
| 5629 | ** |
| 5630 | ** (a) A call to fstat() failed. |
| 5631 | ** (b) A malloc failed. |
| 5632 | ** |
| 5633 | ** Scenario (b) may only occur if the process is holding no other |
| 5634 | ** file descriptors open on the same file. If there were other file |
| 5635 | ** descriptors on this file, then no malloc would be required by |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5636 | ** findInodeInfo(). If this is the case, it is quite safe to close |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5637 | ** handle h - as it is guaranteed that no posix locks will be released |
| 5638 | ** by doing so. |
| 5639 | ** |
| 5640 | ** If scenario (a) caused the error then things are not so safe. The |
| 5641 | ** implicit assumption here is that if fstat() fails, things are in |
| 5642 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 5643 | */ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5644 | robust_close(pNew, h, __LINE__); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5645 | h = -1; |
| 5646 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5647 | unixLeaveMutex(); |
| 5648 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5649 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5650 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 5651 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5652 | /* AFP locking uses the file path so it needs to be included in |
| 5653 | ** the afpLockingContext. |
| 5654 | */ |
| 5655 | afpLockingContext *pCtx; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5656 | pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) ); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5657 | if( pCtx==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5658 | rc = SQLITE_NOMEM_BKPT; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5659 | }else{ |
| 5660 | /* NB: zFilename exists and remains valid until the file is closed |
| 5661 | ** according to requirement F11141. So we do not need to make a |
| 5662 | ** copy of the filename. */ |
| 5663 | pCtx->dbPath = zFilename; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5664 | pCtx->reserved = 0; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5665 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5666 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5667 | rc = findInodeInfo(pNew, &pNew->pInode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5668 | if( rc!=SQLITE_OK ){ |
| 5669 | sqlite3_free(pNew->lockingContext); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5670 | robust_close(pNew, h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5671 | h = -1; |
| 5672 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5673 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5674 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5675 | } |
| 5676 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5677 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5678 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 5679 | /* Dotfile locking uses the file path so it needs to be included in |
| 5680 | ** the dotlockLockingContext |
| 5681 | */ |
| 5682 | char *zLockFile; |
| 5683 | int nFilename; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 5684 | assert( zFilename!=0 ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5685 | nFilename = (int)strlen(zFilename) + 6; |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 5686 | zLockFile = (char *)sqlite3_malloc64(nFilename); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5687 | if( zLockFile==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5688 | rc = SQLITE_NOMEM_BKPT; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5689 | }else{ |
| 5690 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5691 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5692 | pNew->lockingContext = zLockFile; |
| 5693 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5694 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5695 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5696 | else if( pLockingStyle == &semIoMethods ){ |
| 5697 | /* Named semaphore locking uses the file path so it needs to be |
| 5698 | ** included in the semLockingContext |
| 5699 | */ |
| 5700 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5701 | rc = findInodeInfo(pNew, &pNew->pInode); |
| 5702 | if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 5703 | char *zSemName = pNew->pInode->aSemName; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5704 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 5705 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5706 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 5707 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5708 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5709 | pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 5710 | if( pNew->pInode->pSem == SEM_FAILED ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 5711 | rc = SQLITE_NOMEM_BKPT; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5712 | pNew->pInode->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5713 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5714 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5715 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5716 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 5717 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 5718 | |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 5719 | storeLastErrno(pNew, 0); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 5720 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5721 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5722 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
drh | 309e655 | 2010-02-05 18:00:26 +0000 | [diff] [blame] | 5723 | h = -1; |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 5724 | osUnlink(zFilename); |
drh | c579754 | 2013-04-27 12:13:29 +0000 | [diff] [blame] | 5725 | pNew->ctrlFlags |= UNIXFILE_DELETE; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5726 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 5727 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5728 | if( rc!=SQLITE_OK ){ |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 5729 | if( h>=0 ) robust_close(pNew, h, __LINE__); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5730 | }else{ |
drh | 0c52f5a | 2020-07-24 09:17:42 +0000 | [diff] [blame] | 5731 | pId->pMethods = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5732 | OpenCounter(+1); |
drh | fbc7e88 | 2013-04-11 01:16:15 +0000 | [diff] [blame] | 5733 | verifyDbFile(pNew); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 5734 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 5735 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 5736 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 5737 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5738 | /* |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5739 | ** Return the name of a directory in which to put temporary files. |
| 5740 | ** If no suitable temporary file directory can be found, return NULL. |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5741 | */ |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 5742 | static const char *unixTempFileDir(void){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5743 | static const char *azDirs[] = { |
| 5744 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 5745 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5746 | "/var/tmp", |
| 5747 | "/usr/tmp", |
| 5748 | "/tmp", |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5749 | "." |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5750 | }; |
drh | 2aab11f | 2016-04-29 20:30:56 +0000 | [diff] [blame] | 5751 | unsigned int i = 0; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5752 | struct stat buf; |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5753 | const char *zDir = sqlite3_temp_directory; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5754 | |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5755 | if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR"); |
| 5756 | if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); |
drh | 2aab11f | 2016-04-29 20:30:56 +0000 | [diff] [blame] | 5757 | while(1){ |
| 5758 | if( zDir!=0 |
| 5759 | && osStat(zDir, &buf)==0 |
| 5760 | && S_ISDIR(buf.st_mode) |
| 5761 | && osAccess(zDir, 03)==0 |
| 5762 | ){ |
| 5763 | return zDir; |
| 5764 | } |
| 5765 | if( i>=sizeof(azDirs)/sizeof(azDirs[0]) ) break; |
| 5766 | zDir = azDirs[i++]; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5767 | } |
drh | 7694e06 | 2016-04-21 23:37:24 +0000 | [diff] [blame] | 5768 | return 0; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5769 | } |
| 5770 | |
| 5771 | /* |
| 5772 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 5773 | ** by the calling process and must be big enough to hold at least |
| 5774 | ** pVfs->mxPathname bytes. |
| 5775 | */ |
| 5776 | static int unixGetTempname(int nBuf, char *zBuf){ |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 5777 | const char *zDir; |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5778 | int iLimit = 0; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5779 | |
| 5780 | /* It's odd to simulate an io-error here, but really this is just |
| 5781 | ** using the io-error infrastructure to test that SQLite handles this |
| 5782 | ** function failing. |
| 5783 | */ |
drh | 7694e06 | 2016-04-21 23:37:24 +0000 | [diff] [blame] | 5784 | zBuf[0] = 0; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5785 | SimulateIOError( return SQLITE_IOERR ); |
| 5786 | |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 5787 | zDir = unixTempFileDir(); |
drh | 7694e06 | 2016-04-21 23:37:24 +0000 | [diff] [blame] | 5788 | if( zDir==0 ) return SQLITE_IOERR_GETTEMPPATH; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5789 | do{ |
drh | 970942e | 2015-11-25 23:13:14 +0000 | [diff] [blame] | 5790 | u64 r; |
| 5791 | sqlite3_randomness(sizeof(r), &r); |
| 5792 | assert( nBuf>2 ); |
| 5793 | zBuf[nBuf-2] = 0; |
| 5794 | sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c", |
| 5795 | zDir, r, 0); |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 5796 | if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 5797 | }while( osAccess(zBuf,0)==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5798 | return SQLITE_OK; |
| 5799 | } |
| 5800 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5801 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 5802 | /* |
| 5803 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 5804 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 5805 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 5806 | */ |
| 5807 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 5808 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 5809 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5810 | /* |
| 5811 | ** Search for an unused file descriptor that was opened on the database |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 5812 | ** file (not a journal or super-journal file) identified by pathname |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5813 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 5814 | ** argument to this function. |
| 5815 | ** |
| 5816 | ** Such a file descriptor may exist if a database connection was closed |
| 5817 | ** but the associated file descriptor could not be closed because some |
| 5818 | ** other file descriptor open on the same file is holding a file-lock. |
| 5819 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 5820 | ** describing "Posix Advisory Locking" at the start of this file for |
| 5821 | ** further details. Also, ticket #4018. |
| 5822 | ** |
| 5823 | ** If a suitable file descriptor is found, then it is returned. If no |
| 5824 | ** such file descriptor is located, -1 is returned. |
| 5825 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5826 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 5827 | UnixUnusedFd *pUnused = 0; |
| 5828 | |
| 5829 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 5830 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 5831 | ** but because no way to test it is currently available. It is better |
| 5832 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 5833 | ** feature. */ |
| 5834 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5835 | struct stat sStat; /* Results of stat() call */ |
| 5836 | |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 5837 | unixEnterMutex(); |
| 5838 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5839 | /* A stat() call may fail for various reasons. If this happens, it is |
| 5840 | ** almost certain that an open() call on the same path will also fail. |
| 5841 | ** For this reason, if an error occurs in the stat() call here, it is |
| 5842 | ** ignored and -1 is returned. The caller will try to open a new file |
| 5843 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 5844 | ** |
| 5845 | ** Even if a subsequent open() call does succeed, the consequences of |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5846 | ** not searching for a reusable file descriptor are not dire. */ |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 5847 | if( inodeList!=0 && 0==osStat(zPath, &sStat) ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 5848 | unixInodeInfo *pInode; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5849 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5850 | pInode = inodeList; |
| 5851 | while( pInode && (pInode->fileId.dev!=sStat.st_dev |
drh | 25ef7f5 | 2016-12-05 20:06:45 +0000 | [diff] [blame] | 5852 | || pInode->fileId.ino!=(u64)sStat.st_ino) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5853 | pInode = pInode->pNext; |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 5854 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5855 | if( pInode ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5856 | UnixUnusedFd **pp; |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 5857 | assert( sqlite3_mutex_notheld(pInode->pLockMutex) ); |
| 5858 | sqlite3_mutex_enter(pInode->pLockMutex); |
drh | 55220a6 | 2019-08-06 20:55:06 +0000 | [diff] [blame] | 5859 | flags &= (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5860 | for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5861 | pUnused = *pp; |
| 5862 | if( pUnused ){ |
| 5863 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5864 | } |
drh | 095908e | 2018-08-13 20:46:18 +0000 | [diff] [blame] | 5865 | sqlite3_mutex_leave(pInode->pLockMutex); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5866 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5867 | } |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 5868 | unixLeaveMutex(); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 5869 | #endif /* if !OS_VXWORKS */ |
| 5870 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5871 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 5872 | |
| 5873 | /* |
dan | 1bf4ca7 | 2016-08-11 18:05:47 +0000 | [diff] [blame] | 5874 | ** Find the mode, uid and gid of file zFile. |
| 5875 | */ |
| 5876 | static int getFileMode( |
| 5877 | const char *zFile, /* File name */ |
| 5878 | mode_t *pMode, /* OUT: Permissions of zFile */ |
| 5879 | uid_t *pUid, /* OUT: uid of zFile. */ |
| 5880 | gid_t *pGid /* OUT: gid of zFile. */ |
| 5881 | ){ |
| 5882 | struct stat sStat; /* Output of stat() on database file */ |
| 5883 | int rc = SQLITE_OK; |
| 5884 | if( 0==osStat(zFile, &sStat) ){ |
| 5885 | *pMode = sStat.st_mode & 0777; |
| 5886 | *pUid = sStat.st_uid; |
| 5887 | *pGid = sStat.st_gid; |
| 5888 | }else{ |
| 5889 | rc = SQLITE_IOERR_FSTAT; |
| 5890 | } |
| 5891 | return rc; |
| 5892 | } |
| 5893 | |
| 5894 | /* |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5895 | ** This function is called by unixOpen() to determine the unix permissions |
drh | f65bc91 | 2010-07-14 20:51:34 +0000 | [diff] [blame] | 5896 | ** 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] | 5897 | ** and a value suitable for passing as the third argument to open(2) is |
| 5898 | ** written to *pMode. If an IO error occurs, an SQLite error code is |
| 5899 | ** returned and the value of *pMode is not modified. |
| 5900 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5901 | ** In most cases, this routine sets *pMode to 0, which will become |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 5902 | ** an indication to robust_open() to create the file using |
| 5903 | ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask. |
| 5904 | ** 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] | 5905 | ** this function queries the file-system for the permissions on the |
| 5906 | ** corresponding database file and sets *pMode to this value. Whenever |
| 5907 | ** possible, WAL and journal files are created using the same permissions |
| 5908 | ** as the associated database file. |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 5909 | ** |
| 5910 | ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the |
| 5911 | ** original filename is unavailable. But 8_3_NAMES is only used for |
| 5912 | ** FAT filesystems and permissions do not matter there, so just use |
drh | 1116b17 | 2019-09-25 10:36:31 +0000 | [diff] [blame] | 5913 | ** the default permissions. In 8_3_NAMES mode, leave *pMode set to zero. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5914 | */ |
| 5915 | static int findCreateFileMode( |
| 5916 | const char *zPath, /* Path of file (possibly) being created */ |
| 5917 | int flags, /* Flags passed as 4th argument to xOpen() */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5918 | mode_t *pMode, /* OUT: Permissions to open file with */ |
| 5919 | uid_t *pUid, /* OUT: uid to set on the file */ |
| 5920 | gid_t *pGid /* OUT: gid to set on the file */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5921 | ){ |
| 5922 | int rc = SQLITE_OK; /* Return Code */ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 5923 | *pMode = 0; |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 5924 | *pUid = 0; |
| 5925 | *pGid = 0; |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 5926 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5927 | char zDb[MAX_PATHNAME+1]; /* Database file path */ |
| 5928 | int nDb; /* Number of valid bytes in zDb */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5929 | |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5930 | /* zPath is a path to a WAL or journal file. The following block derives |
| 5931 | ** the path to the associated database file from zPath. This block handles |
| 5932 | ** the following naming conventions: |
| 5933 | ** |
| 5934 | ** "<path to db>-journal" |
| 5935 | ** "<path to db>-wal" |
drh | 81cc516 | 2011-05-17 20:36:21 +0000 | [diff] [blame] | 5936 | ** "<path to db>-journalNN" |
| 5937 | ** "<path to db>-walNN" |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5938 | ** |
drh | d337c5b | 2011-10-20 18:23:35 +0000 | [diff] [blame] | 5939 | ** where NN is a decimal number. The NN naming schemes are |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5940 | ** used by the test_multiplex.c module. |
| 5941 | */ |
| 5942 | nDb = sqlite3Strlen30(zPath) - 1; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5943 | while( zPath[nDb]!='-' ){ |
dan | 629ec14 | 2017-09-14 20:41:17 +0000 | [diff] [blame] | 5944 | /* In normal operation, the journal file name will always contain |
| 5945 | ** a '-' character. However in 8+3 filename mode, or if a corrupt |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 5946 | ** rollback journal specifies a super-journal with a goofy name, then |
dan | 629ec14 | 2017-09-14 20:41:17 +0000 | [diff] [blame] | 5947 | ** the '-' might be missing. */ |
drh | 90e5dda | 2015-12-03 20:42:28 +0000 | [diff] [blame] | 5948 | if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK; |
drh | c47167a | 2011-10-05 15:26:13 +0000 | [diff] [blame] | 5949 | nDb--; |
| 5950 | } |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5951 | memcpy(zDb, zPath, nDb); |
| 5952 | zDb[nDb] = '\0'; |
dan | a0c989d | 2010-11-05 18:07:37 +0000 | [diff] [blame] | 5953 | |
dan | 1bf4ca7 | 2016-08-11 18:05:47 +0000 | [diff] [blame] | 5954 | rc = getFileMode(zDb, pMode, pUid, pGid); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5955 | }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 5956 | *pMode = 0600; |
dan | 1bf4ca7 | 2016-08-11 18:05:47 +0000 | [diff] [blame] | 5957 | }else if( flags & SQLITE_OPEN_URI ){ |
| 5958 | /* If this is a main database file and the file was opened using a URI |
| 5959 | ** filename, check for the "modeof" parameter. If present, interpret |
| 5960 | ** its value as a filename and try to copy the mode, uid and gid from |
| 5961 | ** that file. */ |
| 5962 | const char *z = sqlite3_uri_parameter(zPath, "modeof"); |
| 5963 | if( z ){ |
| 5964 | rc = getFileMode(z, pMode, pUid, pGid); |
| 5965 | } |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 5966 | } |
| 5967 | return rc; |
| 5968 | } |
| 5969 | |
| 5970 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 5971 | ** Open the file zPath. |
| 5972 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5973 | ** Previously, the SQLite OS layer used three functions in place of this |
| 5974 | ** one: |
| 5975 | ** |
| 5976 | ** sqlite3OsOpenReadWrite(); |
| 5977 | ** sqlite3OsOpenReadOnly(); |
| 5978 | ** sqlite3OsOpenExclusive(); |
| 5979 | ** |
| 5980 | ** These calls correspond to the following combinations of flags: |
| 5981 | ** |
| 5982 | ** ReadWrite() -> (READWRITE | CREATE) |
| 5983 | ** ReadOnly() -> (READONLY) |
| 5984 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 5985 | ** |
| 5986 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 5987 | ** true, the file was configured to be automatically deleted when the |
| 5988 | ** file handle closed. To achieve the same effect using this new |
| 5989 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 5990 | ** OpenExclusive(). |
| 5991 | */ |
| 5992 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5993 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 5994 | const char *zPath, /* Pathname of file to be opened */ |
| 5995 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 5996 | int flags, /* Input flags to control the opening */ |
| 5997 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5998 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 5999 | unixFile *p = (unixFile *)pFile; |
| 6000 | int fd = -1; /* File descriptor returned by open() */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6001 | int openFlags = 0; /* Flags to pass to open() */ |
drh | c398c65 | 2019-11-22 00:42:01 +0000 | [diff] [blame] | 6002 | int eType = flags&0x0FFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 6003 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6004 | int rc = SQLITE_OK; /* Function Return Code */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6005 | int ctrlFlags = 0; /* UNIXFILE_* flags */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6006 | |
| 6007 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 6008 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 6009 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 6010 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 6011 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6012 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 6013 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 6014 | #endif |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 6015 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 6016 | struct statfs fsInfo; |
| 6017 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6018 | |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 6019 | /* If creating a super- or main-file journal, this function will open |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6020 | ** a file-descriptor on the directory too. The first time unixSync() |
| 6021 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 6022 | */ |
drh | a803a2c | 2017-12-13 20:02:29 +0000 | [diff] [blame] | 6023 | int isNewJrnl = (isCreate && ( |
drh | ccb2113 | 2020-06-19 11:34:57 +0000 | [diff] [blame] | 6024 | eType==SQLITE_OPEN_SUPER_JOURNAL |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 6025 | || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 6026 | || eType==SQLITE_OPEN_WAL |
| 6027 | )); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6028 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 6029 | /* If argument zPath is a NULL pointer, this function is required to open |
| 6030 | ** a temporary file. Use this buffer to store the file name in. |
| 6031 | */ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6032 | char zTmpname[MAX_PATHNAME+2]; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 6033 | const char *zName = zPath; |
| 6034 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6035 | /* Check the following statements are true: |
| 6036 | ** |
| 6037 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 6038 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 6039 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 6040 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6041 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6042 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6043 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6044 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 6045 | assert(isDelete==0 || isCreate); |
| 6046 | |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 6047 | /* The main DB, main journal, WAL file and super-journal are never |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 6048 | ** automatically deleted. Nor are they ever temporary files. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6049 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 6050 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
drh | ccb2113 | 2020-06-19 11:34:57 +0000 | [diff] [blame] | 6051 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_SUPER_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 6052 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6053 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6054 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 6055 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 6056 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
drh | ccb2113 | 2020-06-19 11:34:57 +0000 | [diff] [blame] | 6057 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_SUPER_JOURNAL |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 6058 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6059 | ); |
| 6060 | |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 6061 | /* Detect a pid change and reset the PRNG. There is a race condition |
| 6062 | ** here such that two or more threads all trying to open databases at |
| 6063 | ** the same instant might all reset the PRNG. But multiple resets |
| 6064 | ** are harmless. |
| 6065 | */ |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6066 | if( randomnessPid!=osGetpid(0) ){ |
| 6067 | randomnessPid = osGetpid(0); |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 6068 | sqlite3_randomness(0,0); |
| 6069 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6070 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6071 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6072 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6073 | UnixUnusedFd *pUnused; |
| 6074 | pUnused = findReusableFd(zName, flags); |
| 6075 | if( pUnused ){ |
| 6076 | fd = pUnused->fd; |
| 6077 | }else{ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 6078 | pUnused = sqlite3_malloc64(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6079 | if( !pUnused ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6080 | return SQLITE_NOMEM_BKPT; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6081 | } |
| 6082 | } |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 6083 | p->pPreallocatedUnused = pUnused; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6084 | |
| 6085 | /* Database filenames are double-zero terminated if they are not |
| 6086 | ** URIs with parameters. Hence, they can always be passed into |
| 6087 | ** sqlite3_uri_parameter(). */ |
| 6088 | assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 ); |
| 6089 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6090 | }else if( !zName ){ |
| 6091 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
drh | a803a2c | 2017-12-13 20:02:29 +0000 | [diff] [blame] | 6092 | assert(isDelete && !isNewJrnl); |
drh | b7e50ad | 2015-11-28 21:49:53 +0000 | [diff] [blame] | 6093 | rc = unixGetTempname(pVfs->mxPathname, zTmpname); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 6094 | if( rc!=SQLITE_OK ){ |
| 6095 | return rc; |
| 6096 | } |
| 6097 | zName = zTmpname; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6098 | |
| 6099 | /* Generated temporary filenames are always double-zero terminated |
| 6100 | ** for use by sqlite3_uri_parameter(). */ |
| 6101 | assert( zName[strlen(zName)+1]==0 ); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 6102 | } |
| 6103 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6104 | /* Determine the value of the flags parameter passed to POSIX function |
| 6105 | ** open(). These must be calculated even if open() is not called, as |
| 6106 | ** they may be stored as part of the file handle and used by the |
| 6107 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6108 | if( isReadonly ) openFlags |= O_RDONLY; |
| 6109 | if( isReadWrite ) openFlags |= O_RDWR; |
| 6110 | if( isCreate ) openFlags |= O_CREAT; |
| 6111 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
drh | c398c65 | 2019-11-22 00:42:01 +0000 | [diff] [blame] | 6112 | openFlags |= (O_LARGEFILE|O_BINARY|O_NOFOLLOW); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6113 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6114 | if( fd<0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 6115 | mode_t openMode; /* Permissions to create file with */ |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 6116 | uid_t uid; /* Userid for the file */ |
| 6117 | gid_t gid; /* Groupid for the file */ |
| 6118 | rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 6119 | if( rc!=SQLITE_OK ){ |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 6120 | assert( !p->pPreallocatedUnused ); |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 6121 | assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 6122 | return rc; |
| 6123 | } |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 6124 | fd = robust_open(zName, openFlags, openMode); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6125 | OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); |
drh | 5a2d970 | 2015-11-26 02:21:05 +0000 | [diff] [blame] | 6126 | assert( !isExclusive || (openFlags & O_CREAT)!=0 ); |
dan | a688ca5 | 2018-01-10 11:56:03 +0000 | [diff] [blame] | 6127 | if( fd<0 ){ |
| 6128 | if( isNewJrnl && errno==EACCES && osAccess(zName, F_OK) ){ |
| 6129 | /* If unable to create a journal because the directory is not |
| 6130 | ** writable, change the error code to indicate that. */ |
| 6131 | rc = SQLITE_READONLY_DIRECTORY; |
| 6132 | }else if( errno!=EISDIR && isReadWrite ){ |
| 6133 | /* Failed to open the file for read/write access. Try read-only. */ |
| 6134 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
| 6135 | openFlags &= ~(O_RDWR|O_CREAT); |
| 6136 | flags |= SQLITE_OPEN_READONLY; |
| 6137 | openFlags |= O_RDONLY; |
| 6138 | isReadonly = 1; |
| 6139 | fd = robust_open(zName, openFlags, openMode); |
| 6140 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6141 | } |
| 6142 | if( fd<0 ){ |
dan | a688ca5 | 2018-01-10 11:56:03 +0000 | [diff] [blame] | 6143 | int rc2 = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); |
| 6144 | if( rc==SQLITE_OK ) rc = rc2; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6145 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6146 | } |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 6147 | |
drh | 1116b17 | 2019-09-25 10:36:31 +0000 | [diff] [blame] | 6148 | /* The owner of the rollback journal or WAL file should always be the |
| 6149 | ** same as the owner of the database file. Try to ensure that this is |
| 6150 | ** the case. The chown() system call will be a no-op if the current |
| 6151 | ** process lacks root privileges, be we should at least try. Without |
| 6152 | ** this step, if a root process opens a database file, it can leave |
| 6153 | ** behinds a journal/WAL that is owned by root and hence make the |
| 6154 | ** database inaccessible to unprivileged processes. |
| 6155 | ** |
drh | edf8a7b | 2019-09-25 11:49:36 +0000 | [diff] [blame] | 6156 | ** If openMode==0, then that means uid and gid are not set correctly |
drh | 1116b17 | 2019-09-25 10:36:31 +0000 | [diff] [blame] | 6157 | ** (probably because SQLite is configured to use 8+3 filename mode) and |
| 6158 | ** in that case we do not want to attempt the chown(). |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 6159 | */ |
drh | edf8a7b | 2019-09-25 11:49:36 +0000 | [diff] [blame] | 6160 | if( openMode && (flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL))!=0 ){ |
drh | 6226ca2 | 2015-11-24 15:06:28 +0000 | [diff] [blame] | 6161 | robustFchown(fd, uid, gid); |
drh | ac7c3ac | 2012-02-11 19:23:48 +0000 | [diff] [blame] | 6162 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6163 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6164 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6165 | if( pOutFlags ){ |
| 6166 | *pOutFlags = flags; |
| 6167 | } |
| 6168 | |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 6169 | if( p->pPreallocatedUnused ){ |
| 6170 | p->pPreallocatedUnused->fd = fd; |
drh | 55220a6 | 2019-08-06 20:55:06 +0000 | [diff] [blame] | 6171 | p->pPreallocatedUnused->flags = |
| 6172 | flags & (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6173 | } |
| 6174 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6175 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6176 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 6177 | zPath = zName; |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 6178 | #elif defined(SQLITE_UNLINK_AFTER_CLOSE) |
| 6179 | zPath = sqlite3_mprintf("%s", zName); |
| 6180 | if( zPath==0 ){ |
| 6181 | robust_close(p, fd, __LINE__); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6182 | return SQLITE_NOMEM_BKPT; |
drh | 0bdbc90 | 2014-06-16 18:35:06 +0000 | [diff] [blame] | 6183 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 6184 | #else |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 6185 | osUnlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 6186 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6187 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 6188 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 6189 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6190 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 6191 | } |
| 6192 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6193 | |
| 6194 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6195 | if( fstatfs(fd, &fsInfo) == -1 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6196 | storeLastErrno(p, errno); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6197 | robust_close(p, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6198 | return SQLITE_IOERR_ACCESS; |
| 6199 | } |
| 6200 | if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 6201 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 6202 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6203 | if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) { |
| 6204 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 6205 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6206 | #endif |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6207 | |
| 6208 | /* Set up appropriate ctrlFlags */ |
| 6209 | if( isDelete ) ctrlFlags |= UNIXFILE_DELETE; |
| 6210 | if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY; |
drh | 86151e8 | 2015-12-08 14:37:16 +0000 | [diff] [blame] | 6211 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6212 | if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK; |
drh | a803a2c | 2017-12-13 20:02:29 +0000 | [diff] [blame] | 6213 | if( isNewJrnl ) ctrlFlags |= UNIXFILE_DIRSYNC; |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6214 | if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI; |
| 6215 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6216 | #if SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6217 | #if SQLITE_PREFER_PROXY_LOCKING |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6218 | isAutoProxy = 1; |
| 6219 | #endif |
| 6220 | if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6221 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 6222 | int useProxy = 0; |
| 6223 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 6224 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 6225 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6226 | if( envforce!=NULL ){ |
| 6227 | useProxy = atoi(envforce)>0; |
| 6228 | }else{ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6229 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 6230 | } |
| 6231 | if( useProxy ){ |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6232 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6233 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6234 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6235 | if( rc!=SQLITE_OK ){ |
| 6236 | /* Use unixClose to clean up the resources added in fillInUnixFile |
| 6237 | ** and clear all the structure's references. Specifically, |
| 6238 | ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 6239 | */ |
| 6240 | unixClose(pFile); |
| 6241 | return rc; |
| 6242 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6243 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6244 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 6245 | } |
| 6246 | } |
| 6247 | #endif |
| 6248 | |
dan | 3ed0f1c | 2017-09-14 21:12:07 +0000 | [diff] [blame] | 6249 | assert( zPath==0 || zPath[0]=='/' |
drh | ccb2113 | 2020-06-19 11:34:57 +0000 | [diff] [blame] | 6250 | || eType==SQLITE_OPEN_SUPER_JOURNAL || eType==SQLITE_OPEN_MAIN_JOURNAL |
dan | 3ed0f1c | 2017-09-14 21:12:07 +0000 | [diff] [blame] | 6251 | ); |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 6252 | rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
| 6253 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6254 | open_finished: |
| 6255 | if( rc!=SQLITE_OK ){ |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 6256 | sqlite3_free(p->pPreallocatedUnused); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6257 | } |
| 6258 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6259 | } |
| 6260 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 6261 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6262 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6263 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 6264 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6265 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6266 | static int unixDelete( |
| 6267 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 6268 | const char *zPath, /* Name of file to be deleted */ |
| 6269 | int dirSync /* If true, fsync() directory after deleting file */ |
| 6270 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6271 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6272 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6273 | SimulateIOError(return SQLITE_IOERR_DELETE); |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 6274 | if( osUnlink(zPath)==(-1) ){ |
drh | bd94554 | 2014-08-13 11:39:42 +0000 | [diff] [blame] | 6275 | if( errno==ENOENT |
| 6276 | #if OS_VXWORKS |
drh | 19541f3 | 2014-09-01 13:37:55 +0000 | [diff] [blame] | 6277 | || osAccess(zPath,0)!=0 |
drh | bd94554 | 2014-08-13 11:39:42 +0000 | [diff] [blame] | 6278 | #endif |
| 6279 | ){ |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 6280 | rc = SQLITE_IOERR_DELETE_NOENT; |
| 6281 | }else{ |
drh | b430816 | 2012-11-09 21:40:02 +0000 | [diff] [blame] | 6282 | rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath); |
dan | 9fc5b4a | 2012-11-09 20:17:26 +0000 | [diff] [blame] | 6283 | } |
drh | b430816 | 2012-11-09 21:40:02 +0000 | [diff] [blame] | 6284 | return rc; |
drh | 5d4feff | 2010-07-14 01:45:22 +0000 | [diff] [blame] | 6285 | } |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 6286 | #ifndef SQLITE_DISABLE_DIRSYNC |
drh | e349519 | 2012-01-05 16:07:30 +0000 | [diff] [blame] | 6287 | if( (dirSync & 1)!=0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6288 | int fd; |
drh | 90315a2 | 2011-08-10 01:52:12 +0000 | [diff] [blame] | 6289 | rc = osOpenDirectory(zPath, &fd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6290 | if( rc==SQLITE_OK ){ |
drh | 6d25899 | 2016-02-04 09:48:12 +0000 | [diff] [blame] | 6291 | if( full_fsync(fd,0,0) ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 6292 | rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6293 | } |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6294 | robust_close(0, fd, __LINE__); |
drh | acb6b28 | 2015-11-26 10:37:05 +0000 | [diff] [blame] | 6295 | }else{ |
| 6296 | assert( rc==SQLITE_CANTOPEN ); |
drh | 1ee6f74 | 2011-08-23 20:11:32 +0000 | [diff] [blame] | 6297 | rc = SQLITE_OK; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6298 | } |
| 6299 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 6300 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 6301 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6302 | } |
| 6303 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6304 | /* |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 6305 | ** Test the existence of or access permissions of file zPath. The |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6306 | ** test performed depends on the value of flags: |
| 6307 | ** |
| 6308 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 6309 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 6310 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 6311 | ** |
| 6312 | ** Otherwise return 0. |
| 6313 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6314 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6315 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 6316 | const char *zPath, /* Path of the file to examine */ |
| 6317 | int flags, /* What do we want to learn about the zPath file? */ |
| 6318 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6319 | ){ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6320 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6321 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 6322 | assert( pResOut!=0 ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6323 | |
drh | c398c65 | 2019-11-22 00:42:01 +0000 | [diff] [blame] | 6324 | /* The spec says there are three possible values for flags. But only |
| 6325 | ** two of them are actually used */ |
| 6326 | assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE ); |
drh | d260b5b | 2015-11-25 18:03:33 +0000 | [diff] [blame] | 6327 | |
| 6328 | if( flags==SQLITE_ACCESS_EXISTS ){ |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 6329 | struct stat buf; |
drh | 96e8eeb | 2019-12-26 00:56:50 +0000 | [diff] [blame] | 6330 | *pResOut = 0==osStat(zPath, &buf) && |
drh | 09bee57 | 2019-12-27 13:30:46 +0000 | [diff] [blame] | 6331 | (!S_ISREG(buf.st_mode) || buf.st_size>0); |
drh | 0933aad | 2019-11-18 17:46:38 +0000 | [diff] [blame] | 6332 | }else{ |
drh | c398c65 | 2019-11-22 00:42:01 +0000 | [diff] [blame] | 6333 | *pResOut = osAccess(zPath, W_OK|R_OK)==0; |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 6334 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 6335 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6336 | } |
| 6337 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6338 | /* |
drh | 7f42dcd | 2020-11-16 18:45:21 +0000 | [diff] [blame^] | 6339 | ** If the last component of the pathname in z[0]..z[j-1] is something |
| 6340 | ** other than ".." then back it out and return true. If the last |
| 6341 | ** component is empty or if it is ".." then return false. |
| 6342 | */ |
| 6343 | static int unixBackupDir(const char *z, int *pJ){ |
| 6344 | int j = *pJ; |
| 6345 | int i; |
| 6346 | if( j<=0 ) return 0; |
| 6347 | for(i=j-1; i>0 && z[i-1]!='/'; i--){} |
| 6348 | if( z[i]=='.' && i==j-2 && z[i+1]=='.' ) return 0; |
| 6349 | *pJ = i-1; |
| 6350 | return 1; |
| 6351 | } |
| 6352 | |
| 6353 | /* |
| 6354 | ** Convert a relative pathname into a full pathname. Also |
| 6355 | ** simplify the pathname as follows: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6356 | ** |
drh | 7f42dcd | 2020-11-16 18:45:21 +0000 | [diff] [blame^] | 6357 | ** Remove all instances of /./ |
| 6358 | ** Remove all isntances of /X/../ for any X |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6359 | */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6360 | static int mkFullPathname( |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6361 | const char *zPath, /* Input path */ |
| 6362 | char *zOut, /* Output buffer */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6363 | int nOut /* Allocated size of buffer zOut */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 6364 | ){ |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6365 | int nPath = sqlite3Strlen30(zPath); |
| 6366 | int iOff = 0; |
drh | 7f42dcd | 2020-11-16 18:45:21 +0000 | [diff] [blame^] | 6367 | int i, j; |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6368 | if( zPath[0]!='/' ){ |
| 6369 | if( osGetcwd(zOut, nOut-2)==0 ){ |
dan | e18d495 | 2011-02-21 11:46:24 +0000 | [diff] [blame] | 6370 | return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6371 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6372 | iOff = sqlite3Strlen30(zOut); |
| 6373 | zOut[iOff++] = '/'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6374 | } |
dan | 2349670 | 2016-01-26 13:56:42 +0000 | [diff] [blame] | 6375 | if( (iOff+nPath+1)>nOut ){ |
| 6376 | /* SQLite assumes that xFullPathname() nul-terminates the output buffer |
| 6377 | ** even if it returns an error. */ |
| 6378 | zOut[iOff] = '\0'; |
| 6379 | return SQLITE_CANTOPEN_BKPT; |
| 6380 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6381 | sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath); |
drh | 7f42dcd | 2020-11-16 18:45:21 +0000 | [diff] [blame^] | 6382 | |
| 6383 | /* Remove duplicate '/' characters. Except, two // at the beginning |
| 6384 | ** of a pathname is allowed since this is important on windows. */ |
| 6385 | for(i=j=1; zOut[i]; i++){ |
| 6386 | zOut[j++] = zOut[i]; |
| 6387 | while( zOut[i]=='/' && zOut[i+1]=='/' ) i++; |
| 6388 | } |
| 6389 | zOut[j] = 0; |
| 6390 | |
| 6391 | for(i=j=0; zOut[i]; i++){ |
| 6392 | if( zOut[i]=='/' ){ |
| 6393 | /* Skip over internal "/." directory components */ |
| 6394 | if( zOut[i+1]=='.' && zOut[i+2]=='/' ){ |
| 6395 | i += 1; |
| 6396 | continue; |
| 6397 | } |
| 6398 | |
| 6399 | /* If this is a "/.." directory component then back out the |
| 6400 | ** previous term of the directory if it is something other than "..". |
| 6401 | */ |
| 6402 | if( zOut[i+1]=='.' |
| 6403 | && zOut[i+2]=='.' |
| 6404 | && zOut[i+3]=='/' |
| 6405 | && unixBackupDir(zOut, &j) |
| 6406 | ){ |
| 6407 | i += 2; |
| 6408 | continue; |
| 6409 | } |
| 6410 | } |
| 6411 | if( j>=0 ) zOut[j] = zOut[i]; |
| 6412 | j++; |
| 6413 | } |
| 6414 | if( j==0 ) zOut[j++] = '/'; |
| 6415 | zOut[j] = 0; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6416 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6417 | } |
| 6418 | |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6419 | /* |
| 6420 | ** Turn a relative pathname into a full pathname. The relative path |
| 6421 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 6422 | ** zPath. |
| 6423 | ** |
| 6424 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 6425 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 6426 | ** this buffer before returning. |
| 6427 | */ |
| 6428 | static int unixFullPathname( |
| 6429 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 6430 | const char *zPath, /* Possibly relative input path */ |
| 6431 | int nOut, /* Size of output buffer in bytes */ |
| 6432 | char *zOut /* Output buffer */ |
| 6433 | ){ |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 6434 | #if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT) |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6435 | return mkFullPathname(zPath, zOut, nOut); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6436 | #else |
| 6437 | int rc = SQLITE_OK; |
| 6438 | int nByte; |
drh | c398c65 | 2019-11-22 00:42:01 +0000 | [diff] [blame] | 6439 | int nLink = 0; /* Number of symbolic links followed so far */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6440 | const char *zIn = zPath; /* Input path for each iteration of loop */ |
| 6441 | char *zDel = 0; |
| 6442 | |
| 6443 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
| 6444 | UNUSED_PARAMETER(pVfs); |
| 6445 | |
| 6446 | /* It's odd to simulate an io-error here, but really this is just |
| 6447 | ** using the io-error infrastructure to test that SQLite handles this |
| 6448 | ** function failing. This function could fail if, for example, the |
| 6449 | ** current working directory has been unlinked. |
| 6450 | */ |
| 6451 | SimulateIOError( return SQLITE_ERROR ); |
| 6452 | |
| 6453 | do { |
| 6454 | |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6455 | /* Call stat() on path zIn. Set bLink to true if the path is a symbolic |
| 6456 | ** link, or false otherwise. */ |
| 6457 | int bLink = 0; |
| 6458 | struct stat buf; |
| 6459 | if( osLstat(zIn, &buf)!=0 ){ |
| 6460 | if( errno!=ENOENT ){ |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 6461 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6462 | } |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6463 | }else{ |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6464 | bLink = S_ISLNK(buf.st_mode); |
| 6465 | } |
| 6466 | |
| 6467 | if( bLink ){ |
drh | c398c65 | 2019-11-22 00:42:01 +0000 | [diff] [blame] | 6468 | nLink++; |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6469 | if( zDel==0 ){ |
| 6470 | zDel = sqlite3_malloc(nOut); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 6471 | if( zDel==0 ) rc = SQLITE_NOMEM_BKPT; |
drh | c398c65 | 2019-11-22 00:42:01 +0000 | [diff] [blame] | 6472 | }else if( nLink>=SQLITE_MAX_SYMLINKS ){ |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6473 | rc = SQLITE_CANTOPEN_BKPT; |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6474 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6475 | |
| 6476 | if( rc==SQLITE_OK ){ |
| 6477 | nByte = osReadlink(zIn, zDel, nOut-1); |
| 6478 | if( nByte<0 ){ |
| 6479 | rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn); |
dan | 2349670 | 2016-01-26 13:56:42 +0000 | [diff] [blame] | 6480 | }else{ |
| 6481 | if( zDel[0]!='/' ){ |
| 6482 | int n; |
| 6483 | for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--); |
| 6484 | if( nByte+n+1>nOut ){ |
| 6485 | rc = SQLITE_CANTOPEN_BKPT; |
| 6486 | }else{ |
| 6487 | memmove(&zDel[n], zDel, nByte+1); |
| 6488 | memcpy(zDel, zIn, n); |
| 6489 | nByte += n; |
| 6490 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6491 | } |
| 6492 | zDel[nByte] = '\0'; |
| 6493 | } |
| 6494 | } |
| 6495 | |
| 6496 | zIn = zDel; |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6497 | } |
| 6498 | |
dan | 2349670 | 2016-01-26 13:56:42 +0000 | [diff] [blame] | 6499 | assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' ); |
| 6500 | if( rc==SQLITE_OK && zIn!=zOut ){ |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6501 | rc = mkFullPathname(zIn, zOut, nOut); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6502 | } |
dan | caf6b15 | 2016-01-25 18:05:49 +0000 | [diff] [blame] | 6503 | if( bLink==0 ) break; |
| 6504 | zIn = zOut; |
| 6505 | }while( rc==SQLITE_OK ); |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6506 | |
| 6507 | sqlite3_free(zDel); |
drh | c398c65 | 2019-11-22 00:42:01 +0000 | [diff] [blame] | 6508 | if( rc==SQLITE_OK && nLink ) rc = SQLITE_OK_SYMLINK; |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6509 | return rc; |
dan | af1b36b | 2016-01-25 18:43:05 +0000 | [diff] [blame] | 6510 | #endif /* HAVE_READLINK && HAVE_LSTAT */ |
dan | e88ec18 | 2016-01-25 17:04:48 +0000 | [diff] [blame] | 6511 | } |
| 6512 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 6513 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6514 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 6515 | /* |
| 6516 | ** Interfaces for opening a shared library, finding entry points |
| 6517 | ** within the shared library, and closing the shared library. |
| 6518 | */ |
| 6519 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6520 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 6521 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6522 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 6523 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 6524 | |
| 6525 | /* |
| 6526 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 6527 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 6528 | ** message is available, it is written to zBufOut. If no error message |
| 6529 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 6530 | ** error message. |
| 6531 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6532 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
dan | 3239053 | 2010-11-29 18:36:22 +0000 | [diff] [blame] | 6533 | const char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6534 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6535 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6536 | zErr = dlerror(); |
| 6537 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6538 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6539 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6540 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6541 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6542 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 6543 | /* |
| 6544 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 6545 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 6546 | ** returns a void* which is really a pointer to a function. So how do we |
| 6547 | ** use dlsym() with -pedantic-errors? |
| 6548 | ** |
| 6549 | ** Variable x below is defined to be a pointer to a function taking |
| 6550 | ** parameters void* and const char* and returning a pointer to a function. |
| 6551 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 6552 | ** (That assignment requires a cast.) Then we call the function that |
| 6553 | ** x points to. |
| 6554 | ** |
| 6555 | ** This work-around is unlikely to work correctly on any system where |
| 6556 | ** you really cannot cast a function pointer into void*. But then, on the |
| 6557 | ** other hand, dlsym() will not work on such a system either, so we have |
| 6558 | ** not really lost anything. |
| 6559 | */ |
| 6560 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6561 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6562 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 6563 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6564 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6565 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 6566 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6567 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 6568 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6569 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 6570 | #define unixDlOpen 0 |
| 6571 | #define unixDlError 0 |
| 6572 | #define unixDlSym 0 |
| 6573 | #define unixDlClose 0 |
| 6574 | #endif |
| 6575 | |
| 6576 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6577 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6578 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6579 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 6580 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 6581 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6582 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6583 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 6584 | ** errors. The reports issued by valgrind are incorrect - we would |
| 6585 | ** prefer that the randomness be increased by making use of the |
| 6586 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 6587 | ** some users. Rather than argue, it seems easier just to initialize |
| 6588 | ** the whole array and silence valgrind, even if that means less randomness |
| 6589 | ** in the random seed. |
| 6590 | ** |
| 6591 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 6592 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6593 | ** tests repeatable. |
| 6594 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6595 | memset(zBuf, 0, nBuf); |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6596 | randomnessPid = osGetpid(0); |
drh | 6a412b8 | 2015-04-30 12:31:49 +0000 | [diff] [blame] | 6597 | #if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6598 | { |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 6599 | int fd, got; |
drh | ad4f1e5 | 2011-03-04 15:43:57 +0000 | [diff] [blame] | 6600 | fd = robust_open("/dev/urandom", O_RDONLY, 0); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6601 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 6602 | time_t t; |
| 6603 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 6604 | memcpy(zBuf, &t, sizeof(t)); |
drh | b00d862 | 2014-01-01 15:18:36 +0000 | [diff] [blame] | 6605 | memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid)); |
| 6606 | assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf ); |
| 6607 | nBuf = sizeof(t) + sizeof(randomnessPid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6608 | }else{ |
drh | c18b404 | 2012-02-10 03:10:27 +0000 | [diff] [blame] | 6609 | do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR ); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 6610 | robust_close(0, fd, __LINE__); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 6611 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6612 | } |
| 6613 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 6614 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6615 | } |
| 6616 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6617 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6618 | /* |
| 6619 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6620 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 6621 | ** The return value is the number of microseconds of sleep actually |
| 6622 | ** requested from the underlying operating system, a number which |
| 6623 | ** might be greater than or equal to the argument, but not less |
| 6624 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6625 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6626 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 6627 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 6628 | struct timespec sp; |
| 6629 | |
| 6630 | sp.tv_sec = microseconds / 1000000; |
| 6631 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 6632 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6633 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6634 | return microseconds; |
| 6635 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
drh | ddcfe92 | 2020-09-15 12:29:35 +0000 | [diff] [blame] | 6636 | if( microseconds>=1000000 ) sleep(microseconds/1000000); |
| 6637 | if( microseconds%1000000 ) usleep(microseconds%1000000); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6638 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6639 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6640 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6641 | int seconds = (microseconds+999999)/1000000; |
| 6642 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 6643 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 6644 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 6645 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 6646 | } |
| 6647 | |
| 6648 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6649 | ** The following variable, if set to a non-zero value, is interpreted as |
| 6650 | ** the number of seconds since 1970 and is used to set the result of |
| 6651 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6652 | */ |
| 6653 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6654 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6655 | #endif |
| 6656 | |
| 6657 | /* |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6658 | ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 6659 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 6660 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 6661 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 6662 | ** proleptic Gregorian calendar. |
| 6663 | ** |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6664 | ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
| 6665 | ** cannot be found. |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6666 | */ |
| 6667 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 6668 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6669 | int rc = SQLITE_OK; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6670 | #if defined(NO_GETTOD) |
| 6671 | time_t t; |
| 6672 | time(&t); |
dan | 15eac4e | 2010-11-22 17:26:07 +0000 | [diff] [blame] | 6673 | *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6674 | #elif OS_VXWORKS |
| 6675 | struct timespec sNow; |
| 6676 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 6677 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 6678 | #else |
| 6679 | struct timeval sNow; |
drh | 970942e | 2015-11-25 23:13:14 +0000 | [diff] [blame] | 6680 | (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */ |
| 6681 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6682 | #endif |
| 6683 | |
| 6684 | #ifdef SQLITE_TEST |
| 6685 | if( sqlite3_current_time ){ |
| 6686 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 6687 | } |
| 6688 | #endif |
| 6689 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6690 | return rc; |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6691 | } |
| 6692 | |
drh | c3dfa5e | 2016-01-22 19:44:03 +0000 | [diff] [blame] | 6693 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6694 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6695 | ** Find the current time (in Universal Coordinated Time). Write the |
| 6696 | ** current time and date as a Julian Day number into *prNow and |
| 6697 | ** return 0. Return 1 if the time and date cannot be found. |
| 6698 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6699 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | b87a666 | 2011-10-13 01:01:14 +0000 | [diff] [blame] | 6700 | sqlite3_int64 i = 0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6701 | int rc; |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 6702 | UNUSED_PARAMETER(NotUsed); |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6703 | rc = unixCurrentTimeInt64(0, &i); |
drh | 0dcb0a7 | 2010-05-03 18:22:52 +0000 | [diff] [blame] | 6704 | *prNow = i/86400000.0; |
drh | 3170225 | 2011-10-12 23:13:43 +0000 | [diff] [blame] | 6705 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 6706 | } |
drh | 5337dac | 2015-11-25 15:15:03 +0000 | [diff] [blame] | 6707 | #else |
| 6708 | # define unixCurrentTime 0 |
| 6709 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 6710 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6711 | /* |
drh | 1b9f214 | 2016-03-17 16:01:23 +0000 | [diff] [blame] | 6712 | ** The xGetLastError() method is designed to return a better |
| 6713 | ** low-level error message when operating-system problems come up |
| 6714 | ** during SQLite operation. Only the integer return code is currently |
| 6715 | ** used. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6716 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 6717 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 6718 | UNUSED_PARAMETER(NotUsed); |
| 6719 | UNUSED_PARAMETER(NotUsed2); |
| 6720 | UNUSED_PARAMETER(NotUsed3); |
drh | 1b9f214 | 2016-03-17 16:01:23 +0000 | [diff] [blame] | 6721 | return errno; |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 6722 | } |
| 6723 | |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6724 | |
| 6725 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6726 | ************************ End of sqlite3_vfs methods *************************** |
| 6727 | ******************************************************************************/ |
| 6728 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6729 | /****************************************************************************** |
| 6730 | ************************** Begin Proxy Locking ******************************** |
| 6731 | ** |
| 6732 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 6733 | ** other locking methods on secondary lock files. Proxy locking is a |
| 6734 | ** meta-layer over top of the primitive locking implemented above. For |
| 6735 | ** this reason, the division that implements of proxy locking is deferred |
| 6736 | ** until late in the file (here) after all of the other I/O methods have |
| 6737 | ** been defined - so that the primitive locking methods are available |
| 6738 | ** as services to help with the implementation of proxy locking. |
| 6739 | ** |
| 6740 | **** |
| 6741 | ** |
| 6742 | ** The default locking schemes in SQLite use byte-range locks on the |
| 6743 | ** database file to coordinate safe, concurrent access by multiple readers |
| 6744 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 6745 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 6746 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 6747 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 6748 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 6749 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 6750 | ** address in the shared range is taken for a SHARED lock, the entire |
| 6751 | ** shared range is taken for an EXCLUSIVE lock): |
| 6752 | ** |
drh | f2f105d | 2012-08-20 15:53:54 +0000 | [diff] [blame] | 6753 | ** PENDING_BYTE 0x40000000 |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6754 | ** RESERVED_BYTE 0x40000001 |
| 6755 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 6756 | ** |
| 6757 | ** This works well on the local file system, but shows a nearly 100x |
| 6758 | ** slowdown in read performance on AFP because the AFP client disables |
| 6759 | ** the read cache when byte-range locks are present. Enabling the read |
| 6760 | ** cache exposes a cache coherency problem that is present on all OS X |
| 6761 | ** supported network file systems. NFS and AFP both observe the |
| 6762 | ** close-to-open semantics for ensuring cache coherency |
| 6763 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 6764 | ** address the requirements for concurrent database access by multiple |
| 6765 | ** readers and writers |
| 6766 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 6767 | ** |
| 6768 | ** To address the performance and cache coherency issues, proxy file locking |
| 6769 | ** changes the way database access is controlled by limiting access to a |
| 6770 | ** single host at a time and moving file locks off of the database file |
| 6771 | ** and onto a proxy file on the local file system. |
| 6772 | ** |
| 6773 | ** |
| 6774 | ** Using proxy locks |
| 6775 | ** ----------------- |
| 6776 | ** |
| 6777 | ** C APIs |
| 6778 | ** |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6779 | ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6780 | ** <proxy_path> | ":auto:"); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6781 | ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE, |
| 6782 | ** &<proxy_path>); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6783 | ** |
| 6784 | ** |
| 6785 | ** SQL pragmas |
| 6786 | ** |
| 6787 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 6788 | ** PRAGMA [database.]lock_proxy_file |
| 6789 | ** |
| 6790 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 6791 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 6792 | ** a proxy path based on the user's temp dir |
| 6793 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 6794 | ** actual proxy file name is generated from the name and path of the |
| 6795 | ** database file. For example: |
| 6796 | ** |
| 6797 | ** For database path "/Users/me/foo.db" |
| 6798 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 6799 | ** |
| 6800 | ** Once a lock proxy is configured for a database connection, it can not |
| 6801 | ** be removed, however it may be switched to a different proxy path via |
| 6802 | ** the above APIs (assuming the conch file is not being held by another |
| 6803 | ** connection or process). |
| 6804 | ** |
| 6805 | ** |
| 6806 | ** How proxy locking works |
| 6807 | ** ----------------------- |
| 6808 | ** |
| 6809 | ** Proxy file locking relies primarily on two new supporting files: |
| 6810 | ** |
| 6811 | ** * conch file to limit access to the database file to a single host |
| 6812 | ** at a time |
| 6813 | ** |
| 6814 | ** * proxy file to act as a proxy for the advisory locks normally |
| 6815 | ** taken on the database |
| 6816 | ** |
| 6817 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 6818 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 6819 | ** contents and comparing the host's unique host ID (see below) and lock |
| 6820 | ** proxy path against the values stored in the conch. The conch file is |
| 6821 | ** stored in the same directory as the database file and the file name |
| 6822 | ** is patterned after the database file name as ".<databasename>-conch". |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 6823 | ** 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] | 6824 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 6825 | ** lock and the conch file contents is updated with the host ID and proxy |
| 6826 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 6827 | ** is held by another process (with a shared lock), the exclusive lock |
| 6828 | ** will fail and SQLITE_BUSY is returned. |
| 6829 | ** |
| 6830 | ** The proxy file - a single-byte file used for all advisory file locks |
| 6831 | ** normally taken on the database file. This allows for safe sharing |
| 6832 | ** of the database file for multiple readers and writers on the same |
| 6833 | ** host (the conch ensures that they all use the same local lock file). |
| 6834 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6835 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 6836 | ** only taken when the first request to lock database file is made. |
| 6837 | ** This matches the semantics of the traditional locking behavior, where |
| 6838 | ** opening a connection to a database file does not take a lock on it. |
| 6839 | ** The shared lock and an open file descriptor are maintained until |
| 6840 | ** the connection to the database is closed. |
| 6841 | ** |
| 6842 | ** The proxy file and the lock file are never deleted so they only need |
| 6843 | ** to be created the first time they are used. |
| 6844 | ** |
| 6845 | ** Configuration options |
| 6846 | ** --------------------- |
| 6847 | ** |
| 6848 | ** SQLITE_PREFER_PROXY_LOCKING |
| 6849 | ** |
| 6850 | ** Database files accessed on non-local file systems are |
| 6851 | ** automatically configured for proxy locking, lock files are |
| 6852 | ** named automatically using the same logic as |
| 6853 | ** PRAGMA lock_proxy_file=":auto:" |
| 6854 | ** |
| 6855 | ** SQLITE_PROXY_DEBUG |
| 6856 | ** |
| 6857 | ** Enables the logging of error messages during host id file |
| 6858 | ** retrieval and creation |
| 6859 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6860 | ** LOCKPROXYDIR |
| 6861 | ** |
| 6862 | ** Overrides the default directory used for lock proxy files that |
| 6863 | ** are named automatically via the ":auto:" setting |
| 6864 | ** |
| 6865 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 6866 | ** |
| 6867 | ** Permissions to use when creating a directory for storing the |
| 6868 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 6869 | ** |
| 6870 | ** |
| 6871 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 6872 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 6873 | ** force proxy locking to be used for every database file opened, and 0 |
| 6874 | ** will force automatic proxy locking to be disabled for all database |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6875 | ** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6876 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 6877 | */ |
| 6878 | |
| 6879 | /* |
| 6880 | ** Proxy locking is only available on MacOSX |
| 6881 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6882 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6883 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6884 | /* |
| 6885 | ** The proxyLockingContext has the path and file structures for the remote |
| 6886 | ** and local proxy files in it |
| 6887 | */ |
| 6888 | typedef struct proxyLockingContext proxyLockingContext; |
| 6889 | struct proxyLockingContext { |
| 6890 | unixFile *conchFile; /* Open conch file */ |
| 6891 | char *conchFilePath; /* Name of the conch file */ |
| 6892 | unixFile *lockProxy; /* Open proxy lock file */ |
| 6893 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 6894 | char *dbPath; /* Name of the open file */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6895 | int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 6896 | int nFails; /* Number of conch taking failures */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6897 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 6898 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 6899 | }; |
| 6900 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6901 | /* |
| 6902 | ** The proxy lock file path for the database at dbPath is written into lPath, |
| 6903 | ** which must point to valid, writable memory large enough for a maxLen length |
| 6904 | ** file path. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6905 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6906 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 6907 | int len; |
| 6908 | int dbLen; |
| 6909 | int i; |
| 6910 | |
| 6911 | #ifdef LOCKPROXYDIR |
| 6912 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 6913 | #else |
| 6914 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 6915 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6916 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6917 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6918 | lPath, errno, osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6919 | return SQLITE_IOERR_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6920 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6921 | len = strlcat(lPath, "sqliteplocks", maxLen); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6922 | } |
| 6923 | # else |
| 6924 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 6925 | # endif |
| 6926 | #endif |
| 6927 | |
| 6928 | if( lPath[len-1]!='/' ){ |
| 6929 | len = strlcat(lPath, "/", maxLen); |
| 6930 | } |
| 6931 | |
| 6932 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6933 | dbLen = (int)strlen(dbPath); |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 6934 | for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6935 | char c = dbPath[i]; |
| 6936 | lPath[i+len] = (c=='/')?'_':c; |
| 6937 | } |
| 6938 | lPath[i+len]='\0'; |
| 6939 | strlcat(lPath, ":auto:", maxLen); |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6940 | OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6941 | return SQLITE_OK; |
| 6942 | } |
| 6943 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6944 | /* |
| 6945 | ** Creates the lock file and any missing directories in lockPath |
| 6946 | */ |
| 6947 | static int proxyCreateLockPath(const char *lockPath){ |
| 6948 | int i, len; |
| 6949 | char buf[MAXPATHLEN]; |
| 6950 | int start = 0; |
| 6951 | |
| 6952 | assert(lockPath!=NULL); |
| 6953 | /* try to create all the intermediate directories */ |
| 6954 | len = (int)strlen(lockPath); |
| 6955 | buf[0] = lockPath[0]; |
| 6956 | for( i=1; i<len; i++ ){ |
| 6957 | if( lockPath[i] == '/' && (i - start > 0) ){ |
| 6958 | /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 6959 | if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 6960 | || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 6961 | buf[i]='\0'; |
drh | 9ef6bc4 | 2011-11-04 02:24:02 +0000 | [diff] [blame] | 6962 | if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6963 | int err=errno; |
| 6964 | if( err!=EEXIST ) { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6965 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6966 | "'%s' proxy lock path=%s pid=%d\n", |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 6967 | buf, strerror(err), lockPath, osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6968 | return err; |
| 6969 | } |
| 6970 | } |
| 6971 | } |
| 6972 | start=i+1; |
| 6973 | } |
| 6974 | buf[i] = lockPath[i]; |
| 6975 | } |
drh | 62aaa6c | 2015-11-21 17:27:42 +0000 | [diff] [blame] | 6976 | OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6977 | return 0; |
| 6978 | } |
| 6979 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6980 | /* |
| 6981 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 6982 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 6983 | ** |
| 6984 | ** The caller is responsible not only for closing the file descriptor |
| 6985 | ** but also for freeing the memory associated with the file descriptor. |
| 6986 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6987 | static int proxyCreateUnixFile( |
| 6988 | const char *path, /* path for the new unixFile */ |
| 6989 | unixFile **ppFile, /* unixFile created and returned by ref */ |
| 6990 | int islockfile /* if non zero missing dirs will be created */ |
| 6991 | ) { |
| 6992 | int fd = -1; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6993 | unixFile *pNew; |
| 6994 | int rc = SQLITE_OK; |
drh | c398c65 | 2019-11-22 00:42:01 +0000 | [diff] [blame] | 6995 | int openFlags = O_RDWR | O_CREAT | O_NOFOLLOW; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6996 | sqlite3_vfs dummyVfs; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6997 | int terrno = 0; |
| 6998 | UnixUnusedFd *pUnused = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6999 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7000 | /* 1. first try to open/create the file |
| 7001 | ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 7002 | ** the parent directories and then try again. |
| 7003 | ** 3. if that fails, try to open the file read-only |
| 7004 | ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 7005 | */ |
| 7006 | pUnused = findReusableFd(path, openFlags); |
| 7007 | if( pUnused ){ |
| 7008 | fd = pUnused->fd; |
| 7009 | }else{ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 7010 | pUnused = sqlite3_malloc64(sizeof(*pUnused)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7011 | if( !pUnused ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7012 | return SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7013 | } |
| 7014 | } |
| 7015 | if( fd<0 ){ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 7016 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7017 | terrno = errno; |
| 7018 | if( fd<0 && errno==ENOENT && islockfile ){ |
| 7019 | if( proxyCreateLockPath(path) == SQLITE_OK ){ |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 7020 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7021 | } |
| 7022 | } |
| 7023 | } |
| 7024 | if( fd<0 ){ |
drh | c398c65 | 2019-11-22 00:42:01 +0000 | [diff] [blame] | 7025 | openFlags = O_RDONLY | O_NOFOLLOW; |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 7026 | fd = robust_open(path, openFlags, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7027 | terrno = errno; |
| 7028 | } |
| 7029 | if( fd<0 ){ |
| 7030 | if( islockfile ){ |
| 7031 | return SQLITE_BUSY; |
| 7032 | } |
| 7033 | switch (terrno) { |
| 7034 | case EACCES: |
| 7035 | return SQLITE_PERM; |
| 7036 | case EIO: |
| 7037 | return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 7038 | default: |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 7039 | return SQLITE_CANTOPEN_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7040 | } |
| 7041 | } |
| 7042 | |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 7043 | pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7044 | if( pNew==NULL ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7045 | rc = SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7046 | goto end_create_proxy; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7047 | } |
| 7048 | memset(pNew, 0, sizeof(unixFile)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7049 | pNew->openFlags = openFlags; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 7050 | memset(&dummyVfs, 0, sizeof(dummyVfs)); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7051 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
dan | 211fb08 | 2011-04-01 09:04:36 +0000 | [diff] [blame] | 7052 | dummyVfs.zName = "dummy"; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7053 | pUnused->fd = fd; |
| 7054 | pUnused->flags = openFlags; |
drh | c68886b | 2017-08-18 16:09:52 +0000 | [diff] [blame] | 7055 | pNew->pPreallocatedUnused = pUnused; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7056 | |
drh | c02a43a | 2012-01-10 23:18:38 +0000 | [diff] [blame] | 7057 | rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7058 | if( rc==SQLITE_OK ){ |
| 7059 | *ppFile = pNew; |
| 7060 | return SQLITE_OK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7061 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7062 | end_create_proxy: |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 7063 | robust_close(pNew, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7064 | sqlite3_free(pNew); |
| 7065 | sqlite3_free(pUnused); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7066 | return rc; |
| 7067 | } |
| 7068 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7069 | #ifdef SQLITE_TEST |
| 7070 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 7071 | int sqlite3_hostid_num = 0; |
| 7072 | #endif |
| 7073 | |
| 7074 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 7075 | |
drh | e4079e1 | 2019-09-27 16:33:27 +0000 | [diff] [blame] | 7076 | #if HAVE_GETHOSTUUID |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 7077 | /* Not always defined in the headers as it ought to be */ |
| 7078 | extern int gethostuuid(uuid_t id, const struct timespec *wait); |
drh | 6bca651 | 2015-04-13 23:05:28 +0000 | [diff] [blame] | 7079 | #endif |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 7080 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7081 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 7082 | ** bytes of writable memory. |
| 7083 | */ |
| 7084 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7085 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 7086 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
drh | e4079e1 | 2019-09-27 16:33:27 +0000 | [diff] [blame] | 7087 | #if HAVE_GETHOSTUUID |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 7088 | { |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7089 | struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
drh | 29ecd8a | 2010-12-21 00:16:40 +0000 | [diff] [blame] | 7090 | if( gethostuuid(pHostID, &timeout) ){ |
| 7091 | int err = errno; |
| 7092 | if( pError ){ |
| 7093 | *pError = err; |
| 7094 | } |
| 7095 | return SQLITE_IOERR; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7096 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7097 | } |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 7098 | #else |
| 7099 | UNUSED_PARAMETER(pError); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 7100 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7101 | #ifdef SQLITE_TEST |
| 7102 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 7103 | if( sqlite3_hostid_num != 0){ |
| 7104 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 7105 | } |
| 7106 | #endif |
| 7107 | |
| 7108 | return SQLITE_OK; |
| 7109 | } |
| 7110 | |
| 7111 | /* The conch file contains the header, host id and lock file path |
| 7112 | */ |
| 7113 | #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 7114 | #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 7115 | #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 7116 | #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 7117 | |
| 7118 | /* |
| 7119 | ** Takes an open conch file, copies the contents to a new path and then moves |
| 7120 | ** it back. The newly created file's file descriptor is assigned to the |
| 7121 | ** conch file structure and finally the original conch file descriptor is |
| 7122 | ** closed. Returns zero if successful. |
| 7123 | */ |
| 7124 | static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 7125 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7126 | unixFile *conchFile = pCtx->conchFile; |
| 7127 | char tPath[MAXPATHLEN]; |
| 7128 | char buf[PROXY_MAXCONCHLEN]; |
| 7129 | char *cPath = pCtx->conchFilePath; |
| 7130 | size_t readLen = 0; |
| 7131 | size_t pathLen = 0; |
| 7132 | char errmsg[64] = ""; |
| 7133 | int fd = -1; |
| 7134 | int rc = -1; |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 7135 | UNUSED_PARAMETER(myHostID); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7136 | |
| 7137 | /* create a new path by replace the trailing '-conch' with '-break' */ |
| 7138 | pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 7139 | if( pathLen>MAXPATHLEN || pathLen<6 || |
| 7140 | (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 7141 | sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7142 | goto end_breaklock; |
| 7143 | } |
| 7144 | /* read the conch content */ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7145 | readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7146 | if( readLen<PROXY_PATHINDEX ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 7147 | sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7148 | goto end_breaklock; |
| 7149 | } |
| 7150 | /* write it out to the temporary break file */ |
drh | c398c65 | 2019-11-22 00:42:01 +0000 | [diff] [blame] | 7151 | fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW), 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7152 | if( fd<0 ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 7153 | sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7154 | goto end_breaklock; |
| 7155 | } |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7156 | if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 7157 | sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7158 | goto end_breaklock; |
| 7159 | } |
| 7160 | if( rename(tPath, cPath) ){ |
dan | 0cb3a1e | 2010-11-29 17:55:18 +0000 | [diff] [blame] | 7161 | sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7162 | goto end_breaklock; |
| 7163 | } |
| 7164 | rc = 0; |
| 7165 | fprintf(stderr, "broke stale lock on %s\n", cPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 7166 | robust_close(pFile, conchFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7167 | conchFile->h = fd; |
| 7168 | conchFile->openFlags = O_RDWR | O_CREAT; |
| 7169 | |
| 7170 | end_breaklock: |
| 7171 | if( rc ){ |
| 7172 | if( fd>=0 ){ |
drh | 036ac7f | 2011-08-08 23:18:05 +0000 | [diff] [blame] | 7173 | osUnlink(tPath); |
drh | 0e9365c | 2011-03-02 02:08:13 +0000 | [diff] [blame] | 7174 | robust_close(pFile, fd, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7175 | } |
| 7176 | fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 7177 | } |
| 7178 | return rc; |
| 7179 | } |
| 7180 | |
| 7181 | /* Take the requested lock on the conch file and break a stale lock if the |
| 7182 | ** host id matches. |
| 7183 | */ |
| 7184 | static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 7185 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7186 | unixFile *conchFile = pCtx->conchFile; |
| 7187 | int rc = SQLITE_OK; |
| 7188 | int nTries = 0; |
| 7189 | struct timespec conchModTime; |
| 7190 | |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 7191 | memset(&conchModTime, 0, sizeof(conchModTime)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7192 | do { |
| 7193 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 7194 | nTries ++; |
| 7195 | if( rc==SQLITE_BUSY ){ |
| 7196 | /* If the lock failed (busy): |
| 7197 | * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 7198 | * 2nd try: fail if the mod time changed or host id is different, wait |
| 7199 | * 10 sec and try again |
| 7200 | * 3rd try: break the lock unless the mod time has changed. |
| 7201 | */ |
| 7202 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7203 | if( osFstat(conchFile->h, &buf) ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7204 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7205 | return SQLITE_IOERR_LOCK; |
| 7206 | } |
| 7207 | |
| 7208 | if( nTries==1 ){ |
| 7209 | conchModTime = buf.st_mtimespec; |
drh | ddcfe92 | 2020-09-15 12:29:35 +0000 | [diff] [blame] | 7210 | unixSleep(0,500000); /* wait 0.5 sec and try the lock again*/ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7211 | continue; |
| 7212 | } |
| 7213 | |
| 7214 | assert( nTries>1 ); |
| 7215 | if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 7216 | conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 7217 | return SQLITE_BUSY; |
| 7218 | } |
| 7219 | |
| 7220 | if( nTries==2 ){ |
| 7221 | char tBuf[PROXY_MAXCONCHLEN]; |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7222 | int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7223 | if( len<0 ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7224 | storeLastErrno(pFile, errno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7225 | return SQLITE_IOERR_LOCK; |
| 7226 | } |
| 7227 | if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 7228 | /* don't break the lock if the host id doesn't match */ |
| 7229 | if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 7230 | return SQLITE_BUSY; |
| 7231 | } |
| 7232 | }else{ |
| 7233 | /* don't break the lock on short read or a version mismatch */ |
| 7234 | return SQLITE_BUSY; |
| 7235 | } |
drh | ddcfe92 | 2020-09-15 12:29:35 +0000 | [diff] [blame] | 7236 | unixSleep(0,10000000); /* wait 10 sec and try the lock again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7237 | continue; |
| 7238 | } |
| 7239 | |
| 7240 | assert( nTries==3 ); |
| 7241 | if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 7242 | rc = SQLITE_OK; |
| 7243 | if( lockType==EXCLUSIVE_LOCK ){ |
drh | e6d4173 | 2015-02-21 00:49:00 +0000 | [diff] [blame] | 7244 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7245 | } |
| 7246 | if( !rc ){ |
| 7247 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 7248 | } |
| 7249 | } |
| 7250 | } |
| 7251 | } while( rc==SQLITE_BUSY && nTries<3 ); |
| 7252 | |
| 7253 | return rc; |
| 7254 | } |
| 7255 | |
| 7256 | /* 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] | 7257 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 7258 | ** lockPath means that the lockPath in the conch file will be used if the |
| 7259 | ** host IDs match, or a new lock path will be generated automatically |
| 7260 | ** and written to the conch file. |
| 7261 | */ |
| 7262 | static int proxyTakeConch(unixFile *pFile){ |
| 7263 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7264 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7265 | if( pCtx->conchHeld!=0 ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7266 | return SQLITE_OK; |
| 7267 | }else{ |
| 7268 | unixFile *conchFile = pCtx->conchFile; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7269 | uuid_t myHostID; |
| 7270 | int pError = 0; |
| 7271 | char readBuf[PROXY_MAXCONCHLEN]; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7272 | char lockPath[MAXPATHLEN]; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7273 | char *tempLockPath = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7274 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7275 | int createConch = 0; |
| 7276 | int hostIdMatch = 0; |
| 7277 | int readLen = 0; |
| 7278 | int tryOldLockPath = 0; |
| 7279 | int forceNewLockPath = 0; |
| 7280 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7281 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 91eb93c | 2015-03-03 19:56:20 +0000 | [diff] [blame] | 7282 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 7283 | osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7284 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7285 | rc = proxyGetHostID(myHostID, &pError); |
| 7286 | if( (rc&0xff)==SQLITE_IOERR ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7287 | storeLastErrno(pFile, pError); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7288 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7289 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7290 | rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7291 | if( rc!=SQLITE_OK ){ |
| 7292 | goto end_takeconch; |
| 7293 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7294 | /* read the existing conch file */ |
| 7295 | readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 7296 | if( readLen<0 ){ |
| 7297 | /* I/O error: lastErrno set by seekAndRead */ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7298 | storeLastErrno(pFile, conchFile->lastErrno); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7299 | rc = SQLITE_IOERR_READ; |
| 7300 | goto end_takeconch; |
| 7301 | }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 7302 | readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 7303 | /* a short read or version format mismatch means we need to create a new |
| 7304 | ** conch file. |
| 7305 | */ |
| 7306 | createConch = 1; |
| 7307 | } |
| 7308 | /* if the host id matches and the lock path already exists in the conch |
| 7309 | ** we'll try to use the path there, if we can't open that path, we'll |
| 7310 | ** retry with a new auto-generated path |
| 7311 | */ |
| 7312 | do { /* in case we need to try again for an :auto: named lock file */ |
| 7313 | |
| 7314 | if( !createConch && !forceNewLockPath ){ |
| 7315 | hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 7316 | PROXY_HOSTIDLEN); |
| 7317 | /* if the conch has data compare the contents */ |
| 7318 | if( !pCtx->lockProxyPath ){ |
| 7319 | /* for auto-named local lock file, just check the host ID and we'll |
| 7320 | ** use the local lock file path that's already in there |
| 7321 | */ |
| 7322 | if( hostIdMatch ){ |
| 7323 | size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 7324 | |
| 7325 | if( pathLen>=MAXPATHLEN ){ |
| 7326 | pathLen=MAXPATHLEN-1; |
| 7327 | } |
| 7328 | memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 7329 | lockPath[pathLen] = 0; |
| 7330 | tempLockPath = lockPath; |
| 7331 | tryOldLockPath = 1; |
| 7332 | /* create a copy of the lock path if the conch is taken */ |
| 7333 | goto end_takeconch; |
| 7334 | } |
| 7335 | }else if( hostIdMatch |
| 7336 | && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 7337 | readLen-PROXY_PATHINDEX) |
| 7338 | ){ |
| 7339 | /* conch host and lock path match */ |
| 7340 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7341 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7342 | } |
| 7343 | |
| 7344 | /* if the conch isn't writable and doesn't match, we can't take it */ |
| 7345 | if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 7346 | rc = SQLITE_BUSY; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7347 | goto end_takeconch; |
| 7348 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7349 | |
| 7350 | /* 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] | 7351 | if( !pCtx->lockProxyPath ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7352 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 7353 | tempLockPath = lockPath; |
| 7354 | /* create a copy of the lock path _only_ if the conch is taken */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7355 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7356 | |
| 7357 | /* update conch with host and path (this will fail if other process |
| 7358 | ** has a shared lock already), if the host id matches, use the big |
| 7359 | ** stick. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7360 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7361 | futimes(conchFile->h, NULL); |
| 7362 | if( hostIdMatch && !createConch ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 7363 | if( conchFile->pInode && conchFile->pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7364 | /* We are trying for an exclusive lock but another thread in this |
| 7365 | ** same process is still holding a shared lock. */ |
| 7366 | rc = SQLITE_BUSY; |
| 7367 | } else { |
| 7368 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7369 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7370 | }else{ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7371 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7372 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7373 | if( rc==SQLITE_OK ){ |
| 7374 | char writeBuffer[PROXY_MAXCONCHLEN]; |
| 7375 | int writeSize = 0; |
| 7376 | |
| 7377 | writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 7378 | memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 7379 | if( pCtx->lockProxyPath!=NULL ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7380 | strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, |
| 7381 | MAXPATHLEN); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7382 | }else{ |
| 7383 | strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 7384 | } |
| 7385 | writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 7386 | robust_ftruncate(conchFile->h, writeSize); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7387 | rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
drh | 6d25899 | 2016-02-04 09:48:12 +0000 | [diff] [blame] | 7388 | full_fsync(conchFile->h,0,0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7389 | /* If we created a new conch file (not just updated the contents of a |
| 7390 | ** valid conch file), try to match the permissions of the database |
| 7391 | */ |
| 7392 | if( rc==SQLITE_OK && createConch ){ |
| 7393 | struct stat buf; |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7394 | int err = osFstat(pFile->h, &buf); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7395 | if( err==0 ){ |
| 7396 | mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 7397 | S_IROTH|S_IWOTH); |
| 7398 | /* try to match the database file R/W permissions, ignore failure */ |
| 7399 | #ifndef SQLITE_PROXY_DEBUG |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7400 | osFchmod(conchFile->h, cmode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7401 | #else |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 7402 | do{ |
drh | e562be5 | 2011-03-02 18:01:10 +0000 | [diff] [blame] | 7403 | rc = osFchmod(conchFile->h, cmode); |
drh | ff81231 | 2011-02-23 13:33:46 +0000 | [diff] [blame] | 7404 | }while( rc==(-1) && errno==EINTR ); |
| 7405 | if( rc!=0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7406 | int code = errno; |
| 7407 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 7408 | cmode, code, strerror(code)); |
| 7409 | } else { |
| 7410 | fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 7411 | } |
| 7412 | }else{ |
| 7413 | int code = errno; |
| 7414 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 7415 | err, code, strerror(code)); |
| 7416 | #endif |
| 7417 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7418 | } |
| 7419 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7420 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 7421 | |
| 7422 | end_takeconch: |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7423 | OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7424 | if( rc==SQLITE_OK && pFile->openFlags ){ |
drh | 3d4435b | 2011-08-26 20:55:50 +0000 | [diff] [blame] | 7425 | int fd; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7426 | if( pFile->h>=0 ){ |
drh | e84009f | 2011-03-02 17:54:32 +0000 | [diff] [blame] | 7427 | robust_close(pFile, pFile->h, __LINE__); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7428 | } |
| 7429 | pFile->h = -1; |
drh | 8c815d1 | 2012-02-13 20:16:37 +0000 | [diff] [blame] | 7430 | fd = robust_open(pCtx->dbPath, pFile->openFlags, 0); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7431 | OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7432 | if( fd>=0 ){ |
| 7433 | pFile->h = fd; |
| 7434 | }else{ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 7435 | rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7436 | during locking */ |
| 7437 | } |
| 7438 | } |
| 7439 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 7440 | char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 7441 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 7442 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 7443 | /* we couldn't create the proxy lock file with the old lock file path |
| 7444 | ** so try again via auto-naming |
| 7445 | */ |
| 7446 | forceNewLockPath = 1; |
| 7447 | tryOldLockPath = 0; |
dan | 2b0ef47 | 2010-02-16 12:18:47 +0000 | [diff] [blame] | 7448 | continue; /* go back to the do {} while start point, try again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7449 | } |
| 7450 | } |
| 7451 | if( rc==SQLITE_OK ){ |
| 7452 | /* Need to make a copy of path if we extracted the value |
| 7453 | ** from the conch file or the path was allocated on the stack |
| 7454 | */ |
| 7455 | if( tempLockPath ){ |
| 7456 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 7457 | if( !pCtx->lockProxyPath ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7458 | rc = SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7459 | } |
| 7460 | } |
| 7461 | } |
| 7462 | if( rc==SQLITE_OK ){ |
| 7463 | pCtx->conchHeld = 1; |
| 7464 | |
| 7465 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 7466 | afpLockingContext *afpCtx; |
| 7467 | afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 7468 | afpCtx->dbPath = pCtx->lockProxyPath; |
| 7469 | } |
| 7470 | } else { |
| 7471 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 7472 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7473 | OSTRACE(("TAKECONCH %d %s\n", conchFile->h, |
| 7474 | rc==SQLITE_OK?"ok":"failed")); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7475 | return rc; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7476 | } while (1); /* in case we need to retry the :auto: lock file - |
| 7477 | ** we should never get here except via the 'continue' call. */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7478 | } |
| 7479 | } |
| 7480 | |
| 7481 | /* |
| 7482 | ** If pFile holds a lock on a conch file, then release that lock. |
| 7483 | */ |
| 7484 | static int proxyReleaseConch(unixFile *pFile){ |
drh | 1c5bb4d | 2010-05-10 17:29:28 +0000 | [diff] [blame] | 7485 | int rc = SQLITE_OK; /* Subroutine return code */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7486 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 7487 | unixFile *conchFile; /* Name of the conch file */ |
| 7488 | |
| 7489 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7490 | conchFile = pCtx->conchFile; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7491 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7492 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 7493 | osGetpid(0))); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7494 | if( pCtx->conchHeld>0 ){ |
| 7495 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 7496 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7497 | pCtx->conchHeld = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7498 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| 7499 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7500 | return rc; |
| 7501 | } |
| 7502 | |
| 7503 | /* |
| 7504 | ** 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] | 7505 | ** Store the conch filename in memory obtained from sqlite3_malloc64(). |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7506 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 7507 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 7508 | ** |
| 7509 | ** The caller is responsible for ensuring that the allocated memory |
| 7510 | ** space is eventually freed. |
| 7511 | ** |
| 7512 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 7513 | */ |
| 7514 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 7515 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7516 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7517 | char *conchPath; /* buffer in which to construct conch name */ |
| 7518 | |
| 7519 | /* Allocate space for the conch filename and initialize the name to |
| 7520 | ** the name of the original database file. */ |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 7521 | *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7522 | if( conchPath==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7523 | return SQLITE_NOMEM_BKPT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7524 | } |
| 7525 | memcpy(conchPath, dbPath, len+1); |
| 7526 | |
| 7527 | /* now insert a "." before the last / character */ |
| 7528 | for( i=(len-1); i>=0; i-- ){ |
| 7529 | if( conchPath[i]=='/' ){ |
| 7530 | i++; |
| 7531 | break; |
| 7532 | } |
| 7533 | } |
| 7534 | conchPath[i]='.'; |
| 7535 | while ( i<len ){ |
| 7536 | conchPath[i+1]=dbPath[i]; |
| 7537 | i++; |
| 7538 | } |
| 7539 | |
| 7540 | /* append the "-conch" suffix to the file */ |
| 7541 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7542 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7543 | |
| 7544 | return SQLITE_OK; |
| 7545 | } |
| 7546 | |
| 7547 | |
| 7548 | /* Takes a fully configured proxy locking-style unix file and switches |
| 7549 | ** the local lock file path |
| 7550 | */ |
| 7551 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 7552 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 7553 | char *oldPath = pCtx->lockProxyPath; |
| 7554 | int rc = SQLITE_OK; |
| 7555 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7556 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7557 | return SQLITE_BUSY; |
| 7558 | } |
| 7559 | |
| 7560 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 7561 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 7562 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 7563 | return SQLITE_OK; |
| 7564 | }else{ |
| 7565 | unixFile *lockProxy = pCtx->lockProxy; |
| 7566 | pCtx->lockProxy=NULL; |
| 7567 | pCtx->conchHeld = 0; |
| 7568 | if( lockProxy!=NULL ){ |
| 7569 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 7570 | if( rc ) return rc; |
| 7571 | sqlite3_free(lockProxy); |
| 7572 | } |
| 7573 | sqlite3_free(oldPath); |
| 7574 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 7575 | } |
| 7576 | |
| 7577 | return rc; |
| 7578 | } |
| 7579 | |
| 7580 | /* |
| 7581 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 7582 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 7583 | ** |
| 7584 | ** This routine find the filename associated with pFile and writes it |
| 7585 | ** int dbPath. |
| 7586 | */ |
| 7587 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7588 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7589 | if( pFile->pMethod == &afpIoMethods ){ |
| 7590 | /* afp style keeps a reference to the db path in the filePath field |
| 7591 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 7592 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7593 | strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, |
| 7594 | MAXPATHLEN); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7595 | } else |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7596 | #endif |
| 7597 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 7598 | /* dot lock style uses the locking context to store the dot lock |
| 7599 | ** file path */ |
| 7600 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 7601 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 7602 | }else{ |
| 7603 | /* all other styles use the locking context to store the db file path */ |
| 7604 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7605 | strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7606 | } |
| 7607 | return SQLITE_OK; |
| 7608 | } |
| 7609 | |
| 7610 | /* |
| 7611 | ** Takes an already filled in unix file and alters it so all file locking |
| 7612 | ** will be performed on the local proxy lock file. The following fields |
| 7613 | ** are preserved in the locking context so that they can be restored and |
| 7614 | ** the unix structure properly cleaned up at close time: |
| 7615 | ** ->lockingContext |
| 7616 | ** ->pMethod |
| 7617 | */ |
| 7618 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 7619 | proxyLockingContext *pCtx; |
| 7620 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 7621 | char *lockPath=NULL; |
| 7622 | int rc = SQLITE_OK; |
| 7623 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7624 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7625 | return SQLITE_BUSY; |
| 7626 | } |
| 7627 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 7628 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 7629 | lockPath=NULL; |
| 7630 | }else{ |
| 7631 | lockPath=(char *)path; |
| 7632 | } |
| 7633 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7634 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
drh | 5ac9365 | 2015-03-21 20:59:43 +0000 | [diff] [blame] | 7635 | (lockPath ? lockPath : ":auto:"), osGetpid(0))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7636 | |
drh | f3cdcdc | 2015-04-29 16:50:28 +0000 | [diff] [blame] | 7637 | pCtx = sqlite3_malloc64( sizeof(*pCtx) ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7638 | if( pCtx==0 ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7639 | return SQLITE_NOMEM_BKPT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7640 | } |
| 7641 | memset(pCtx, 0, sizeof(*pCtx)); |
| 7642 | |
| 7643 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 7644 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7645 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 7646 | if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 7647 | /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 7648 | ** (c) the file system is read-only, then enable no-locking access. |
| 7649 | ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 7650 | ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 7651 | */ |
| 7652 | struct statfs fsInfo; |
| 7653 | struct stat conchInfo; |
| 7654 | int goLockless = 0; |
| 7655 | |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7656 | if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7657 | int err = errno; |
| 7658 | if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 7659 | goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 7660 | } |
| 7661 | } |
| 7662 | if( goLockless ){ |
| 7663 | pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 7664 | rc = SQLITE_OK; |
| 7665 | } |
| 7666 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7667 | } |
| 7668 | if( rc==SQLITE_OK && lockPath ){ |
| 7669 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 7670 | } |
| 7671 | |
| 7672 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7673 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 7674 | if( pCtx->dbPath==NULL ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 7675 | rc = SQLITE_NOMEM_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7676 | } |
| 7677 | } |
| 7678 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7679 | /* all memory is allocated, proxys are created and assigned, |
| 7680 | ** switch the locking context and pMethod then return. |
| 7681 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7682 | pCtx->oldLockingContext = pFile->lockingContext; |
| 7683 | pFile->lockingContext = pCtx; |
| 7684 | pCtx->pOldMethod = pFile->pMethod; |
| 7685 | pFile->pMethod = &proxyIoMethods; |
| 7686 | }else{ |
| 7687 | if( pCtx->conchFile ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7688 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7689 | sqlite3_free(pCtx->conchFile); |
| 7690 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7691 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7692 | sqlite3_free(pCtx->conchFilePath); |
| 7693 | sqlite3_free(pCtx); |
| 7694 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7695 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 7696 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7697 | return rc; |
| 7698 | } |
| 7699 | |
| 7700 | |
| 7701 | /* |
| 7702 | ** This routine handles sqlite3_file_control() calls that are specific |
| 7703 | ** to proxy locking. |
| 7704 | */ |
| 7705 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 7706 | switch( op ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7707 | case SQLITE_FCNTL_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7708 | unixFile *pFile = (unixFile*)id; |
| 7709 | if( pFile->pMethod == &proxyIoMethods ){ |
| 7710 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 7711 | proxyTakeConch(pFile); |
| 7712 | if( pCtx->lockProxyPath ){ |
| 7713 | *(const char **)pArg = pCtx->lockProxyPath; |
| 7714 | }else{ |
| 7715 | *(const char **)pArg = ":auto: (not held)"; |
| 7716 | } |
| 7717 | } else { |
| 7718 | *(const char **)pArg = NULL; |
| 7719 | } |
| 7720 | return SQLITE_OK; |
| 7721 | } |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7722 | case SQLITE_FCNTL_SET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7723 | unixFile *pFile = (unixFile*)id; |
| 7724 | int rc = SQLITE_OK; |
| 7725 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 7726 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 7727 | if( isProxyStyle ){ |
drh | 4bf66fd | 2015-02-19 02:43:02 +0000 | [diff] [blame] | 7728 | /* turn off proxy locking - not supported. If support is added for |
| 7729 | ** switching proxy locking mode off then it will need to fail if |
| 7730 | ** the journal mode is WAL mode. |
| 7731 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7732 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 7733 | }else{ |
| 7734 | /* turn off proxy locking - already off - NOOP */ |
| 7735 | rc = SQLITE_OK; |
| 7736 | } |
| 7737 | }else{ |
| 7738 | const char *proxyPath = (const char *)pArg; |
| 7739 | if( isProxyStyle ){ |
| 7740 | proxyLockingContext *pCtx = |
| 7741 | (proxyLockingContext*)pFile->lockingContext; |
| 7742 | if( !strcmp(pArg, ":auto:") |
| 7743 | || (pCtx->lockProxyPath && |
| 7744 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 7745 | ){ |
| 7746 | rc = SQLITE_OK; |
| 7747 | }else{ |
| 7748 | rc = switchLockProxyPath(pFile, proxyPath); |
| 7749 | } |
| 7750 | }else{ |
| 7751 | /* turn on proxy file locking */ |
| 7752 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 7753 | } |
| 7754 | } |
| 7755 | return rc; |
| 7756 | } |
| 7757 | default: { |
| 7758 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 7759 | } |
| 7760 | } |
drh | 8616cff | 2019-07-13 16:15:23 +0000 | [diff] [blame] | 7761 | /*NOTREACHED*/ assert(0); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7762 | return SQLITE_ERROR; |
| 7763 | } |
| 7764 | |
| 7765 | /* |
| 7766 | ** Within this division (the proxying locking implementation) the procedures |
| 7767 | ** above this point are all utilities. The lock-related methods of the |
| 7768 | ** proxy-locking sqlite3_io_method object follow. |
| 7769 | */ |
| 7770 | |
| 7771 | |
| 7772 | /* |
| 7773 | ** This routine checks if there is a RESERVED lock held on the specified |
| 7774 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 7775 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 7776 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 7777 | */ |
| 7778 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 7779 | unixFile *pFile = (unixFile*)id; |
| 7780 | int rc = proxyTakeConch(pFile); |
| 7781 | if( rc==SQLITE_OK ){ |
| 7782 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7783 | if( pCtx->conchHeld>0 ){ |
| 7784 | unixFile *proxy = pCtx->lockProxy; |
| 7785 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 7786 | }else{ /* conchHeld < 0 is lockless */ |
| 7787 | pResOut=0; |
| 7788 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7789 | } |
| 7790 | return rc; |
| 7791 | } |
| 7792 | |
| 7793 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7794 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7795 | ** of the following: |
| 7796 | ** |
| 7797 | ** (1) SHARED_LOCK |
| 7798 | ** (2) RESERVED_LOCK |
| 7799 | ** (3) PENDING_LOCK |
| 7800 | ** (4) EXCLUSIVE_LOCK |
| 7801 | ** |
| 7802 | ** Sometimes when requesting one lock state, additional lock states |
| 7803 | ** are inserted in between. The locking might fail on one of the later |
| 7804 | ** transitions leaving the lock state different from what it started but |
| 7805 | ** still short of its goal. The following chart shows the allowed |
| 7806 | ** transitions and the inserted intermediate states: |
| 7807 | ** |
| 7808 | ** UNLOCKED -> SHARED |
| 7809 | ** SHARED -> RESERVED |
| 7810 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 7811 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 7812 | ** PENDING -> EXCLUSIVE |
| 7813 | ** |
| 7814 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 7815 | ** routine to lower a locking level. |
| 7816 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7817 | static int proxyLock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7818 | unixFile *pFile = (unixFile*)id; |
| 7819 | int rc = proxyTakeConch(pFile); |
| 7820 | if( rc==SQLITE_OK ){ |
| 7821 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7822 | if( pCtx->conchHeld>0 ){ |
| 7823 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7824 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); |
| 7825 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7826 | }else{ |
| 7827 | /* conchHeld < 0 is lockless */ |
| 7828 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7829 | } |
| 7830 | return rc; |
| 7831 | } |
| 7832 | |
| 7833 | |
| 7834 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7835 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7836 | ** must be either NO_LOCK or SHARED_LOCK. |
| 7837 | ** |
| 7838 | ** If the locking level of the file descriptor is already at or below |
| 7839 | ** the requested locking level, this routine is a no-op. |
| 7840 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7841 | static int proxyUnlock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7842 | unixFile *pFile = (unixFile*)id; |
| 7843 | int rc = proxyTakeConch(pFile); |
| 7844 | if( rc==SQLITE_OK ){ |
| 7845 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7846 | if( pCtx->conchHeld>0 ){ |
| 7847 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 7848 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); |
| 7849 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7850 | }else{ |
| 7851 | /* conchHeld < 0 is lockless */ |
| 7852 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7853 | } |
| 7854 | return rc; |
| 7855 | } |
| 7856 | |
| 7857 | /* |
| 7858 | ** Close a file that uses proxy locks. |
| 7859 | */ |
| 7860 | static int proxyClose(sqlite3_file *id) { |
drh | a8de1e1 | 2015-11-30 00:05:39 +0000 | [diff] [blame] | 7861 | if( ALWAYS(id) ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7862 | unixFile *pFile = (unixFile*)id; |
| 7863 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 7864 | unixFile *lockProxy = pCtx->lockProxy; |
| 7865 | unixFile *conchFile = pCtx->conchFile; |
| 7866 | int rc = SQLITE_OK; |
| 7867 | |
| 7868 | if( lockProxy ){ |
| 7869 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 7870 | if( rc ) return rc; |
| 7871 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 7872 | if( rc ) return rc; |
| 7873 | sqlite3_free(lockProxy); |
| 7874 | pCtx->lockProxy = 0; |
| 7875 | } |
| 7876 | if( conchFile ){ |
| 7877 | if( pCtx->conchHeld ){ |
| 7878 | rc = proxyReleaseConch(pFile); |
| 7879 | if( rc ) return rc; |
| 7880 | } |
| 7881 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 7882 | if( rc ) return rc; |
| 7883 | sqlite3_free(conchFile); |
| 7884 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7885 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7886 | sqlite3_free(pCtx->conchFilePath); |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 7887 | sqlite3DbFree(0, pCtx->dbPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7888 | /* restore the original locking context and pMethod then close it */ |
| 7889 | pFile->lockingContext = pCtx->oldLockingContext; |
| 7890 | pFile->pMethod = pCtx->pOldMethod; |
| 7891 | sqlite3_free(pCtx); |
| 7892 | return pFile->pMethod->xClose(id); |
| 7893 | } |
| 7894 | return SQLITE_OK; |
| 7895 | } |
| 7896 | |
| 7897 | |
| 7898 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7899 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 7900 | /* |
| 7901 | ** The proxy locking style is intended for use with AFP filesystems. |
| 7902 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 7903 | ** restricted to MacOSX. |
| 7904 | ** |
| 7905 | ** |
| 7906 | ******************* End of the proxy lock implementation ********************** |
| 7907 | ******************************************************************************/ |
| 7908 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7909 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7910 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7911 | ** |
| 7912 | ** This routine registers all VFS implementations for unix-like operating |
| 7913 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 7914 | ** should be the only routines in this file that are visible from other |
| 7915 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7916 | ** |
| 7917 | ** This routine is called once during SQLite initialization and by a |
| 7918 | ** single thread. The memory allocation and mutex subsystems have not |
| 7919 | ** necessarily been initialized when this routine is called, and so they |
| 7920 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 7921 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 7922 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7923 | /* |
| 7924 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7925 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 7926 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 7927 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 7928 | ** and so we have to go through the intermediate pointer to avoid problems |
| 7929 | ** when compiling with -pedantic-errors on GCC.) |
| 7930 | ** |
| 7931 | ** 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] | 7932 | ** finder-function. The finder-function returns a pointer to the |
| 7933 | ** sqlite_io_methods object that implements the desired locking |
| 7934 | ** behaviors. See the division above that contains the IOMETHODS |
| 7935 | ** macro for addition information on finder-functions. |
| 7936 | ** |
| 7937 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 7938 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 7939 | ** more than that; it looks at the filesystem type that hosts the |
| 7940 | ** database file and tries to choose an locking method appropriate for |
| 7941 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7942 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7943 | #define UNIXVFS(VFSNAME, FINDER) { \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7944 | 3, /* iVersion */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7945 | sizeof(unixFile), /* szOsFile */ \ |
| 7946 | MAX_PATHNAME, /* mxPathname */ \ |
| 7947 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7948 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 7949 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7950 | unixOpen, /* xOpen */ \ |
| 7951 | unixDelete, /* xDelete */ \ |
| 7952 | unixAccess, /* xAccess */ \ |
| 7953 | unixFullPathname, /* xFullPathname */ \ |
| 7954 | unixDlOpen, /* xDlOpen */ \ |
| 7955 | unixDlError, /* xDlError */ \ |
| 7956 | unixDlSym, /* xDlSym */ \ |
| 7957 | unixDlClose, /* xDlClose */ \ |
| 7958 | unixRandomness, /* xRandomness */ \ |
| 7959 | unixSleep, /* xSleep */ \ |
| 7960 | unixCurrentTime, /* xCurrentTime */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 7961 | unixGetLastError, /* xGetLastError */ \ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 7962 | unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ |
drh | 99ab3b1 | 2011-03-02 15:09:07 +0000 | [diff] [blame] | 7963 | unixSetSystemCall, /* xSetSystemCall */ \ |
drh | 1df3096 | 2011-03-02 19:06:42 +0000 | [diff] [blame] | 7964 | unixGetSystemCall, /* xGetSystemCall */ \ |
| 7965 | unixNextSystemCall, /* xNextSystemCall */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7966 | } |
| 7967 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 7968 | /* |
| 7969 | ** All default VFSes for unix are contained in the following array. |
| 7970 | ** |
| 7971 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 7972 | ** by the SQLite core when the VFS is registered. So the following |
| 7973 | ** array cannot be const. |
| 7974 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 7975 | static sqlite3_vfs aVfs[] = { |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7976 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7977 | UNIXVFS("unix", autolockIoFinder ), |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7978 | #elif OS_VXWORKS |
| 7979 | UNIXVFS("unix", vxworksIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7980 | #else |
| 7981 | UNIXVFS("unix", posixIoFinder ), |
| 7982 | #endif |
| 7983 | UNIXVFS("unix-none", nolockIoFinder ), |
| 7984 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | a7e61d8 | 2011-03-12 17:02:57 +0000 | [diff] [blame] | 7985 | UNIXVFS("unix-excl", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7986 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7987 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7988 | #endif |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7989 | #if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7990 | UNIXVFS("unix-posix", posixIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7991 | #endif |
drh | e89b291 | 2015-03-03 20:42:01 +0000 | [diff] [blame] | 7992 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 7993 | UNIXVFS("unix-flock", flockIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 7994 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 7995 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7996 | UNIXVFS("unix-afp", afpIoFinder ), |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 7997 | UNIXVFS("unix-nfs", nfsIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 7998 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 7999 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 8000 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 8001 | unsigned int i; /* Loop counter */ |
| 8002 | |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 8003 | /* Double-check that the aSyscall[] array has been constructed |
| 8004 | ** correctly. See ticket [bb3a86e890c8e96ab] */ |
dan | efe1697 | 2017-07-20 19:49:14 +0000 | [diff] [blame] | 8005 | assert( ArraySize(aSyscall)==29 ); |
drh | 2aa5a00 | 2011-04-13 13:42:25 +0000 | [diff] [blame] | 8006 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 8007 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 8008 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 8009 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 8010 | } |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame] | 8011 | unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1); |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 8012 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 8013 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 8014 | |
| 8015 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 8016 | ** Shutdown the operating system interface. |
| 8017 | ** |
| 8018 | ** Some operating systems might need to do some cleanup in this routine, |
| 8019 | ** to release dynamically allocated objects. But not on unix. |
| 8020 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 8021 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 8022 | int sqlite3_os_end(void){ |
drh | 5611589 | 2018-02-05 16:39:12 +0000 | [diff] [blame] | 8023 | unixBigLock = 0; |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 8024 | return SQLITE_OK; |
| 8025 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 8026 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 8027 | #endif /* SQLITE_OS_UNIX */ |